Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(ci): seed constrained schemas in migration release guard (#1933)
Call Whitea58644528deb
diff --git a/scripts/migration_release_guard.py b/scripts/migration_release_guard.pyindex e77185ebb..a69b4d0fb 100644--- a/scripts/migration_release_guard.py+++ b/scripts/migration_release_guard.py@@ -57,15 +57,18 @@ from __future__ import annotationsimport argparseimport astimport hashlib+import itertoolsimport jsonimport reimport sqlite3+import stringimport subprocessimport sysimport tempfile-from collections import defaultdict+from collections import defaultdict, dequefrom collections.abc import Iterablefrom dataclasses import dataclass+from decimal import Decimalfrom pathlib import PathREPO_ROOT = Path(__file__).resolve().parents[1]@@ -798,6 +801,11 @@ CHECK_FAILURE = re.compile(r"CHECK constraint failed: (\w+)")# that a table whose constraints cannot be satisfied this way is given up on rather than# searched for. Reached only on the tables that refuse the first row at all.SEED_ATTEMPTS = 60+# Shape proposals are witnesses, not an invitation to allocate arbitrary declared widths.+SEED_TEXT_LIMIT = 4096+SEED_GLOB_STATES = 16384+SQLITE_INT_MIN = -(2**63)+SQLITE_INT_MAX = 2**63 - 1# Where `varied` records a row's step inside a JSON document. Nothing reads it back; it is# there so the document differs while staying a document, and it is namespaced so it cannot@@ -813,6 +821,10 @@ JSON_REQUIREMENT = re.compile(r"('[^']*'|-?[0-9]+(?:\.[0-9]+)?)",re.IGNORECASE,)+SUBSTRING_REQUIREMENT = re.compile(+ r'\bsubstr\s*\(\s*"?(\w+)"?\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*=\s*"?(\w+)"?',+ re.IGNORECASE,+)# Every name SQLite's `json_type` can return, against the emptiest document member of that# type. A `json_type` clause states the type where the `json_extract` form states the@@ -884,6 +896,20 @@ def check_expression(ddl: str, name: str) -> str:return ""+def identifier_key(value: str) -> str:+ """SQLite folds ASCII identifier case, not Unicode or JSON member names."""+ return value.translate(str.maketrans(string.ascii_uppercase, string.ascii_lowercase))+++def bounded_integer(literal: str, lower: int, upper: int) -> int | None:+ """Reject oversized decimal tokens before Python parsing or SQLite binding."""+ digits = literal.lstrip("-").lstrip("0") or "0"+ if len(digits) > len(str(max(abs(lower), abs(upper)))):+ return None+ value = int(digits) * (-1 if literal.startswith("-") else 1)+ return value if lower <= value <= upper else None++def check_proposals(expression: str, columns: Iterable[str]) -> list[tuple[str, str]]:"""``(column, value)`` pairs ``expression`` itself suggests, for the columns it names.@@ -897,7 +923,8 @@ def check_proposals(expression: str, columns: Iterable[str]) -> list[tuple[str,than producing a row the schema would have refused."""literals = re.findall(r"'([^']*)'", expression)- named = [column for column in columns if re.search(rf"\b{re.escape(column)}\b", expression)]+ folded = identifier_key(expression)+ named = [column for column in columns if re.search(rf"\b{re.escape(identifier_key(column))}\b", folded)]return [(column, literal) for column in named for literal in literals]@@ -911,10 +938,11 @@ def json_proposals(one column are folded into a single document, because a constraint requiring two pathsis not satisfied by a document carrying either one."""- wanted = set(columns)+ wanted = {identifier_key(column): column for column in columns}required: dict[str, list[tuple[str, str, object]]] = {}for function, column, path, literal in JSON_REQUIREMENT.findall(expression):- if column not in wanted:+ column = wanted.get(identifier_key(column))+ if column is None:continueif function.lower() == "type":named = literal.strip("'").lower()@@ -928,7 +956,7 @@ def json_proposals(elif literal.startswith("'"):required.setdefault(column, []).append((path, "?", literal[1:-1]))else:- required.setdefault(column, []).append((path, "?", float(literal) if "." in literal else int(literal)))+ required.setdefault(column, []).append((path, "json(?)", str(Decimal(literal))))proposals = []for column, clauses in required.items():@@ -941,6 +969,231 @@ def json_proposals(return proposals+def glob_witness(+ connection: sqlite3.Connection,+ patterns: tuple[str, ...],+ *,+ width: int | None = None,+ alphabet: str = string.printable,+) -> str | None:+ """Search the intersection of finite GLOB automata, with SQLite as the oracle.++ A state retains every possible token position after the current prefix. Stars+ both consume characters and admit an empty transition to the next token.+ """+ if (width is not None and not 0 <= width <= SEED_TEXT_LIMIT) or sum(map(len, patterns)) > SEED_TEXT_LIMIT:+ return None+ machines: list[list[str]] = []+ for pattern in patterns:+ tokens = []+ index = 0+ while index < len(pattern):+ end = index + 1+ if pattern[index] == "[":+ if pattern[end:end + 1] == "^":+ end += 1+ # SQLite admits ']' as the first member, including after '^'.+ if pattern[end:end + 1] == "]":+ end += 1+ closing = pattern.find("]", end)+ if closing < 0:+ return None+ end = closing + 1+ tokens.append(pattern[index:end])+ index = end+ machines.append(tokens)+ alphabet = "".join(dict.fromkeys(alphabet.replace("\x00", "")))+ members = {+ token: frozenset(+ char for char in alphabet+ if connection.execute("select ? glob ?", (char, token)).fetchone()[0]+ )+ for token in sorted({token for tokens in machines for token in tokens if token != "*"})+ }+ # Equivalent characters have identical transitions in every pattern.+ representatives: dict[tuple[bool, ...], str] = {}+ for char in alphabet:+ representatives.setdefault(tuple(char in accepted for accepted in members.values()), char)++ def closure(tokens: list[str], positions: Iterable[int]) -> frozenset[int]:+ result = set(positions)+ for position in tuple(result):+ while position < len(tokens) and tokens[position] == "*":+ position += 1+ if position in result:+ break+ result.add(position)+ return frozenset(result)++ initial = tuple(closure(tokens, [0]) for tokens in machines)+ start = (initial, 0)+ parents = {start: None}+ queue = deque([(start, 0)])+ while queue:+ key, depth = queue.popleft()+ states, _ = key+ if (width is None or depth == width) and all(+ len(tokens) in state for tokens, state in zip(machines, states)+ ):+ parts = []+ cursor = key+ while parents[cursor] is not None:+ cursor, char = parents[cursor]+ parts.append(char)+ witness = "".join(reversed(parts))+ if all(connection.execute("select ? glob ?", (witness, pattern)).fetchone()[0] for pattern in patterns):+ return witness+ if depth >= (SEED_TEXT_LIMIT if width is None else width):+ continue+ for char in representatives.values():+ next_states = tuple(+ closure(tokens, (+ position if tokens[position] == "*" else position + 1+ for position in state if position < len(tokens)+ and (tokens[position] == "*" or char in members[tokens[position]])+ ))+ for tokens, state in zip(machines, states)+ )+ if not all(next_states):+ continue+ next_key = (next_states, depth + 1 if width is not None else 0)+ if next_key in parents:+ continue+ if len(parents) >= SEED_GLOB_STATES:+ return None+ parents[next_key] = (key, char)+ queue.append((next_key, depth + 1))+ return None+++def shape_proposals(+ connection: sqlite3.Connection,+ expression: str,+ required: list[tuple[str, str]],+ values: dict[str, object],+ *,+ text_constraints: str | None = None,+ omitted: Iterable[tuple[str, str, str | None]] = (),+ unavailable: Iterable[str] = (),+) -> list[tuple[str, object]]:+ """Derive bounded text-shape and numeric candidates from the rejected CHECK.++ This extends the literal/JSON proposals, not the seeder's coverage claim. Unsupported+ expressions still fail visibly; every proposed value must survive a real insert.+ """+ names = {identifier_key(name): name for name in values}+ text = {name: value for name, value in values.items() if isinstance(value, str)}+ widths = {}+ shapes = text_constraints if text_constraints is not None else expression+ for name, width in re.findall(r'\blength\s*\(\s*"?(\w+)"?\s*\)\s*=\s*(\d+)', shapes, re.IGNORECASE):+ name = names.get(identifier_key(name), name)+ size = bounded_integer(width, 0, SEED_TEXT_LIMIT)+ if name in text and size is not None:+ widths[name] = size+ text[name] = text[name][:size].ljust(size, "0")+ globs = re.findall(+ r"\b\"?(\w+)\"?\s+(not\s+)?glob\s*'((?:[^']|'')*)'",+ shapes,+ re.IGNORECASE,+ )+ positive: dict[str, list[str]] = defaultdict(list)+ alphabets = {name: string.printable for name in text}+ for name, negated, quoted in globs:+ name = names.get(identifier_key(name), name)+ if name in text:+ pattern = quoted.replace("''", "'")+ alphabets[name] += pattern+ if not negated:+ positive[name].append(pattern)+ alphabets = {name: "".join(dict.fromkeys(alphabet)) for name, alphabet in alphabets.items()}+ # Restrict the alphabet before generating a witness so later CHECK order cannot+ # erase a required width or introduce characters forbidden by another CHECK.+ for name, negated, quoted in globs:+ name = names.get(identifier_key(name), name)+ if name not in text:+ continue+ pattern = quoted.replace("''", "'")+ if negated and pattern.startswith("*[^") and pattern.endswith("]*"):+ allowed = "".join(+ char+ for char in alphabets[name]+ if not connection.execute("select ? glob ?", (char, pattern)).fetchone()[0]+ )+ alphabets[name] = allowed+ if allowed:+ text[name] = "".join(char if char in allowed else allowed[0] for char in text[name])+ for name, patterns in positive.items():+ if all(connection.execute("select ? glob ?", (text[name], pattern)).fetchone()[0] for pattern in patterns):+ continue+ witness = glob_witness(connection, tuple(patterns), width=widths.get(name), alphabet=alphabets[name])+ if witness is not None:+ text[name] = witness+ for name, start, width, other in SUBSTRING_REQUIREMENT.findall(shapes):+ name, other = names.get(identifier_key(name), name), names.get(identifier_key(other), other)+ position = bounded_integer(start, 1, SEED_TEXT_LIMIT)+ size = bounded_integer(width, 0, SEED_TEXT_LIMIT)+ if name in text and isinstance(values.get(other), str) and position is not None and size is not None:+ offset = position - 1+ source = text.get(other, str(values[other]))+ if len(source) == size:+ text[name] = text[name][:offset].ljust(offset, "0") + source + text[name][offset + size :]+ proposals: list[tuple[str, object]] = [(name, value) for name, value in text.items() if value != values[name]]+ bounds = {+ bound+ for token in re.findall(r"(?<![\w.])-?[0-9]+(?![\w.])", expression)+ if (bound := bounded_integer(token, SQLITE_INT_MIN, SQLITE_INT_MAX)) is not None+ }+ projection = ", ".join(+ f'cast(? as {declared}) as "{name}"' if declared else f'? as "{name}"'+ for name, declared in required+ )+ for name, declared, default in omitted:+ term = f"({default})" if default is not None else "NULL"+ if declared:+ term = f"cast({term} as {declared})"+ projection += f', {term} as "{name}"'+ numeric = [+ name for name, declared in required+ if "INT" in declared.upper() and re.search(rf"\b{re.escape(identifier_key(name))}\b", identifier_key(expression))+ ]+ candidates = list(dict.fromkeys(+ value for bound in sorted(bounds) for value in (bound, bound + 1, bound - 1)+ if SQLITE_INT_MIN <= value <= SQLITE_INT_MAX+ ))+ fallback = [(name, candidate) for candidate in candidates for name in numeric if candidate != values[name]]+ if any(re.search(rf"\b{re.escape(identifier_key(name))}\b", identifier_key(expression)) for name in unavailable):+ return [*proposals, *fallback]+ # Reserve half the budget for whole-row boundary assignments before a large+ # single-column search or Cartesian prefix can starve coordinated moves.+ domains = [list(dict.fromkeys([values[name], *candidates])) for name in numeric]+ single_changes = (+ tuple(candidate if name == changed else values[name] for name in numeric)+ for changed in numeric for candidate in candidates if candidate != values[changed]+ )+ assignments = itertools.chain(+ itertools.islice(single_changes, SEED_ATTEMPTS // 2),+ (tuple(candidate for _ in numeric) for candidate in candidates),+ single_changes,+ itertools.product(*domains),+ ) if numeric else ()+ for assignment in itertools.islice(assignments, SEED_ATTEMPTS):+ changes = dict(zip(numeric, assignment))+ parameters = [changes.get(column, values[column]) for column, _ in required]+ try:+ accepted = connection.execute(+ f"select coalesce(cast(({expression}) as numeric), 1) != 0 from (select {projection})", parameters+ ).fetchone()[0]+ except sqlite3.OperationalError:+ # Generated columns or unsupported context cannot reject a candidate.+ # Offer the bounded values to the real INSERT, which supplies that context.+ proposals.extend(fallback)+ break+ if accepted:+ proposals.extend((name, value) for name, value in changes.items() if value != values[name])+ break+ return proposals++def moved_row(base: dict[str, object], step: int, *, held: str | None) -> dict[str, object]:"""``base`` with every column moved to its ``step`` variant, except ``held``.@@ -1107,7 +1360,7 @@ def varied(value: object, step: int) -> object:if isinstance(value, bool):return not valueif isinstance(value, int):- return value + step+ return (value + step - SQLITE_INT_MIN) % (SQLITE_INT_MAX - SQLITE_INT_MIN + 1) + SQLITE_INT_MINif isinstance(value, float):return value + float(step)if isinstance(value, bytes):@@ -1214,7 +1467,26 @@ def insert_seed_row(placeholders = ", ".join("?" for _ in required)statement = f'insert into "{table}" ({columns}) values ({placeholders})'names = [column for column, _ in required]- proposed: set[tuple[str, str]] = set()+ # Related columns may be constrained by a later CHECK. Derive their text shapes+ # together so declaration order does not decide whether a prefix can be offered.+ constraints = [+ check_expression(ddl, name)+ for name in re.findall(r'\bconstraint\s+"?(\w+)"?\s+check\s*\(', ddl, re.IGNORECASE)+ ]+ text_constraints = "\n".join(constraints)+ omitted = []+ unavailable = []+ for info in connection.execute(f'pragma table_xinfo("{table}")'):+ if str(info[1]) in values:+ continue+ if info[6] or (info[5] and str(info[2]).upper() == "INTEGER" and info[4] is None):+ # Generated columns and implicit primary keys depend on the INSERT.+ # In particular, a missing double-quoted name may silently become text.+ unavailable.append(str(info[1]))+ else:+ omitted.append((str(info[1]), str(info[2]), info[4]))+ prefix_sources = {identifier_key(other) for _, _, _, other in SUBSTRING_REQUIREMENT.findall(text_constraints)}+ proposed: set[tuple[str, object]] = set()objection = ""for _ in range(SEED_ATTEMPTS):try:@@ -1231,11 +1503,24 @@ def insert_seed_row(untried = [pairfor pair in [+ *shape_proposals(+ connection, expression, required, values,+ text_constraints=text_constraints, omitted=omitted, unavailable=unavailable,+ ),*check_proposals(expression, names),*json_proposals(connection, expression, names),]- if pair not in proposed+ if pair not in proposed and values[pair[0]] != pair[1]]+ if not untried:+ # Follow only prefix-source dependencies into other CHECKs. Unrelated+ # literals must not overwrite columns that already satisfy their checks.+ untried = [+ pair+ for related in constraints+ for pair in [*check_proposals(related, names), *json_proposals(connection, related, names)]+ if identifier_key(pair[0]) in prefix_sources and pair not in proposed and values[pair[0]] != pair[1]+ ]if not untried:return objection, Truecolumn, literal = untried[0]diff --git a/tests/test_migration_release_guard.py b/tests/test_migration_release_guard.pyindex 2f4343719..0a5f6f2cf 100644--- a/tests/test_migration_release_guard.py+++ b/tests/test_migration_release_guard.py@@ -14,6 +14,7 @@ properties through the CLI with the full history fetched.from __future__ import annotationsimport inspect+import itertoolsimport jsonimport sqlite3from pathlib import Path@@ -966,6 +967,361 @@ def test_a_repeat_the_rows_do_not_carry_is_a_violation_however_it_got_there():connection.close()+def test_current_schema_gets_valid_rows_in_every_table(tmp_path):+ """New constrained tables must be seedable even before their first release tag exists."""+ db_path = tmp_path / "vibe.sqlite"+ guard.run_migrations(db_path)++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ tables = {+ name+ for (name,) in connection.execute(+ "select name from sqlite_master where type = 'table' and name not like 'sqlite_%'"+ )+ if name != guard.ALEMBIC_BOOKKEEPING_TABLE+ }+ assert tables == guard.HEAD_TABLES+ for table in tables:+ assert connection.execute(f'select count(*) from "{table}"').fetchone()[0] >= guard.SEED_ROWS, table+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+ assert connection.execute("pragma foreign_key_check").fetchall() == []++[email protected]("minimum", [0, 4])[email protected]("order", list(itertools.permutations(range(4))))[email protected]("mixed_case", [False, True])[email protected]("source_check", ["a glob 'R-[A-C][0-9]'", "a in ('R-A0')"])+def test_shape_repairs_follow_constraints_not_column_names(tmp_path, minimum, order, mixed_case, source_check):+ db_path = tmp_path / "vibe.sqlite"+ columns = "id integer primary key, a text not null, b text not null, c text not null, d integer not null, e integer not null"+ if mixed_case:+ columns = columns.upper()+ checks = (+ f"constraint ck_a check ({source_check})",+ "constraint ck_b check (length(b) = 12 and b not glob '*[^0-9a-f]*')",+ "constraint ck_c check (length(c) = 9 and substr(c, 1, 4) = a)",+ f"constraint ck_counts check (typeof(d) = 'integer' and d >= 0 "+ f"and typeof(e) = 'integer' and e >= 0 and d + e > {minimum} and (d = 0 or c = ''))",+ )+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped ({columns}, {', '.join(checks[index] for index in order)})")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++def test_numeric_candidates_use_the_column_affinity():+ with sqlite3.connect(":memory:") as connection:+ proposals = guard.shape_proposals(connection, "n > '0'", [("n", "INTEGER")], {"n": 0})+ assert ("n", 1) in proposals+ assert ("n", -1) not in proposals++[email protected]("pattern,width", [("[A-Z]*", 4), ("*[A-Z]", 4), ("[A-Z]*[A-Z]", 4), ("*[A-Z]*", 4), ("[A-Z]???", 4), ("[A-Z][A-Z][A-Z][A-Z]", 4), ("*", 0), ("*", guard.SEED_TEXT_LIMIT)])[email protected]("order", list(itertools.permutations(range(3))))[email protected]("separate", [False, True])[email protected]("alphabet", ["A-Z", "0-9A-Z"])+def test_text_shape_composition_preserves_every_requirement(tmp_path, pattern, width, order, separate, alphabet):+ requirements = [f"length(code) = {width}", f"code glob '{pattern}'", f"code not glob '*[^{alphabet}]*'"]+ expressions = [requirements[index] for index in order]+ if not separate:+ expressions = [" and ".join(expressions)]+ checks = ", ".join(f"constraint ck_{index} check ({expression})" for index, expression in enumerate(expressions))+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (id integer primary key, code text not null, {checks})")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++ "columns,clauses",+ [+ (columns, clauses)+ for names in [("low", "high"), ("low", "middle", "high")]+ for columns in itertools.permutations(names)+ for clauses in itertools.permutations(["low > 0", *(f"{right} >= {left}" for left, right in itertools.pairwise(names))])+ ],+)+def test_numeric_repairs_coordinate_columns_without_order_dependence(tmp_path, columns, clauses):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ declared = ", ".join(f'"{name}" integer not null' for name in columns)+ connection.execute(f"create table shaped (id integer primary key, {declared}, constraint ck_counts check ({' and '.join(clauses)}))")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++def test_joint_numeric_evaluation_has_a_finite_budget():+ required = [(f"n{index}", "INTEGER") for index in range(6)]+ values = {name: 0 for name, _ in required}+ expression = " and ".join(f"{name} > 0" for name in values) + " and n5 < 0"+ statements = []+ with sqlite3.connect(":memory:") as connection:+ connection.set_trace_callback(statements.append)+ assert guard.shape_proposals(connection, expression, required, values) == []+ assert len(statements) == guard.SEED_ATTEMPTS++[email protected]("changed", range(6))+def test_joint_numeric_search_preserves_single_column_repairs(changed):+ required = [(f"n{index}", "INTEGER") for index in range(6)]+ values = {name: 0 for name, _ in required}+ expression = " and ".join(f"{name} >= 0" for name in values) + f" and n{changed} > 0"+ with sqlite3.connect(":memory:") as connection:+ assert guard.shape_proposals(connection, expression, required, values) == [(f"n{changed}", 1)]++[email protected]("order", list(itertools.permutations(range(3))))[email protected]("separate", [False, True])[email protected]("patterns", [("A*", "*Z"), ("[A-C]*", "*[Y-Z]"), ("*A*", "*Z*"), ("\u00e9*", "*]")])+def test_positive_globs_are_composed_in_every_check_order(tmp_path, order, separate, patterns):+ requirements = ["length(code) = 4", *(f"code glob '{pattern}'" for pattern in patterns)]+ expressions = [requirements[index] for index in order]+ if not separate:+ expressions = [" and ".join(expressions)]+ checks = ", ".join(f"constraint ck_{index} check ({expression})" for index, expression in enumerate(expressions))+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (id integer primary key, code text not null, {checks})")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]++[email protected]("pattern", ["[]]", "[^]]", "[]-]", "[-]", "[[]", "[]a]", "[a-]", "[\u00e9]", "?[]]*"])+def test_glob_classes_follow_sqlite_token_semantics(tmp_path, pattern):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (id integer primary key, code text not null, constraint ck_code check (code glob '{pattern}'))")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++def test_glob_intersection_matches_sqlite_over_a_finite_domain():+ alphabet = "ab[]-"+ patterns = ("*", "a*", "*b", "?a", "[ab]", "[]]", "[^]]", "[[]", "[-a]", "a**b", "[a-]", "[", "[]", "[^]")+ with sqlite3.connect(":memory:") as connection:+ connection.execute("create table domain (value text, width integer)")+ connection.executemany("insert into domain values (?, ?)", [+ ("".join(chars), width) for width in range(4) for chars in itertools.product(alphabet, repeat=width)+ ])+ for left, right, width in itertools.product(patterns, patterns, range(4)):+ expected = connection.execute(+ "select value from domain where width = ? and value glob ? and value glob ? limit 1",+ (width, left, right),+ ).fetchone()+ witness = guard.glob_witness(connection, (left, right), width=width, alphabet=alphabet)+ assert (witness is None) == (expected is None), (left, right, width, witness)+ if witness is not None:+ assert len(witness) == width+ assert connection.execute("select ? glob ? and ? glob ?", (witness, left, witness, right)).fetchone()[0]+++def test_glob_search_has_explicit_width_pattern_and_state_limits(monkeypatch):+ with sqlite3.connect(":memory:") as connection:+ assert guard.glob_witness(connection, ("*",), width=guard.SEED_TEXT_LIMIT + 1) is None+ assert guard.glob_witness(connection, ("a" * (guard.SEED_TEXT_LIMIT + 1),)) is None+ monkeypatch.setattr(guard, "SEED_GLOB_STATES", 3)+ assert guard.glob_witness(connection, ("????",)) is None++[email protected]("patterns", [("A*", "B*"), ("[]", "*"), ("[", "*")])+def test_unsatisfiable_glob_intersections_remain_visible_failures(tmp_path, patterns):+ db_path = tmp_path / "vibe.sqlite"+ expression = " and ".join(f"code glob '{pattern}'" for pattern in patterns)+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (code text not null, constraint ck_code check ({expression}))")+ short, _ = guard.seed_representative_rows(db_path)+ assert "ck_code" in short["shaped"]++[email protected]("count", [5, 6, 16])[email protected]("bound", [0, 7, -7])[email protected]("reverse", [False, True])+def test_joint_boundary_repairs_are_not_starved_by_cartesian_prefix(tmp_path, count, bound, reverse):+ names = [f"n{index}" for index in range(count)]+ clauses = [f"{name} > {bound}" if bound >= 0 else f"{name} < {bound}" for name in names]+ if reverse:+ names.reverse()+ columns = ", ".join(f"{name} integer not null" for name in names)+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (id integer primary key, {columns}, constraint ck_counts check ({' and '.join(clauses)}))")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++ "column,check,expected",+ [+ ("tag TEXT", "tag is null", None),+ ("tag TEXT DEFAULT 'ready'", "tag = 'ready'", "ready"),+ ("tag TEXT DEFAULT (upper('ready'))", "tag = 'READY'", "READY"),+ ("tag INTEGER DEFAULT 7", "tag = 7", 7),+ ("tag INTEGER DEFAULT NULL", "tag is null", None),+ ("tag INTEGER", "tag = 7", None),+ ("tag TEXT GENERATED ALWAYS AS (case when n > 0 then 'ready' end)", "tag = 'ready'", "ready"),+ ("tag TEXT GENERATED ALWAYS AS (case when n > 0 then 'ready' end)", "\"tag\" = 'ready'", "ready"),+ ("tag INTEGER PRIMARY KEY", "\"tag\" > 0", 1),+ ],+)[email protected]("reverse", [False, True])+def test_numeric_check_context_includes_omitted_columns(column, check, expected, reverse):+ columns = ["n INTEGER NOT NULL", column]+ if reverse:+ columns.reverse()+ ddl = f"create table shaped ({', '.join(columns)}, constraint ck_n check (n > 0 and {check}))"+ with sqlite3.connect(":memory:") as connection:+ connection.execute(ddl)+ objection, settled = guard.insert_seed_row(connection, "shaped", ddl, [("n", "INTEGER")], {"n": 0})+ assert (objection, settled) == ("", True)+ assert connection.execute("select n, tag from shaped").fetchall() == [(1, expected)]+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]++[email protected]("reverse", [False, True])+def test_prefix_sources_can_be_derived_from_json_checks(tmp_path, reverse):+ db_path = tmp_path / "vibe.sqlite"+ checks = [+ "constraint ck_source check (json_valid(a) = 1 and json_extract(a, '$.version') = 1)",+ "constraint ck_prefix check (length(b) = 20 and substr(b, 1, 13) = a)",+ ]+ if reverse:+ checks.reverse()+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table shaped (id integer primary key, a text not null, b text not null, {', '.join(checks)})")++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]+++ "comparison",+ ["> 9223372036854775807", "< -9223372036854775808", "> " + "9" * 5000],+ ids=["above-int64", "below-int64", "arbitrary-precision-literal"],+)+def test_unbindable_numeric_boundaries_are_reported_not_raised(tmp_path, comparison):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"create table extreme (id integer primary key, n integer not null, constraint ck_n check (n {comparison}))")++ short, _ = guard.seed_representative_rows(db_path)++ assert "ck_n" in short["extreme"]++[email protected]("value", [-(2**63), 2**63 - 1])+def test_integer_variations_remain_sqlite_bindable(value):+ with sqlite3.connect(":memory:") as connection:+ variants = [guard.varied(value, step) for step in range(1, guard.SEED_ATTEMPTS + 1)]+ assert len(set(variants)) == len(variants)+ assert value not in variants+ for varied in variants:+ assert connection.execute("select ?", (varied,)).fetchone()[0] == varied+++def test_literal_and_json_repairs_use_sqlite_identifier_case_rules(tmp_path):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute("""+ create table named (id integer primary key, Kind text not null, Payload text not null,+ constraint ck_kind check (kIND in ('ready')),+ constraint ck_payload check (json_valid(PAYLOAD) = 1 and json_extract(payload, '$.CaseSensitive') = 1))+ """)++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from named where Kind = 'ready' and json_extract(Payload, '$.CaseSensitive') = 1").fetchone()[0] >= guard.SEED_ROWS+++def test_identifier_folding_matches_sqlite_without_changing_unicode():+ with sqlite3.connect(":memory:") as connection:+ connection.execute('create table names ("A" text, "\u00c4" text, "\u00e4" text)')+ connection.execute('insert into names values (\'ascii\', \'upper\', \'lower\')')+ assert connection.execute('select a, "\u00c4", "\u00e4" from names').fetchone() == ("ascii", "upper", "lower")+ assert guard.identifier_key("A") == guard.identifier_key("a")+ assert guard.identifier_key("\u00c4") != guard.identifier_key("\u00e4")++[email protected]("width", [str(guard.SEED_TEXT_LIMIT + 1), "9" * 5000], ids=["width-limit", "huge-width"])+def test_unsupported_shapes_remain_a_visible_seed_failure(tmp_path, width):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"""+ create table oversized (id integer primary key, value text not null,+ constraint ck_width check (length(value) = {width}))+ """)++ short, _ = guard.seed_representative_rows(db_path)++ assert "ck_width" in short["oversized"]+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from oversized").fetchone()[0] == 0+++def test_bounded_numeric_tokens_ignore_leading_zeroes():+ assert guard.bounded_integer("0" * 5000 + "12", 0, 4096) == 12+ assert guard.bounded_integer("-" + "0" * 5000 + "12", -4096, 0) == -12+ with sqlite3.connect(":memory:") as connection:+ assert guard.shape_proposals(connection, "substr(a, " + "9" * 5000 + ", 1) = b", [("a", "TEXT"), ("b", "TEXT")], {"a": "x", "b": "y"}) == []++[email protected]("literal", ["9223372036854775808", "0001", "1.0", "-0001.5"])+def test_json_numeric_candidates_are_parsed_by_sqlite_without_integer_binding(tmp_path, literal):+ db_path = tmp_path / "vibe.sqlite"+ with sqlite3.connect(db_path) as connection:+ connection.execute(f"""+ create table shaped (id integer primary key, payload text not null,+ constraint ck_payload check (json_valid(payload) = 1 and json_extract(payload, '$.large') = {literal}+ and typeof(json_extract(payload, '$.large')) = typeof({literal})))+ """)++ short, _ = guard.seed_representative_rows(db_path)++ assert short == {}+ with sqlite3.connect(db_path) as connection:+ assert connection.execute("select count(*) from shaped").fetchone()[0] >= guard.SEED_ROWS+ assert connection.execute("pragma integrity_check").fetchall() == [("ok",)]++@requires_release_historydef test_the_upgrade_property_runs_over_a_database_that_carries_rows(monkeypatch):"""The seeding is only worth anything if the upgrade under test is the thing it precedes.