跪拜 Guibai
← Back to the summary

The 'No Handwritten SQL' Rule Behind 230,000 Trusted Data Retrievals

Let the Agent Not Guess: A Red Line Forbidding Handwritten SQL Supports 230,000 Trusted Data Retrievals

A business stakeholder asks in a group chat, "What was yesterday's paid conversion rate?" The Agent produces a result table in three seconds. With many AI data retrieval systems, the story ends there — a number, looking plausible.

But the real test is the next question: Is this number correct? Which table was used? Is the definition the one the business wanted? Did any filter condition get dropped? Is the time range right? If the answer can't be given, no one dares use that table for a decision, even if the value is numerically correct. It just packages "I don't know" into "looks like I know".

I've seen too many data Agents stop at "can write SQL" and then prove unreliable. Recently I looked at Taotian's live-streaming tech team's practice. They've run more than 230,000 calls, served 190+ users, and the core constraint of the whole system is a single "no" — no handwritten SQL.

That ban made me see it clearly. It exposes something many haven't thought through: A data Agent's reliability never comes from how well the model writes SQL, but from what it is not allowed to do. I want to explain this "trust by prohibition" design thoroughly.

1. Why the Ban on Handwritten SQL Is the First Principle

Let's clarify what exactly this red line prohibits. Prohibiting handwritten SQL means: metrics and dimensions must be verified through the semantic layer, generated by the controlled DSL2SQL transformation, and then run by a read-only executor. Neither the Agent nor users can bypass this pipeline to write a raw SQL and pull data from the database — even if that SQL is syntactically correct and returns the right result.

It looks like tying your own hands. But the value of this ban is not in "limiting capability" but in changing the nature of data retrieval:

It turns data retrieval from an "unauditable ad-hoc action" into a fixed pipeline with a complete product chain, replayable, and reconcilable.

Once handwriting is banned, every retrieval must leave a trace: where the SQL came from, which semantic assets were used, where filter conditions are attached, which data source is used, and execution status. These are not side details; they're part of the result. An Agent should never deliver just a table, but also the definition, conditions, sources, and necessary risk explanations, so the result can be understood, checked, and reused.

The reverse makes it clear: allowing handwritten SQL as a fallback is convenient in the short term — mismatch in definition? I'll write a quick SQL to bypass the semantic layer and patch a number. But every "patch" destroys a chance to surface the problem. Precisely because patching is not allowed, the gap becomes visible, recorded, and fixed.

This is the fundamental logic of the whole system: use a single prohibition to split "getting a number" from "getting the right number", and unwaveringly stand on the side of the latter.

2. Three Foundations: Making "No Guessing" Executable

A ban alone isn't enough; you also need the infrastructure to support it. This system has three foundations. I'll restate what each solves in my own understanding:

Foundation Question it answers Core risk it addresses
Semantic layer What does the business question actually mean Wrong metric, wrong dimension, missing filter condition
DSL2SQL How to turn the business definition into an auditable query Whether the query lands correctly on data, whether results can be traced and audited
Cloud Agent Who understands, retrieves, orchestrates, and explains Whether users get context-aware, evidence-backed answers

These three are strung into a continuous pipeline:

diagram

The semantic layer ensures "correct understanding", DSL2SQL ensures "clear expression", and the Agent drives the whole process to "run and deliver". Under the three foundations there's an easily overlooked base — data supply: using lakehouse-architectures like Paimon for storage, and engines like StarRocks for query serving, decoupling storage, processing, and serving. Without this layer, even the most beautiful semantics are only a castle in the air.

Here's a design decision I find particularly critical: Correct data retrieval cannot rely only on the Agent "understanding cleverly"; accuracy must be guaranteed jointly by semantic definitions, query constraints, permission and filter checks, and execution evidence.

When the definition is unclear, assets don't match, or permissions are limited, the Agent should explicitly mark the request as "pending confirmation" or "blocked" rather than fill in the definition itself or bypass the semantic layer to fabricate a result. In a word — rather fail to deliver than deliver a fabricated answer.

