fix(ai): harden write SQL confirmation flow

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-28 11:56:23 +08:00
1 parent 1e72d28ed3
commit cb9d0f7cbf
20 files changed
+1258 -51

No files matched your search

+10 -2
View File
@@ -95,6 +95,11 @@ export interface AiRequestInput {
instruction: string;
context: AiContext;
allowWriteSql?: boolean;
/** When allowWriteSql is true, the specific write SQL the user confirmed. */
confirmedWriteSql?: string;
/** Connection/database snapshot at confirmation time; verified at backend. */
confirmedConnectionId?: string;
confirmedDatabase?: string;
}
export interface CustomPromptContext {
@@ -187,6 +192,9 @@ export async function runAgentStream(input: AiRequestInput, history: api.AiMessa
onEvent,
input.mode || "ask",
input.allowWriteSql || false,
input.confirmedWriteSql,
input.confirmedConnectionId,
input.confirmedDatabase,
);
}
@@ -348,8 +356,8 @@ function buildModePromptLines(mode: AiAssistantMode, isZh: boolean): string[] {
? "用户提出数据查询意图时,必须调用 execute_query 工具执行 SQL,不要只输出 SQL 文本后停止。先用 list_tables/get_columns 了解 schema,再调用 execute_query 获取真实结果,最后基于结果回答用户。"
: "When the user expresses a data query intent, you MUST call the execute_query tool to run the SQL — do NOT just output SQL text and stop. Use list_tables/get_columns to understand the schema first, then call execute_query to get real results, then answer based on the actual data.",
isZh
? "只有 SELECT、WITH、SHOW、DESCRIBE、EXPLAIN 可以通过 execute_query 执行。如果用户要求写入操作,先解释原因,不要执行。"
: "Only SELECT, WITH, SHOW, DESCRIBE, EXPLAIN can be executed via execute_query. If the user requests a write operation, explain why it is blocked instead of executing.",
? "当用户要求写入操作(INSERT/UPDATE/DELETE/CREATE/ALTER/DROP/TRUNCATE 等)时,先在一个 ```sql 代码块中给出精确的写 SQL,再在回复末尾用问句明确询问用户是否确认执行(例如'需要我执行这条 CREATE TABLE 语句吗?')。待用户明确确认后再调用 execute_query,并原样使用该代码块中的 SQL,不得改写、重新格式化或补充语句。禁止不经确认直接执行写入。"
: "When the user requests a write operation (INSERT/UPDATE/DELETE/CREATE/ALTER/DROP/TRUNCATE, etc.), first put the exact proposed write SQL in one ```sql code block, then ask for explicit confirmation at the end of your reply with a question that names the specific operation (e.g., 'Should I execute this CREATE TABLE?'). Only call execute_query for writes after the user explicitly confirms, and use the exact SQL from that code block without rewriting, reformatting, or adding statements. Never execute writes without confirmation.",
isZh ? "如果安全执行条件不满足,先说明原因,再给只读预览或澄清问题。" : "If safe execution requirements are not met, explain why first, then provide a read-only preview or a clarifying question.",
];
}
+136 -1
View File
@@ -22,7 +22,7 @@ function stripOuterPunctuation(input: string): string {
}
/** Returns the last non-empty line/sentence-ish chunk of the assistant message. */
function lastNonEmptyLine(content: string): string {
export function lastNonEmptyLine(content: string): string {
const lines = content.split(/\r?\n/);
for (let i = lines.length - 1; i >= 0; i--) {
const trimmed = lines[i].trim();
@@ -166,3 +166,138 @@ export function isShortNegative(content: string): boolean {
const all = [...ZH_NEGATIVE, ...EN_NEGATIVE];
return all.some((re) => re.test(cleaned));
}
/**
* Regular expression matching common SQL write/DDL keywords.
* Shared between AiAssistant.vue and tests so the regex stays in one place.
*/
export const WRITE_SQL_KEYWORD_RE = /\b(insert|update|delete|replace|merge|create|alter|drop|truncate|rename|grant|revoke)\b/i;
/**
* Returns true when the text contains SQL write or DDL keywords.
*/
export function containsWriteSql(text: string): boolean {
return WRITE_SQL_KEYWORD_RE.test(text);
}
/**
* Returns true when the assistant message ends with an action proposal AND
* the message as a whole contains write/DDL SQL keywords.
*
* Check order:
* 1. Last non-empty line must be an action proposal (question mark + ask
* phrase + action verb).
* 2. The *entire message* (not just the last line) must contain at least
* one write/DDL keyword (INSERT, DELETE, CREATE, etc.).
*
* This two-step check handles the common pattern where the model outputs
* a SQL code block followed by a generic confirmation question like
* "需要我执行这条 SQL 吗?" — the write keywords live in the code block,
* not in the last line.
*
* **Safety rule**: the last line (the proposal itself) MUST explicitly
* reference the write SQL or the write operation. A message where the
* last line asks about a read operation but a write keyword appears
* elsewhere is NOT treated as a write-SQL proposal — the assistant is
* offering a read, not a write. Without this rule, confirming "need me
* to query the remaining records?" could inadvertently grant write
* permission because DELETE appeared earlier in the message.
*/
export function looksLikeWriteSqlProposal(content: string): boolean {
if (!content) return false;
const lastLine = lastNonEmptyLine(content);
if (!lastLine) return false;
// Step 1: last line must be an action proposal (ask + action + question mark).
if (!/[??]\s*$/.test(lastLine)) return false;
const isZh = containsChinese(lastLine);
const askPhrases = isZh ? ZH_ASK_PHRASES : EN_ASK_PHRASES;
const actionPhrases = isZh ? ZH_ACTION_PHRASES : EN_ACTION_PHRASES;
if (!askPhrases.some((re) => re.test(lastLine))) return false;
if (!actionPhrases.some((re) => re.test(lastLine))) return false;
// Step 2: the full message must contain write/DDL SQL keywords somewhere
// (typically in a code block or earlier sentence).
if (!containsWriteSql(content)) return false;
// Safety check: the last line must explicitly reference the write SQL or
// the write/DDL operation itself. A last line that asks about a read
// ("查询"/"query") but happens to have a write keyword elsewhere in the
// message is NOT a write-SQL proposal — it is a read proposal.
// We check for three signals:
// a) The last line contains a write/DDL keyword directly, OR
// b) The last line contains a "this SQL" / "这条 SQL" reference that
// points back to the code block with the write SQL, OR
// c) The content has a SQL code block AND the last line mentions
// SQL / 执行 without specifying a different action (e.g. 查询).
return lastLineMentionsWriteSql(lastLine, isZh, content);
}
/**
* Returns true when the proposal line explicitly references a write/DDL
* SQL operation (not a read-only action that happens to coexist with a
* write keyword elsewhere in the message).
*/
function lastLineMentionsWriteSql(lastLine: string, isZh: boolean, fullContent: string): boolean {
// Signal (a): last line contains a write/DDL keyword itself.
if (containsWriteSql(lastLine)) return true;
// Signal (b): last line references "this SQL" / "这条 SQL" / "这条语句"
// pointing back to a code block that contains the write SQL.
if (isZh) {
if (/这条\s*SQL|这条\s*语句|这个\s*SQL|这段\s*SQL|上述\s*SQL|以上\s*SQL/.test(lastLine)) return true;
} else {
if (/this\s+SQL|the\s+SQL|this\s+statement|the\s+statement|this\s+query|the\s+query|above\s+SQL/.test(lastLine)) return true;
}
// Signal (c): message has a SQL code block AND the last line mentions
// 执行/execute/run without specifying a different action like 查询/query.
const hasCodeBlock = /```sql|```mysql|```postgresql|```sqlite|```tsql|```clickhouse|```\s*\n/.test(fullContent);
if (hasCodeBlock) {
if (isZh) {
// Must mention 执行 or 运行 (execute/write-like), NOT 查询/查看 (read-like).
if (/(?:执行|运行)/.test(lastLine) && !/(?:查询|查看|看看|看一下|读取)/.test(lastLine)) return true;
} else {
if (/\b(?:execute|run)\b/i.test(lastLine) && !/\b(?:query|read|fetch|retrieve|list|inspect|check|sample)\b/i.test(lastLine)) return true;
}
}
return false;
}
/** Parameters for {@link shouldGrantWriteSqlOnShortAffirmative}. */
export interface WriteSqlGrantParams {
mode: string;
/** True when allowWriteSqlForNextRun has already been set, e.g. by the button path. */
alreadyGranted: boolean;
isProduction: boolean;
userText: string;
/**
* Conversation history BEFORE the current user text was pushed.
* The function scans backward from the last message, skipping
* contextSummary entries, and stops at the first user message.
*/
messages: Array<{ role: "user" | "assistant"; content: string; kind?: "contextSummary" }>;
}
/**
* Pure-function extraction of the manual-confirmation gating logic used in
* AiAssistant.vue send(). Returns true when the user just typed a short
* affirmative reply (e.g. "可以"/"go ahead") and the most recent assistant
* message before the user message is a write-SQL action proposal.
*
* Shared between the component and its unit tests so the two cannot drift.
*/
export function shouldGrantWriteSqlOnShortAffirmative(params: WriteSqlGrantParams): boolean {
if (params.mode !== "agent" || params.isProduction || params.alreadyGranted) return false;
if (!isShortAffirmative(params.userText)) return false;
for (let i = params.messages.length - 1; i >= 0; i--) {
const msg = params.messages[i];
if (msg.kind === "contextSummary") continue;
if (msg.role === "assistant" && msg.content && looksLikeWriteSqlProposal(msg.content)) return true;
if (msg.role === "user") return false;
}
return false;
}
+8 -2
View File
@@ -237,8 +237,14 @@ export const AI_SKILL_DEFINITIONS: AiSkillDefinition[] = [
zh: "执行当前的 SQL 并解释结果。先调用 execute_query 运行当前 SQL,再基于真实结果解释查询含义、数据特征和值得注意的点。",
},
systemRules: {
en: ["Only SELECT/WITH/SHOW/DESCRIBE/EXPLAIN can be executed. If the current SQL is a write operation, do not execute; explain its impact instead.", "Base explanations on real execution results; do not speculate about data."],
zh: ["只有 SELECT/WITH/SHOW/DESCRIBE/EXPLAIN 可执行。如果当前 SQL 是写操作,不要执行,改为解释其影响。", "解释要基于真实执行结果,不要凭空推测数据。"],
en: [
"If the current SQL is a write operation, first put its exact text in one ```sql code block and ask the user for explicit confirmation (e.g., 'Should I execute this SQL?') instead of executing immediately. After confirmation, execute that code-block SQL verbatim without rewriting, reformatting, or adding statements. Do not execute writes without user confirmation.",
"Base explanations on real execution results; do not speculate about data.",
],
zh: [
"如果当前 SQL 是写操作,先在一个 ```sql 代码块中给出其精确文本,并在回复末尾明确询问用户是否确认执行(例如'需要我执行这条 SQL 吗?'),不要直接执行。确认后必须原样执行该代码块中的 SQL,不得改写、重新格式化或补充语句。禁止不经确认直接执行写入。",
"解释要基于真实执行结果,不要凭空推测数据。",
],
},
outputContract: {
en: ["Output format: lead with an execution-result summary, then step through the key data and its meaning."],
@@ -123,3 +123,23 @@ export function extractFirstSqlCodeBlock(content: string): string | undefined {
const sql = match?.[1]?.trim();
return sql || undefined;
}
/**
* Count SQL code blocks in content. Used to reject ambiguous multi-block
* proposals — when a message contains more than one SQL code block, we
* cannot determine which one the user intended to confirm.
*/
export function countSqlCodeBlocks(content: string): number {
const matches = content.match(/```(?:sql|mysql|postgresql|sqlite|tsql|clickhouse)?\s*\n[\s\S]*?```/gi);
return matches ? matches.length : 0;
}
/**
* Extract a single SQL code block from content. Returns undefined when the
* message has zero or more than one code block — only an unambiguous
* single-block proposal yields a confirmed SQL binding.
*/
export function extractSingleSqlCodeBlock(content: string): string | undefined {
if (countSqlCodeBlocks(content) !== 1) return undefined;
return extractFirstSqlCodeBlock(content);
}