feat(memory): retention_tier 档位列 + defer 与档位合并为同一判定(M37)
主人 2026-10-26 对「记忆分型」裁定「两列」:kind(关于什么)保留,新增 retention_tier(留多久 / 干什么用)。本片把这条轴从列、判定、写入、读侧过滤 一路打通,并把「判第五态 defer」与「判 retention_tier=temp」合并成一个判定。 结构(迁移 0026 + 自举前滚): - memory_items 新增 retention_tier TEXT NOT NULL DEFAULT ''(空串 = 尚未分型, 历史行与受限来源行),索引 idx_memory_items_retention_tier;up/down 均实跑过 (在隔离的生产库副本上 migrate → 建 Store → down → 再 up,5073 行零丢失)。 - NewStore 幂等前滚该列 + 建索引(ensureRetentionSchema),与迁移同形由测试钉住。 判定(retention.go,全仓唯一入口): - RetentionTier 闭集 temp/daily/work/long/knowledge + 空串未分型;与 kind 正交。 - resolveRetention 的优先级:调用方显式档位 > 显式有效期(⇒ temp)> 受限来源 不分型 > 留存规则 N1~N7 ⇒ temp > 默认 long。判据靠元数据,规则不许推翻显式档位。 - tempDeadline 是「档位 → expires_at」的唯一换算:temp ⇔ 有效期非空,两列同一笔 INSERT 落下(M2 登记过的「INSERT 与补写之间的短窗口」就此关闭)。 合并(defer ⇔ temp): - judgeIngest 不再跑留存规则,只读 Add 落定的 item.RetentionTier;规则调用点因此 全仓唯一(源码级断言钉住)。 - 判定不再依赖同步闸是否开启:闸未接/关着时档位与有效期照样落库。 - 异步层对已是 temp 的行不重复改写(避免 TTL 随审查延迟漂移)。 - IngestRuleVersion d3.3 → d3.4(判据作用范围变了,按既有硬规矩升版)。 读侧: - 四条列表读路径的 SELECT 与 scanItem 读出 retention_tier(14 列);Get 同样读回。 - 新增 ListByPersonaTiers / ListScopeTiers / ListTiers / ListProjectTiers(可选档位 过滤,缺省不过滤 ⇒ 历史行照常返回;显式传空串只取未分型;非法档位报错)。 原方法签名逐字不变 —— 网关 MemoryStore 接口钉着它们,可变参数会编译不过。 - store.go 的读路径拆到 store_list.go,守 500 行上限。 回归:go build ./... + go vet ./... + go test ./... 全绿(内存包另有 -race)。
This commit is contained in:
1 parent
240b59708d
commit
cc6904b1b4
23 files changed
+1592
-181
No files matched your search
@@ -78,7 +78,21 @@ func (s *Store) AddScoped(ctx context.Context, actorID string, item MemoryItem)
|
||||
// (expires_at / valid_to 均未到期)的行 —— 闸只有这里与 ListByPersona /
|
||||
// ListProject / List 四处,检索路径经 Retriever.visibleCandidates 组合调用
|
||||
// 本方法,因此自动获得同一判据(timegate.go 头注、design/03 §11.2)。
|
||||
//
|
||||
// `tiers` 的档位过滤见同族的 ListScopeTiers(签名不变的理由见
|
||||
// store_list.go 的 ListByPersona 注释:网关接口钉着这批精确签名)。
|
||||
func (s *Store) ListScope(ctx context.Context, scope Scope, personaID string) ([]MemoryItem, error) {
|
||||
return s.listScope(ctx, scope, personaID, nil)
|
||||
}
|
||||
|
||||
// ListScopeTiers 是 ListScope 的**可选档位过滤**版本(空 tiers = 不过滤;
|
||||
// `RetentionTierUnset` = 只看尚未分型的行)。
|
||||
func (s *Store) ListScopeTiers(ctx context.Context, scope Scope, personaID string, tiers ...RetentionTier) ([]MemoryItem, error) {
|
||||
return s.listScope(ctx, scope, personaID, tiers)
|
||||
}
|
||||
|
||||
// listScope 是 ListScope / ListScopeTiers 的共同实现(tiers 为空不过滤)。
|
||||
func (s *Store) listScope(ctx context.Context, scope Scope, personaID string, tiers []RetentionTier) ([]MemoryItem, error) {
|
||||
if strings.TrimSpace(personaID) == "" {
|
||||
return nil, ErrMissingPersona
|
||||
}
|
||||
@@ -86,11 +100,16 @@ func (s *Store) ListScope(ctx context.Context, scope Scope, personaID string) ([
|
||||
return nil, ErrInvalidScope
|
||||
}
|
||||
now := timeGateNow()
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT id, persona_id, role_id, project_id, scope, kind,
|
||||
query := `SELECT id, persona_id, role_id, project_id, scope, kind,
|
||||
content, tags, confidence, source_conversation, created_at, updated_at,
|
||||
decay_weight FROM memory_items WHERE scope = ? AND status = ?`+timeGateClause+`
|
||||
ORDER BY created_at DESC, rowid DESC`,
|
||||
string(scope), string(tidyStatusActive), now, now)
|
||||
decay_weight, retention_tier FROM memory_items WHERE scope = ? AND status = ?`
|
||||
args := []any{string(scope), string(tidyStatusActive)}
|
||||
query, args, err := retentionTierList(query, args, tiers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, query+timeGateClause+`
|
||||
ORDER BY created_at DESC, rowid DESC`, append(args, now, now)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("memory: list scope items: %w", err)
|
||||
}
|
||||
@@ -105,15 +124,34 @@ func (s *Store) ListScope(ctx context.Context, scope Scope, personaID string) ([
|
||||
// 记忆所有角色可读,因此不施加角色过滤。projectID 为空
|
||||
// 返回 ErrMissingProjectID。与其余三条列表读路径同口径:只返回在册且过
|
||||
// **时效闸**的行(timegate.go、design/03 §9.6/§11.2)。
|
||||
//
|
||||
// `tiers` 的档位过滤见同族的 ListProjectTiers —— 本方法虽然是深检索入口,但
|
||||
// 「留多久」这根轴对项目记忆同样成立,因此一并支持。
|
||||
func (s *Store) ListProject(ctx context.Context, projectID string) ([]MemoryItem, error) {
|
||||
return s.listProject(ctx, projectID, nil)
|
||||
}
|
||||
|
||||
// ListProjectTiers 是 ListProject 的**可选档位过滤**版本(空 tiers = 不过滤)。
|
||||
func (s *Store) ListProjectTiers(ctx context.Context, projectID string, tiers ...RetentionTier) ([]MemoryItem, error) {
|
||||
return s.listProject(ctx, projectID, tiers)
|
||||
}
|
||||
|
||||
// listProject 是 ListProject / ListProjectTiers 的共同实现(tiers 为空不过滤)。
|
||||
func (s *Store) listProject(ctx context.Context, projectID string, tiers []RetentionTier) ([]MemoryItem, error) {
|
||||
if strings.TrimSpace(projectID) == "" {
|
||||
return nil, ErrMissingProjectID
|
||||
}
|
||||
now := timeGateNow()
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT id, persona_id, role_id, project_id, scope, kind,
|
||||
query := `SELECT id, persona_id, role_id, project_id, scope, kind,
|
||||
content, tags, confidence, source_conversation, created_at, updated_at,
|
||||
decay_weight FROM memory_items WHERE project_id = ? AND status = ?`+timeGateClause+`
|
||||
ORDER BY created_at DESC, rowid DESC`, projectID, string(tidyStatusActive), now, now)
|
||||
decay_weight, retention_tier FROM memory_items WHERE project_id = ? AND status = ?`
|
||||
args := []any{projectID, string(tidyStatusActive)}
|
||||
query, args, err := retentionTierList(query, args, tiers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, query+timeGateClause+`
|
||||
ORDER BY created_at DESC, rowid DESC`, append(args, now, now)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("memory: list project items: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user