-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: implement high-order tier vector search, embedding backfill, an… #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tanmay-008
wants to merge
1
commit into
rohitg00:main
Choose a base branch
from
Tanmay-008:feat/high-order-vector-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import type { ISdk } from "iii-sdk"; | ||
| import type { StateKV } from "../state/kv.js"; | ||
| import { KV } from "../state/schema.js"; | ||
| import type { SemanticMemory, ProceduralMemory, Crystal, Insight } from "../types.js"; | ||
| import { getEmbeddingProvider } from "./search.js"; | ||
| import { float32ToBase64 } from "../state/vector-index.js"; | ||
| import { logger } from "../logger.js"; | ||
|
|
||
| const BACKFILL_BATCH_SIZE = 20; | ||
|
|
||
| export function registerHighOrderBackfillFunction(sdk: ISdk, kv: StateKV): void { | ||
| sdk.registerFunction("mem::backfill-embeddings::high-order", async () => { | ||
| const ep = getEmbeddingProvider(); | ||
| if (!ep) { | ||
| return { success: false, error: "No embedding provider available" }; | ||
| } | ||
|
|
||
| const results = { | ||
| semantic: 0, | ||
| procedural: 0, | ||
| crystals: 0, | ||
| insights: 0, | ||
| }; | ||
|
|
||
| try { | ||
| // 1. Semantic Facts | ||
| const semantics = await kv.list<SemanticMemory>(KV.semantic); | ||
| const semToUpdate = semantics.filter( | ||
| (s) => !s.embedding || s.embeddingModel !== ep.name | ||
| ); | ||
| for (let i = 0; i < semToUpdate.length; i += BACKFILL_BATCH_SIZE) { | ||
| const batch = semToUpdate.slice(i, i + BACKFILL_BATCH_SIZE); | ||
| const texts = batch.map((s) => s.fact); | ||
| try { | ||
| const vectors = await ep.embedBatch(texts); | ||
| for (let j = 0; j < batch.length; j++) { | ||
| batch[j].embedding = float32ToBase64(vectors[j]); | ||
| batch[j].embeddingModel = ep.name; | ||
| await kv.set(KV.semantic, batch[j].id, batch[j]); | ||
| } | ||
| results.semantic += batch.length; | ||
| } catch (e) { | ||
| logger.warn("Semantic backfill batch failed", { error: String(e) }); | ||
| } | ||
| } | ||
|
|
||
| // 2. Procedural Skills | ||
| const procedurals = await kv.list<ProceduralMemory>(KV.procedural); | ||
| const procToUpdate = procedurals.filter( | ||
| (p) => !p.embedding || p.embeddingModel !== ep.name | ||
| ); | ||
| for (let i = 0; i < procToUpdate.length; i += BACKFILL_BATCH_SIZE) { | ||
| const batch = procToUpdate.slice(i, i + BACKFILL_BATCH_SIZE); | ||
| const texts = batch.map((p) => `${p.name} ${p.triggerCondition} ${p.steps.join(" ")}`); | ||
| try { | ||
| const vectors = await ep.embedBatch(texts); | ||
| for (let j = 0; j < batch.length; j++) { | ||
| batch[j].embedding = float32ToBase64(vectors[j]); | ||
| batch[j].embeddingModel = ep.name; | ||
| await kv.set(KV.procedural, batch[j].id, batch[j]); | ||
| } | ||
| results.procedural += batch.length; | ||
| } catch (e) { | ||
| logger.warn("Procedural backfill batch failed", { error: String(e) }); | ||
| } | ||
| } | ||
|
|
||
| // 3. Crystals | ||
| const crystals = await kv.list<Crystal>(KV.crystals); | ||
| const crysToUpdate = crystals.filter( | ||
| (c) => !c.embedding || c.embeddingModel !== ep.name | ||
| ); | ||
| for (let i = 0; i < crysToUpdate.length; i += BACKFILL_BATCH_SIZE) { | ||
| const batch = crysToUpdate.slice(i, i + BACKFILL_BATCH_SIZE); | ||
| const texts = batch.map((c) => `${c.narrative} ${c.lessons.join(" ")}`); | ||
| try { | ||
| const vectors = await ep.embedBatch(texts); | ||
| for (let j = 0; j < batch.length; j++) { | ||
| batch[j].embedding = float32ToBase64(vectors[j]); | ||
| batch[j].embeddingModel = ep.name; | ||
| await kv.set(KV.crystals, batch[j].id, batch[j]); | ||
| } | ||
| results.crystals += batch.length; | ||
| } catch (e) { | ||
| logger.warn("Crystal backfill batch failed", { error: String(e) }); | ||
| } | ||
| } | ||
|
|
||
| // 4. Insights | ||
| const insights = await kv.list<Insight>(KV.insights); | ||
| const insToUpdate = insights.filter( | ||
| (ins) => !ins.deleted && (!ins.embedding || ins.embeddingModel !== ep.name) | ||
| ); | ||
| for (let i = 0; i < insToUpdate.length; i += BACKFILL_BATCH_SIZE) { | ||
| const batch = insToUpdate.slice(i, i + BACKFILL_BATCH_SIZE); | ||
| const texts = batch.map((ins) => `${ins.title} ${ins.content}`); | ||
| try { | ||
| const vectors = await ep.embedBatch(texts); | ||
| for (let j = 0; j < batch.length; j++) { | ||
| batch[j].embedding = float32ToBase64(vectors[j]); | ||
| batch[j].embeddingModel = ep.name; | ||
| await kv.set(KV.insights, batch[j].id, batch[j]); | ||
| } | ||
| results.insights += batch.length; | ||
| } catch (e) { | ||
| logger.warn("Insight backfill batch failed", { error: String(e) }); | ||
| } | ||
| } | ||
|
|
||
| const total = results.semantic + results.procedural + results.crystals + results.insights; | ||
| if (total > 0) { | ||
| logger.info("High-order embedding backfill complete", { backfilled: results }); | ||
| } | ||
|
|
||
| return { success: true, backfilled: results }; | ||
| } catch (err) { | ||
| const errorMsg = err instanceof Error ? err.message : String(err); | ||
| logger.error("High-order backfill encountered a fatal error", { error: errorMsg }); | ||
| return { success: false, error: errorMsg }; | ||
| } | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an audit record for high-order backfills.
This function rewrites four KV scopes but never calls
recordAudit(), so manual or scheduled backfills leave no audit trail.Suggested fix
import { float32ToBase64 } from "../state/vector-index.js"; import { logger } from "../logger.js"; +import { recordAudit } from "./audit.js"; @@ const total = results.semantic + results.procedural + results.crystals + results.insights; if (total > 0) { logger.info("High-order embedding backfill complete", { backfilled: results }); } + + await recordAudit( + kv, + "backfill_embeddings", + "mem::backfill-embeddings::high-order", + [], + { backfilled: results, embeddingModel: ep.name }, + ); return { success: true, backfilled: results };As per coding guidelines,
src/functions/**/*.ts: "UserecordAudit()for all state-changing operations".🤖 Prompt for AI Agents