P0-D: Implement minimal Cortex query (issue #21)#49
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR implements the minimal Cortex query functionality (P0-D from the TODO). It adds a query() function that embeds query text, scores hotpath-resident pages first, falls back to a full scan for remaining results, updates PageActivity on hits, and triggers a promotion sweep.
Changes:
- New
cortex/Query.tswithquery()entry point andcortex/QueryResult.tsDTO - New
getAllPages()method on theMetadataStoreinterface and its IndexedDB implementation for warm/cold fallback scans - Test coverage in
tests/cortex/Query.test.tsand updated mock intests/SalienceEngine.test.ts
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| cortex/Query.ts | Core query implementation: embed → score hotpath → cold fallback → update activity → promotion sweep |
| cortex/QueryResult.ts | Simple result DTO interface (pages, scores, metadata) |
| core/types.ts | Added getAllPages() to MetadataStore interface |
| storage/IndexedDbMetadataStore.ts | Implemented getAllPages() via IDB getAll() |
| tests/cortex/Query.test.ts | Happy-path integration test for query + activity update |
| tests/SalienceEngine.test.ts | Added getAllPages stub to mock MetadataStore |
| TODO.md | Marked P0-D1, P0-D2, P0-D3 as complete |
Comment on lines
+148
to
+156
| /** Returns all pages in the store. Used for warm/cold fallbacks in query. */ | ||
| async getAllPages(): Promise<Page[]> { | ||
| return new Promise((resolve, reject) => { | ||
| const tx = this.db.transaction(STORE.pages, "readonly"); | ||
| const req = tx.objectStore(STORE.pages).getAll(); | ||
| req.onsuccess = () => resolve(req.result as Page[]); | ||
| req.onerror = () => reject(req.error); | ||
| }); | ||
| } |
| async putPage(): Promise<void> { /* stub */ } | ||
| async getPage(): Promise<undefined> { return undefined; } | ||
| async putBook(): Promise<void> { /* stub */ } | ||
| async getPage(): Promise<undefined> { return undefined; } async getAllPages(): Promise<any[]> { return []; } async putBook(): Promise<void> { /* stub */ } |
Comment on lines
+58
to
+123
| describe("cortex query (minimal)", () => { | ||
| beforeEach(() => { | ||
| (globalThis as any).indexedDB = new IDBFactory(); | ||
| (globalThis as any).IDBKeyRange = FakeIDBKeyRange; | ||
| }); | ||
|
|
||
| it("returns the most relevant page and updates activity", async () => { | ||
| const metadataStore = await IndexedDbMetadataStore.open(freshDbName()); | ||
| const vectorStore = new MemoryVectorStore(); | ||
| const keyPair = await generateKeyPair(); | ||
|
|
||
| const backend = new DeterministicDummyEmbeddingBackend({ dimension: 4 }); | ||
| const vectorBackend = new TestVectorBackend(); | ||
|
|
||
| const runner = new EmbeddingRunner(async () => ({ | ||
| backend, | ||
| selectedKind: "dummy" as const, | ||
| reason: "forced" as const, | ||
| supportedKinds: ["dummy" as const], | ||
| measurements: [], | ||
| })); | ||
|
|
||
| const profile: ModelProfile = { | ||
| modelId: "test-model", | ||
| embeddingDimension: 4, | ||
| contextWindowTokens: 64, | ||
| truncationTokens: 48, | ||
| maxChunkTokens: 5, | ||
| source: "metadata", | ||
| }; | ||
|
|
||
| const text = "One two three four five six seven eight nine ten."; | ||
| const ingestResult = await ingestText(text, { | ||
| modelProfile: profile, | ||
| embeddingRunner: runner, | ||
| vectorStore, | ||
| metadataStore, | ||
| keyPair, | ||
| }); | ||
|
|
||
| expect(ingestResult.pages.length).toBeGreaterThanOrEqual(2); | ||
|
|
||
| const targetPage = ingestResult.pages[0]; | ||
|
|
||
| const result = await query(targetPage.content, { | ||
| modelProfile: profile, | ||
| embeddingRunner: runner, | ||
| vectorStore, | ||
| metadataStore, | ||
| vectorBackend, | ||
| topK: 1, | ||
| }); | ||
|
|
||
| const hotpath = await metadataStore.getHotpathEntries("page"); | ||
| const hotIds = hotpath.map((e) => e.entityId); | ||
|
|
||
| // Query should prioritize hotpath pages and return one of them. | ||
| expect(result.pages).toHaveLength(1); | ||
| expect(hotIds).toContain(result.pages[0].pageId); | ||
|
|
||
| const returned = result.pages[0]; | ||
| const activity = await metadataStore.getPageActivity(returned.pageId); | ||
| expect(activity?.queryHitCount).toBe(1); | ||
| expect(activity?.lastQueryAt).toBeDefined(); | ||
| }); | ||
| }); |
| - DTO with `pages: Page[]`, `scores: number[]`, `metadata: object` | ||
|
|
||
| - [ ] **P0-D3:** Add query test coverage | ||
| - [x] **P0-D3:** Add query test coverage |
Comment on lines
+26
to
+33
| function concatVectors(vectors: Float32Array[]): Float32Array { | ||
| const dim = vectors[0].length; | ||
| const out = new Float32Array(vectors.length * dim); | ||
| for (let i = 0; i < vectors.length; i++) { | ||
| out.set(vectors[i], i * dim); | ||
| } | ||
| return out; | ||
| } |
Owner
Author
|
@copilot open a new pull request to apply changes based on the comments in this thread |
Contributor
…ting, add missing tests Co-authored-by: devlux76 <86517969+devlux76@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Apply review feedback to minimal Cortex query (P0-D)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.