Skip to content

P0-D: Implement minimal Cortex query (issue #21)#49

Merged
devlux76 merged 11 commits intomainfrom
p0d/cortex-query-minimal
Mar 13, 2026
Merged

P0-D: Implement minimal Cortex query (issue #21)#49
devlux76 merged 11 commits intomainfrom
p0d/cortex-query-minimal

Conversation

@devlux76
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings March 13, 2026 08:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ts with query() entry point and cortex/QueryResult.ts DTO
  • New getAllPages() method on the MetadataStore interface and its IndexedDB implementation for warm/cold fallback scans
  • Test coverage in tests/cortex/Query.test.ts and updated mock in tests/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 thread cortex/Query.ts
Comment thread storage/IndexedDbMetadataStore.ts Outdated
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);
});
}
Comment thread tests/SalienceEngine.test.ts Outdated
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();
});
});
Comment thread TODO.md
- DTO with `pages: Page[]`, `scores: number[]`, `metadata: object`

- [ ] **P0-D3:** Add query test coverage
- [x] **P0-D3:** Add query test coverage
Comment thread cortex/Query.ts
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;
}
@devlux76
Copy link
Copy Markdown
Owner Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

@devlux76 I've opened a new pull request, #50, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 7 commits March 13, 2026 08:41
…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>
@devlux76 devlux76 merged commit d516404 into main Mar 13, 2026
2 checks passed
@devlux76 devlux76 deleted the p0d/cortex-query-minimal branch March 13, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants