@fabianactor/dsh-lil-whale-group 1.12.0 → 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/code-fp-history-core.js +52 -0
- package/lib/index.js +26 -1
- package/lib/media-cache-core.js +17 -6
- package/package.json +1 -1
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// 代码指纹的历史账(2026-11-19,小鱼丸私聊第 53 行)。
|
|
2
|
+
//
|
|
3
|
+
// boot 行里的 `codeFp` 只回答"启动那一刻磁盘是什么样";拿它跟"启动那一刻的磁盘"比,
|
|
4
|
+
// 是拿一个量跟它的自述比 —— 恒真。要回答"这段代码改过没有",得像 log-format 那样
|
|
5
|
+
// 在值变化时留一行:账上有历史值,读的人就不用靠谁记性好。
|
|
6
|
+
//
|
|
7
|
+
// 这里只做纯计算;文件读写留在 index.js,方便测试。
|
|
8
|
+
|
|
9
|
+
export const CODE_FP_LEDGER = 'code-fp.json'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 比较上一次记下的指纹与这一次算出来的。
|
|
13
|
+
* @returns {{kind:'baseline',newFp:string,newFiles:number}
|
|
14
|
+
* | {kind:'same',newFp:string,newFiles:number}
|
|
15
|
+
* | {kind:'changed',oldFp:string,oldFiles:number,newFp:string,newFiles:number}}
|
|
16
|
+
*/
|
|
17
|
+
export function codeFpChangeOf(prev, current) {
|
|
18
|
+
const newFp = typeof current?.fp === 'string' && current.fp !== '' ? current.fp : 'unknown'
|
|
19
|
+
const newFiles = Number.isFinite(current?.files) ? current.files : 0
|
|
20
|
+
if (!prev || typeof prev.fp !== 'string' || prev.fp === '') {
|
|
21
|
+
return { kind: 'baseline', newFp, newFiles }
|
|
22
|
+
}
|
|
23
|
+
if (prev.fp === newFp) return { kind: 'same', newFp, newFiles }
|
|
24
|
+
return {
|
|
25
|
+
kind: 'changed',
|
|
26
|
+
oldFp: prev.fp,
|
|
27
|
+
oldFiles: Number.isFinite(prev.files) ? prev.files : 0,
|
|
28
|
+
newFp,
|
|
29
|
+
newFiles,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 变化时写一行;没变返回 null("没变不记事件")。 */
|
|
34
|
+
export function codeFpHistoryLine(change) {
|
|
35
|
+
if (change?.kind === 'baseline') {
|
|
36
|
+
return `note=code-fp-baseline fp=${change.newFp} files=${change.newFiles}`
|
|
37
|
+
}
|
|
38
|
+
if (change?.kind === 'changed') {
|
|
39
|
+
return (
|
|
40
|
+
`note=code-fp-changed old=${change.oldFp} new=${change.newFp}` +
|
|
41
|
+
` files-old=${change.oldFiles} files-new=${change.newFiles}`
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 账本里留的最新一份(下一次启动拿它当"上一次")。 */
|
|
48
|
+
export function codeFpLedgerOf(current, at) {
|
|
49
|
+
const fp = typeof current?.fp === 'string' && current.fp !== '' ? current.fp : 'unknown'
|
|
50
|
+
const files = Number.isFinite(current?.files) ? current.files : 0
|
|
51
|
+
return { fp, files, at }
|
|
52
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -12,6 +12,7 @@ import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renam
|
|
|
12
12
|
import { printableText } from './text-clean.js'
|
|
13
13
|
import { sayProblemOf, channelProblemOf } from './say-core.js'
|
|
14
14
|
import { codeFingerprint } from './code-fp.js'
|
|
15
|
+
import { codeFpChangeOf, codeFpHistoryLine, codeFpLedgerOf, CODE_FP_LEDGER } from './code-fp-history-core.js'
|
|
15
16
|
import { join, dirname, basename } from 'node:path'
|
|
16
17
|
import { homedir } from 'node:os'
|
|
17
18
|
import { pathToFileURL, fileURLToPath } from 'node:url'
|
|
@@ -183,6 +184,9 @@ const GROUP_DIR = CFG.groupDir
|
|
|
183
184
|
// 不在任何一条鱼的家里 —— 它们记的是"谁读到哪、谁被叫过",是共享的。
|
|
184
185
|
// 共享区在家之外(pool),她家里有 Junction 指回去,所以她的路径没变。
|
|
185
186
|
const POOL_STATE = CFG.poolStateDir
|
|
187
|
+
// 代码指纹的历史账(2026-11-19,小鱼丸私聊第 53 行):上一次启动记下的值,
|
|
188
|
+
// 让“这段代码改过没有”能从账上答,而不是靠谁记性好。
|
|
189
|
+
const CODE_FP_LEDGER_FILE = join(POOL_STATE, CODE_FP_LEDGER)
|
|
186
190
|
// 图片:字节进 images\<yyyymm>\<id>.<ext>,消息里只记 id/ext/bytes。
|
|
187
191
|
// 用内容哈希当 id —— 同一张图发两次只落一份,而且天然不可变(可以永久缓存)。
|
|
188
192
|
const IMAGES_DIR = join(GROUP_DIR, 'images')
|
|
@@ -1830,6 +1834,20 @@ function logLine(record) {
|
|
|
1830
1834
|
* repoRoot 来自 `import.meta.url`(这一行代码自己站在哪),配置写错也改不动它。
|
|
1831
1835
|
*/
|
|
1832
1836
|
const BOOT_LOG = join(CFG.repoRoot, 'logs', 'plugin-boot.log')
|
|
1837
|
+
function recordCodeFpHistory() {
|
|
1838
|
+
try {
|
|
1839
|
+
const prev = readJson(CODE_FP_LEDGER_FILE, null)
|
|
1840
|
+
const change = codeFpChangeOf(prev, CODE_FP)
|
|
1841
|
+
if (change.kind !== 'same') {
|
|
1842
|
+
const line = codeFpHistoryLine(change)
|
|
1843
|
+
if (line !== null) watchmanLog('note', line)
|
|
1844
|
+
}
|
|
1845
|
+
writeJson(CODE_FP_LEDGER_FILE, codeFpLedgerOf(CODE_FP, new Date().toISOString()))
|
|
1846
|
+
} catch {
|
|
1847
|
+
// 历史账写不进去不该拦住启动;下一个能写的地方会把它写出来
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1833
1851
|
function bootLog(record) {
|
|
1834
1852
|
try {
|
|
1835
1853
|
mkdirSync(dirname(BOOT_LOG), { recursive: true })
|
|
@@ -1950,6 +1968,9 @@ function apply(ctx) {
|
|
|
1950
1968
|
// 修法不是补一个词,而是"一次拼好、两处都用同一个对象":以后 bootRecord 加字段,镜像自动跟上。
|
|
1951
1969
|
const bootRecordWithNote = { note: 'boot', ...bootRecord }
|
|
1952
1970
|
bootLog(bootRecordWithNote)
|
|
1971
|
+
// 小鱼丸私聊第 53 行(2026-11-19):boot 行只留了一个时刻的指纹,
|
|
1972
|
+
// 回答不了“改过没有”;在值变化时补一行,账上有历史值才判得动。
|
|
1973
|
+
recordCodeFpHistory()
|
|
1953
1974
|
// P4(2026-11-16 第三份装机沉淀):只把两个 bundle add 进 profile(最小安装)时,
|
|
1954
1975
|
// 没有安装器来问"鱼塘放哪",也没有配置文件 ⇒ 默认值会把 homes/pool 建在仓库旁边
|
|
1955
1976
|
// (实测:<workspace>\whale-src\ 里同时出现仓库和 homes/pool)。用户以为数据在别处,
|
|
@@ -3836,8 +3857,12 @@ function maybePruneMediaCache() {
|
|
|
3836
3857
|
watchmanLog('error', `note=media-prune-failed reason=${String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 120)}`)
|
|
3837
3858
|
return
|
|
3838
3859
|
}
|
|
3860
|
+
// 目录层失败与文件层失败分开写:前者读 error,后者读 failed(小鱼丸 2026-11-19 群第 2197 行)
|
|
3861
|
+
if (result.error !== null) {
|
|
3862
|
+
watchmanLog('error', `note=media-prune-failed scope=dir reason=${result.error}`)
|
|
3863
|
+
}
|
|
3839
3864
|
for (const item of result.failed) {
|
|
3840
|
-
watchmanLog('error', `note=media-prune-failed file=${item.name || '-'} reason=${item.code}`)
|
|
3865
|
+
watchmanLog('error', `note=media-prune-failed scope=${item.scope ?? 'file'} file=${item.name || '-'} reason=${item.code}`)
|
|
3841
3866
|
}
|
|
3842
3867
|
if (result.removed > 0) {
|
|
3843
3868
|
watchmanLog(
|
package/lib/media-cache-core.js
CHANGED
|
@@ -20,18 +20,29 @@ export const MEDIA_CACHE_MAX_AGE_DAYS = 7
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* 删掉 dir 里 mtime 早于 maxAgeDays 的普通文件。
|
|
23
|
-
*
|
|
23
|
+
*
|
|
24
|
+
* 返回 { removed, bytes, existed, error, failed },语义与 mediaCacheStats 对齐
|
|
25
|
+
* (小鱼丸 2026-11-19 群第 2197 行:两个函数同名同义,读者不用记两套):
|
|
26
|
+
* - existed: true —— 目录读到了;error: null;
|
|
27
|
+
* - existed: false, error: null —— 目录还没建(没下过视频),不是失败;
|
|
28
|
+
* - existed: false, error: '<CODE>' —— 试过、没读成(ENOTDIR / EACCES …),码进 error;
|
|
29
|
+
* - failed: [{ scope:'dir'|'file', name?, code }] —— 只装“具体某一层”的失败;
|
|
30
|
+
* 目录层失败进 error,文件层失败进 failed(带 name,下一个人才知道该动哪个)。
|
|
24
31
|
*/
|
|
25
32
|
export function pruneMediaCache({ dir, maxAgeDays = MEDIA_CACHE_MAX_AGE_DAYS, now = Date.now() } = {}) {
|
|
26
|
-
const out = { removed: 0, bytes: 0,
|
|
27
|
-
if (!dir) return { ...out,
|
|
33
|
+
const out = { removed: 0, bytes: 0, existed: false, error: null, failed: [] }
|
|
34
|
+
if (!dir) return { ...out, error: 'NO-ARG' }
|
|
28
35
|
let names
|
|
29
36
|
try {
|
|
30
37
|
names = readdirSync(dir)
|
|
31
38
|
} catch (error) {
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
const code = error?.code ?? 'error'
|
|
40
|
+
// ENOENT:目录没建 = 没东西可删,不是失败(与 mediaCacheStats 对“父目录还在”的区分不同:
|
|
41
|
+
// 清理路径不关心“是谁改错了路”,只关心“这一轮没东西可清”——路径算错会由 stats 那格报出来)。
|
|
42
|
+
if (code === 'ENOENT') return out
|
|
43
|
+
return { ...out, error: code }
|
|
34
44
|
}
|
|
45
|
+
out.existed = true
|
|
35
46
|
const cutoff = now - maxAgeDays * 86400_000
|
|
36
47
|
for (const name of names.slice().sort()) {
|
|
37
48
|
const full = join(dir, name)
|
|
@@ -42,7 +53,7 @@ export function pruneMediaCache({ dir, maxAgeDays = MEDIA_CACHE_MAX_AGE_DAYS, no
|
|
|
42
53
|
out.removed += 1
|
|
43
54
|
out.bytes += stat.size
|
|
44
55
|
} catch (error) {
|
|
45
|
-
out.failed.push({ name, code: error?.code ?? 'error' })
|
|
56
|
+
out.failed.push({ scope: 'file', name, code: error?.code ?? 'error' })
|
|
46
57
|
}
|
|
47
58
|
}
|
|
48
59
|
return out
|