recall

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package recall 实现音乐召回池:补全与 recent 命令共用的候选数据基础设施。

召回池聚合三类来源(主动/隐式/远端)的事件流,落 append-only JSONL, 按 frecency 排序供补全(--id <TAB>)与 recent 命令消费。三类来源用 Src 字段 区分,不拆三文件;容量 1000 行 drop oldest(FIFO),打分与裁剪解耦。

这是 PRD-0014 G 片段:本包只建池子(写 + 内存读 + frecency 排序), recent(#H)与补全挂载(#I)消费它。路径经 kit.HistoryPath()(PRD-0015 #44 已建可注入 seam),测试覆写 userConfigDir 即可重定向到临时目录。

Index

Constants

View Source
const MaxLines = 1000

MaxLines 是召回池 JSONL 的容量上限,超限 drop oldest(CONTEXT.md 召回池持久化段)。 约 80KB(每行 ~80B),单用户工具足够;打分与裁剪解耦,只有退出保留窗口的条目才丢。

View Source
const RemoteTTL = 24 * time.Hour

RemoteTTL 是远端快照的有效期,超期强制重拉(CONTEXT.md 预热与兜底段)。

Variables

This section is empty.

Functions

func NeedsRemoteRefresh

func NeedsRemoteRefresh(pool *Pool) bool

NeedsRemoteRefresh 判断是否需要重拉远端:池里最近的 remote 事件超过 TTL,或无 remote 事件。

决策依据是 remote 事件流的最新 TS——而非单独的「上次拉取时间」字段(避免新增元数据)。 主命令/裸跑 onboarding 调本函数决定是否起后台预热;返回 false 时不启动 goroutine。

func WarmBackground

func WarmBackground(pool *Pool, fetcher RemoteFetcher) (done <-chan struct{})

WarmBackground 起 fire-and-forget goroutine 拉远端快照并写入池子。

**绝不阻塞调用方**:立即返回,拉取在后台;kite 是 run-and-exit CLI,进程退出时 未完成的 Append 直接丢弃——tmp+rename 保证主文件不被半拉子数据污染(Pool.Append 已保证)。 fetcher 为 nil 或拉取失败:静默放弃,下次启动再试。

调用方(主命令/裸跑)负责先判 NeedsRemoteRefresh 决定是否调本函数。 返回的 done channel 在后台拉取完成(成功写盘或放弃)时关闭,供测试同步等待; 生产调用方可忽略返回值。

Types

type Event

type Event struct {
	ID     int64     `json:"id"`
	Name   string    `json:"name,omitempty"`
	Artist string    `json:"artist,omitempty"`
	Src    Src       `json:"src"`
	TS     time.Time `json:"ts"`
}

Event 是召回池事件流中的一条记录,对应 JSONL 一行。

ID 必填;Name/Artist 尽量填(补全候选的描述列),缺失留空。TS 是事件发生时间, frecency 据此算时段桶权重。Src 决定该事件的 src 权重。

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool 是召回池的本地存储,落 append-only JSONL。

一个 Pool 绑定一个文件路径(由 kit.HistoryPath() 提供,PRD-0015 #44 已建可注入 seam)。 Append 追加事件(原子 tmp+rename 保证主文件不被中断污染);Load 读全事件流 (单行损坏只丢该行);TopN 读 + frecency 打分返回 top N 候选。

Pool 不负责埋点触发(那在 kit.Record helper,#46 方案 c)与远端预热(那在 warmer, 后台 fire-and-forget)——本类型只管本地 JSONL 读写。

func NewPool

func NewPool(path func() (string, error)) *Pool

NewPool 用给定路径函数构造 Pool(now 默认 time.Now)。

func (*Pool) Append

func (p *Pool) Append(events ...Event) error

Append 把事件追加到 JSONL,并在超容量时 drop oldest。

原子写:不直接 append 到主文件(避免并发/中断留半行),而是读全量 + 追加新事件 + 裁剪 + 写 tmp + rename。kite 是 run-and-exit 单进程,无并发,但仍用 tmp+rename 保证进程被杀时主文件完整(沿 kit.SaveSession 模式)。

单次 Append 多个事件时一起裁剪。失败返回 error(调用方决定是否告警,埋点失败不阻塞主命令)。

func (*Pool) Load

func (p *Pool) Load() ([]Ranked, error)

Load 读全事件流,按 frecency 降序返回候选。

单行损坏只丢该行(坏行跳过),不报错——召回池历史可被 grep/tail 直读,容忍人工编辑。 文件不存在视为空池(新用户)。

func (*Pool) TopN

func (p *Pool) TopN(n int) ([]Ranked, error)

TopN 读事件流并返回 frecency top N 候选(补全/recent 用)。 N<=0 或不足时返回实际数量。

func (*Pool) WithClock

func (p *Pool) WithClock(now func() time.Time) *Pool

WithClock 注入时钟(测试用),返回新 Pool。

type Ranked

type Ranked struct {
	Event Event   `json:"-"`
	Score float64 `json:"score"`
}

Ranked 是 frecency 打分后的候选,补全与 recent 消费。 Score 是聚合后的 frecency 分数(越高越靠前);Event 是该 id 的代表性事件 (取最新一条,Name/Artist 优先非空)。

func Score

func Score(events []Event, now time.Time) []Ranked

Score 对事件流按 id 聚合,返回按 frecency 降序排列的候选。

每个事件贡献 score = srcWeight[src] × timeBucket[ts, now];同一 id 的所有事件 求和得该 id 的总 score。代表性 Event 取该 id 最新一条,Name/Artist 优先非空 (旧事件可能缺 name,新事件补上)。

纯函数(now 注入保证确定性),存储不变、无新字段——分数仅在装载时算出。 空事件流返回 nil。

type RemoteFetcher

type RemoteFetcher interface {
	Fetch() ([]Event, error)
}

RemoteFetcher 拉取远端快照事件(红心/歌单/每日推荐),供预热后台 goroutine 调用。

生产实现接网易云 endpoint(需登录态);测试用 fake。返回 error 时预热静默放弃 (fire-and-forget,失败不影响主命令;下次启动再试)。

type RemoteFetcherFunc

type RemoteFetcherFunc func() ([]Event, error)

RemoteFetcherFunc 让普通函数实现 RemoteFetcher。

func (RemoteFetcherFunc) Fetch

func (f RemoteFetcherFunc) Fetch() ([]Event, error)

type Src

type Src string

Src 标识事件来源类型,影响 frecency 的 src 权重。

权重分级(Mozilla frecency 访问类型加权):

  • SrcPlay/SrcDownload(主动消费,强意图)权重最高
  • SrcSearch/SrcDetail(显式查询)次之
  • SrcRemote(被动快照)最低
const (
	// SrcPlay 播放成功(song play 的 --id 隐式埋点)。
	SrcPlay Src = "play"
	// SrcDownload 下载成功(song download / playlist download 的 --id 隐式埋点)。
	SrcDownload Src = "download"
	// SrcSearch 主动搜索(search 命令的 keyword 记录为候选,命中详情后补 id)。
	SrcSearch Src = "search"
	// SrcDetail 主动详情查询(song detail 的 --id 显式记录)。
	SrcDetail Src = "detail"
	// SrcRemote 远端快照(红心/歌单/每日推荐,24h TTL,后台预热拉取)。
	SrcRemote Src = "remote"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL