3 Commits

Author SHA1 Message Date
Claude 8ec6948918 fix(graph): backfill 讀 entity 不帶 owner 過濾(存量 owner=None,濾了讀0);owner 只套建立的 gloss entry 2026-07-05 11:26:16 +00:00
Claude 65075b4615 feat(graph): 收斂 gloss-bridge + owner-threading 為單一可部署分支
合併 arcrun-7-gloss-bridge(A1 gloss 橋)與 owner-threading(owner_id 寫入鏈必經):
- node-persist / triplet-ingest 衝突解為「兩者都要」:persistNodes 既寫 entity
  record(帶真 owner)又 upsert gloss entry(帶真 owner、帶 source);ingestEnvelope
  既帶 source.uri 又 thread 必填 owner 到底。
- gloss 路徑補 owner 必填(配合 owner-mandatory D28):
  * upsertGlossEntry(..., owner_id: string) owner 改必填
  * backfillGlossEntries(client, owner_id: string) owner 改必填
  * POST /backfill-gloss-entries 缺 owner_id → 400(存量 record owner=None,
    owner 由 caller 明確指定,不從 record 帶)
- 合併兩支測試:gloss 測試補 owner 參數以配合 base owner-required mock。

tsc:除 index.ts:57 swaggerUI 既存 baseline 外 0 error。
vitest:34 passed(gloss 7 + owner/graph 27)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ck8bjuBTLFkiFxqLGHMpqG
2026-07-05 11:20:47 +00:00
Claude 4190fbcf90 feat(graph): owner_id 寫入鏈必經(根治無主資料)——配合 base owner-mandatory(D28)
- kbdb-client requireOwner 守衛:漏 owner 插件端即 throw,不再靜默送 owner:''
- owner 一路 thread:persistNodes/ingestEnvelope/entity-crud/triplet-crud 全必填
- 病灶修:POST /triplets/ingest 原本沒傳 owner→加 owner_id query+400
- 寫入 route 缺 owner→400;vitest 23→27
- 註:gloss-bridge 分支的 backfill/gloss-entry 另需套 owner 必填(附說明),不跨支疊改
2026-07-05 11:00:30 +00:00
19 changed files with 167 additions and 65 deletions
+4 -2
View File
@@ -21,10 +21,12 @@ export type BackfillGlossResult = {
*/
export async function backfillGlossEntries(
client: KbdbClient,
owner_id?: string,
owner_id: string, // 必經:存量 entity record 本身 owner=Noneowner 由 caller 明確指定(配合 owner-mandatory D28
): Promise<BackfillGlossResult> {
await ensurePluginTemplates(client);
const records = await client.listRecordsByTemplate(TPL_ENTITY, owner_id);
// 讀「全部」存量 entity record(不帶 owner 過濾)——存量 entity 本身 owner=None,拿 owner 去濾來源會讀到 0。
// owner 只套在下面建立的 gloss entry(caller 明確指定),不套來源讀取。
const records = await client.listRecordsByTemplate(TPL_ENTITY);
const res: BackfillGlossResult = { scanned: records.length, created: 0, updated: 0, unchanged: 0, skipped: 0 };
for (const r of records) {
+8 -6
View File
@@ -13,12 +13,12 @@ const norm = (s: string): string => s.toLowerCase().trim();
// ─── Entity ──────────────────────────────────────────────────────────────────
/** 建立 Entitycanonical name)。底層 = 一筆 entity template record。 */
export async function createEntity(client: KbdbClient, canonical: string, owner?: string): Promise<Entity> {
export async function createEntity(client: KbdbClient, canonical: string, owner: string): Promise<Entity> {
await ensurePluginTemplates(client);
const id = await client.createRecord(
TPL_ENTITY,
{ canonical, aliases_json: '[]', entity_type: '', owner: owner ?? '' },
owner,
{ canonical, aliases_json: '[]', entity_type: '', owner }, // 真 owner 落 slot(不再 ?? ''
owner, // 必經:createRecord 內 requireOwner 守衛
);
return { id, canonical, aliases: [] };
}
@@ -45,21 +45,23 @@ export async function listEntities(client: KbdbClient, limit = 100, owner?: stri
* 新增 alias。base 無 PUT /records/:id → 改「重建一筆新 entity record」覆寫(含舊 canonical + 既有 aliases + 新 alias)。
* [→arcrun] base 缺 PUT /records/:id:補上後改為原地 patch aliases_json,省一次重建。
*/
export async function addAlias(client: KbdbClient, entityId: string, alias: string, owner?: string): Promise<void> {
export async function addAlias(client: KbdbClient, entityId: string, alias: string, owner: string): Promise<void> {
const rec = await client.getRecord(entityId);
if (!rec) throw new Error(`Entity ${entityId} not found`);
const ent = recordToEntity(rec);
if (ent.aliases.includes(alias)) return;
const aliases = [...ent.aliases, alias];
await ensurePluginTemplates(client);
// 重建時沿用原 record 既有 owner;原 record 無主(舊資料 owner=None)才退回 caller 指定的 owner。
const effectiveOwner = (rec.values.owner || '').trim() || owner;
await client.createRecord(
TPL_ENTITY,
{
canonical: ent.canonical,
aliases_json: JSON.stringify(aliases),
entity_type: rec.values.entity_type ?? '',
owner: rec.values.owner ?? owner ?? '',
owner: effectiveOwner,
},
owner,
effectiveOwner, // 必經:createRecord 內 requireOwner 守衛
);
}
+1 -1
View File
@@ -18,7 +18,7 @@ import type { KbdbClient } from '../lib/kbdb-client';
export async function normalizeEntity(
client: KbdbClient,
rawName: string,
owner?: string,
owner: string, // 必經:未命中會建新 entity,必帶 owner
): Promise<string> {
try {
const exact = await findEntityByName(client, rawName, owner);
+4 -4
View File
@@ -16,7 +16,7 @@ export async function createPendingAlias(
candidateEntityId: string,
candidateCanonical: string,
similarity: number,
owner?: string,
owner: string, // 必經:pending record 也不可無主
): Promise<PendingAlias> {
await ensurePluginTemplates(client);
const id = await client.createRecord(
@@ -27,7 +27,7 @@ export async function createPendingAlias(
candidate_canonical: candidateCanonical,
similarity: String(similarity),
},
owner,
owner, // 必經:createRecord 內 requireOwner 守衛
);
return {
id,
@@ -56,14 +56,14 @@ export async function getPendingAliases(client: KbdbClient, limit = 100, owner?:
}
/** 確認 → addAlias 到候選 entity。pending soft 保留([→arcrun] base 缺 DELETE record)。 */
export async function confirmPendingAlias(client: KbdbClient, pendingId: string, owner?: string): Promise<void> {
export async function confirmPendingAlias(client: KbdbClient, pendingId: string, owner: string): Promise<void> {
const rec = await client.getRecord(pendingId);
if (!rec || !rec.values.raw_name) throw new Error(`Pending alias ${pendingId} not found`);
await addAlias(client, rec.values.candidate_entity_id, rec.values.raw_name, owner);
}
/** 拒絕 → 以 raw_name 建新 entity。pending soft 保留([→arcrun] base 缺 DELETE record)。 */
export async function rejectPendingAlias(client: KbdbClient, pendingId: string, owner?: string): Promise<Entity> {
export async function rejectPendingAlias(client: KbdbClient, pendingId: string, owner: string): Promise<Entity> {
const rec = await client.getRecord(pendingId);
if (!rec || !rec.values.raw_name) throw new Error(`Pending alias ${pendingId} not found`);
return createEntity(client, rec.values.raw_name, owner);
+1 -1
View File
@@ -38,7 +38,7 @@ function glossKey(canonical: string, node_id?: string): string {
export async function upsertGlossEntry(
client: KbdbClient,
node: GlossEntryInput,
owner_id?: string,
owner_id: string, // 必經:owner 從 persistNodes / backfill caller 帶真 owner,不得為 None(配合 owner-mandatory D28
): Promise<GlossEntryOutcome> {
const canonical = (node.canonical || '').trim();
const gloss = (node.gloss || '').trim();
+3 -3
View File
@@ -23,7 +23,7 @@ export type IngestNode = {
export async function persistNodes(
client: KbdbClient,
nodes: IngestNode[],
owner_id?: string,
owner_id: string, // 必經:owner 一路從 ingest envelope / route 帶到底,不得掉成空字串
source?: string, // envelope source.uri,帶進 gloss entry 的 metadata.source(供 base backfill 依 source 過濾)
): Promise<void> {
if (!nodes || nodes.length === 0) return;
@@ -44,9 +44,9 @@ export async function persistNodes(
gloss: n.gloss ?? '',
// contract 預設 true;只在明確 false 時存標(base 看 'false' 跳過 embed)。
embed: n.embed === false ? 'false' : 'true',
owner: owner_id ?? '',
owner: owner_id, // 真 owner 落 slot(不再 ?? ''
},
owner_id,
owner_id, // createRecord 內 requireOwner 守衛:缺→throw
);
// 另落一筆 embeddable base entrymetadata.embed=true)——record 的 gloss 標 base embed 讀不到,
+1 -1
View File
@@ -13,7 +13,7 @@ export type CreateTripletData = {
object: string;
source_block_id?: string;
confidence?: number;
owner_id?: string;
owner_id: string; // 必經:三元組寫入必帶 ownercreateRecord 內 requireOwner 守衛)
clusters?: string[];
bridge_score?: number;
subject_entity_type?: string;
+2 -2
View File
@@ -55,7 +55,7 @@ export async function extractTripletsViaLLM(ai: Ai, chunks: string[]): Promise<L
export async function writeTripletToDb(
client: KbdbClient,
t: { subject: string; predicate: string; object: string; confidence?: number },
owner: string | null,
owner: string, // 必經:萃取寫入必帶 owner(不再收 null → 不會 owner_id: undefined
): Promise<boolean> {
// 查重:以 S-P-O 三欄精確比對(queryTriplets 取 template record 後在插件層 filter
const { count } = await queryTriplets(client, {
@@ -71,7 +71,7 @@ export async function writeTripletToDb(
predicate: t.predicate,
object: t.object,
confidence: t.confidence ?? 0.8,
owner_id: owner ?? undefined,
owner_id: owner,
});
return true;
}
+1 -1
View File
@@ -53,7 +53,7 @@ export type IngestResult = { skipped: boolean; ingested: number; deprecated: num
export async function ingestEnvelope(
client: KbdbClient,
env: IngestEnvelope,
owner_id?: string,
owner_id: string, // 必經:owner 從 route 帶入,一路 thread 進 triplet + node 寫入,不得掉成空
): Promise<IngestResult> {
await ensurePluginTemplates(client);
+24 -4
View File
@@ -29,7 +29,8 @@ export type BaseRecord = {
export type CreateEntryInput = {
content: string | null;
entry_type: string;
owner_id?: string;
// owner_id 必經(D27/D28):型別上必填、且執行期缺→throw。不再靜默送空 → 不寫出無主資料。
owner_id: string;
parent_id?: string;
// page_name = base 文件化的 idempotency keyentry-crud.tsexact-match lookup)。
page_name?: string;
@@ -37,6 +38,23 @@ export type CreateEntryInput = {
metadata_json?: string;
};
/**
* owner_id 必經守衛(D27/D282026-07-05)。
* 病根:漏 owner → 靜默送 owner:'' → base 寫成 owner=None → mira owner 過濾濾掉 → 使用者查不到
* (實測回填 16 筆 gloss entry owner=None)。base 即將把 owner 設必填(缺→400);插件端先擋,fail loud。
* 任何寫入前呼叫此守衛:漏 owner 在插件端就爆、不會送到 base,錯誤訊息指名該從最外層 route/envelope 帶入。
*/
export function requireOwner(owner_id: string | null | undefined, op: string): string {
const v = (owner_id ?? '').trim();
if (!v) {
throw new Error(
`[kbdb-graph] ${op} 缺 owner_id:owner 為必經欄位,不可寫出無主資料。` +
`請從最外層(route 參數 / ingest envelope)把真 owner thread 到底。`,
);
}
return v;
}
/** 基本盤 API client。所有方法 = 一個 HTTP 呼叫,零 SQL。 */
export class KbdbClient {
constructor(
@@ -76,7 +94,8 @@ export class KbdbClient {
// --- entries ---
async createEntry(input: CreateEntryInput): Promise<BaseEntry> {
const { entry } = await this.req<{ entry: BaseEntry }>('POST', '/entries', input);
const owner_id = requireOwner(input.owner_id, 'createEntry');
const { entry } = await this.req<{ entry: BaseEntry }>('POST', '/entries', { ...input, owner_id });
return entry;
}
@@ -160,11 +179,12 @@ export class KbdbClient {
// --- records= template 實例,填 slot ---
async createRecord(template: string, values: Record<string, string>, owner_id?: string): Promise<string> {
async createRecord(template: string, values: Record<string, string>, owner_id: string): Promise<string> {
const owner = requireOwner(owner_id, `createRecord(template=${template})`);
const { record } = await this.req<{ record: { record_id: string } }>('POST', '/records', {
template,
values,
owner_id,
owner_id: owner,
});
return record.record_id;
}
+11 -3
View File
@@ -55,13 +55,19 @@ entityRoutes.openapi(listPendingRoute, async (c) => {
entityRoutes.post('/pending/:id/confirm', async (c) => {
const id = c.req.param('id');
await confirmPendingAlias(makeKbdbClient(c.env), id);
// owner 必經:confirm 會 addAlias(重建 entity record),缺 owner→400 不寫無主資料。
const owner = c.req.query('owner_id')?.trim();
if (!owner) return c.json({ error: 'owner_id query parameter required(資料不可無主)' }, 400);
await confirmPendingAlias(makeKbdbClient(c.env), id, owner);
return c.json({ success: true, action: 'confirmed', id });
});
entityRoutes.post('/pending/:id/reject', async (c) => {
const id = c.req.param('id');
const newEntity = await rejectPendingAlias(makeKbdbClient(c.env), id);
// owner 必經:reject 會 createEntity,缺 owner→400 不寫無主資料。
const owner = c.req.query('owner_id')?.trim();
if (!owner) return c.json({ error: 'owner_id query parameter required(資料不可無主)' }, 400);
const newEntity = await rejectPendingAlias(makeKbdbClient(c.env), id, owner);
return c.json({ success: true, action: 'rejected', newEntity });
});
@@ -69,7 +75,9 @@ entityRoutes.post('/pending/:id/reject', async (c) => {
// route 只驗參數 + 呼叫 action(樂高法:無業務邏輯)。冪等,可重複呼叫。
entityRoutes.post('/backfill-gloss-entries', async (c) => {
const body = (await c.req.json().catch(() => ({}))) as { owner_id?: string };
const owner_id = c.req.query('owner_id') || body?.owner_id || undefined;
// owner 必經:存量 entity record 本身 owner=Noneowner 由 caller 明確指定(不從 record 帶)。缺→400 不寫無主資料。
const owner_id = c.req.query('owner_id') || body?.owner_id;
if (!owner_id) return c.json({ success: false, error: 'owner_id required' }, 400);
const result = await backfillGlossEntries(makeKbdbClient(c.env), owner_id);
return c.json({ success: true, ...result });
});
+8 -2
View File
@@ -77,7 +77,7 @@ const createRouteDefinition = createRoute({
subject: z.string().min(1),
predicate: z.string().min(1),
object: z.string().min(1),
owner_id: z.string().optional(),
owner_id: z.string().min(1), // 必填:缺→400(資料不可無主)
source_block_id: z.string().optional(),
confidence: z.number().optional(),
clusters: z.array(z.string()).optional(),
@@ -103,10 +103,13 @@ const ingestRoute = createRoute({
method: 'post',
path: '/ingest',
request: {
// owner_id 走 route 參數(envelope 是 .strict() 凍結契約,不塞 owner 進去)。缺→400。
query: z.object({ owner_id: z.string().optional().describe('資料所有者(必填,缺→400') }),
body: { content: { 'application/json': { schema: IngestEnvelopeSchema } } },
},
responses: {
200: { description: 'Envelope ingested (or skipped if same content_hash)' },
400: { description: 'Missing owner_id (資料不可無主)' },
422: { description: 'Invalid envelope (forbidden field or shape mismatch)' },
},
tags: ['Triplets'],
@@ -115,8 +118,11 @@ const ingestRoute = createRoute({
tripletRoutes.openapi(
ingestRoute,
async (c) => {
// owner 必經:缺→400(不寫出無主 triplet/node;避免再現 owner=None 被 mira 過濾)。
const owner_id = c.req.query('owner_id')?.trim();
if (!owner_id) return c.json({ error: 'owner_id query parameter required(資料不可無主)' }, 400);
const env = c.req.valid('json');
const result = await ingestEnvelope(makeKbdbClient(c.env), env);
const result = await ingestEnvelope(makeKbdbClient(c.env), env, owner_id);
return c.json(result, 200);
},
// strict() 驗證失敗(如送禁止欄位 bridge_score)→ 422,不是預設 400。
+20 -8
View File
@@ -5,17 +5,29 @@ import { normalizeEntity } from '../src/actions/entity-normalize';
import { mockClient } from './mock-client';
describe('entity-crud', () => {
it('建立後可 exact 查回(大小寫不敏感)', async () => {
it('建立後可 exact 查回(大小寫不敏感)owner slot 帶真 owner', async () => {
const c = mockClient();
await createEntity(c, 'InkStone');
const found = await findEntityByName(c, 'inkstone');
const ent = await createEntity(c, 'InkStone', 'leo');
const found = await findEntityByName(c, 'inkstone', 'leo');
expect(found?.canonical).toBe('InkStone');
// owner 落底:record 掛在 leo 名下(非無主),用錯 owner 查不到。
const rec = await c.getRecord(ent.id);
expect(rec?.values.owner).toBe('leo');
expect(await findEntityByName(c, 'inkstone', 'someone-else')).toBeNull();
});
it('漏 owner → 插件端 fail(不寫出無主 entity', async () => {
const c = mockClient();
await expect(
// @ts-expect-error 蓄意漏 owner:型別上 owner 必填,執行期也應 throw
createEntity(c, 'NoOwner'),
).rejects.toThrow(/owner/i);
});
it('listEntities 列出', async () => {
const c = mockClient();
await createEntity(c, 'A');
await createEntity(c, 'B');
await createEntity(c, 'A', 'leo');
await createEntity(c, 'B', 'leo');
const all = await listEntities(c);
expect(all.map((e) => e.canonical).sort()).toEqual(['A', 'B']);
});
@@ -24,8 +36,8 @@ describe('entity-crud', () => {
describe('normalizeEntity', () => {
it('已存在回 canonical,不存在建新回原值', async () => {
const c = mockClient();
await createEntity(c, 'InkStone');
expect(await normalizeEntity(c, 'INKSTONE')).toBe('InkStone');
expect(await normalizeEntity(c, '新公司')).toBe('新公司');
await createEntity(c, 'InkStone', 'leo');
expect(await normalizeEntity(c, 'INKSTONE', 'leo')).toBe('InkStone');
expect(await normalizeEntity(c, '新公司', 'leo')).toBe('新公司');
});
});
+7 -7
View File
@@ -40,13 +40,13 @@ describe('persistNodes — 前向:每個 node 另落一筆 embeddable base ent
it('embed=false 的 node → 不落 embeddable entry', async () => {
const c = mockClient();
await persistNodes(c, [{ name: '黃仁勳', id: '黃仁勳', gloss: 'NVIDIA 創辦人', embed: false }]);
await persistNodes(c, [{ name: '黃仁勳', id: '黃仁勳', gloss: 'NVIDIA 創辦人', embed: false }], 'leo21c');
expect((await glossEntries(c)).length).toBe(0);
});
it('空 gloss 的 node → 不落 embeddable entry(不造無意義空殼)', async () => {
const c = mockClient();
await persistNodes(c, [{ name: '某卡', id: 'card.md' }]);
await persistNodes(c, [{ name: '某卡', id: 'card.md' }], 'leo21c');
expect((await glossEntries(c)).length).toBe(0);
});
@@ -81,8 +81,8 @@ describe('backfillGlossEntries — 存量:對既有 entity records 補 embedda
entity_type: '',
gloss: v.gloss ?? '',
embed: v.embed ?? 'true',
owner: '',
});
owner: '', // slot 存量 owner=None(模擬 D28 前的無主 record);backfill 的 owner 由 caller 明確指定
}, 'leo21c'); // 記錄層 owner_idbase D28 後 createRecord 一律要 ownercaller 明指
}
it('有 gloss 的既有 record → 落 entryembed=false / 空 gloss → 略過', async () => {
@@ -92,7 +92,7 @@ describe('backfillGlossEntries — 存量:對既有 entity records 補 embedda
await seedEntity(c, { canonical: '無標', node_id: 'x', gloss: '不該嵌', embed: 'false' });
await seedEntity(c, { canonical: '空描述', node_id: 'y' }); // 空 gloss
const res = await backfillGlossEntries(c);
const res = await backfillGlossEntries(c, 'leo21c');
expect(res.scanned).toBe(4);
expect(res.created).toBe(2);
expect(res.skipped).toBe(2); // embed=false + 空 gloss
@@ -109,10 +109,10 @@ describe('backfillGlossEntries — 存量:對既有 entity records 補 embedda
const c = mockClient();
await seedEntity(c, { canonical: '長壽', node_id: 'longevity', gloss: '活得更久本身具有價值' });
const first = await backfillGlossEntries(c);
const first = await backfillGlossEntries(c, 'leo21c');
expect(first.created).toBe(1);
const second = await backfillGlossEntries(c);
const second = await backfillGlossEntries(c, 'leo21c');
expect(second.created).toBe(0);
expect(second.unchanged).toBe(1);
expect((await glossEntries(c)).length).toBe(1);
+3 -3
View File
@@ -12,7 +12,7 @@ describe('getSource — 回節點的原文來源指標', () => {
source: { uri: 'github:u/w@a.md', content_hash: 'h1', anchor: '#graph-rag' },
extractor: { model: 'm', tier: 'deep' },
triplets: [{ subject: 'GraphRAG', predicate: '是', object: 'RAG 變體' }],
});
}, 'leo');
const refs = await getSource(c, 'GraphRAG');
expect(refs.length).toBe(1);
@@ -27,12 +27,12 @@ describe('getSource — 回節點的原文來源指標', () => {
source: { uri: 'github:u/w@a.md', content_hash: 'h1', anchor: '#old' },
extractor: { model: 'm', tier: 'deep' },
triplets: [{ subject: 'X', predicate: 'r', object: 'old' }],
});
}, 'leo');
await ingestEnvelope(c, {
source: { uri: 'github:u/w@a.md', content_hash: 'h2', anchor: '#new' },
extractor: { model: 'm', tier: 'deep' },
triplets: [{ subject: 'X', predicate: 'r', object: 'new' }],
});
}, 'leo');
const refs = await getSource(c, 'X');
expect(refs.length).toBe(1);
+4 -4
View File
@@ -7,10 +7,10 @@ import { mockClient } from './mock-client';
import type { KbdbClient } from '../src/lib/kbdb-client';
async function seed(c: KbdbClient) {
// A — B — CD 孤立連 A
await createTriplet(c, { subject: 'A', predicate: 'r', object: 'B' });
await createTriplet(c, { subject: 'B', predicate: 'r', object: 'C' });
await createTriplet(c, { subject: 'A', predicate: 'r', object: 'D' });
// A — B — CD 孤立連 A(owner 必經:所有寫入帶真 owner)
await createTriplet(c, { subject: 'A', predicate: 'r', object: 'B', owner_id: 'leo' });
await createTriplet(c, { subject: 'B', predicate: 'r', object: 'C', owner_id: 'leo' });
await createTriplet(c, { subject: 'A', predicate: 'r', object: 'D', owner_id: 'leo' });
}
describe('graph-nodes', () => {
+4
View File
@@ -15,6 +15,8 @@ export class MockKbdbClient {
}
async createEntry(input: any): Promise<BaseEntry> {
// 對齊基本盤 D27/D28 即將上線的 owner 必填:缺 owner → 400(模擬 base 擋死無主寫入)。
if (!(input?.owner_id ?? '').trim()) throw new Error('[kbdb-base] POST /entries: owner_id required (400)');
const id = this.id('entry');
// 對齊 base entry-crud:保留 page_nameidempotency key)、metadata_jsonembed 打標)、parent_id。
const entry: BaseEntry = {
@@ -75,6 +77,8 @@ export class MockKbdbClient {
}
async createRecord(template: string, values: Record<string, string>, owner_id?: string): Promise<string> {
// 對齊基本盤 D27/D28 即將上線的 owner 必填:缺 owner → 400(模擬 base 擋死無主寫入)。
if (!(owner_id ?? '').trim()) throw new Error('[kbdb-base] POST /records: owner_id required (400)');
const id = this.id('rec');
this.records.set(id, { template, values: { ...values }, owner_id });
return id;
+14 -4
View File
@@ -6,7 +6,7 @@ import { mockClient } from './mock-client';
describe('createTriplet → records API', () => {
it('建立後可由 id 取回', async () => {
const c = mockClient();
const r = await createTriplet(c, { subject: 'InkStone', predicate: '是', object: '創業 OS' });
const r = await createTriplet(c, { subject: 'InkStone', predicate: '是', object: '創業 OS', owner_id: 'leo' });
expect(r.id).toBeDefined();
expect(r.subject).toBe('InkStone');
@@ -14,13 +14,23 @@ describe('createTriplet → records API', () => {
expect(got?.predicate).toBe('是');
expect(got?.object).toBe('創業 OS');
});
it('漏 owner → 插件端 fail(不靜默送空,不寫出無主 triplet)', async () => {
const c = mockClient();
await expect(
// @ts-expect-error 蓄意漏 owner:型別上 owner 必填,執行期也應 throw
createTriplet(c, { subject: 'X', predicate: 'p', object: 'Y' }),
).rejects.toThrow(/owner/i);
const { count } = await queryTriplets(c, { includeDeprecated: true });
expect(count).toBe(0);
});
});
describe('queryTriplets → 插件層 filter', () => {
it('by subject 過濾', async () => {
const c = mockClient();
await createTriplet(c, { subject: 'KBDB', predicate: '使用', object: 'D1' });
await createTriplet(c, { subject: 'Other', predicate: '使用', object: 'X' });
await createTriplet(c, { subject: 'KBDB', predicate: '使用', object: 'D1', owner_id: 'leo' });
await createTriplet(c, { subject: 'Other', predicate: '使用', object: 'X', owner_id: 'leo' });
const { triplets, count } = await queryTriplets(c, { subject: 'KBDB' });
expect(count).toBe(1);
@@ -29,7 +39,7 @@ describe('queryTriplets → 插件層 filter', () => {
it('limit/offset 分頁', async () => {
const c = mockClient();
for (let i = 0; i < 5; i++) await createTriplet(c, { subject: `s${i}`, predicate: 'p', object: 'o' });
for (let i = 0; i < 5; i++) await createTriplet(c, { subject: `s${i}`, predicate: 'p', object: 'o', owner_id: 'leo' });
const { triplets } = await queryTriplets(c, { limit: 2, offset: 1 });
expect(triplets.length).toBe(2);
});
+47 -9
View File
@@ -1,10 +1,13 @@
// ingest 寫入端 — 走 mock KbdbClientAPI-as-Wall),零 SQL、不打網路。
// 覆蓋 T3.4 五案:正常 envelope / 同 hash no-op / 新 hash deprecate / 污染 envelope 422 / rollback。
// + owner 必經(D27/D28):漏 owner→插件端 fail;真 owner 一路 thread 進 triplet + node 寫入。
import { describe, it, expect } from 'vitest';
import { ingestEnvelope, IngestEnvelopeSchema, type IngestEnvelope } from '../src/actions/triplet-ingest';
import { queryTriplets } from '../src/actions/triplet-crud';
import { mockClient } from './mock-client';
const OWNER = 'leo';
function envelope(hash: string, triplets: IngestEnvelope['triplets']): IngestEnvelope {
return {
source: { uri: 'github:uncle6me-web/wiki@a.md', content_hash: hash },
@@ -19,7 +22,7 @@ describe('ingestEnvelope — 正常 envelope', () => {
const res = await ingestEnvelope(c, envelope('h1', [
{ subject: 'A', predicate: 'rel', object: 'B' },
{ subject: 'B', predicate: 'rel', object: 'C' },
]));
]), OWNER);
expect(res).toEqual({ skipped: false, ingested: 2, deprecated: 0 });
const { triplets } = await queryTriplets(c, {});
@@ -30,11 +33,46 @@ describe('ingestEnvelope — 正常 envelope', () => {
});
});
describe('ingestEnvelope — owner 必經(D27/D28', () => {
it('漏 owner → 插件端 fail(不靜默送空,不寫出無主 triplet)', async () => {
const c = mockClient();
await expect(
// @ts-expect-error 蓄意漏 owner:型別上 owner 必填,執行期也應 throw
ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'B' }])),
).rejects.toThrow(/owner/i);
// fail loud:一筆都不該寫進去(第一筆寫入即擋)。
const { triplets } = await queryTriplets(c, {});
expect(triplets.length).toBe(0);
});
it('真 owner 一路 thread 到 triplet + node 寫入(owner slot 非空、record owner_id=真 owner', async () => {
const c = mockClient();
const env: IngestEnvelope = {
source: { uri: 'github:uncle6me-web/wiki@own.md', content_hash: 'ho' },
extractor: { model: 'm', tier: 'deep' },
nodes: [{ name: 'Graph RAG', id: 'graph-rag.md', gloss: '關係遍歷檢索' }],
triplets: [{ subject: 'Graph RAG', predicate: 'r', object: 'X' }],
};
await ingestEnvelope(c, env, OWNER);
// triplet record 帶真 owner。
const triplets = await c.listRecordsByTemplate('triplet', OWNER);
expect(triplets.length).toBe(1);
// node → entity record 帶真 ownerowner slot + record owner_id 皆為 leo,非 None/空)。
const entities = await c.listRecordsByTemplate('entity', OWNER);
const gr = entities.find((e) => e.values.canonical === 'Graph RAG');
expect(gr).toBeDefined();
expect(gr!.values.owner).toBe(OWNER);
// 用錯 owner 查 → 濾掉(證明 record 確實掛在 leo 名下,非無主)。
expect((await c.listRecordsByTemplate('entity', 'someone-else')).length).toBe(0);
});
});
describe('ingestEnvelope — 同 hash no-op', () => {
it('同 uri+hash 再送 → skipped,不新增', async () => {
const c = mockClient();
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'B' }]));
const res = await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'B' }]));
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'B' }]), OWNER);
const res = await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'B' }]), OWNER);
expect(res.skipped).toBe(true);
const { triplets } = await queryTriplets(c, {});
@@ -45,8 +83,8 @@ describe('ingestEnvelope — 同 hash no-op', () => {
describe('ingestEnvelope — 新 hash deprecate-then-append', () => {
it('同 uri 新 hash → 舊批轉 deprecated、新批 active;查詢 active-only', async () => {
const c = mockClient();
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'old' }]));
const res = await ingestEnvelope(c, envelope('h2', [{ subject: 'A', predicate: 'r', object: 'new' }]));
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'old' }]), OWNER);
const res = await ingestEnvelope(c, envelope('h2', [{ subject: 'A', predicate: 'r', object: 'new' }]), OWNER);
expect(res).toEqual({ skipped: false, ingested: 1, deprecated: 1 });
// active-only 查詢只見新批。
@@ -103,7 +141,7 @@ describe('ingestEnvelope — 向量化打標欄位(contract 升格,ingest#1
expect(IngestEnvelopeSchema.safeParse(env).success).toBe(true);
// 落地:triplet 寫入、node 打標存進 entity slot。
const res = await ingestEnvelope(c, env);
const res = await ingestEnvelope(c, env, OWNER);
expect(res).toEqual({ skipped: false, ingested: 1, deprecated: 0 });
const { triplets } = await queryTriplets(c, {});
@@ -131,7 +169,7 @@ describe('ingestEnvelope — 向量化打標欄位(contract 升格,ingest#1
],
triplets: [{ subject: 'Graph RAG', predicate: 'r', object: 'X' }],
};
await ingestEnvelope(c, env);
await ingestEnvelope(c, env, OWNER);
const entities = await c.listRecordsByTemplate('entity');
expect(entities.filter((e) => e.values.node_id === 'graph-rag.md').length).toBe(1);
});
@@ -160,8 +198,8 @@ describe('ingestEnvelope — 向量化打標欄位(contract 升格,ingest#1
describe('ingestEnvelope — rollback(翻回 status', () => {
it('把 deprecated 翻回 active 後,active 查詢重新見到它', async () => {
const c = mockClient();
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'old' }]));
await ingestEnvelope(c, envelope('h2', [{ subject: 'A', predicate: 'r', object: 'new' }]));
await ingestEnvelope(c, envelope('h1', [{ subject: 'A', predicate: 'r', object: 'old' }]), OWNER);
await ingestEnvelope(c, envelope('h2', [{ subject: 'A', predicate: 'r', object: 'new' }]), OWNER);
// 取出被 deprecate 的舊批 id,手動 rollback(翻回 active、清 superseded_by)。
const all = await queryTriplets(c, { includeDeprecated: true });