From 96c580080738ce8c6e2a77d9f423fc24b619eb47 Mon Sep 17 00:00:00 2001 From: Manoj K Date: Tue, 2 Sep 2025 21:47:45 +0530 Subject: [PATCH] Fix: invalidation of evolved facts --- .../app/services/knowledgeGraph.server.ts | 18 ++++++++++++----- .../webapp/app/services/prompts/statements.ts | 20 +++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/apps/webapp/app/services/knowledgeGraph.server.ts b/apps/webapp/app/services/knowledgeGraph.server.ts index 425c1f7..1dbab6c 100644 --- a/apps/webapp/app/services/knowledgeGraph.server.ts +++ b/apps/webapp/app/services/knowledgeGraph.server.ts @@ -101,17 +101,21 @@ export class KnowledgeGraphService { }; } - logger.log(`Analyzing ${previousStatements.length} statements from previous document version`); + logger.log( + `Analyzing ${previousStatements.length} statements from previous document version`, + ); // Step 2: Generate embedding for new document content - const newDocumentEmbedding = await this.getEmbedding(params.newDocumentContent); + const newDocumentEmbedding = await this.getEmbedding( + params.newDocumentContent, + ); // Step 3: For each statement, check if it's still semantically supported by new content for (const statement of previousStatements) { try { // Generate embedding for the statement fact const statementEmbedding = await this.getEmbedding(statement.fact); - + // Calculate semantic similarity between statement and new document const semanticSimilarity = this.calculateCosineSimilarity( statementEmbedding, @@ -120,10 +124,14 @@ export class KnowledgeGraphService { if (semanticSimilarity < threshold) { invalidatedStatements.push(statement.uuid); - logger.log(`Invalidating statement: "${statement.fact}" (similarity: ${semanticSimilarity.toFixed(3)})`); + logger.log( + `Invalidating statement: "${statement.fact}" (similarity: ${semanticSimilarity.toFixed(3)})`, + ); } else { preservedStatements.push(statement.uuid); - logger.log(`Preserving statement: "${statement.fact}" (similarity: ${semanticSimilarity.toFixed(3)})`); + logger.log( + `Preserving statement: "${statement.fact}" (similarity: ${semanticSimilarity.toFixed(3)})`, + ); } } catch (error) { logger.error(`Error analyzing statement ${statement.uuid}:`, { error }); diff --git a/apps/webapp/app/services/prompts/statements.ts b/apps/webapp/app/services/prompts/statements.ts index 25f44ec..497d14a 100644 --- a/apps/webapp/app/services/prompts/statements.ts +++ b/apps/webapp/app/services/prompts/statements.ts @@ -214,8 +214,9 @@ export const resolveStatementPrompt = ( content: `You are a knowledge graph expert that analyzes statements to detect duplications and TRUE contradictions. You analyze multiple new statements against existing statements to determine whether the new statement duplicates any existing statement or ACTUALLY contradicts any existing statement. -CRITICAL: Distinguish between CONTRADICTIONS vs PROGRESSIONS: +CRITICAL: Distinguish between CONTRADICTIONS, SUPERSEDING EVOLUTION, and PROGRESSIONS: - CONTRADICTIONS: Statements that CANNOT both be true (mutually exclusive facts) +- SUPERSEDING EVOLUTION: Sequential changes where the new state invalidates the previous state (e.g., technology migrations, job changes, relationship status changes) - PROGRESSIONS: Sequential states or developments that CAN both be true (e.g., planning → execution, researching → deciding) @@ -247,12 +248,22 @@ TRUE CONTRADICTIONS (mark as contradictions): - "Project completed" vs "Project cancelled" (mutually exclusive outcomes) - "Caroline is single" vs "Caroline is married" (same time period, opposite states) +SUPERSEDING EVOLUTION (mark as contradictions - old statement becomes invalid): + - "Application built with NextJS" vs "Application migrated to Remix" (technology stack change) + - "John works at CompanyA" vs "John joined CompanyB" (job change invalidates previous employment) + - "Database uses MySQL" vs "Database migrated to PostgreSQL" (infrastructure change) + - "System deployed on AWS" vs "System moved to Google Cloud" (platform migration) + - "Caroline living in Boston" vs "Caroline moved to Seattle" (location change) + - "Project using Python" vs "Project rewritten in TypeScript" (language migration) + NOT CONTRADICTIONS (do NOT mark as contradictions): - "Caroline researching adoption agencies" vs "Caroline finalized adoption agency" (research → decision progression) - "Caroline planning camping next week" vs "Caroline went camping" (planning → execution progression) - "User studying Python" vs "User completed Python course" (learning progression) - "Meeting scheduled for 3pm" vs "Meeting was held at 3pm" (planning → execution) - "Considering job offers" vs "Accepted job offer" (consideration → decision) + - "Project in development" vs "Project launched" (development → deployment progression) + - "Learning React" vs "Built app with React" (skill → application progression) 5. MANDATORY OUTPUT FORMAT: @@ -278,10 +289,11 @@ CRITICAL FORMATTING RULES: - Include NO text before or after - Return valid JSON array with all statement IDs from NEW_STATEMENTS - If the new statement is a duplicate, include the UUID of the duplicate statement -- For TRUE contradictions only, list statement UUIDs that the new statement contradicts +- For TRUE contradictions AND superseding evolution, list statement UUIDs that the new statement contradicts - If a statement is both a contradiction AND a duplicate (rare case), mark it as a duplicate -- DO NOT mark progressions, temporal sequences, or state developments as contradictions -- ONLY mark genuine mutually exclusive facts as contradictions +- DO NOT mark progressions, temporal sequences, or cumulative developments as contradictions +- MARK superseding evolution (technology/job/location changes) as contradictions to invalidate old state +- ONLY mark genuine mutually exclusive facts and superseding evolution as contradictions `, }, {