Skip to content

[P1][bug] Schema context truncation breaks mid-table DDL — model sees incomplete CREATE TABLE blocks #313

Closed

Description

@EVWorth

Context

src-tauri/crates/mas-ai/src/service.rs:96-114 (build_schema_context truncation). Audit finding F6 in docs/audits/ai.md. ADR-007 (TECH_DECISIONS.md) promises "Intelligent schema pruning: only include tables and columns relevant to the conversation" and "embedding-based relevance scoring for 200+ tables" — implementation has neither.

Problem

The 4000-char size check sits between tables (after context.push_str(");\n\n")), so a single large table pushes the limit and triggers truncation — but the truncation handler appends "-- ... (schema truncated)\n" to a string that already contains a complete CREATE TABLE, so the marker ends the prompt. For schemas with multiple tables the model sees a sensible "truncated here" marker — OK. But the 4000-char budget is a guess — Copilot models have 8k-128k context windows; 4000 chars (about 1000 tokens) leaves 99%+ of context unused on a 16k model. There is no per-table size guard, no relevance pruning, no views/routines/PK/FK info sent.

Files

  • src-tauri/crates/mas-ai/src/service.rs:65-115 (build_schema_context)
  • docs/design/TECH_DECISIONS.md:478-490 (ADR-007 mitigations)

Repro

Feed a SchemaInspector mock returning a 50-column table (~6000 chars). The produced context ends with a complete CREATE TABLE followed by \n\n-- ... (schema truncated)\n — never partially mid-DDL. Now feed 50 tables: the first N fit, the marker appears, but the model can't tell which tables were dropped.

Expected

Truncation is table-aware and token-budget-aware. Each emitted CREATE TABLE is either fully present or absent. The model sees a clear "..." marker and knows which tables were skipped. Large schemas surface a "Send full vs. subset" choice in the UI.

Proposed fix

Scope M. (a) Make the truncation table-aware: track current_table, finish appending the current table's close-paren before yielding to the truncation guard. (b) Replace the 4000-char constant with a token-aware budget measured against the configured model's max_input_tokens minus prompt + response overhead. (c) Send indexes (row_count, PK, FK) after the column list per table so the model can reason about joins.

Acceptance

A unit test feeds a SchemaInspector mock returning a poisoned single-table schema; asserts no produced context string ends with an unterminated DDL fragment. A second test asserts large schemas surface a UI banner.

Needs human verify

no (string-shape is verifiable)

Labels: audit, area/ai, severity/p1, kind/bug, kind/doc-drift

Activity

  1. added
    audit
    severity/p1
    kind/bug
    kind/doc-drift
    area/ai
    on Jul 31, 2026
  2. EVWorth commented on Sep 12, 2026

    @EVWorth
    OwnerAuthor

    Closing as obsolete rather than fixed: this describes the implementation that ADR-011 replaces.

    The AI feature is being rebuilt around a decision made in #673 — SQLPilot does not call a model. It exposes the database, under its own policy, to whatever harness the user already runs and pays for. The mas-ai crate, its Copilot SDK session, its system prompt and its tool guards are all going.

    What that does to this issue specifically: the truncation problem disappears with the schema dump that caused it. Nothing hands an agent a whole schema now: search_schema and related_tables let it retrieve what it needs, which is also what stops a model inventing a column it saw half of (§4.1).

    The design is in docs/design/AI_INTEGRATION.md and ADR-011. If any part of this issue survives the redesign in a form I have missed, reopen it — I would rather re-argue it than lose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/aiauditkind/bugkind/doc-driftseverity/p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions