#!/usr/bin/env node /** * 过期容器回收(2026-12-04)—— 默认只读,`--apply` 才执行。 * * ## 规则(用户裁决) * * 到期 → 能力立刻失效(各接口读时判定,不靠本脚本) * 到期满 7 天未续费 → 本脚本回收: * ① R730 上 docker stop + docker rm(容器消失) * ② 会话数据软删(FmodeChatThread 按 agentId)—— 用户再开通时不串旧数据 * ③ 平台记录软删 + accessState=revoked;绑定 revoked * ④ 数据目录默认**保留**并打印路径,`--purge-data` 才删除 * * 用法: * node scripts/recycle-expired.mjs # 干跑,列出清单 * node scripts/recycle-expired.mjs --apply # 执行 * node scripts/recycle-expired.mjs --apply --days=7 # 自定义宽限天数 * node scripts/recycle-expired.mjs --apply --purge-data # 连数据目录一起删 */ import fs from 'node:fs'; import { execFileSync } from 'node:child_process'; import { resolve } from 'node:path'; const ROOT = process.cwd(); const argv = process.argv.slice(2); const has = (f) => argv.includes(f); const arg = (f, d) => argv.find((a) => a.startsWith(`--${f}=`))?.split('=')[1] ?? d; const APPLY = has('--apply'); const PURGE = has('--purge-data'); const DAYS = Number(arg('days', '7')); const R730 = resolve(ROOT, '.local-backup/skill-lock-20261201/r730.py'); const cfg = JSON.parse(fs.readFileSync(resolve(ROOT, '.claude/dev-login.json'), 'utf8')); const H = { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster', 'X-Parse-Master-Key': cfg.masterKey }; const B = 'https://server.fmode.cn/parse/classes'; const now = Date.now(); const GRACE_MS = DAYS * 86400_000; const list = async (cls, where, keys) => (await (await fetch(`${B}/${cls}?where=${encodeURIComponent(JSON.stringify(where))}&limit=500${keys ? '&keys=' + keys : ''}`, { headers: H })).json()).results ?? []; const put = async (cls, id, body) => (await fetch(`${B}/${cls}/${id}`, { method: 'PUT', headers: H, body: JSON.stringify(body) })).status; /* 1. 找候选:已过期且超过宽限期 */ const agents = await list('FmodeAgent', { isDeleted: { $ne: true } }, 'objectId,name,agentId,planCode,user,accessExpiresAt,trialEndsAt,accessState'); const due = []; for (const a of agents) { const raw = a.accessExpiresAt?.iso || a.trialEndsAt?.iso; const exp = raw ? Date.parse(raw) : 0; if (!exp || now - exp <= GRACE_MS) continue; due.push({ ...a, exp, overDays: Math.floor((now - exp) / 86400_000) }); } console.log(`候选 ${due.length} 条(宽限 ${DAYS} 天,当前时间 ${new Date(now).toISOString()})\n`); if (!due.length) { console.log('没有需要回收的容器。'); process.exit(0); } /* 2. 附带信息:会话数 */ for (const a of due) { const threads = await list('FmodeChatThread', { agentId: a.agentId, isDeleted: { $ne: true } }, 'objectId'); a.threadCount = threads.length; const isPlatform = (a.planCode === 'advanced' || a.planCode === 'enterprise'); console.log( ` ${a.name || a.objectId} | ${a.agentId} | ${a.planCode} | 超期 ${a.overDays} 天 | 会话 ${a.threadCount} 条 | ${isPlatform ? '平台容器' : '本地容器(无容器可停)'}`, ); } if (!APPLY) { console.log('\n[干跑] 未加 --apply,未做任何修改。'); process.exit(0); } /* 3. 执行 */ const platformAgents = due.filter((a) => a.planCode === 'advanced' || a.planCode === 'enterprise' || a.agentId); if (platformAgents.length) { const lines = ['set -e']; for (const a of platformAgents) { lines.push(`echo "[RECYCLE] ${a.agentId}"`); lines.push(`docker stop ${a.agentId} >/dev/null 2>&1 || true`); lines.push(`docker rm ${a.agentId} >/dev/null 2>&1 || echo " (容器已不在)"`); if (PURGE) lines.push(`[ -d /opt/fmode-containers/${a.agentId} ] && rm -rf /opt/fmode-containers/${a.agentId} && echo " [PURGE] 数据目录已删" || true`); else lines.push(`[ -d /opt/fmode-containers/${a.agentId} ] && echo " [KEEP] 数据目录保留: /opt/fmode-containers/${a.agentId}" || true`); } const f = resolve(ROOT, `.local-release/recycle-${Date.now()}.sh`); fs.mkdirSync(resolve(ROOT, '.local-release'), { recursive: true }); fs.writeFileSync(f, lines.join('\n')); const out = execFileSync('python', [R730, f, '180'], { encoding: 'utf8', cwd: ROOT }); console.log(out.split('\n').filter((l) => l.includes('[') || l.includes('已不在') || l.includes('KEEP') || l.includes('PURGE')).join('\n')); } for (const a of due) { /* 会话数据软删:用户再开通时不串旧数据 */ const threads = await list('FmodeChatThread', { agentId: a.agentId, isDeleted: { $ne: true } }, 'objectId'); for (const t of threads) await put('FmodeChatThread', t.objectId, { isDeleted: true }); /* 平台记录与绑定 */ await put('FmodeAgent', a.objectId, { isDeleted: true, accessState: 'revoked' }); const binds = await list('FmodeMatrixBinding', { agentId: a.agentId }, 'objectId,status'); for (const b of binds) await put('FmodeMatrixBinding', b.objectId, { status: 'revoked' }); console.log(`[DONE] ${a.name || a.objectId}:会话软删 ${threads.length} 条,记录 revoked,绑定 ${binds.length} 条`); } console.log('\n回收完成。审计记录已打印在终端,按规范需补写操作追踪。');