cleanup-task-state.js 1.57 KB
#!/usr/bin/env node
/**
 * 任务态清理的命令行入口(28 §5.6)。
 *
 *   node scripts/cleanup-task-state.js                 # dry-run,14 天窗口
 *   node scripts/cleanup-task-state.js --days 30       # 换窗口
 *   node scripts/cleanup-task-state.js --apply         # 真删
 *   node scripts/cleanup-task-state.js --why           # 连「为什么没清」一起打
 *
 * **公司态永不自动清理**(`memory/` / `conversations/`)—— 那是 dream 的唯一原料,
 * 删掉不可再生。判据与保护名单都在 server/services/task-state-cleanup.js,改之前先读文件头。
 */
const cleanup = require('../server/services/task-state-cleanup');

const argv = process.argv.slice(2);
const has = (f) => argv.includes(f);
const val = (f, d) => { const i = argv.indexOf(f); return i >= 0 && argv[i + 1] ? Number(argv[i + 1]) : d; };

const apply = has('--apply');
const maxAgeDays = val('--days', cleanup.DEFAULT_MAX_AGE_DAYS);

const r = cleanup.sweep({ maxAgeDays, apply });

for (const c of r.candidates) console.log(`${apply ? '删' : '待删'} ${c}`);
if (has('--why')) {
  const by = new Map();
  for (const s of r.skipped) by.set(s.reason, (by.get(s.reason) || 0) + 1);
  console.log('\n跳过统计:');
  for (const [reason, n] of [...by].sort((a, b) => b[1] - a[1])) console.log(`  ${String(n).padStart(4)}  ${reason}`);
}
console.log(`\n窗口 ${maxAgeDays} 天:${apply ? `已删 ${r.removed.length}` : `待删 ${r.candidates.length}`} 个,跳过 ${r.skipped.length} 个`);
if (!apply && r.candidates.length) console.log('要真删:加 --apply');