From aa6523f8e5cf758985ac4eceda6bf9181b0813c0 Mon Sep 17 00:00:00 2001 From: dal Date: Thu, 25 Sep 2025 12:52:06 -0600 Subject: [PATCH] fix prompts add tests --- .../analyst-agent/analyst-agent-prompt.txt | 10 +++--- .../get-analyst-agent-system-prompt.test.ts | 31 ++++++++++++++++ ...think-and-prep-agent-system-prompt.test.ts | 36 +++++++++++++++++++ ...nk-and-prep-agent-investigation-prompt.txt | 8 ++--- .../think-and-prep-agent-standard-prompt.txt | 8 ++--- 5 files changed, 80 insertions(+), 13 deletions(-) diff --git a/packages/ai/src/agents/analyst-agent/analyst-agent-prompt.txt b/packages/ai/src/agents/analyst-agent/analyst-agent-prompt.txt index e5933d045..d88919849 100644 --- a/packages/ai/src/agents/analyst-agent/analyst-agent-prompt.txt +++ b/packages/ai/src/agents/analyst-agent/analyst-agent-prompt.txt @@ -344,12 +344,12 @@ You operate in a loop to complete tasks: - Strict JOINs: Only join tables where relationships are explicitly defined via `relationships` or `entities` keys in the provided data context/metadata. Do not join tables without a pre-defined relationship. - SQL Requirements: - Use database-qualified schema-qualified table names (`..`). - - Use fully qualified column names with table aliases (e.g., `.`). - - MANDATORY SQL NAMING CONVENTIONS: + - Use column names qualified with table aliases (e.g., `.`). + - MANDATORY SQL NAMING CONVENTIONS: - All Table References: MUST be fully qualified: `DATABASE_NAME.SCHEMA_NAME.TABLE_NAME`. - - All Column References: MUST be qualified with their table alias (e.g., `alias.column_name`) or CTE name (e.g., `cte_alias.column_name_from_cte`). - - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT t.column1 FROM DATABASE.SCHEMA.TABLE1 t ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `t.column1`, not just `column1`). This applies even if the CTE is simple and selects from only one table. - - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column1 FROM my_cte mc ...`). + - All Column References: MUST be qualified with their table alias (e.g., `c.customerid`) or CTE name (e.g., `cte_alias.column_name_from_cte`). + - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT c.customerid FROM DATABASE.SCHEMA.TABLE1 c ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `c.customerid`, not just `customerid`). This applies even if the CTE is simple and selects from only one table. + - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column_name FROM my_cte mc ...`). - Universal Application: These naming conventions are strict requirements and apply universally to all parts of the SQL query, including every CTE definition and every subsequent SELECT statement. Non-compliance will lead to errors. - Context Adherence: Strictly use only columns that are present in the data context provided by search results. Never invent or assume columns. - Select specific columns (avoid `SELECT *` or `COUNT(*)`). diff --git a/packages/ai/src/agents/analyst-agent/get-analyst-agent-system-prompt.test.ts b/packages/ai/src/agents/analyst-agent/get-analyst-agent-system-prompt.test.ts index f6053280a..d95e585ef 100644 --- a/packages/ai/src/agents/analyst-agent/get-analyst-agent-system-prompt.test.ts +++ b/packages/ai/src/agents/analyst-agent/get-analyst-agent-system-prompt.test.ts @@ -75,4 +75,35 @@ describe('Analyst Agent Instructions', () => { getAnalystAgentSystemPrompt(' '); // whitespace only }).toThrow('SQL dialect guidance is required'); }); + + it('should contain mandatory SQL naming conventions', () => { + const result = getAnalystAgentSystemPrompt('Test guidance'); + + // Check for MANDATORY SQL NAMING CONVENTIONS section + expect(result).toContain('MANDATORY SQL NAMING CONVENTIONS'); + + // Ensure table references require full qualification + expect(result).toContain('All Table References: MUST be fully qualified: `DATABASE_NAME.SCHEMA_NAME.TABLE_NAME`'); + + // Ensure column references use table aliases (not full qualifiers) + expect(result).toContain('All Column References: MUST be qualified with their table alias (e.g., `c.customerid`)'); + + // Ensure examples show table alias usage without full qualification + expect(result).toContain('c.customerid'); + expect(result).not.toContain('postgres.ont_ont.customer.customerid'); + + // Ensure CTE examples use table aliases correctly + expect(result).toContain('SELECT c.customerid FROM DATABASE.SCHEMA.TABLE1 c'); + expect(result).toContain('c.customerid`, not just `customerid`'); + }); + + it('should use column names qualified with table aliases', () => { + const result = getAnalystAgentSystemPrompt('Test guidance'); + + // Check for the updated description + expect(result).toContain('Use column names qualified with table aliases'); + + // Ensure the old verbose description is not present + expect(result).not.toContain('Use fully qualified column names with table aliases'); + }); }); diff --git a/packages/ai/src/agents/think-and-prep-agent/get-think-and-prep-agent-system-prompt.test.ts b/packages/ai/src/agents/think-and-prep-agent/get-think-and-prep-agent-system-prompt.test.ts index 3dc5b5421..8a4bca719 100644 --- a/packages/ai/src/agents/think-and-prep-agent/get-think-and-prep-agent-system-prompt.test.ts +++ b/packages/ai/src/agents/think-and-prep-agent/get-think-and-prep-agent-system-prompt.test.ts @@ -145,4 +145,40 @@ describe('Think and Prep Agent Instructions', () => { getThinkAndPrepAgentSystemPrompt(' '); // whitespace only }).toThrow('SQL dialect guidance is required'); }); + + describe.each([ + ['standard', 'standard'], + ['investigation', 'investigation'], + ])('SQL naming conventions in %s mode', (modeName, mode) => { + it(`should contain mandatory SQL naming conventions in ${modeName} mode`, () => { + const result = getThinkAndPrepAgentSystemPrompt('Test guidance', mode as 'standard' | 'investigation'); + + // Check for MANDATORY SQL NAMING CONVENTIONS section + expect(result).toContain('MANDATORY SQL NAMING CONVENTIONS'); + + // Ensure table references require full qualification + expect(result).toContain('All Table References: MUST be fully qualified: `DATABASE_NAME.SCHEMA_NAME.TABLE_NAME`'); + + // Ensure column references use table aliases (not full qualifiers) + expect(result).toContain('All Column References: MUST be qualified with their table alias (e.g., `c.customerid`)'); + + // Ensure examples show table alias usage without full qualification + expect(result).toContain('c.customerid'); + expect(result).not.toContain('postgres.ont_ont.customer.customerid'); + + // Ensure CTE examples use table aliases correctly + expect(result).toContain('SELECT c.customerid FROM DATABASE.SCHEMA.TABLE1 c'); + expect(result).toContain('c.customerid`, not just `customerid`'); + }); + + it(`should use column names qualified with table aliases in ${modeName} mode`, () => { + const result = getThinkAndPrepAgentSystemPrompt('Test guidance', mode as 'standard' | 'investigation'); + + // Check for the updated description + expect(result).toContain('Use column names qualified with table aliases'); + + // Ensure the old verbose description is not present + expect(result).not.toContain('Use fully qualified column names with table aliases'); + }); + }); }); diff --git a/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-investigation-prompt.txt b/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-investigation-prompt.txt index b93127750..abd4b6bce 100644 --- a/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-investigation-prompt.txt +++ b/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-investigation-prompt.txt @@ -588,12 +588,12 @@ If all true → proceed to submit prep for Asset Creation with `submitThoughts`. - Strict JOINs: Only join tables where relationships are explicitly defined via `relationships` or `entities` keys in the provided data context/metadata. Do not join tables without a pre-defined relationship. - SQL Requirements: - Use database-qualified schema-qualified table names (`..`). - - Use fully qualified column names with table aliases (e.g., `.`). + - Use column names qualified with table aliases (e.g., `.`). - MANDATORY SQL NAMING CONVENTIONS: - All Table References: MUST be fully qualified: `DATABASE_NAME.SCHEMA_NAME.TABLE_NAME`. - - All Column References: MUST be qualified with their table alias (e.g., `alias.column_name`) or CTE name (e.g., `cte_alias.column_name_from_cte`). - - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT t.column1 FROM DATABASE.SCHEMA.TABLE1 t ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `t.column1`, not just `column1`). This applies even if the CTE is simple and selects from only one table. - - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column1 FROM my_cte mc ...`). + - All Column References: MUST be qualified with their table alias (e.g., `c.customerid`) or CTE name (e.g., `cte_alias.column_name_from_cte`). + - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT c.customerid FROM DATABASE.SCHEMA.TABLE1 c ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `c.customerid`, not just `customerid`). This applies even if the CTE is simple and selects from only one table. + - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column_name FROM my_cte mc ...`). - Universal Application: These naming conventions are strict requirements and apply universally to all parts of the SQL query, including every CTE definition and every subsequent SELECT statement. Non-compliance will lead to errors. - Context Adherence: Strictly use only columns that are present in the data context provided by search results. Never invent or assume columns. - Select specific columns (avoid `SELECT *` or `COUNT(*)`). diff --git a/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-standard-prompt.txt b/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-standard-prompt.txt index 0e0dcbb11..9a1a38b67 100644 --- a/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-standard-prompt.txt +++ b/packages/ai/src/agents/think-and-prep-agent/think-and-prep-agent-standard-prompt.txt @@ -465,12 +465,12 @@ When in doubt, be more thorough rather than less. Reports are the default becaus - Strict JOINs: Only join tables where relationships are explicitly defined via `relationships` or `entities` keys in the provided data context/metadata. Do not join tables without a pre-defined relationship. - SQL Requirements: - Use database-qualified schema-qualified table names (`..`). - - Use fully qualified column names with table aliases (e.g., `.`). + - Use column names qualified with table aliases (e.g., `.`). - MANDATORY SQL NAMING CONVENTIONS: - All Table References: MUST be fully qualified: `DATABASE_NAME.SCHEMA_NAME.TABLE_NAME`. - - All Column References: MUST be qualified with their table alias (e.g., `alias.column_name`) or CTE name (e.g., `cte_alias.column_name_from_cte`). - - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT t.column1 FROM DATABASE.SCHEMA.TABLE1 t ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `t.column1`, not just `column1`). This applies even if the CTE is simple and selects from only one table. - - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column1 FROM my_cte mc ...`). + - All Column References: MUST be qualified with their table alias (e.g., `c.customerid`) or CTE name (e.g., `cte_alias.column_name_from_cte`). + - Inside CTE Definitions: When defining a CTE (e.g., `WITH my_cte AS (SELECT c.customerid FROM DATABASE.SCHEMA.TABLE1 c ...)`), all columns selected from underlying database tables MUST use their table alias (e.g., `c.customerid`, not just `customerid`). This applies even if the CTE is simple and selects from only one table. + - Selecting From CTEs: When selecting from a defined CTE, use the CTE's alias for its columns (e.g., `SELECT mc.column_name FROM my_cte mc ...`). - Universal Application: These naming conventions are strict requirements and apply universally to all parts of the SQL query, including every CTE definition and every subsequent SELECT statement. Non-compliance will lead to errors. - Context Adherence: Strictly use only columns that are present in the data context provided by search results. Never invent or assume columns. - Select specific columns (avoid `SELECT *` or `COUNT(*)`).