Skip to content

Commit

Permalink
packages/core: remove getConcurrentAncestors
Browse files Browse the repository at this point in the history
  • Loading branch information
joeltg committed Jan 15, 2025
1 parent b20c70d commit b342117
Showing 1 changed file with 0 additions and 109 deletions.
109 changes: 0 additions & 109 deletions packages/core/src/runtime/AbstractRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,115 +308,6 @@ export abstract class AbstractRuntime {

protected static getKeyHash = (key: string) => bytesToHex(blake3(key, { dkLen: 16 }))

private async getConcurrentAncestors(context: ExecutionContext, model: string, key: string) {
const keyHash = AbstractRuntime.getKeyHash(key)
const lowerBound = `${model}/${keyHash}/${MIN_MESSAGE_ID}`
const upperBound = `${model}/${keyHash}/${MAX_MESSAGE_ID}`

const result = new Set<string>()

type Position = { branch: number; clock: number }
const stack: Position[] = []

const parentMessageRecords = await context.messageLog.db.getMany("$messages", context.message.parents)

for (let i = 0; i < parentMessageRecords.length; i++) {
const parentMessageRecord = parentMessageRecords[i]
const parentMessageId = context.message.parents[i]
if (parentMessageRecord == null) {
throw new Error(`message ${parentMessageId} not found`)
}
stack.push({ branch: parentMessageRecord.branch, clock: parentMessageRecord.message.clock })
}

const visited = new Set()

// eslint-disable-next-line no-constant-condition
while (true) {
const currentMessagePosition = stack.pop()
// no more messages to visit
if (!currentMessagePosition) {
break
}
visited.add(`${currentMessagePosition.branch}/${currentMessagePosition.clock}`)

const effectsOnThisBranch = await this.db.query<{
key: string
value: Uint8Array | undefined
branch: number
clock: number
}>("$effects", {
where: {
key: { gte: lowerBound, lt: upperBound },
branch: currentMessagePosition.branch,
clock: { lte: currentMessagePosition.clock },
},
orderBy: { clock: "desc" },
limit: 1,
})

const parentPositions: Position[] = []
const matchingEffectOnThisBranch = effectsOnThisBranch[0]
if (matchingEffectOnThisBranch) {
if (matchingEffectOnThisBranch.clock === currentMessagePosition.clock) {
// the current message is the latest effect on this branch
result.add(matchingEffectOnThisBranch.key)
// don't explore this message's parents
continue
} else {
parentPositions.push({ branch: matchingEffectOnThisBranch.branch, clock: matchingEffectOnThisBranch.clock })
}
}

// check for branches that merge into this branch
const branchMergeQuery = {
where: {
target_branch: currentMessagePosition.branch,
target_clock: { lte: currentMessagePosition.clock },
},
}
if (matchingEffectOnThisBranch) {
// @ts-ignore
branchMergeQuery.where.target_clock.gt = matchingEffectOnThisBranch.clock
}
for (const branchMerge of await context.messageLog.db.query<BranchMergeRecord>(
"$branch_merges",
branchMergeQuery,
)) {
parentPositions.push({
branch: branchMerge.source_branch,
clock: branchMerge.source_clock,
})
}

for (const parentPosition of parentPositions) {
if (!visited.has(`${parentPosition.branch}/${parentPosition.clock}`)) {
stack.push(parentPosition)
}
}
}

// remove messages that are parents of each other
const messagesToRemove = new Set<string>()
for (const parentEffectId of result) {
const parentMessageId = parentEffectId.split("/")[2]
for (const otherEffectId of result) {
const otherMessageId = otherEffectId.split("/")[2]
if (parentMessageId !== otherMessageId) {
if (await context.messageLog.isAncestor(parentMessageId, otherMessageId)) {
messagesToRemove.add(parentMessageId)
}
}
}
}

for (const m of messagesToRemove) {
result.delete(m)
}

return Array.from(result)
}

protected async getModelValue<T extends ModelValue = ModelValue>(
context: ExecutionContext,
model: string,
Expand Down

0 comments on commit b342117

Please sign in to comment.