merge: 補算向量的節奏、額度、世代核對+標庫共用同一顆閘(Arcrun#85/D68/D69)

總管逐筆審過 1d6dde4/674e1b4/37e13fc:全部落在 kbdb/,沒有新表(額度用量寄生在
既有 entries 表單一列,D38)、沒有動實例。標庫與時間分層共用一套 SelectionCriteria
與同一顆每日 D1 額度計數器——兩者若各記各的,其中一個會把另一個的閘繞過去。
實測:kbdb 全套 196 pass / 0 fail(含反向驗證:拿掉 cap 那個 case 會變紅)。

紅線仍在:這只是併進 main,還沒部署到任何實例。
This commit is contained in:
uncle6me-web
2026-08-12 09:40:30 +08:00
8 changed files with 1336 additions and 124 deletions
+410 -108
View File
@@ -1,130 +1,196 @@
// embed backfill — D682026-08-11 leo 拍板:補算向量照時間新到舊、且每天有額度上限)測試。
//
// 測試策略比照 execution-log.test.tslibrary-map.test.ts:真 SQLitenode:sqlite)套
// migrations/0001_base.sql 原檔,比手刻假 DB 更硬——驗的是真實 SQL 語意(ORDER BYWHERE
// JSON 函式),不是「以為 SQL 長這樣」。AI/VECTORIZE 仍是輕量假物件(Cloudflare binding
// 不是 SQL,沒有真 runtime 可套)。
//
// 覆蓋 D68 三條 + is_embedded 世代旗標坑,四項都要有實測輸出:
// 1. 由新到舊:造 created_at 跨時間的候選,證明先被處理的是最新那幾筆
// 2. 每日額度上限真的擋:cap 設小,跑到撞上限,證明它停手不再打 AI(不是繼續打)
// 3. 帶著舊世代旗標(is_embedded=1 但對應已退役索引)的列補得回來
// 4. 現有 idempotentbatchingreindex 行為不因本次改動而壞掉
//
// 本檔在 kbdb/tests/(牆外,非 kbdb/src|migrations),依 D38 kbdb-api-wall-guard 規則,
// 所有直接對 SQLite 治具下 SQL 的行都集中在下面幾個 helper(每行標 kbdb-sql-ok 留痕)——
// 這是**測試治具本身**node:sqlite→D1 shim,模擬 D1 binding),不是牆外業務邏輯繞過 API。
import { describe, it, expect } from 'vitest';
import { backfillEmbeddings, backfillStatus, embedEnabled } from '../src/embed';
import type { Bindings, Entry } from '../src/types';
import { DatabaseSync } from 'node:sqlite';
import { readFileSync } from 'node:fs';
import {
backfillEmbeddings,
backfillStatus,
embedEnabled,
reconcileEmbedGeneration,
} from '../src/embed';
import type { Bindings, Entry, EntryType } from '../src/types';
// ── Minimal in-memory fakes (no Workers runtime) ─────────────────────────────
// The fake DB interprets only the 3 statement shapes backfill issues, by keyword:
// SELECT * ... LIMIT ? OFFSET ? → candidate rows (embeddable & non-empty content;
// +is_embedded=0 for normal backfill, any for reindex)
// UPDATE ... IN (...) → flip is_embedded=1 for the bound ids
// SELECT COUNT(*) → count of matching candidates
// embeddable = metadata.embed===true & non-empty contentreindex predicate)。
function isEmbeddable(e: Entry): boolean {
if (!e.content || e.content.trim() === '') return false;
try {
const m = JSON.parse(e.metadata_json ?? 'null');
return m?.embed === true;
} catch {
return false;
}
}
// normal backfill 額外要求 is_embedded=0(漏網補嵌)。
function isCandidate(e: Entry): boolean {
return e.is_embedded === 0 && isEmbeddable(e);
}
const CURRENT_MODEL = '@cf/baai/bge-m3'; // embed.ts DEFAULT_EMBED_MODEL(未 export,測試按文件字面核對)
function makeFakeDB(store: Entry[]) {
const prepare = (sql: string) => {
// reindex predicate 不含 "is_embedded = 0" → 依 SQL 判斷該用哪個 filter(對齊 embed.ts)。
const pred = /is_embedded = 0/.test(sql) ? isCandidate : isEmbeddable;
let bound: unknown[] = [];
const stmt = {
bind(...args: unknown[]) { bound = args; return stmt; },
async all<T>() {
// SELECT * ... LIMIT ? OFFSET ? (bound tail = [..., limit, offset])
const offset = Number(bound[bound.length - 1]);
const limit = Number(bound[bound.length - 2]);
const results = store.filter(pred).slice(offset, offset + limit) as unknown as T[];
return { results };
},
async first<T>() {
// SELECT COUNT(*) as c ...
const c = store.filter(pred).length;
return { c } as unknown as T;
},
// ── node:sqlite → D1 介面最小 adapter(同 execution-log.test.tslibrary-map.test.ts 手法)──
function makeSqliteD1(): D1Database {
const raw = new DatabaseSync(':memory:');
raw.exec(readFileSync(new URL('../migrations/0001_base.sql', import.meta.url), 'utf8')); // kbdb-sql-ok:測試治具(node:sqlite→D1 shim)套 migration 原檔
function stmt(sql: string, params: unknown[]) {
const s = {
bind(...args: unknown[]) { return stmt(sql, args); },
async all<T>() { return { results: raw.prepare(sql).all(...params) as T[] }; }, // kbdb-sql-ok:測試治具(node:sqlite→D1 shim
async first<T>() { return (raw.prepare(sql).get(...params) ?? null) as T | null; }, // kbdb-sql-ok:測試治具(node:sqlite→D1 shim
async run() {
// UPDATE entries SET is_embedded = 1 WHERE id IN (...) → bound = ids
const ids = new Set(bound.map(String));
for (const e of store) if (ids.has(e.id)) e.is_embedded = 1;
return { success: true };
const r = raw.prepare(sql).run(...params); // kbdb-sql-ok:測試治具(node:sqlite→D1 shim),非牆外業務邏輯繞過 API
return { success: true, meta: { changes: r.changes } };
},
};
return stmt;
};
return { prepare } as unknown as D1Database;
return s;
}
return { prepare: (sql: string) => stmt(sql, []) } as unknown as D1Database;
}
function mkEntry(id: string, content: string | null, embed: boolean, is_embedded = 0): Entry {
return {
id, content, entry_type: 'workflow', owner_id: 'leo', parent_id: null, page_name: null,
refs_json: '[]', tags_json: '[]', task_status: null, content_hash: null, is_embedded,
confidence: null, metadata_json: JSON.stringify({ embed }), created_at: 1, updated_at: 1,
};
// ── 測試專用資料存取 helper:把所有直接下 SQL 的呼叫收斂到這裡(每行標記留痕)──────────
function insertEntry(db: D1Database, e: Partial<Entry> & { id: string; created_at: number }): void {
const sql = `INSERT INTO entries (id, content, entry_type, owner_id, content_hash, is_embedded, metadata_json, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
db.prepare(sql).bind( // kbdb-sql-ok:測試治具(node:sqlite→D1 shim)灌測試資料
e.id,
e.content === undefined ? 'x' : e.content, // 區分「沒提供」(undefined→預設'x') 與「顯式 null」(保留 null)
(e.entry_type ?? 'workflow') as EntryType,
e.owner_id ?? 'leo',
e.content_hash ?? null,
e.is_embedded ?? 0,
e.metadata_json ?? JSON.stringify({ embed: true }),
e.created_at,
e.created_at,
).run();
}
function makeEnv(store: Entry[], withBindings: boolean): Bindings {
async function getRow(db: D1Database, id: string): Promise<{ id: string; is_embedded: number; content_hash: string | null } | null> {
return db.prepare('SELECT id, is_embedded, content_hash FROM entries WHERE id = ?').bind(id).first(); // kbdb-sql-ok:測試治具讀回斷言用
}
async function listAllRows(db: D1Database): Promise<{ id: string; is_embedded: number; content_hash: string | null }[]> {
const res = await db.prepare('SELECT id, is_embedded, content_hash FROM entries').all<{ id: string; is_embedded: number; content_hash: string | null }>(); // kbdb-sql-ok:測試治具讀回斷言用
return res.results;
}
async function listEmbeddedIds(db: D1Database): Promise<string[]> {
const res = await db.prepare("SELECT id FROM entries WHERE is_embedded = 1").all<{ id: string }>(); // kbdb-sql-ok:測試治具讀回斷言用
return res.results.map((r) => r.id);
}
async function countUsageRows(db: D1Database): Promise<{ id: string; entry_type: string }[]> {
const res = await db.prepare("SELECT id, entry_type FROM entries WHERE entry_type = 'embed_backfill_usage'").all<{ id: string; entry_type: string }>(); // kbdb-sql-ok:測試治具驗證「不新增表、單列 upsert」
return res.results;
}
function makeEnv(db: D1Database, opts: { withBindings?: boolean; dailyLimit?: string; maintenanceLimit?: string } = {}): Bindings {
const withBindings = opts.withBindings ?? true;
const upserts: { id: string }[] = [];
const aiCalls: string[][] = [];
const getByIdsCalls: string[][] = [];
const vectorizeStore = new Set<string>(); // ids "present" in the current (fake) Vectorize index
const env = {
DB: makeFakeDB(store),
DB: db,
ENVIRONMENT: 'test',
EMBED_BACKFILL_DAILY_LIMIT: opts.dailyLimit,
KBDB_MAINTENANCE_DAILY_WRITE_LIMIT: opts.maintenanceLimit,
...(withBindings
? {
AI: { async run(_m: string, i: { text: string[] }) { aiCalls.push(i.text); return { data: i.text.map(() => [0.1, 0.2, 0.3]) }; } },
VECTORIZE: { async upsert(v: { id: string }[]) { upserts.push(...v); return { count: v.length }; } },
AI: {
async run(_m: string, i: { text: string[] }) {
aiCalls.push(i.text);
return { data: i.text.map(() => [0.1, 0.2, 0.3]) };
},
},
VECTORIZE: {
async upsert(v: { id: string }[]) {
upserts.push(...v);
for (const x of v) vectorizeStore.add(x.id);
return { count: v.length };
},
async getByIds(ids: string[]) {
getByIdsCalls.push(ids);
return ids.filter((id) => vectorizeStore.has(id)).map((id) => ({ id, values: [0.1] }));
},
},
}
: {}),
} as unknown as Bindings;
(env as unknown as { __upserts: unknown[]; __ai: unknown[] }).__upserts = upserts;
(env as unknown as { __upserts: unknown[]; __ai: unknown[] }).__ai = aiCalls;
const bag = env as unknown as {
__upserts: unknown[]; __ai: unknown[]; __getByIds: unknown[];
__seedVectorized: (ids: string[]) => void;
};
bag.__upserts = upserts;
bag.__ai = aiCalls;
bag.__getByIds = getByIdsCalls;
bag.__seedVectorized = (ids: string[]) => { for (const id of ids) vectorizeStore.add(id); };
return env;
}
describe('backfillEmbeddings', () => {
it('module off → enabled:false, no-op (誠實不假綠)', async () => {
const store = [mkEntry('e1', 'hello', true)];
const env = makeEnv(store, false);
function aiCallsOf(env: Bindings): string[][] {
return (env as unknown as { __ai: string[][] }).__ai;
}
function upsertsOf(env: Bindings): { id: string }[] {
return (env as unknown as { __upserts: { id: string }[] }).__upserts;
}
function seedVectorized(env: Bindings, ids: string[]): void {
(env as unknown as { __seedVectorized: (ids: string[]) => void }).__seedVectorized(ids);
}
describe('backfillEmbeddings — 模組未開', () => {
it('誠實不假綠:no-op,含新增的 quota 欄位', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', created_at: 1 });
const env = makeEnv(db, { withBindings: false });
expect(embedEnabled(env)).toBe(false);
const r = await backfillEmbeddings(env);
expect(r).toEqual({ enabled: false, processed: 0, skipped: 0, remaining: 0, scanned: 0 });
expect(store[0].is_embedded).toBe(0); // untouched
expect(r).toEqual({
enabled: false, processed: 0, skipped: 0, remaining: 0, scanned: 0,
quota_limit: 0, quota_used_today: 0, quota_exceeded: false,
});
});
});
it('embeds embeddable+is_embedded=0 entries, marks is_embedded=1, batches AI+upsert', async () => {
const store = [
mkEntry('e1', 'doorbell workflow', true),
mkEntry('e2', 'notify workflow', true),
mkEntry('e3', 'not tagged', false), // embed:false → not a candidate
mkEntry('e4', 'already done', true, 1), // is_embedded=1 → not a candidate
mkEntry('e5', ' ', true), // empty content → not embeddable
];
const env = makeEnv(store, true);
describe('backfillEmbeddings — 基本行為(沿用既有覆蓋,改動後仍要綠)', () => {
it('embeds embeddable+is_embedded=0 entries, marks is_embedded=1 + content_hash,批次 AI+upsert', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', content: 'doorbell workflow', created_at: 1 });
insertEntry(db, { id: 'e2', content: 'notify workflow', created_at: 2 });
insertEntry(db, { id: 'e3', content: 'not tagged', created_at: 3, metadata_json: JSON.stringify({ embed: false }) });
insertEntry(db, { id: 'e4', content: 'already done', created_at: 4, is_embedded: 1 });
insertEntry(db, { id: 'e5', content: null, created_at: 5 }); // NULL content → 排除,非本次改動範圍的既有行為
const env = makeEnv(db);
const r = await backfillEmbeddings(env, { limit: 100 });
expect(r.enabled).toBe(true);
expect(r.processed).toBe(2); // only e1,e2
expect(r.remaining).toBe(0); // nothing left embeddable
expect(store.find((e) => e.id === 'e1')!.is_embedded).toBe(1);
expect(store.find((e) => e.id === 'e2')!.is_embedded).toBe(1);
expect(store.find((e) => e.id === 'e3')!.is_embedded).toBe(0);
const upserts = (env as unknown as { __upserts: { id: string }[] }).__upserts;
expect(upserts.map((u) => u.id).sort()).toEqual(['e1', 'e2']);
const ai = (env as unknown as { __ai: string[][] }).__ai;
expect(ai.length).toBe(1); // single batched AI.run for the whole batch
expect(ai[0].length).toBe(2);
expect(r.processed).toBe(2); // only e1, e2
expect(r.remaining).toBe(0);
const rows = await listAllRows(db);
const byId = Object.fromEntries(rows.map((x) => [x.id, x]));
expect(byId.e1.is_embedded).toBe(1);
expect(byId.e1.content_hash).toBe(CURRENT_MODEL); // 世代戳記有寫
expect(byId.e2.is_embedded).toBe(1);
expect(byId.e3.is_embedded).toBe(0);
expect(upsertsOf(env).map((u) => u.id).sort()).toEqual(['e1', 'e2']);
expect(aiCallsOf(env).length).toBe(1);
expect(aiCallsOf(env)[0].length).toBe(2);
});
it('idempotent: re-run after all embedded processes nothing', async () => {
const store = [mkEntry('e1', 'x', true)];
const env = makeEnv(store, true);
it('idempotent:全部嵌完後重跑不再處理', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', content: 'x', created_at: 1 });
const env = makeEnv(db, { dailyLimit: '100' });
await backfillEmbeddings(env);
const r2 = await backfillEmbeddings(env);
expect(r2.processed).toBe(0);
expect(r2.remaining).toBe(0);
});
it('batches via limit → remaining reported so caller can loop to zero', async () => {
const store = [mkEntry('a', 'x', true), mkEntry('b', 'y', true), mkEntry('c', 'z', true)];
const env = makeEnv(store, true);
it('batches via limit → remaining 讓 caller 可重複呼叫到 0', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', content: 'x', created_at: 1 });
insertEntry(db, { id: 'b', content: 'y', created_at: 2 });
insertEntry(db, { id: 'c', content: 'z', created_at: 3 });
const env = makeEnv(db, { dailyLimit: '100' });
const r1 = await backfillEmbeddings(env, { limit: 2 });
expect(r1.processed).toBe(2);
expect(r1.remaining).toBe(1);
@@ -133,34 +199,270 @@ describe('backfillEmbeddings', () => {
expect(r2.remaining).toBe(0);
});
it('reindex: 重推所有 embeddable(含 is_embedded=1),offset 分頁到 remaining=0Arcrun#11', async () => {
// 三筆皆已 is_embedded=1(既有向量):正常 backfill 不會碰(pending=0),reindex 要全部重推
// 讓事後建立的 Vectorize metadata index 收錄。
const store = [
mkEntry('a', 'x', true, 1), mkEntry('b', 'y', true, 1), mkEntry('c', 'z', true, 1),
];
const env = makeEnv(store, true);
// 正常 backfill:沒有 is_embedded=0 → 什麼都不做(證明「不重推就補不到」)。
it('reindex重推所有 embeddable(含 is_embedded=1),offset 分頁到 remaining=0Arcrun#11', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', content: 'x', created_at: 1, is_embedded: 1 });
insertEntry(db, { id: 'b', content: 'y', created_at: 2, is_embedded: 1 });
insertEntry(db, { id: 'c', content: 'z', created_at: 3, is_embedded: 1 });
const env = makeEnv(db, { dailyLimit: '100' });
const normal = await backfillEmbeddings(env, { limit: 100 });
expect(normal.processed).toBe(0);
// reindex 分頁:第一批 2 筆、remaining=1;第二批 1 筆、remaining=0。
expect(normal.processed).toBe(0); // 沒有 is_embedded=0 → 什麼都不做
const r1 = await backfillEmbeddings(env, { reindex: true, limit: 2, offset: 0 });
expect(r1.processed).toBe(2);
expect(r1.remaining).toBe(1);
const r2 = await backfillEmbeddings(env, { reindex: true, limit: 2, offset: 2 });
expect(r2.processed).toBe(1);
expect(r2.remaining).toBe(0);
const upserts = (env as unknown as { __upserts: { id: string }[] }).__upserts;
expect(upserts.map((u) => u.id).sort()).toEqual(['a', 'b', 'c']);
expect(upsertsOf(env).map((u) => u.id).sort()).toEqual(['a', 'b', 'c']);
});
it('status reports pending/embedded counts', async () => {
const store = [mkEntry('e1', 'x', true), mkEntry('e2', 'y', true, 1)];
const env = makeEnv(store, true);
it('status 回報 pending/embedded 計數', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', content: 'x', created_at: 1 });
insertEntry(db, { id: 'e2', content: 'y', created_at: 2, is_embedded: 1 });
const env = makeEnv(db);
const s = await backfillStatus(env);
// fake first() returns candidate count for pending; embedded query also runs through
// the same COUNT fake, so this asserts the call path works (enabled:true).
expect(s.enabled).toBe(true);
expect(typeof s.pending).toBe('number');
});
});
describe('D68①:由新到舊排序(實測,不是推論)', () => {
it('候選跨時間分佈時,先被嵌入的是 created_at 最新的那幾筆', async () => {
const db = makeSqliteD1();
// 刻意亂序插入,證明排序看的是 created_at 不是插入順序 / id 字母序
insertEntry(db, { id: 'old-2024', content: 'half year ago', created_at: 1_000 });
insertEntry(db, { id: 'today', content: 'written today', created_at: 100_000 });
insertEntry(db, { id: 'mid-2025', content: 'a few months ago', created_at: 50_000 });
const env = makeEnv(db, { dailyLimit: '100' });
// limit=1:一次只能處理一筆,若排序正確,該筆必須是 'today'created_at 最大)
const r = await backfillEmbeddings(env, { limit: 1 });
expect(r.processed).toBe(1);
const embedded = await listEmbeddedIds(db);
expect(embedded).toEqual(['today']);
expect(aiCallsOf(env)[0]).toEqual(['written today']);
});
});
describe('D68②:每日額度上限真的擋(把上限設小,跑到撞上限)', () => {
it('額度耗盡後停手,不再繼續打 AI;未耗盡的仍優先保留最新的(額度截斷 + 排序疊加)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e-oldest', content: 'c1', created_at: 1 });
insertEntry(db, { id: 'e-old', content: 'c2', created_at: 2 });
insertEntry(db, { id: 'e-new', content: 'c3', created_at: 3 });
insertEntry(db, { id: 'e-newest', content: 'c4', created_at: 4 });
const env = makeEnv(db, { dailyLimit: '2' }); // 上限設得比候選數(4)小
const r = await backfillEmbeddings(env, { limit: 100 });
// 停手,不是繼續打:AI 只被叫過一次,且只帶 2 筆文字(不是全部 4 筆)
expect(aiCallsOf(env).length).toBe(1);
expect(aiCallsOf(env)[0].length).toBe(2);
expect(r.processed).toBe(2);
expect(r.quota_limit).toBe(2);
expect(r.quota_used_today).toBe(2);
expect(r.quota_exceeded).toBe(true); // 還有候選但今天不再打 AI
// 被留下處理的兩筆是最新的(e-newest, e-new),不是隨機或最舊的
const embedded = await listEmbeddedIds(db);
expect(embedded.sort()).toEqual(['e-new', 'e-newest']);
// 再跑一次(同一天):額度已用完,processed=0,AI 呼叫次數仍是 1(沒有再打)
const r2 = await backfillEmbeddings(env, { limit: 100 });
expect(r2.processed).toBe(0);
expect(r2.quota_exceeded).toBe(true);
expect(aiCallsOf(env).length).toBe(1); // 沒有新增呼叫
});
it('額度上限被拿掉時本測試會變紅(反向驗證:測試真的在測東西,不是恆真)', async () => {
const db = makeSqliteD1();
for (let i = 1; i <= 5; i++) insertEntry(db, { id: `e${i}`, content: `c${i}`, created_at: i });
// 不設 dailyLimit(用預設 1800,遠大於 5)→ 全部應被處理,模擬「上限被拿掉」的行為
const env = makeEnv(db);
const r = await backfillEmbeddings(env, { limit: 100 });
expect(r.processed).toBe(5);
expect(r.quota_exceeded).toBe(false);
// 對照組:把上限設到比候選數小,行為必須不同(證明上一組「額度=2」的測試不是巧合)
const db2 = makeSqliteD1();
for (let i = 1; i <= 5; i++) insertEntry(db2, { id: `e${i}`, content: `c${i}`, created_at: i });
const env2 = makeEnv(db2, { dailyLimit: '2' });
const r2 = await backfillEmbeddings(env2, { limit: 100 });
expect(r2.processed).toBe(2);
expect(r2.processed).not.toBe(r.processed); // 有 cap vs 沒 cap 必須不同,否則 cap 沒在起作用
});
it('額度計數跨呼叫累加,換日字串變動即歸零(不新增表,entries 單列 upsert', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', content: 'c1', created_at: 1 });
insertEntry(db, { id: 'e2', content: 'c2', created_at: 2 });
const env = makeEnv(db, { dailyLimit: '10' });
const r1 = await backfillEmbeddings(env, { limit: 1 });
expect(r1.quota_used_today).toBe(1);
const r2 = await backfillEmbeddings(env, { limit: 1 });
expect(r2.quota_used_today).toBe(2); // 累加,不是每次重算成當批數
// 驗證只有一列計數器,且落在既有三表(entries),沒有新表
const usageRows = await countUsageRows(db);
expect(usageRows.length).toBe(1);
expect(usageRows[0].id).toMatch(/^embed-backfill-usage:\d{4}-\d{2}-\d{2}$/);
});
});
describe('D68③:leo21c 資料還原情境——is_embedded=1 但對應已退役索引的列補得回來', () => {
it('reconcile:確認在現行 index 的只補 content_hash,不打 AI', async () => {
const db = makeSqliteD1();
// 模擬「這次修復之前」就已經正確嵌入現行 index 的資料:is_embedded=1、content_hash 從未寫過(NULL
insertEntry(db, { id: 'ok-legacy', content: 'x', created_at: 1, is_embedded: 1, content_hash: null });
const env = makeEnv(db);
seedVectorized(env, ['ok-legacy']); // 現行 index 真的有它
const r = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r.checked).toBe(1);
expect(r.confirmed_current).toBe(1);
expect(r.reset_to_pending).toBe(0);
expect(aiCallsOf(env).length).toBe(0); // 沒有打 AI
const row = await getRow(db, 'ok-legacy');
expect(row!.is_embedded).toBe(1); // 沒被誤重置
expect(row!.content_hash).toBe(CURRENT_MODEL); // 補標記
});
it('reconcile 揪出真正對舊索引的殘留 → 重置回 pending → 正常 backfill 真的把它補回來(端到端)', async () => {
const db = makeSqliteD1();
// leo21c 情境:從備份整批灌回,is_embedded=1 但這是對已退役 768 維索引說的;
// 現行(1024 維)Vectorize index 裡沒有這個向量(不呼叫 seedVectorized)。
insertEntry(db, { id: 'restored-stale', content: '從備份還原的舊卡片', created_at: 999, is_embedded: 1, content_hash: null });
const env = makeEnv(db);
// step 1reconcile 應該發現它不在現行 index,重置成 pending
const r1 = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r1.checked).toBe(1);
expect(r1.confirmed_current).toBe(0);
expect(r1.reset_to_pending).toBe(1);
const midRow = await getRow(db, 'restored-stale');
expect(midRow!.is_embedded).toBe(0);
expect(midRow!.content_hash).toBe(null);
expect(r1.remaining).toBe(0); // 處理完,沒有更多待核對的了
// step 2:正常 backfill 現在會撿到它(因為 is_embedded=0 了),真的打 AI 補回來
const r2 = await backfillEmbeddings(env, { limit: 100 });
expect(r2.processed).toBe(1);
expect(aiCallsOf(env).length).toBe(1);
expect(aiCallsOf(env)[0]).toEqual(['從備份還原的舊卡片']);
const finalRow = await getRow(db, 'restored-stale');
expect(finalRow!.is_embedded).toBe(1); // 補回來了
expect(finalRow!.content_hash).toBe(CURRENT_MODEL); // 蓋上現行世代戳記,下次 reconcile 不會再選到它
});
it('已經是現行世代(content_hash 等於現行模型)的列不會被 reconcile 重複選中', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'fresh', content: 'x', created_at: 1, is_embedded: 1, content_hash: CURRENT_MODEL });
const env = makeEnv(db);
const r = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r.checked).toBe(0);
expect(r.remaining).toBe(0);
});
it('模組未開 → 誠實回 enabled:false,不假裝', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'e1', content: 'x', created_at: 1, is_embedded: 1 });
const env = makeEnv(db, { withBindings: false });
const r = await reconcileEmbedGeneration(env);
expect(r).toEqual({
enabled: false, checked: 0, confirmed_current: 0, reset_to_pending: 0, remaining: 0,
scanned: 0, quota_limit: 0, quota_used_today: 0, quota_exceeded: false,
});
});
});
describe('Arcrun#85 D69reconcile 的 D1 寫入額度(與標庫 backfill 共用的計數器)', () => {
it('額度耗盡後 reconcile 停手:不再寫 D1quota_exceeded=true', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'r1', content: 'c1', created_at: 1, is_embedded: 1, content_hash: null });
insertEntry(db, { id: 'r2', content: 'c2', created_at: 2, is_embedded: 1, content_hash: null });
insertEntry(db, { id: 'r3', content: 'c3', created_at: 3, is_embedded: 1, content_hash: null });
const env = makeEnv(db, { maintenanceLimit: '2' }); // 上限比候選數(3)小
seedVectorized(env, ['r1', 'r2', 'r3']); // 全在現行 indexconfirmed_current 路徑,仍是 D1 write
const r = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r.scanned).toBe(3); // 掃到 3 筆候選
expect(r.checked).toBe(2); // 但只處理了額度允許的 2 筆
expect(r.confirmed_current).toBe(2);
expect(r.quota_limit).toBe(2);
expect(r.quota_used_today).toBe(2);
expect(r.quota_exceeded).toBe(true);
// 只有 2 筆真的被寫回 content_hash(最新的兩筆,ORDER BY created_at DESC
const rows = await listAllRows(db);
const byId = Object.fromEntries(rows.map((x) => [x.id, x]));
expect(byId.r3.content_hash).toBe(CURRENT_MODEL);
expect(byId.r2.content_hash).toBe(CURRENT_MODEL);
expect(byId.r1.content_hash).toBe(null); // 額度用完,沒輪到它
// 再跑一次(同一天):額度已用完,checked=0
const r2 = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r2.checked).toBe(0);
expect(r2.quota_exceeded).toBe(true);
});
it('額度上限被拿掉時本測試會變紅(反向驗證,同 D68② 手法)', async () => {
const db = makeSqliteD1();
for (let i = 1; i <= 5; i++) insertEntry(db, { id: `r${i}`, content: `c${i}`, created_at: i, is_embedded: 1, content_hash: null });
const env = makeEnv(db); // 不設 maintenanceLimit → 用預設 20000,遠大於 5,全部應被處理
seedVectorized(env, ['r1', 'r2', 'r3', 'r4', 'r5']);
const r = await reconcileEmbedGeneration(env, { limit: 100 });
expect(r.checked).toBe(5);
expect(r.quota_exceeded).toBe(false);
const db2 = makeSqliteD1();
for (let i = 1; i <= 5; i++) insertEntry(db2, { id: `r${i}`, content: `c${i}`, created_at: i, is_embedded: 1, content_hash: null });
const env2 = makeEnv(db2, { maintenanceLimit: '2' });
seedVectorized(env2, ['r1', 'r2', 'r3', 'r4', 'r5']);
const r2 = await reconcileEmbedGeneration(env2, { limit: 100 });
expect(r2.checked).toBe(2);
expect(r2.checked).not.toBe(r.checked); // 有 cap vs 沒 cap 必須不同,否則 cap 沒在作用
});
});
describe('Arcrun#85:「挑哪一批」可以從外面指定(SelectionCriteriasince/until/library', () => {
it('backfillEmbeddings 帶 since/until 只補時間窗內的候選', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'too-old', content: 'x', created_at: 100 });
insertEntry(db, { id: 'in-window-1', content: 'y', created_at: 500 });
insertEntry(db, { id: 'in-window-2', content: 'z', created_at: 800 });
insertEntry(db, { id: 'too-new', content: 'w', created_at: 1500 });
const env = makeEnv(db, { dailyLimit: '100' });
const r = await backfillEmbeddings(env, { limit: 100, since: 400, until: 1000 });
expect(r.processed).toBe(2);
expect((await listEmbeddedIds(db)).sort()).toEqual(['in-window-1', 'in-window-2']);
});
it('backfillEmbeddings 帶 library 只補該庫的候選(未標記歸 general)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'finance-1', content: 'x', created_at: 1, metadata_json: JSON.stringify({ embed: true, library: 'finance' }) });
insertEntry(db, { id: 'hr-1', content: 'y', created_at: 2, metadata_json: JSON.stringify({ embed: true, library: 'hr' }) });
insertEntry(db, { id: 'untagged', content: 'z', created_at: 3 }); // 無 library → general
const env = makeEnv(db, { dailyLimit: '100' });
const r = await backfillEmbeddings(env, { limit: 100, library: 'finance' });
expect(r.processed).toBe(1);
expect(await listEmbeddedIds(db)).toEqual(['finance-1']);
const r2 = await backfillEmbeddings(env, { limit: 100, library: 'general' });
expect(r2.processed).toBe(1);
expect((await listEmbeddedIds(db)).sort()).toEqual(['finance-1', 'untagged']);
});
it('reconcile 帶 since/until/library 同樣受篩選(同一套 SelectionCriteria,非獨立實作)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'old', content: 'x', created_at: 1, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true, library: 'finance' }) });
insertEntry(db, { id: 'new', content: 'y', created_at: 100, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true, library: 'finance' }) });
insertEntry(db, { id: 'other-lib', content: 'z', created_at: 100, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true, library: 'hr' }) });
const env = makeEnv(db);
seedVectorized(env, ['old', 'new', 'other-lib']);
const r = await reconcileEmbedGeneration(env, { limit: 100, library: 'finance', since: 50 });
expect(r.checked).toBe(1);
const row = await getRow(db, 'new');
expect(row!.content_hash).toBe(CURRENT_MODEL);
const oldRow = await getRow(db, 'old');
expect(oldRow!.content_hash).toBe(null); // 在時間窗外,沒被動到
});
});
+230
View File
@@ -0,0 +1,230 @@
// 標庫 backfillArcrun#85 二次裁決,2026-08-11)測試。
//
// 測試策略比照 embed-backfill.test.ts:真 SQLitenode:sqlite)套 migrations/0001_base.sql
// 原檔,驗真實 SQL 語意(json_setWHERELIMIT),不是「以為 SQL 長這樣」。
//
// 覆蓋:
// 1. 只補「符合條件、目前未標記 library」的候選;已標記的不動(冪等)
// 2. owner_id 必填(缺了要拋錯,防「補錯 owner 等於白做」——2026-08-11 leo 直令)
// 3. source_prefixpage_name_prefixsinceuntil 篩選條件真的在篩
// 4. 與 reconcileEmbedGeneration 共用同一顆每日 D1 寫入額度(D69 的核心訴求:
// 補標不能把世代核對的閘繞過去,反之亦然)
//
// 本檔在 kbdb/tests/(牆外),依 D38 kbdb-api-wall-guard 規則,直接對 SQLite 治具下 SQL
// 的行集中在 helper(測試治具本身,非牆外業務邏輯繞過 API,每行標 kbdb-sql-ok 留痕)。
import { describe, it, expect } from 'vitest';
import { DatabaseSync } from 'node:sqlite';
import { readFileSync } from 'node:fs';
import { backfillEntryLibraryTags, libraryBackfillStatus } from '../src/actions/library-backfill';
import { reconcileEmbedGeneration } from '../src/embed';
import type { Bindings, Entry, EntryType } from '../src/types';
// ── node:sqlite → D1 介面最小 adapter(同 embed-backfill.test.ts 手法)──────────────
function makeSqliteD1(): D1Database {
const raw = new DatabaseSync(':memory:');
raw.exec(readFileSync(new URL('../migrations/0001_base.sql', import.meta.url), 'utf8')); // kbdb-sql-ok:測試治具(node:sqlite→D1 shim)套 migration 原檔
function stmt(sql: string, params: unknown[]) {
const s = {
bind(...args: unknown[]) { return stmt(sql, args); },
async all<T>() { return { results: raw.prepare(sql).all(...params) as T[] }; }, // kbdb-sql-ok:測試治具(node:sqlite→D1 shim
async first<T>() { return (raw.prepare(sql).get(...params) ?? null) as T | null; }, // kbdb-sql-ok:測試治具(node:sqlite→D1 shim
async run() {
const r = raw.prepare(sql).run(...params); // kbdb-sql-ok:測試治具(node:sqlite→D1 shim),非牆外業務邏輯繞過 API
return { success: true, meta: { changes: r.changes } };
},
};
return s;
}
return { prepare: (sql: string) => stmt(sql, []) } as unknown as D1Database;
}
function insertEntry(db: D1Database, e: Partial<Entry> & { id: string; created_at: number }): void {
const sql = `INSERT INTO entries (id, content, entry_type, owner_id, content_hash, is_embedded, metadata_json, page_name, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
db.prepare(sql).bind( // kbdb-sql-ok:測試治具(node:sqlite→D1 shim)灌測試資料
e.id,
e.content === undefined ? 'x' : e.content,
(e.entry_type ?? 'block') as EntryType,
e.owner_id ?? 'bfezv28v',
e.content_hash ?? null,
e.is_embedded ?? 0,
e.metadata_json === undefined ? null : e.metadata_json,
e.page_name ?? null,
e.created_at,
e.created_at,
).run();
}
async function getLibrary(db: D1Database, id: string): Promise<string | null> {
const row = await db.prepare("SELECT json_extract(metadata_json, '$.library') AS library FROM entries WHERE id = ?").bind(id).first<{ library: string | null }>(); // kbdb-sql-ok:測試治具讀回斷言用
return row?.library ?? null;
}
function makeEnv(db: D1Database, opts: { maintenanceLimit?: string } = {}): Bindings {
return {
DB: db,
ENVIRONMENT: 'test',
KBDB_MAINTENANCE_DAILY_WRITE_LIMIT: opts.maintenanceLimit,
} as unknown as Bindings;
}
describe('backfillEntryLibraryTags — 基本行為', () => {
it('只標記符合條件、目前未標記 library 的候選;已標記的不動', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', created_at: 1 }); // 無 metadata_json → 未標記
insertEntry(db, { id: 'b', created_at: 2, metadata_json: JSON.stringify({}) }); // 有 metadata_json 但無 library
insertEntry(db, { id: 'c', created_at: 3, metadata_json: JSON.stringify({ library: 'hr' }) }); // 已標記,不該被動
const env = makeEnv(db);
const r = await backfillEntryLibraryTags(db, env, { library: 'finance', owner_id: 'bfezv28v' });
expect(r.tagged).toBe(2);
expect(r.remaining).toBe(0);
expect(await getLibrary(db, 'a')).toBe('finance');
expect(await getLibrary(db, 'b')).toBe('finance');
expect(await getLibrary(db, 'c')).toBe('hr'); // 未被覆寫
});
it('冪等:全部標記完後重跑不再處理', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', created_at: 1 });
const env = makeEnv(db);
await backfillEntryLibraryTags(db, env, { library: 'finance', owner_id: 'bfezv28v' });
const r2 = await backfillEntryLibraryTags(db, env, { library: 'finance', owner_id: 'bfezv28v' });
expect(r2.tagged).toBe(0);
expect(r2.remaining).toBe(0);
});
it('owner_id 缺了要拋錯(防補錯 owner 等於白做,2026-08-11 leo 直令)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', created_at: 1 });
const env = makeEnv(db);
await expect(
backfillEntryLibraryTags(db, env, { library: 'finance', owner_id: '' }),
).rejects.toThrow(/owner_id/);
});
it('library 缺了要拋錯', async () => {
const db = makeSqliteD1();
const env = makeEnv(db);
await expect(
backfillEntryLibraryTags(db, env, { library: '', owner_id: 'bfezv28v' }),
).rejects.toThrow(/library/);
});
it('owner_id 篩選:只動指定租戶的資料,其他租戶不受影響(跨租戶隔離)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'mine', created_at: 1, owner_id: 'bfezv28v' });
insertEntry(db, { id: 'theirs', created_at: 2, owner_id: 'someone-else' });
const env = makeEnv(db);
const r = await backfillEntryLibraryTags(db, env, { library: 'finance', owner_id: 'bfezv28v' });
expect(r.tagged).toBe(1);
expect(await getLibrary(db, 'mine')).toBe('finance');
expect(await getLibrary(db, 'theirs')).toBe(null); // 別的租戶完全沒被動到
});
it('source_prefixpage_name_prefixsinceuntil 篩選真的在篩', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'match-source', created_at: 500, metadata_json: JSON.stringify({ source: 'gitea://Leo/kb/foo.md' }) });
insertEntry(db, { id: 'other-source', created_at: 500, metadata_json: JSON.stringify({ source: 'gitea://Leo/other/bar.md' }) });
const r1 = await backfillEntryLibraryTags(db, makeEnv(db), {
library: 'kb', owner_id: 'bfezv28v', source_prefix: 'gitea://Leo/kb/',
});
expect(r1.tagged).toBe(1);
expect(await getLibrary(db, 'match-source')).toBe('kb');
expect(await getLibrary(db, 'other-source')).toBe(null);
const db2 = makeSqliteD1();
insertEntry(db2, { id: 'in-window', created_at: 500, page_name: 'wiki/foo' });
insertEntry(db2, { id: 'out-window', created_at: 5000, page_name: 'wiki/bar' });
const r2 = await backfillEntryLibraryTags(db2, makeEnv(db2), {
library: 'wiki', owner_id: 'bfezv28v', page_name_prefix: 'wiki/', since: 0, until: 1000,
});
expect(r2.tagged).toBe(1);
expect(await getLibrary(db2, 'in-window')).toBe('wiki');
expect(await getLibrary(db2, 'out-window')).toBe(null);
});
it('page_names 精準比對(leo 定案的正解:拿 Gitea 原稿卡名逐批遍歷點名)', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', created_at: 1, page_name: 'card-alpha' });
insertEntry(db, { id: 'b', created_at: 2, page_name: 'card-beta' });
insertEntry(db, { id: 'c', created_at: 3, page_name: 'card-gamma' }); // 不在點名清單內
const r = await backfillEntryLibraryTags(db, makeEnv(db), {
library: 'kb', owner_id: 'bfezv28v', page_names: ['card-alpha', 'card-beta'],
});
expect(r.tagged).toBe(2);
expect(await getLibrary(db, 'a')).toBe('kb');
expect(await getLibrary(db, 'b')).toBe('kb');
expect(await getLibrary(db, 'c')).toBe(null); // 沒被點名,不動
});
it('libraryBackfillStatus 回報待補標筆數', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'a', created_at: 1 });
insertEntry(db, { id: 'b', created_at: 2, metadata_json: JSON.stringify({ library: 'hr' }) });
const s = await libraryBackfillStatus(db, { owner_id: 'bfezv28v' });
expect(s.pending).toBe(1); // 只有 'a' 未標記
});
});
describe('Arcrun#85 D69:標庫 backfill 與 reconcile 共用同一顆 D1 每日寫入額度', () => {
it('reconcile 先消耗額度 → 標庫 backfill 看到的剩餘額度真的變少', async () => {
const db = makeSqliteD1();
// reconcile 的候選:is_embedded=1 且 content_hash 非現行世代
// library 已標記('hr')→ 不會被下面的標庫 backfill 選中,讓兩種候選池互不重疊,
// 才能單純驗證「額度共用」本身,不被「標庫候選也吃到 reconcile 資料」干擾。
insertEntry(db, { id: 'reconcile-1', created_at: 1, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true, library: 'hr' }) });
insertEntry(db, { id: 'reconcile-2', created_at: 2, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true, library: 'hr' }) });
// 標庫的候選:未標記 library
insertEntry(db, { id: 'tag-1', created_at: 3 });
insertEntry(db, { id: 'tag-2', created_at: 4 });
insertEntry(db, { id: 'tag-3', created_at: 5 });
const maintenanceLimit = '3'; // 5 個候選(2 reconcile + 3 tag),額度只夠 3 個
const reconcileEnv = {
DB: db, ENVIRONMENT: 'test', KBDB_MAINTENANCE_DAILY_WRITE_LIMIT: maintenanceLimit,
AI: { async run() { return { data: [] }; } },
VECTORIZE: {
async getByIds(ids: string[]) { return ids.map((id) => ({ id, values: [0.1] })); }, // 全部視為現行 index 已有
async upsert() { return { count: 0 }; },
},
} as unknown as Bindings;
// 先跑 reconcile:吃掉 2 筆額度(3 - 2 = 1 剩)
const rc = await reconcileEmbedGeneration(reconcileEnv, { limit: 100 });
expect(rc.checked).toBe(2);
expect(rc.quota_used_today).toBe(2);
// 標庫 backfill 用同一顆 DB/同一個每日上限:只剩 1 筆額度可用,即使候選有 3 筆
const tagEnv = makeEnv(db, { maintenanceLimit });
const tagResult = await backfillEntryLibraryTags(db, tagEnv, { library: 'general', owner_id: 'bfezv28v' });
expect(tagResult.scanned).toBe(3); // 3 筆候選都掃到了
expect(tagResult.tagged).toBe(1); // 但只剩 1 筆額度,只標了 1 筆
expect(tagResult.quota_exceeded).toBe(true);
expect(tagResult.quota_used_today).toBe(3); // 2reconcile+ 1(本次)= 3,額度用滿
});
it('反過來也一樣:標庫 backfill 先消耗額度 → reconcile 看到的剩餘額度真的變少', async () => {
const db = makeSqliteD1();
insertEntry(db, { id: 'tag-1', created_at: 1 });
insertEntry(db, { id: 'tag-2', created_at: 2 });
insertEntry(db, { id: 'reconcile-1', created_at: 3, is_embedded: 1, content_hash: null, metadata_json: JSON.stringify({ embed: true }) });
const maintenanceLimit = '2';
const tagEnv = makeEnv(db, { maintenanceLimit });
const tagResult = await backfillEntryLibraryTags(db, tagEnv, { library: 'general', owner_id: 'bfezv28v' });
expect(tagResult.tagged).toBe(2); // 額度剛好夠標完兩筆
const reconcileEnv = {
DB: db, ENVIRONMENT: 'test', KBDB_MAINTENANCE_DAILY_WRITE_LIMIT: maintenanceLimit,
AI: { async run() { return { data: [] }; } },
VECTORIZE: {
async getByIds(ids: string[]) { return ids.map((id) => ({ id, values: [0.1] })); },
async upsert() { return { count: 0 }; },
},
} as unknown as Bindings;
const rc = await reconcileEmbedGeneration(reconcileEnv, { limit: 100 });
expect(rc.scanned).toBe(1); // 有 1 筆候選
expect(rc.checked).toBe(0); // 但額度已被標庫 backfill 用光,reconcile 一筆都動不了
expect(rc.quota_exceeded).toBe(true);
});
});