@remnic/core 9.3.573 → 9.3.575

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/semantic-rule-promotion.ts"],"sourcesContent":["import { StorageManager } from \"./storage.js\";\nimport type { MemoryFile, MemoryLink } from \"./types.js\";\n\nexport interface SemanticRulePromotionCandidate {\n id: string;\n sourceMemoryId: string;\n content: string;\n confidence: number;\n tags: string[];\n memoryKind: \"note\";\n lineage: string[];\n}\n\nexport interface SemanticRulePromotionSkip {\n sourceMemoryId: string;\n reason:\n | \"disabled\"\n | \"source-memory-missing\"\n | \"source-memory-forgotten\"\n | \"source-memory-not-episode\"\n | \"no-explicit-rule\"\n | \"duplicate-rule\";\n existingRuleId?: string;\n}\n\nexport interface SemanticRulePromotionReport {\n enabled: boolean;\n dryRun: boolean;\n promoted: SemanticRulePromotionCandidate[];\n skipped: SemanticRulePromotionSkip[];\n}\n\nfunction normalizeRuleWhitespace(value: string): string {\n return value.replace(/\\s+/g, \" \").trim();\n}\n\nfunction stripTrailingClausePunctuation(value: string): string {\n return value.replace(/[,:;]+$/g, \"\").trim();\n}\n\nfunction canonicalizeRuleContent(value: string): string {\n return extractExplicitIfThenRule(value) ?? normalizeRuleWhitespace(value);\n}\n\nfunction canonicalizeRuleKey(value: string): string {\n return canonicalizeRuleContent(value).toLowerCase();\n}\n\nfunction extractExplicitIfThenRule(content: string): string | null {\n const match = content.match(/\\bif\\b([\\s\\S]+?)\\bthen\\b([\\s\\S]+?)(?:[.!?](?:\\s|$)|$)/i);\n if (!match) return null;\n const condition = stripTrailingClausePunctuation(normalizeRuleWhitespace(match[1] ?? \"\"));\n const outcome = stripTrailingClausePunctuation(normalizeRuleWhitespace(match[2] ?? \"\"));\n if (condition.length === 0 || outcome.length === 0) return null;\n return `IF ${condition} THEN ${outcome}.`;\n}\n\nfunction promotionConfidence(memory: MemoryFile): number {\n const base = Number.isFinite(memory.frontmatter.confidence) ? memory.frontmatter.confidence : 0.8;\n return Math.max(0.6, Math.min(0.98, base));\n}\n\nfunction promotionTags(memory: MemoryFile): string[] {\n return Array.from(new Set([...(memory.frontmatter.tags ?? []), \"semantic-rule\", \"promoted-rule\"]));\n}\n\nfunction buildSupportLinks(sourceMemoryId: string, confidence: number): MemoryLink[] {\n return [\n {\n targetId: sourceMemoryId,\n linkType: \"supports\",\n strength: confidence,\n reason: \"Promoted from verified episodic memory\",\n },\n ];\n}\n\nexport async function promoteSemanticRuleFromMemory(options: {\n memoryDir: string;\n enabled: boolean;\n sourceMemoryId: string;\n dryRun?: boolean;\n}): Promise<SemanticRulePromotionReport> {\n const report: SemanticRulePromotionReport = {\n enabled: options.enabled,\n dryRun: options.dryRun === true,\n promoted: [],\n skipped: [],\n };\n if (!options.enabled) {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"disabled\",\n });\n return report;\n }\n\n const storage = new StorageManager(options.memoryDir);\n const sourceMemory = await storage.getMemoryById(options.sourceMemoryId);\n if (!sourceMemory) {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"source-memory-missing\",\n });\n return report;\n }\n if (sourceMemory.frontmatter.status === \"forgotten\") {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"source-memory-forgotten\",\n });\n return report;\n }\n if (\n sourceMemory.frontmatter.status === \"archived\" ||\n sourceMemory.frontmatter.memoryKind !== \"episode\"\n ) {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"source-memory-not-episode\",\n });\n return report;\n }\n\n const content = extractExplicitIfThenRule(sourceMemory.content);\n if (!content) {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"no-explicit-rule\",\n });\n return report;\n }\n\n const ruleKey = canonicalizeRuleKey(content);\n const existingRule = (await storage.readAllMemories()).find(\n (memory) =>\n memory.frontmatter.category === \"rule\" &&\n memory.frontmatter.status !== \"archived\" &&\n memory.frontmatter.status !== \"forgotten\" &&\n canonicalizeRuleKey(memory.content) === ruleKey,\n );\n if (existingRule) {\n report.skipped.push({\n sourceMemoryId: options.sourceMemoryId,\n reason: \"duplicate-rule\",\n existingRuleId: existingRule.frontmatter.id,\n });\n return report;\n }\n\n const confidence = promotionConfidence(sourceMemory);\n const candidateBase = {\n sourceMemoryId: options.sourceMemoryId,\n content,\n confidence,\n tags: promotionTags(sourceMemory),\n memoryKind: \"note\" as const,\n lineage: [options.sourceMemoryId],\n };\n\n if (options.dryRun === true) {\n report.promoted.push({\n id: `dry-run:${options.sourceMemoryId}`,\n ...candidateBase,\n });\n return report;\n }\n\n const id = await storage.writeMemory(\"rule\", content, {\n confidence,\n tags: candidateBase.tags,\n source: \"semantic-rule-promotion\",\n lineage: candidateBase.lineage,\n sourceMemoryId: options.sourceMemoryId,\n memoryKind: \"note\",\n links: buildSupportLinks(options.sourceMemoryId, confidence),\n });\n report.promoted.push({\n id,\n ...candidateBase,\n });\n return report;\n}\n"],"mappings":";;;;;AAgCA,SAAS,wBAAwB,OAAuB;AACtD,SAAO,MAAM,QAAQ,QAAQ,GAAG,EAAE,KAAK;AACzC;AAEA,SAAS,+BAA+B,OAAuB;AAC7D,SAAO,MAAM,QAAQ,YAAY,EAAE,EAAE,KAAK;AAC5C;AAEA,SAAS,wBAAwB,OAAuB;AACtD,SAAO,0BAA0B,KAAK,KAAK,wBAAwB,KAAK;AAC1E;AAEA,SAAS,oBAAoB,OAAuB;AAClD,SAAO,wBAAwB,KAAK,EAAE,YAAY;AACpD;AAEA,SAAS,0BAA0B,SAAgC;AACjE,QAAM,QAAQ,QAAQ,MAAM,wDAAwD;AACpF,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,YAAY,+BAA+B,wBAAwB,MAAM,CAAC,KAAK,EAAE,CAAC;AACxF,QAAM,UAAU,+BAA+B,wBAAwB,MAAM,CAAC,KAAK,EAAE,CAAC;AACtF,MAAI,UAAU,WAAW,KAAK,QAAQ,WAAW,EAAG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