chore: v3.26.227(测试版:图库一致性快修)

This commit is contained in:
qiqi committed 2026-12-03 00:25:50 +08:00
1 parent 7d106d2f2c
commit 0b9d7d4cfc
12 files changed
+129 -54

No files matched your search

+100 -44
View File
@@ -1949,7 +1949,9 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
try { if (typeof wPrimeDisplayThumbForRef === 'function') wPrimeDisplayThumbForRef(ref, du) } catch (e5) { /* ignore */ }
// 先等原图 + 320 缩略图**真的写完**,再把索引标 ok 并通知图库 ——
// 否则插件先看到 ok 却取不到缩略图,就是用户看到的「图库显示缩略图加载失败」。
return Promise.resolve(storeOutputThumb({ name: name, url: url, data: du }, true)).catch(() => null).then(() => {
return Promise.resolve(storeOutputThumb({ name: name, url: url, data: du }, true)).catch(() => null).then((written) => {
// 2026-12-03:原图没真正落盘就不许标 ok —— 否则会出现「图库有卡片、images/gallery 没文件」。
if (!written) { try { wIngestLog('orig-write-fail name=' + name) } catch (eW) { /* ignore */ } return false }
// 202:必须写进 **ModularOutputStore**(图库/插件读的是它),只改 KV 索引不会生效。
const markOk = (list) => (Array.isArray(list) ? list.map((f) => (f && f.name === name)
? Object.assign({}, f, { orig: 'ok', origIdb: IDB_ORIG_PREFIX + name })
@@ -1981,39 +1983,53 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
// 这里扫一遍索引:orig 未 ok 且落地队列已有本地引用的,直接补写;幂等,可反复跑。
// 2026-12-02:图库索引去重 —— 220 那次「补漏」读错字段导致同一 url 被写入多条。
// 这里按 url 分组,同 url 只保留一条(优先保留已落地原图的那条),幂等,可反复跑。
function wDedupeOutputs() {
// 按 url 去重(同一张图只留一行,优先保留已落地的)。写回走 ModularOutputStore.mutate(同一把锁),
// 不再直接 lsSet —— 直写会绕过锁,与归档并发时互相覆盖。
async function wDedupeOutputs() {
try {
const idx = lsGet(OUT_KEY, []) || []
if (idx.length < 2) return 0
const best = new Map() // url -> entry
let removed = 0
for (const f of idx) {
const u = String((f && f.url) || '')
if (!u) continue
const prev = best.get(u)
if (!prev) { best.set(u, f); continue }
const score = (x) => ((x && x.orig === 'ok') ? 2 : 0) + ((x && x.origIdb) ? 1 : 0)
if (score(f) > score(prev)) best.set(u, f)
removed++
const compute = (list) => {
const idx = Array.isArray(list) ? list : []
if (idx.length < 2) return { kept: idx, removed: 0 }
const best = new Map()
let removed = 0
for (const f of idx) {
const u = String((f && f.url) || '')
if (!u) continue
const prev = best.get(u)
if (!prev) { best.set(u, f); continue }
const score = (x) => ((x && x.orig === 'ok') ? 2 : 0) + ((x && x.origIdb) ? 1 : 0)
if (score(f) > score(prev)) best.set(u, f)
removed++
}
if (!removed) return { kept: idx, removed: 0 }
const kept = []
const seen = new Set()
for (const f of idx) {
const u = String((f && f.url) || '')
if (!u) { kept.push(f); continue }
if (seen.has(u)) continue
seen.add(u)
kept.push(best.get(u))
}
return { kept, removed }
}
let removed = 0, left = 0
if (ModularOutputStore && typeof ModularOutputStore.mutate === 'function') {
const r = await ModularOutputStore.mutate((list) => { const out = compute(list); removed = out.removed; left = out.kept.length; return out.removed ? out.kept : null })
if (r && r.ok === false) return 0
} else {
const out = compute(lsGet(OUT_KEY, []) || [])
removed = out.removed; left = out.kept.length
if (removed) lsSet(OUT_KEY, out.kept)
}
if (!removed) return 0
const kept = []
const seen = new Set()
for (const f of idx) {
const u = String((f && f.url) || '')
if (!u) { kept.push(f); continue }
if (seen.has(u)) continue
seen.add(u)
kept.push(best.get(u))
}
lsSet(OUT_KEY, kept)
try { wIngestLog('dedupe removed=' + removed + ' left=' + kept.length) } catch (e1) { /* ignore */ }
try { wIngestLog('dedupe removed=' + removed + ' left=' + left) } catch (e1) { /* ignore */ }
try { if (typeof wGalleryChanged === 'function') wGalleryChanged('output') } catch (e2) { /* ignore */ }
try { if (typeof window !== 'undefined' && typeof window.__iwbRefreshOutputs === 'function') window.__iwbRefreshOutputs() } catch (e3) { /* ignore */ }
return removed
} catch (e4) { return 0 }
}
function wIngestReconcile() {
function wIngestReconcile(repairOnly) {
try {
const idx = lsGet(OUT_KEY, []) || []
const view = (typeof window !== 'undefined') ? window.IWBImageIngestView : null
@@ -2026,7 +2042,7 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
}
if (n) { try { wIngestLog('reconcile fixed=' + n) } catch (e2) { /* ignore */ } }
// 2026-12-02:超时失败不判死 —— 下次窗口聚焦时放回队列重试(慢 CDN 超过 5 分钟也能续上)
try {
if (!repairOnly) try {
const ing = (typeof window !== 'undefined') ? window.__iwbIngest : null
if (ing && typeof ing.retry === 'function') {
let re = 0
@@ -2041,6 +2057,18 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
return n
} catch (e3) { return 0 }
}
// 图库面板打开/刷新时的轻量自愈(节流 1.5s):去重 + 把「有落地字节但索引没标 ok」的行补上。
// 运行期出现状态漂移时,打开图库就能立刻修好,不必重启。
let wLastGalleryRepairAt = 0
function wGalleryRepair() {
try {
const now = Date.now()
if (now - wLastGalleryRepairAt < 1500) return
wLastGalleryRepairAt = now
try { wDedupeOutputs() } catch (e1) { /* ignore */ }
try { wIngestReconcile(true) } catch (e2) { /* ignore */ }
} catch (e3) { /* ignore */ }
}
function wIngestOrigBackfill(results) {
try {
const idx = lsGet(OUT_KEY, []) || []
@@ -2189,13 +2217,33 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
const awaitArchive = !!(opts && opts.awaitArchive)
if (ModularOutputStore) {
const items = []
let _skNoOk = 0, _skNoUrl = 0
let _skNoOk = 0, _skNoUrl = 0, _skDup = 0
// 2026-12-03:幂等 —— 同一 url 只允许一行。已存在的复用(还没落地就用本地字节补),本批内重复只取第一条。
const _idx0 = lsGet(OUT_KEY, []) || []
const _byUrl = new Map()
for (const f of _idx0) { const u = String((f && f.url) || ''); if (u && !_byUrl.has(u)) _byUrl.set(u, f) }
const _seen = new Set()
const _reuse = []
for (const r of (results || [])) {
if (!r || !r.ok) { _skNoOk++; continue }
if (!r.url) { _skNoUrl++; continue }
const _u = String(r.url)
if (_byUrl.has(_u) || _seen.has(_u)) {
_skDup++
const ex = _byUrl.get(_u)
if (ex && ex.orig !== 'ok' && !ex.origIdb) _reuse.push(ex)
continue
}
_seen.add(_u)
items.push({ name: 'gen_' + Date.now() + '_' + Math.floor(Math.random() * 1e6) + '.png', url: r.url, size: 0, mtime: Date.now(), width: r.width || 0, height: r.height || 0, label: r.label || '', snapshot: r.snapshot || null, orig: 'pending' })
}
try { wIngestLog('add-outputs in=' + (results || []).length + ' accepted=' + items.length + ' skipNoOk=' + _skNoOk + ' skipNoUrl=' + _skNoUrl) } catch (eL0) { /* ignore */ }
try { wIngestLog('add-outputs in=' + (results || []).length + ' accepted=' + items.length + ' skipNoOk=' + _skNoOk + ' skipNoUrl=' + _skNoUrl + ' skipDup=' + _skDup) } catch (eL0) { /* ignore */ }
if (!items.length) {
// 没有新行要建;命中的旧行若还没落地,用本地字节补上(不新建行)
const _ing0 = !!(typeof window !== 'undefined' && window.IWBImageIngestView)
for (const item of _reuse) if (_ing0) { try { wFillOutputFromIngest(item) } catch (eR) { /* ignore */ } }
return []
}
const prevIdx = lsGet(OUT_KEY, [])
const modular = await ModularOutputStore.add(items, { retention: wRawCfg().outputsRetention })
if (modular && modular.ok) {
@@ -2228,8 +2276,12 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
}
const idx = lsGet(OUT_KEY, [])
const added = []
const _seen2 = new Set(idx.map((f) => String((f && f.url) || '')).filter(Boolean))
for (const r of (results || [])) {
if (!r || !r.ok || !r.url) continue
const _u2 = String(r.url)
if (_seen2.has(_u2)) continue
_seen2.add(_u2)
const name = 'gen_' + Date.now() + '_' + Math.floor(Math.random() * 1e6) + '.png'
idx.push({ name, url: r.url, size: 0, mtime: Date.now(), w: r.width || 0, h: r.height || 0, label: r.label || '', snapshot: r.snapshot || null, orig: 'pending' })
added.push(name)
@@ -3987,32 +4039,36 @@ try { if (window.IWBMiniMapFit && typeof window.IWBMiniMapFit.install === 'funct
// 先入落地队列(不等下载完):节点先显示远程,就绪后换成 localUrl;失败保留 url 可重试
try { if (window.__iwbIngest) for (const r of results) if (r && r.ok && r.url) window.__iwbIngest.enqueue({ url: r.url, source: 'gen', w: r.width, h: r.height }) } catch (eI) { /* 落地失败不得影响出图 */ }
iwbDebugCounter('archiveGenerationResults', { entries: results.length })
// 2026-12-02:wasArchived 是按「条目对象」记的,单张归档路径标记后,批量归档会整批跳过 ——
// 于是「一次出 2 张,图库只进 1 张」。这里加一道校验:已标记归档但图库索引里查不到该 url 的,仍然补。
const _idxNow = lsGet(OUT_KEY, []) || []
const _urlInIdx = new Set(_idxNow.map((f) => String((f && f.url) || '')))
const pendingEntries = all.filter((e) => {
const archived = !!(wGallerySync && wGallerySync.wasArchived(e))
if (!archived) return true
// 条目形状是 {promptId,vi,label,items:[{ok,url}]},**没有 e.url** —— 必须下沉到 item 级取 url。
// 只要有任一 item 的 url 不在图库索引里才补;全都在索引里就跳过(否则会重复入库)。
const urls = flattenGenerationEntries([e]).map((i) => String((i && i.url) || '')).filter(Boolean)
if (!urls.length) return false
return urls.some((u) => !_urlInIdx.has(u))
})
try { wIngestLog('archive entries=' + all.length + ' pending=' + pendingEntries.length) } catch (eL1) { /* ignore */ }
for (const hook of wPluginGenHooks) {
if (typeof hook.afterResult !== 'function') continue
try { hook.afterResult(results) } catch (e) { /* 插件钩子异常不得影响结果归档 */ }
}
// 归档 → 刷新 → 自动下载串行:自动下载必须等本批原图写进图库(out_orig_)后再跑,才谈得上「从本地字节复制」。
Promise.resolve().then(async () => {
// 2026-12-03:先把单张归档队列排空,再判断补漏 —— 否则最后一张的索引还在写,会被误判成
// 「未归档」再 add 一次,同一 url 出现两行(图库重复卡 + Gallery 重复文件)。
try { if (wGallerySync && typeof wGallerySync.whenIdle === 'function') await wGallerySync.whenIdle() } catch (eW) { /* ignore */ }
// 2026-12-02:wasArchived 是按「条目对象」记的,单张归档路径标记后,批量归档会整批跳过 ——
// 于是「一次出 2 张,图库只进 1 张」。这里加一道校验:已标记归档但图库索引里查不到该 url 的,仍然补。
const _idxNow = lsGet(OUT_KEY, []) || []
const _urlInIdx = new Set(_idxNow.map((f) => String((f && f.url) || '')))
const pendingEntries = all.filter((e) => {
const archived = !!(wGallerySync && wGallerySync.wasArchived(e))
if (!archived) return true
// 条目形状是 {promptId,vi,label,items:[{ok,url}]},**没有 e.url** —— 必须下沉到 item 级取 url。
// 只要有任一 item 的 url 不在图库索引里才补;全都在索引里就跳过(否则会重复入库)。
const urls = flattenGenerationEntries([e]).map((i) => String((i && i.url) || '')).filter(Boolean)
if (!urls.length) return false
return urls.some((u) => !_urlInIdx.has(u))
})
try { wIngestLog('archive entries=' + all.length + ' pending=' + pendingEntries.length) } catch (eL1) { /* ignore */ }
// 只有「自动下载」开启时才等归档完成:它要从本地原图复制,必须等 out_orig_ 落盘;
// 没开就保持后台异步归档,避免出图到图库可点开的时间被拖长。
const needArchiveWait = !!(wAutoSaver && typeof wAutoSaver.enabled === 'function' && wAutoSaver.enabled())
const rest = flattenGenerationEntries(pendingEntries)
if (rest.length) { try { await wAddOutputs(rest, { awaitArchive: needArchiveWait }) } catch (e) { /* 图库缓存失败不影响画布结果 */ } }
try { wIngestOrigBackfill(rest) } catch (eBk) { /* 回填不影响出图 */ }
try { wDedupeOutputs() } catch (eDd) { /* 去重失败不影响出图 */ }
try { if (typeof refresh === 'function') await refresh() } catch (e) { /* ignore */ }
try { wGalleryChanged('output') } catch (e) { /* ignore */ }
try { if (wAutoSaver) await wAutoSaver.saveResults(results) } catch (e) { /* 自动下载失败不影响生图 */ }
@@ -8157,7 +8213,7 @@ const tbCenterSeed = (type, refs) => {
})
}
if (window.IWBPluginGallery) window.IWBPluginGallery.setImpl({
list: (o) => window.IWBPluginGallery.query(outputs, o),
list: (o) => { try { wGalleryRepair() } catch (eRp) { /* ignore */ } return window.IWBPluginGallery.query(outputs, o) },
// v3.26.61:缩略图取源顺序(清单 data → images/thumbs → 本地原图 → 远程)在 gallery-sync 里,
// 原因是「生图后图库不显示」有一半出在这里 —— 新图 data 还没 hydrate 就只剩远程 url 可退。
thumb: (name) => (wGallerySync ? wGallerySync.thumbFor(name, { outputs, displayThumbOf: wDisplayThumbOf, galleryThumb: (h) => { const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null; return (F && typeof F.galleryThumb === 'function') ? F.galleryThumb(h) : Promise.resolve('') }, galleryThumbUrl: (h) => { const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null; return (F && typeof F.galleryThumbUrl === 'function') ? F.galleryThumbUrl(h) : Promise.resolve('') }, galleryThumbOf: (f, max) => {