Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-graph-path-parallel-relations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@inkcre/core': patch
---

图谱寻路按 Block 数量计算探索预算。大量平行关系占满 PostgREST 返回的一页时,继续检查较早的关系。
图谱邻域和寻路跳过缺少端点的关系,保留可用的邻域和路径。
从自环关系打开邻域时,返回该关系及其唯一的端点 Block。
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ jobs:
with:
ref: ${{ github.sha }}
persist-credentials: false
submodules: true

- name: Install the pinned pnpm release
uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
Expand Down
68 changes: 38 additions & 30 deletions packages/core/src/graph-navigation-retrieval/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -134,10 +135,13 @@ export class GraphNavigationRetrievalManager {
static async getRelationNeighborhood(
focalRelation: RelationRef
): Promise<RelationNeighborhood | null> {
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] },
Expand Down Expand Up @@ -176,6 +180,7 @@ export class GraphNavigationRetrievalManager {
relation_path: [],
}) as PathResult
}
if (maxExplored < 2) return { status: 'limit_reached' }

const forwardSteps = new Map<BlockRef, PathStep>([[fromBlock, null]])
const backwardSteps = new Map<BlockRef, PathStep>([[toBlock, null]])
Expand All @@ -184,6 +189,7 @@ export class GraphNavigationRetrievalManager {
let forwardFrontier = new Set([fromBlock])
let backwardFrontier = new Set([toBlock])
const traversedRelations = new Map<RelationRef, Relation>()
const exploredBlocks = new Set([fromBlock, toBlock])

while (forwardFrontier.size > 0 && backwardFrontier.size > 0) {
const forwardLevel = forwardDepths.get(forwardFrontier.values().next().value!)!
Expand All @@ -199,13 +205,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,
Expand All @@ -229,29 +235,33 @@ export class GraphNavigationRetrievalManager {
ownDepths: Map<BlockRef, number>
otherDepths: Map<BlockRef, number>
maxHops: number
maxExplored: number
exploredBlocks: Set<BlockRef>
traversedRelations: Map<RelationRef, Relation>
}): Promise<{ frontier: Set<BlockRef>; meeting: BlockRef | null }> {
}): Promise<{ frontier: Set<BlockRef>; meeting: BlockRef | null } | null> {
const blocks = [...options.frontier]
const nextFrontier = new Set<BlockRef>()
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,
options.direction,
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)
Expand All @@ -268,36 +278,34 @@ export class GraphNavigationRetrievalManager {
return { frontier: nextFrontier, meeting }
}

private static async getFrontierRelations(
private static async *getFrontierRelations(
frontier: Set<BlockRef>,
direction: GraphDirection,
contents: string[],
reverse: boolean
): Promise<Relation[]> {
): AsyncGenerator<Relation> {
const needsFrom = reverse ? direction !== 'out' : direction !== 'in'
const needsTo = reverse ? direction !== 'in' : direction !== 'out'
const queries: Promise<Relation[]>[] = []
if (needsFrom) {
queries.push(
Relation.getEndpointPage({
blockIds: frontier,
endpoint: 'from_',
contents,
limit: MAX_MAX_EXPLORED_BLOCKS,
})
)
}
if (needsTo) {
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: 'to_',
endpoint,
contents,
limit: MAX_MAX_EXPLORED_BLOCKS,
cursor,
limit: FRONTIER_RELATION_PAGE_SIZE,
})
)
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(
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/info-base/relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ export class Relation extends Z.class({
return relation
}

static async find(id: RelationRef): Promise<Relation | null> {
const result = await this.dbApi.from().select().eq('id', id).maybeSingle()
static async find(
id: RelationRef,
options: { requireEndpoints?: boolean } = {}
): Promise<Relation | null> {
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
}
Expand All @@ -53,10 +58,12 @@ export class Relation extends Z.class({
}): Promise<Relation[]> {
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 ?? [])]
Expand Down
Loading
Loading