3. Institutionalize Failure as an Asset: The Loop's Output Is Not a Conclusion

If the ban is the bone of this system, then "how to treat failure" is its soul. I paused for a long time when I read this, because it overturns a common default: everyone building Agents aims to reduce errors. But this system's organizational principle reads:

The output of the loop is not a conclusion, but an expanding set of answerable questions.

It turns the processing of a problem encountered in a retrieval into an eight-ring closed loop:

diagram

Among these, I believe three constraints are the essence of the whole design:

First, gaps must be logged, not patched. Unclear definitions, assets not covered, abnormal numbers — all go into a pending-confirmation list with status. Combined with the "no handwritten SQL fallback" rule, the deep effect of this constraint is not error prevention but preventing "patching a number" from erasing a learning opportunity. Every unresolved gap is raw material for the semantic layer's next improvement.

Second, hypotheses must have predefined criteria. When offering competing explanations, first write down "what phenomenon would confirm which explanation", then wait for the data. This step looks tedious, but it keeps long-pending propositions progressively movable — they don't hang indefinitely on "check later".

Third, feed back upstream. Gaps are not only recorded; they are also fed back to the semantic layer as a list, each with business impact and priority. This system is both the consumer of the semantic layer and its primary requirement-raiser — because it's the one that keeps hitting walls on real problems.

Following this line, I read a sentence I think is the most valuable in the whole article: The system's capability growth comes mainly from institutionalized handling of failures, not from model capability improvement. It doesn't pursue being error-free, but pursuing that after an error, it can be discovered, traced, and converted into the next rule. This shift is profound. The traditional approach treats errors as flaws to quickly smooth over; this approach treats errors as a mine, systematically extracting value.

4. The Destination of Rules: Moving from Prompts to Code

At this point, you might ask: with so many constraints, why not just put them all in the prompt? Because that doesn't work. This is the design insight I most agree with and that is most actionable: Any rule that can be machine-enforced should not stay in the prompt.

It divides the context into four layers by "reliability" rather than "content type":

There's an elegant mechanism here — demotion and gating are mutually conditional: a rule can be demoted from the load layer because it has already been moved into code and enforced. So the stronger the machine constraints, the fewer rules need to be carried by prompts.

I understand it as a "rule migration path": a definition first written in the prompt → after two corrections, promoted to a ledger rule → after repeated verification, solidified into tooling/validation → finally demoted from the load layer because code now guards it. The prompt is a temporary home for rules; code is their permanent home.

There's also a self-protective rule: Rules that conflict with the red line cannot be promoted. Even if some experience genuinely improves efficiency, as long as it violates constraints, it doesn't enter long-term memory — otherwise, memory will spontaneously evolve toward "bypassing constraints". This detail shows the designer's awareness of the system's degradation path: every opening you leave, the system will learn to exploit.

5. Fences and Gates: Two Kinds of Protection, Don't Confuse Them

For quality assurance, I've made my own distinction, which I think explains the problem better than the original wording: fences prevent bypass, gates prevent wrong values.

The third one is especially worth discussing. Treating "zero rows" and "hit the limit" as "undetermined" rather than "no data" is the most seasoned rigor in distributed systems — it acknowledges the system may be in a state it cannot judge, so it doesn't jump to conclusions. This is the opposite of an operator's intuition of "if you can't find it, it doesn't exist".

And these three gate layers themselves have failure modes. The article honestly lists two: constant false negatives (the validator didn't handle the artifact's preceding statements, each failed judgment was ignored as normal, so the mechanism never actually took effect) and false positives (read-only commands are wrongly blocked because parameters contain sensitive words). From this, an engineering discipline I strongly endorse emerges — when tightening rules, you must simultaneously provide a set of legitimate-path test cases, otherwise you cannot prove you haven't broken the normal chain; acceptance also requires probes in the real environment, because synthetic events can only verify logic, not distribution and delivery.

6. Knowledge Grows Along Actual Paths, Not Ideal Ones

The last point that resonates with me is about "how a knowledge system should grow".

