autosnippet
Version:
Extract code patterns into a knowledge base for AI coding assistants
133 lines (132 loc) • 4.44 kB
TypeScript
/**
* CleanupService — 统一数据清理策略(垃圾桶模式)
*
* 提供两种清理模式:
* - fullReset(): 全量清理 — 将旧数据打包到时间戳垃圾桶文件夹,DB 表清空
* - rescanClean(): Rescan 清理 — 保留 Recipe,清除衍生缓存
* - snapshotRecipes(): 快照当前活跃 Recipe 信息
* - purgeExpiredTrash(): 清除超时限的垃圾桶文件夹
*
* 垃圾桶设计:
* - 位于 .autosnippet/.trash/<ISO-timestamp>/ 下
* - fullReset 时先将 candidates/ recipes/ skills/ wiki/ 移入垃圾桶,再清 DB
* - DB 数据导出为 db-snapshot.jsonl 保存在垃圾桶内
* - 超过保留天数(默认 7 天)的垃圾桶在下次 fullReset 或服务启动时自动清除
* - 暂不提供恢复功能(需要 merge 处理过于复杂)
*
* 保留原则:
* - 配置数据 (config.json, constitution.yaml, boxspec.json) 永不清理
* - IDE 集成配置 (.vscode/, .cursor/, .github/) 永不清理
* - 交付物 (.cursor/rules/autosnippet-*) 由 R4 重建,不在此清理
*
* @module service/cleanup/CleanupService
*/
/** Logger 接口 */
interface CleanupLogger {
info(msg: string, meta?: Record<string, unknown>): void;
warn(msg: string, meta?: Record<string, unknown>): void;
}
/** 清理结果 */
export interface CleanupResult {
deletedFiles: number;
clearedTables: string[];
preservedRecipes: number;
errors: string[];
/** 垃圾桶信息(fullReset 时填充) */
trash?: {
/** 垃圾桶文件夹路径 */
folder: string;
/** 移入垃圾桶的文件/目录数 */
movedItems: number;
/** DB 快照行数 */
dbSnapshotRows: number;
};
/** 本次清除的过期垃圾桶 */
purgedTrash?: {
/** 清除的垃圾桶数 */
count: number;
/** 释放的磁盘空间估算 (bytes) */
freedBytes: number;
};
}
/** Recipe 快照条目 */
export interface RecipeSnapshotEntry {
id: string;
title: string;
trigger: string;
category: string;
knowledgeType: string;
doClause: string;
sourceFile?: string;
lifecycle: string;
/** Recipe 完整内容 (JSON parsed) — Evolution Agent 需要 */
content?: {
markdown?: string;
rationale?: string;
coreCode?: string;
};
/** 源文件引用列表 (JSON parsed) — Evolution Agent 需要 */
sourceRefs?: string[];
}
/** Recipe 快照 */
export interface RecipeSnapshot {
count: number;
entries: RecipeSnapshotEntry[];
coverageByDimension: Record<string, number>;
}
export declare class CleanupService {
#private;
constructor(opts: {
projectRoot: string;
db?: unknown;
logger?: CleanupLogger;
});
/** 更新 DB 引用(fullReset 后重连时调用) */
setDb(db: unknown): void;
/**
* 全量清理 — 用于 bootstrap 冷启动(垃圾桶模式)
*
* 流程:
* 1. 先清除过期垃圾桶(超过 TRASH_RETENTION_DAYS)
* 2. 创建时间戳垃圾桶文件夹
* 3. 将 candidates/ recipes/ skills/ wiki/ 移入垃圾桶
* 4. 导出 DB 关键表数据到 db-snapshot.jsonl
* 5. 清空 DB 所有数据表
* 6. 清除向量索引、bootstrap-report、logs 等缓存
*
* 保留: config.json、constitution.yaml、boxspec.json、IDE 配置
*/
fullReset(): Promise<CleanupResult>;
/**
* Rescan 清理 — 保留 Recipe,清除衍生缓存
*
* 清除: 衍生 DB 表、pending/rejected/deprecated 知识条目、
* candidates/、skills/、wiki/、向量索引、bootstrap-report
* 保留: recipes/、active/published/staging/evolving 知识条目、
* knowledge_edges、evolution_proposals
*/
rescanClean(): Promise<CleanupResult>;
/**
* 快照当前活跃 Recipe 信息
* 用于 rescan 前记录保留的知识条目
*/
snapshotRecipes(): Promise<RecipeSnapshot>;
/**
* 清除超过保留期限的垃圾桶文件夹
* 可在服务启动时或 fullReset 前调用
*/
purgeExpiredTrash(): {
count: number;
freedBytes: number;
folders: string[];
};
/**
* 列出当前所有垃圾桶(供 Dashboard 展示)
*/
listTrashFolders(): Array<{
name: string;
createdAt: Date;
sizeMB: number;
}>;
}
export {};