Files
memoryweave/go/internal/distill/bitemporal.go
T

159 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 织忆 MemoryWeave — bi-temporal 有效期检测 + provenance(t_3ada332b,血统 Zep/Graphiti)
//
// 设计(方案C「长期正确性」):时间敏感事实用【有效区间】(valid_from/valid_to) 而非删除。
// 双时间线:episode T'(CreatedAt,何时说的)+ 语义 T(valid_to 表达的失效终点)。
// non-lossy:失效只标区间,历史永久保留(可追溯、当前状态干净)。
// provenance:来源类型枚举 user|inferred|import —— 新语句不自动覆盖旧的(来源权威性)。
//
// ⚠️ 误报刹车(AC 止损线):时间敏感识别误报(把常驻事实标成临时的)>5% → 收紧识别规则
// 只认【强时间词】+【带触发上下文的显式日期】,宽口径一律不进(宁漏不误伤)。
// 返回空串 = 永久有效(NULL 语义,读路径不降权)。
package distill
import (
"regexp"
"strings"
"time"
)
// 显式日期(绝对日期)触发词:只有紧邻这些上下文才解析,防把「生日是3月15日」这类常驻事实标成临时。
var dateTriggerRe = regexp.MustCompile(
`截止到|截止|截至|到|持续到|有效到|用到|在|于|(之前|前|为止|为止\b)`)
// dateTokenRe 匹配一个日期 token(含触发上下文内/前的日期片段)。
// 支持:2006-01-02 / 2006/01/02 / 2006年01月02日 / 01月02日 / 1月2号 / 1月2日
var dateTokenRe = regexp.MustCompile(
`(?:(\d{4})[年/\-])?(\d{1,2})月(\d{1,2})[日号]|(\d{4})[-/](\d{1,2})[-/](\d{1,2})`)
// DetectValidTo 识别时间敏感事实的失效终点(RFC3339;空串 = 永久有效)。
// now 为「本事实写入时刻」(episode T' 的审计时间),据此计算语义 T 的区间末端。
func DetectValidTo(content string, now time.Time) string {
// 0) 强时间词先于显式日期("截止到这周" 这类,「这周」优先给周末)
// 只认【绝对强词】,避免「下」「这」单独出现的误判。
if s := matchStrongTimeWord(content, now); s != "" {
return s
}
// 1) 带触发上下文的显式日期(要求有 截止/到/之前/为止 等前后缀,否则太宽进)
if !dateTriggerRe.MatchString(content) {
return ""
}
if d := parseExplicitDate(content, now); d != "" {
return d
}
return ""
}
// DetectProvenance 从写入来源(source)字符串归一到 provenance 三枚举。
// 映射口径:显式 user/口头/直接用户 段 → user;import/导入/wiki(笔记通道) → import;
// agent/推断/inferred/llm/蒸馏 → inferred;其余兜底 → inferred。
func DetectProvenance(source string) string {
s := strings.ToLower(source)
switch {
case containsAny(s, []string{"user", "口头", "直接用户", "user_said", "直接说"}):
return "user"
case containsAny(s, []string{"import", "导入", "wiki:", "wiki/", "笔记通道", "从外部"}):
return "import"
case containsAny(s, []string{"agent", "inferred", "推断", "infer", "llm", "distill", "蒸馏", "autogen"}):
return "inferred"
default:
return "inferred"
}
}
// matchStrongTimeWord 只认强时间词,命中即返回对应区间末端。
func matchStrongTimeWord(content string, now time.Time) string {
// 顺序:长词在前避免「这个月」被「这」提前命中(我们只精确匹配完整词)。
type kw struct {
word string
end func(time.Time) time.Time
}
table := []kw{
{"这个月", endOfThisMonth},
{"本月", endOfThisMonth},
{"下周", endOfNextWeek},
{"这周", endOfThisWeek},
{"本周", endOfThisWeek},
{"今晚", endOfToday},
{"今天", endOfToday},
{"今日", endOfToday},
{"明天", endOfTomorrow},
{"临时", addDays7},
{"暂定", addDays7},
{"暂时", addDays7},
}
for _, k := range table {
if strings.Contains(content, k.word) {
return k.end(now).Format(time.RFC3339)
}
}
return ""
}
func endOfToday(now time.Time) time.Time {
return dayEnd(now)
}
func endOfTomorrow(now time.Time) time.Time {
return dayEnd(now.AddDate(0, 0, 1))
}
func endOfThisWeek(now time.Time) time.Time {
// 周日起 0,距下周日天数 = (7 - weekday) % 7(今天周日 → 0 → 今天)
days := (7 - int(now.Weekday())) % 7
return dayEnd(now.AddDate(0, 0, days))
}
func endOfNextWeek(now time.Time) time.Time {
days := (7 - int(now.Weekday())) % 7
return dayEnd(now.AddDate(0, 0, days+7))
}
func endOfThisMonth(now time.Time) time.Time {
first := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
return dayEnd(first.AddDate(0, 1, -1)) // 下月1号 -1天 = 月末
}
func addDays7(now time.Time) time.Time {
return now.Add(7 * 24 * time.Hour)
}
func dayEnd(t time.Time) time.Time {
y, m, d := t.Date()
return time.Date(y, m, d, 23, 59, 59, 0, t.Location())
}
// parseExplicitDate 在带触发词的上下文里找日期 token 并解析为「当日 23:59:59」。
func parseExplicitDate(content string, now time.Time) string {
loc := now.Location()
for _, m := range dateTokenRe.FindAllStringSubmatch(content, -1) {
var y, mo, d int
switch {
case m[2] != "" && m[3] != "":
// 月日式:M月D日 / M月D号(m[1] 可空=今年)
mo = atoiSafe(m[2])
d = atoiSafe(m[3])
y = now.Year()
if m[1] != "" {
y = atoiSafe(m[1])
}
case m[4] != "" && m[5] != "" && m[6] != "":
// 完整式:YYYY-MM-DD / YYYY/MM/DD
y = atoiSafe(m[4])
mo = atoiSafe(m[5])
d = atoiSafe(m[6])
default:
continue
}
if mo < 1 || mo > 12 || d < 1 || d > 31 {
continue
}
return dayEnd(time.Date(y, time.Month(mo), d, 0, 0, 0, 0, loc)).Format(time.RFC3339)
}
return ""
}
func atoiSafe(s string) int {
n := 0
for _, c := range s {
if c < '0' || c > '9' {
return 0
}
n = n*10 + int(c-'0')
}
return n
}