The semantic ecosystem in this system is not a tree designed at the start, but grows through successive needs: each need is a leaf, starting from the consumption need and tracing along lineage to the real data; repeated base measures and dimensions gradually emerge, and the key action becomes "identify which existing assets can be reused"; models validated by probes and numeric reconciliation become the starting point for future needs.

An ecosystem becomes richer not by increasing the number of assets, but by having more shared nodes and more complete relationships among nodes. This is another expression of "knowledge compilation" and "experience compounding" in the same direction.

The knowledge retrieval layer (Wiki-KBase) is therefore designed with restraint: define the scope before retrieving (three modes: only/all/off), have a fallback when confidence is low (if the highest score is below 0.6, supplement with terms and ontology), and when retrieval fails, don't silently give a seemingly complete answer; instead, route it to a badcase queue, review it, and then update the knowledge.

But what moved me most is a "counter-example": the design document originally defined a three-stage knowledge promotion channel of "observation → candidate → establishment", the scaffolding was fully built, but it wasn't actually used — the real knowledge flow followed an informal path of "ledger + daily accumulation + load-layer distillation + tool solidification".

The designer's conclusion: Knowledge management mechanisms should be designed according to the actual path, not the ideal path. This honesty is rare. We too easily fall in love with designing elegant mechanisms, only to find the team never uses them. Admitting "the scaffolding has no users, the real path is something else" and then restructuring along the real path requires more courage than sticking to the ideal design.

A subtle detail: a threshold-based rule (compress a file when it exceeds a threshold) solved the big-file problem in the first version, but two months later the disk alerted again, and the root cause became accumulation of many small files, making the single-file threshold completely useless. The author's summary — threshold-based rules usually fail not because the threshold is wrong, but because the shape of the data distribution changed. This sentence deserves to be pinned on the wall.

7. Honest Boundaries: What It Hasn't Solved Yet

I don't want to write this practice as a silver bullet; it also sets its own boundaries.

The scale is significant, but the positioning is restrained. More than 230,000 calls and 190+ users are real numbers. But the team repeatedly emphasizes that the most important thing at this stage is not building an "omnicapable Data Agent", but making retrieval and analysis solid — stable, reliable, accurate, and efficient. You eat a meal one bite at a time.

Having a feedback loop does not equal having solved the problem. One round of badcase reports recorded 466 retrieval records; 282 had no hits or low scores, forming 237 independent issues. One triage recorded 122 open issues, closed 20, and left 102 to govern. Some tier improvements measured 0.902 recall, but that's not an A/B proof of overall system accuracy. "Having a governance mechanism" and "problem solved" are two different statements; they must not be conflated.

The evaluation system is still being built. The team itself outlines the outlook: build product-level pipeline experience, Agent and Harness stability, and an evaluation system that continuously assesses accuracy, stability, timeliness, and business fitness. Without an evaluation loop, "better with use" is just a design intention, not a proven fact.

These "unsolved" parts are more useful than the nice conclusions — they tell you where this system stands now, and what foundations you need to fill in if you want to copy it.

Conclusion

Looking back, this red line forbidding handwritten SQL essentially redefines the data Agent from a "clever query generator" into an "auditable data delivery pipeline".

Its reliability doesn't come from the model being smarter, but from a series of "no's": no guessing, no handwritten fallback, no silently swallowing errors, no promotion of experience that conflicts with the red line. And a series of "yes": must record gaps, must have evidence chain, must reconcile, and must move rules from prompts to code.

On a deeper level, there is a role shift. Data engineers are no longer just "processing data" but "designing data supply"; users no longer "wait for data" but "co-create with AI"; data moves from the last link in the business chain to a decision capability running inside the business. Underpinning all this is a system that turns every failure into the next rule — the system doesn't aim to avoid errors, only to ensure that after an error, something new can grow.

References

  1. The Industrial Revolution Brought by Semantic Layer in the AI Era — Semantic (Semantic Layer) — Taotian Group Live Streaming Tech Team, Ying Ke
  2. Apache Paimon. Paimon — Streaming Lakehouse
  3. StarRocks. StarRocks Documentation
  4. Apache Flink. Apache Flink