From 7cff504011f7edcb36439c12def6151433c2318b Mon Sep 17 00:00:00 2001 From: Lan_zhijiang Date: Sat, 28 Nov 2026 22:30:02 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix(core):=20=E6=8C=89=20Block=20=E9=A2=84?= =?UTF-8?q?=E7=AE=97=E7=BB=A7=E7=BB=AD=E5=9B=BE=E8=B0=B1=E5=AF=BB=E8=B7=AF?= =?UTF-8?q?=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fix-graph-path-parallel-relations.md | 5 ++ .../src/graph-navigation-retrieval/main.ts | 59 ++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 .changeset/fix-graph-path-parallel-relations.md diff --git a/.changeset/fix-graph-path-parallel-relations.md b/.changeset/fix-graph-path-parallel-relations.md new file mode 100644 index 00000000..ad7aea68 --- /dev/null +++ b/.changeset/fix-graph-path-parallel-relations.md @@ -0,0 +1,5 @@ +--- +'@inkcre/core': patch +--- + +图谱寻路按 Block 数量计算探索预算。大量平行关系占满 PostgREST 返回的一页时,继续检查较早的关系。 diff --git a/packages/core/src/graph-navigation-retrieval/main.ts b/packages/core/src/graph-navigation-retrieval/main.ts index d869a6e7..b67a872a 100644 --- a/packages/core/src/graph-navigation-retrieval/main.ts +++ b/packages/core/src/graph-navigation-retrieval/main.ts @@ -61,6 +61,7 @@ const MAX_MAX_HOPS = 8 const DEFAULT_MAX_EXPLORED_BLOCKS = 1000 const MAX_MAX_EXPLORED_BLOCKS = 10000 const FRONTIER_QUERY_SIZE = 200 +const FRONTIER_RELATION_PAGE_SIZE = 1000 type PathStep = { block: BlockRef; relation: RelationRef } | null @@ -176,6 +177,7 @@ export class GraphNavigationRetrievalManager { relation_path: [], }) as PathResult } + if (maxExplored < 2) return { status: 'limit_reached' } const forwardSteps = new Map([[fromBlock, null]]) const backwardSteps = new Map([[toBlock, null]]) @@ -184,6 +186,7 @@ export class GraphNavigationRetrievalManager { let forwardFrontier = new Set([fromBlock]) let backwardFrontier = new Set([toBlock]) const traversedRelations = new Map() + const exploredBlocks = new Set([fromBlock, toBlock]) while (forwardFrontier.size > 0 && backwardFrontier.size > 0) { const forwardLevel = forwardDepths.get(forwardFrontier.values().next().value!)! @@ -199,13 +202,13 @@ export class GraphNavigationRetrievalManager { ownDepths: expandForward ? forwardDepths : backwardDepths, otherDepths: expandForward ? backwardDepths : forwardDepths, maxHops, + maxExplored, + exploredBlocks, traversedRelations, }) + if (!expanded) return { status: 'limit_reached' } if (expandForward) forwardFrontier = expanded.frontier else backwardFrontier = expanded.frontier - if (new Set([...forwardSteps.keys(), ...backwardSteps.keys()]).size > maxExplored) { - return { status: 'limit_reached' } - } if (expanded.meeting !== null) { return this.assemblePath({ fromBlock, @@ -229,22 +232,23 @@ export class GraphNavigationRetrievalManager { ownDepths: Map otherDepths: Map maxHops: number + maxExplored: number + exploredBlocks: Set traversedRelations: Map - }): Promise<{ frontier: Set; meeting: BlockRef | null }> { + }): Promise<{ frontier: Set; meeting: BlockRef | null } | null> { const blocks = [...options.frontier] const nextFrontier = new Set() let meeting: BlockRef | null = null let bestHops = Number.POSITIVE_INFINITY for (let offset = 0; offset < blocks.length; offset += FRONTIER_QUERY_SIZE) { const chunk = new Set(blocks.slice(offset, offset + FRONTIER_QUERY_SIZE)) - const relations = await this.getFrontierRelations( + const relations = this.getFrontierRelations( chunk, options.direction, options.contents, options.reverse ) - for (const relation of relations) { - options.traversedRelations.set(relation.id, relation) + for await (const relation of relations) { for (const [current, neighbor] of this.relationSteps( relation, chunk, @@ -252,6 +256,9 @@ export class GraphNavigationRetrievalManager { options.reverse )) { if (options.ownSteps.has(neighbor)) continue + options.exploredBlocks.add(neighbor) + if (options.exploredBlocks.size > options.maxExplored) return null + options.traversedRelations.set(relation.id, relation) options.ownSteps.set(neighbor, { block: current, relation: relation.id }) options.ownDepths.set(neighbor, options.ownDepths.get(current)! + 1) nextFrontier.add(neighbor) @@ -268,36 +275,34 @@ export class GraphNavigationRetrievalManager { return { frontier: nextFrontier, meeting } } - private static async getFrontierRelations( + private static async *getFrontierRelations( frontier: Set, direction: GraphDirection, contents: string[], reverse: boolean - ): Promise { + ): AsyncGenerator { const needsFrom = reverse ? direction !== 'out' : direction !== 'in' const needsTo = reverse ? direction !== 'in' : direction !== 'out' - const queries: Promise[] = [] - if (needsFrom) { - queries.push( - Relation.getEndpointPage({ + const endpoints: ('from_' | 'to_')[] = [] + if (needsFrom) endpoints.push('from_') + if (needsTo) endpoints.push('to_') + for (const endpoint of endpoints) { + let cursor: RelationRef | undefined + // Parallel edges do not exhaust the Block budget, and PostgREST may shorten a page. + while (true) { + const page = await Relation.getEndpointPage({ blockIds: frontier, - endpoint: 'from_', + endpoint, contents, - limit: MAX_MAX_EXPLORED_BLOCKS, + cursor, + limit: FRONTIER_RELATION_PAGE_SIZE, }) - ) - } - if (needsTo) { - queries.push( - Relation.getEndpointPage({ - blockIds: frontier, - endpoint: 'to_', - contents, - limit: MAX_MAX_EXPLORED_BLOCKS, - }) - ) + const last = page.at(-1) + if (!last) break + yield* page + cursor = last.id + } } - return [...new Map((await Promise.all(queries)).flat().map((item) => [item.id, item])).values()] } private static relationSteps( From e294e97839c29f3058e83b8f02c3928eaa9094c2 Mon Sep 17 00:00:00 2001 From: Lan_zhijiang Date: Sat, 28 Nov 2026 22:30:02 +0800 Subject: [PATCH 2/4] =?UTF-8?q?test(core):=20=E7=94=A8=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=20PostgREST=20=E9=AA=8C=E8=AF=81=E5=85=B1=E4=BA=AB=E5=9B=BE?= =?UTF-8?q?=E8=B0=B1=E8=AF=AD=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 2 + tests/e2e/graph-navigation.mjs | 225 ++++++++++++++++++++++++++++++++ tests/e2e/peer-database.spec.ts | 2 + 3 files changed, 229 insertions(+) create mode 100644 tests/e2e/graph-navigation.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b4e14f8d..39b44fdf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -155,6 +155,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + submodules: true - name: Install the pinned pnpm release uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 diff --git a/tests/e2e/graph-navigation.mjs b/tests/e2e/graph-navigation.mjs new file mode 100644 index 00000000..ceb45f74 --- /dev/null +++ b/tests/e2e/graph-navigation.mjs @@ -0,0 +1,225 @@ +import assert from 'node:assert/strict' +import { randomUUID } from 'node:crypto' +import { readFile } from 'node:fs/promises' +import { test } from '@playwright/test' + +const postgrestUrl = process.env.INKCRE_E2E_POSTGREST_URL +let graph +let headers + +async function createRows(table, rows) { + const response = await fetch(new URL(table, postgrestUrl), { + method: 'POST', + headers: { ...headers, Prefer: 'return=representation' }, + body: JSON.stringify(rows), + }) + assert.equal(response.status, 201, `Cannot create graph fixture in ${table}`) + return response.json() +} + +async function removeBlocks(ids) { + if (ids.length === 0) return + const response = await fetch(new URL(`blocks?id=in.(${ids.join(',')})`, postgrestUrl), { + method: 'DELETE', + headers: { ...headers, Prefer: 'return=minimal' }, + }) + assert.equal(response.status, 204, 'Cannot remove graph fixture Blocks') +} + +async function createBlock(alias) { + const [{ id }] = await createRows('blocks', { + resolver: 'core.text.v1', + storage: null, + content: `graph-contract.${randomUUID()}.${alias}`, + }) + return id +} + +async function readScenario({ operation, input }, blocks, relations) { + const { direction, contents, limit } = input + switch (operation) { + case 'block_neighborhood': + return graph.getBlockNeighborhood(blocks.get(input.focal), { direction, contents, limit }) + case 'relation_neighborhood': + return graph.getRelationNeighborhood(relations.get(input.focal)) + case 'find_path': + return graph.findPath(blocks.get(input.from), blocks.get(input.to), { + direction, + contents, + maxHops: input.max_hops, + maxExploredBlocks: input.max_explored_blocks, + }) + default: + assert.fail(`Unknown graph operation ${operation}`) + } +} + +function verifyPath(result, input, blocks) { + const { block_path: blockPath, relation_path: relationPath, graph: actual } = result + assert.equal(blockPath[0], blocks.get(input.from)) + assert.equal(blockPath.at(-1), blocks.get(input.to)) + assert.equal(blockPath.length, relationPath.length + 1) + assert.deepEqual(new Set(blockPath), new Set(actual.blocks.map((block) => block.id))) + assert.deepEqual(new Set(relationPath), new Set(actual.relations.map((edge) => edge.id))) + const edges = new Map(actual.relations.map((edge) => [edge.id, edge])) + for (const [index, relationId] of relationPath.entries()) { + const relation = edges.get(relationId) + assert.ok(relation) + const from = blockPath[index] + const to = blockPath[index + 1] + const follows = relation.from_ === from && relation.to_ === to + const reverses = relation.from_ === to && relation.to_ === from + assert.ok((input.direction !== 'in' && follows) || (input.direction !== 'out' && reverses)) + if (input.contents?.length) assert.ok(input.contents.includes(relation.content)) + } +} + +function verifyScenario(scenario, result, blocks, relations) { + const { operation, input, assert: expected } = scenario + const status = result === null ? 'missing' : (result.status ?? 'found') + assert.equal(status, expected.status) + if (status !== 'found') return + + const blockIds = new Set(result.graph.blocks.map((block) => block.id)) + const relationIds = new Set(result.graph.relations.map((edge) => edge.id)) + if (expected.focal) assert.equal(result.focal_block, blocks.get(expected.focal)) + if (operation === 'relation_neighborhood') { + assert.equal(result.focal_relation, relations.get(input.focal)) + } + if (expected.block_set) { + assert.deepEqual(blockIds, new Set(expected.block_set.map((alias) => blocks.get(alias)))) + } + if (expected.relation_set) { + assert.deepEqual( + relationIds, + new Set(expected.relation_set.map((alias) => relations.get(alias))) + ) + } + if (expected.endpoint_closed) { + for (const edge of result.graph.relations) { + assert.ok(blockIds.has(edge.from_) && blockIds.has(edge.to_)) + } + } + if (expected.block_path) { + assert.deepEqual( + result.block_path, + expected.block_path.map((alias) => blocks.get(alias)) + ) + } + if (expected.relation_path) { + assert.deepEqual( + result.relation_path, + expected.relation_path.map((alias) => relations.get(alias)) + ) + } + if (expected.hop_count !== undefined) + assert.equal(result.relation_path.length, expected.hop_count) + if (operation === 'find_path') verifyPath(result, input, blocks) +} + +export function registerGraphNavigationTests() { + test.describe('Graph navigation SDK', () => { + test.beforeAll(async () => { + const jwtSecret = process.env.INKCRE_E2E_JWT_SECRET + assert.ok(postgrestUrl && jwtSecret, 'Run this test with the isolated Web E2E database') + const { GraphNavigationRetrievalManager, configStore } = + await import('../../packages/core/dist/index.js') + const { signDatabaseToken } = await import('../../packages/core/dist/sdk/sdk.js') + graph = GraphNavigationRetrievalManager + Object.assign(configStore.metaConfig, { + INKCRE_PGREST_URL: postgrestUrl, + INKCRE_JWT_SECRET: jwtSecret, + telemetry_enabled: false, + }) + headers = { + Authorization: `Bearer ${await signDatabaseToken(jwtSecret)}`, + 'Content-Type': 'application/json', + } + }) + + test('Web Core matches the shared graph navigation corpus over PostgREST', async () => { + const corpus = JSON.parse( + await readFile( + new URL( + '../../docs/_shared/20-product-tdd/graph-navigation-retrieval.corpus.json', + import.meta.url + ), + 'utf8' + ) + ) + assert.equal(corpus.contract, 'core.graph_navigation_retrieval.v1') + const blocks = new Map() + const relations = new Map() + try { + for (const alias of [...corpus.topology.blocks, 'missing']) { + blocks.set(alias, await createBlock(alias)) + } + await removeBlocks([blocks.get('missing')]) + for (const edge of corpus.topology.relations) { + const [{ id }] = await createRows('relations', { + from_: blocks.get(edge.from), + to_: blocks.get(edge.to), + content: edge.content, + }) + relations.set(edge.alias, id) + } + for (const scenario of corpus.scenarios) { + await test.step(scenario.name, async () => { + verifyScenario( + scenario, + await readScenario(scenario, blocks, relations), + blocks, + relations + ) + }) + } + } finally { + await removeBlocks([...blocks.values()]) + } + }) + + test('parallel relations cannot mask a path within the Block budget', async () => { + const [start, bridge, target, noise] = await createRows( + 'blocks', + ['start', 'bridge', 'target', 'noise'].map((alias) => ({ + resolver: 'core.text.v1', + storage: null, + content: `graph-parallel.${randomUUID()}.${alias}`, + })) + ) + try { + const path = await createRows('relations', [ + { from_: start.id, to_: bridge.id, content: 'path' }, + { from_: bridge.id, to_: target.id, content: 'path' }, + ]) + const response = await fetch(new URL('relations', postgrestUrl), { + method: 'POST', + headers: { ...headers, Prefer: 'return=minimal' }, + body: JSON.stringify( + Array.from({ length: 10_000 }, (_, index) => ({ + from_: start.id, + to_: noise.id, + content: `parallel-${index}`, + })) + ), + }) + assert.equal(response.status, 201) + const options = { direction: 'out', maxHops: 4, maxExploredBlocks: 10 } + const result = await graph.findPath(start.id, target.id, options) + assert.equal(result.status, 'found') + assert.deepEqual(result.block_path, [start.id, bridge.id, target.id]) + assert.deepEqual( + result.relation_path, + path.map((relation) => relation.id) + ) + const insufficient = await graph.findPath(start.id, target.id, { + ...options, + maxExploredBlocks: 3, + }) + assert.deepEqual(insufficient, { status: 'limit_reached' }) + } finally { + await removeBlocks([start.id, bridge.id, target.id, noise.id]) + } + }) + }) +} diff --git a/tests/e2e/peer-database.spec.ts b/tests/e2e/peer-database.spec.ts index 036bf9e4..958bf406 100644 --- a/tests/e2e/peer-database.spec.ts +++ b/tests/e2e/peer-database.spec.ts @@ -2,6 +2,7 @@ import { expect, test, type Page } from '@playwright/test' import { SignJWT } from 'jose' import { readFileSync } from 'node:fs' import { registerGitHubNativeTests } from './github-native' +import { registerGraphNavigationTests } from './graph-navigation.mjs' const postgrestUrl = process.env.INKCRE_E2E_POSTGREST_URL! const jwtSecret = process.env.INKCRE_E2E_JWT_SECRET! @@ -2036,3 +2037,4 @@ test('Source titles follow saved names across navigation and refresh', async ({ }) registerGitHubNativeTests() +registerGraphNavigationTests() From 83ed6bc1f74e594e6e8b4ac95a4f889c0fad3e96 Mon Sep 17 00:00:00 2001 From: Lan_zhijiang Date: Sat, 28 Nov 2026 22:43:39 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(core):=20=E6=8C=89=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E7=AB=AF=E7=82=B9=E8=BA=AB=E4=BB=BD=E8=AF=BB=E5=8F=96=E5=9B=BE?= =?UTF-8?q?=E8=B0=B1=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fix-graph-path-parallel-relations.md | 2 + .../src/graph-navigation-retrieval/main.ts | 12 +++- packages/core/src/info-base/relation.ts | 14 ++++- tests/e2e/graph-navigation.mjs | 63 +++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/.changeset/fix-graph-path-parallel-relations.md b/.changeset/fix-graph-path-parallel-relations.md index ad7aea68..57a78c13 100644 --- a/.changeset/fix-graph-path-parallel-relations.md +++ b/.changeset/fix-graph-path-parallel-relations.md @@ -3,3 +3,5 @@ --- 图谱寻路按 Block 数量计算探索预算。大量平行关系占满 PostgREST 返回的一页时,继续检查较早的关系。 +图谱邻域和寻路跳过缺少端点的关系,保留可用的邻域和路径。 +从自环关系打开邻域时,返回该关系及其唯一的端点 Block。 diff --git a/packages/core/src/graph-navigation-retrieval/main.ts b/packages/core/src/graph-navigation-retrieval/main.ts index b67a872a..49ead2fb 100644 --- a/packages/core/src/graph-navigation-retrieval/main.ts +++ b/packages/core/src/graph-navigation-retrieval/main.ts @@ -96,6 +96,7 @@ export class GraphNavigationRetrievalManager { contents: options.contents, cursor: options.cursor, limit: requested, + requireEndpoints: true, }) ) } @@ -107,6 +108,7 @@ export class GraphNavigationRetrievalManager { contents: options.contents, cursor: options.cursor, limit: requested, + requireEndpoints: true, }) ) } @@ -135,10 +137,13 @@ export class GraphNavigationRetrievalManager { static async getRelationNeighborhood( focalRelation: RelationRef ): Promise { - const relation = await Relation.find(focalRelation) + const relation = await Relation.find(focalRelation, { requireEndpoints: true }) if (!relation) return null - const blocks = await Block.getMany([relation.from_, relation.to_]) - if (new Set(blocks.map((block) => block.id)).size !== 2) return null + const endpoints = new Set([relation.from_, relation.to_]) + const blocks = await Block.getMany(endpoints) + const blockIds = new Set(blocks.map((block) => block.id)) + if (blockIds.size !== endpoints.size || [...endpoints].some((id) => !blockIds.has(id))) + return null return RelationNeighborhoodSchema.parse({ focal_relation: focalRelation, graph: { blocks, relations: [relation] }, @@ -296,6 +301,7 @@ export class GraphNavigationRetrievalManager { contents, cursor, limit: FRONTIER_RELATION_PAGE_SIZE, + requireEndpoints: true, }) const last = page.at(-1) if (!last) break diff --git a/packages/core/src/info-base/relation.ts b/packages/core/src/info-base/relation.ts index 9c31091d..5d5ac2f2 100644 --- a/packages/core/src/info-base/relation.ts +++ b/packages/core/src/info-base/relation.ts @@ -34,8 +34,13 @@ export class Relation extends Z.class({ return relation } - static async find(id: RelationRef): Promise { - const result = await this.dbApi.from().select().eq('id', id).maybeSingle() + static async find( + id: RelationRef, + options: { requireEndpoints?: boolean } = {} + ): Promise { + let query = this.dbApi.from().select().eq('id', id) + if (options.requireEndpoints) query = query.not('from_', 'is', null).not('to_', 'is', null) + const result = await query.maybeSingle() if (result.error) throw result.error return result.data ? Relation.parse(result.data) : null } @@ -50,6 +55,7 @@ export class Relation extends Z.class({ contents?: Iterable cursor?: RelationRef limit: number + requireEndpoints?: boolean }): Promise { const blockIds = [...new Set(options.blockIds)] if (blockIds.length === 0 || options.limit <= 0) return [] @@ -62,6 +68,10 @@ export class Relation extends Z.class({ const contents = [...new Set(options.contents ?? [])] if (contents.length > 0) query = query.in('content', contents) if (options.cursor !== undefined) query = query.lt('id', options.cursor) + if (options.requireEndpoints) { + const opposite = options.endpoint === 'from_' ? 'to_' : 'from_' + query = query.not(opposite, 'is', null) + } const result = await query if (result.error) throw result.error return (result.data ?? []).map((item) => Relation.parse(item)) diff --git a/tests/e2e/graph-navigation.mjs b/tests/e2e/graph-navigation.mjs index ceb45f74..47b6cd82 100644 --- a/tests/e2e/graph-navigation.mjs +++ b/tests/e2e/graph-navigation.mjs @@ -178,6 +178,69 @@ export function registerGraphNavigationTests() { } }) + test('graph navigation preserves relations whose endpoints are present', async () => { + const start = await createBlock('start') + const blockIds = [start] + try { + const target = await createBlock('target') + blockIds.push(target) + const [connected, ...incomplete] = await createRows('relations', [ + { from_: start, to_: target, content: 'connected' }, + { from_: start, to_: null, content: 'first absent target' }, + { from_: start, to_: null, content: 'second absent target' }, + { from_: null, to_: target, content: 'first absent source' }, + { from_: null, to_: target, content: 'second absent source' }, + ]) + assert.equal(incomplete.filter((relation) => relation.to_ === null).length, 2) + assert.equal(incomplete.filter((relation) => relation.from_ === null).length, 2) + for (const focal of blockIds) { + const result = await graph.getBlockNeighborhood(focal, { limit: 1 }) + assert.equal(result.focal_block, focal) + assert.deepEqual(new Set(result.graph.blocks.map((block) => block.id)), new Set(blockIds)) + assert.deepEqual( + result.graph.relations.map((relation) => relation.id), + [connected.id] + ) + assert.equal(result.next_cursor, null) + } + + const outgoing = await graph.findPath(start, target, { direction: 'out' }) + const incoming = await graph.findPath(target, start, { direction: 'in' }) + for (const [path, expectedBlocks] of [ + [outgoing, [start, target]], + [incoming, [target, start]], + ]) { + assert.equal(path.status, 'found') + assert.deepEqual(path.block_path, expectedBlocks) + assert.deepEqual(path.relation_path, [connected.id]) + } + + const { Relation } = await import('../../packages/core/dist/index.js') + for (const relation of incomplete) { + assert.equal(await graph.getRelationNeighborhood(relation.id), null) + await assert.rejects(() => Relation.find(relation.id), { name: 'ZodError' }) + } + + const [selfLoop] = await createRows('relations', { + from_: start, + to_: start, + content: 'self loop', + }) + const neighborhood = await graph.getRelationNeighborhood(selfLoop.id) + assert.equal(neighborhood.focal_relation, selfLoop.id) + assert.deepEqual( + neighborhood.graph.blocks.map((block) => block.id), + [start] + ) + assert.deepEqual( + neighborhood.graph.relations.map((relation) => relation.id), + [selfLoop.id] + ) + } finally { + await removeBlocks(blockIds) + } + }) + test('parallel relations cannot mask a path within the Block budget', async () => { const [start, bridge, target, noise] = await createRows( 'blocks', From 7c54c7eba06954b951d0c159ea99d2847920b845 Mon Sep 17 00:00:00 2001 From: Lan_zhijiang Date: Sat, 28 Nov 2026 22:46:44 +0800 Subject: [PATCH 4/4] =?UTF-8?q?ref(core):=20=E9=BB=98=E8=AE=A4=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=AB=AF=E7=82=B9=E5=AE=8C=E6=95=B4=E7=9A=84=20Relati?= =?UTF-8?q?on=20=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/graph-navigation-retrieval/main.ts | 3 --- packages/core/src/info-base/relation.ts | 7 ++----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/core/src/graph-navigation-retrieval/main.ts b/packages/core/src/graph-navigation-retrieval/main.ts index 49ead2fb..2215b929 100644 --- a/packages/core/src/graph-navigation-retrieval/main.ts +++ b/packages/core/src/graph-navigation-retrieval/main.ts @@ -96,7 +96,6 @@ export class GraphNavigationRetrievalManager { contents: options.contents, cursor: options.cursor, limit: requested, - requireEndpoints: true, }) ) } @@ -108,7 +107,6 @@ export class GraphNavigationRetrievalManager { contents: options.contents, cursor: options.cursor, limit: requested, - requireEndpoints: true, }) ) } @@ -301,7 +299,6 @@ export class GraphNavigationRetrievalManager { contents, cursor, limit: FRONTIER_RELATION_PAGE_SIZE, - requireEndpoints: true, }) const last = page.at(-1) if (!last) break diff --git a/packages/core/src/info-base/relation.ts b/packages/core/src/info-base/relation.ts index 5d5ac2f2..fc899a4c 100644 --- a/packages/core/src/info-base/relation.ts +++ b/packages/core/src/info-base/relation.ts @@ -55,23 +55,20 @@ export class Relation extends Z.class({ contents?: Iterable cursor?: RelationRef limit: number - requireEndpoints?: boolean }): Promise { const blockIds = [...new Set(options.blockIds)] if (blockIds.length === 0 || options.limit <= 0) return [] + const opposite = options.endpoint === 'from_' ? 'to_' : 'from_' let query = this.dbApi .from() .select() .in(options.endpoint, blockIds) + .not(opposite, 'is', null) .order('id', { ascending: false }) .limit(options.limit) const contents = [...new Set(options.contents ?? [])] if (contents.length > 0) query = query.in('content', contents) if (options.cursor !== undefined) query = query.lt('id', options.cursor) - if (options.requireEndpoints) { - const opposite = options.endpoint === 'from_' ? 'to_' : 'from_' - query = query.not(opposite, 'is', null) - } const result = await query if (result.error) throw result.error return (result.data ?? []).map((item) => Relation.parse(item))