Repository navigation
feat(health): surface search-index freshness #1177
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
|
|
||
| vi.mock("../src/logger.js", () => ({ | ||
| logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| import { evaluateHealth } from "../src/health/thresholds.js"; | ||
| import type { HealthSnapshot } from "../src/types.js"; | ||
|
|
||
| function snapshot( | ||
| indexPersistence?: HealthSnapshot["indexPersistence"], | ||
| ): HealthSnapshot { | ||
| return { | ||
| connectionState: "connected", | ||
| workers: [], | ||
| memory: { heapUsed: 10, heapTotal: 100, rss: 10 * 1024 * 1024, external: 0 }, | ||
| cpu: { userMicros: 0, systemMicros: 0, percent: 1 }, | ||
| eventLoopLagMs: 1, | ||
| uptimeSeconds: 100, | ||
| kvConnectivity: { status: "ok", latencyMs: 1 }, | ||
| indexPersistence, | ||
| status: "healthy", | ||
| alerts: [], | ||
| }; | ||
| } | ||
|
|
||
| describe("search-index freshness in health", () => { | ||
| it("reports docs, vectors and save age as a note when the index is being written", () => { | ||
| const res = evaluateHealth( | ||
| snapshot({ | ||
| bm25Docs: 12400, | ||
| vectors: 12400, | ||
| lastSavedAt: new Date(Date.now() - 5 * 60_000).toISOString(), | ||
| lastError: null, | ||
| dirtySince: null, | ||
| stale: false, | ||
| }), | ||
| ); | ||
| expect(res.status).toBe("healthy"); | ||
| expect(res.notes.join(" ")).toContain("index_docs_12400_vec_12400_saved_5m_ago"); | ||
| }); | ||
|
|
||
| it("degrades when the index has been dirty past the stale window", () => { | ||
| const res = evaluateHealth( | ||
| snapshot({ | ||
| bm25Docs: 12400, | ||
| vectors: 12400, | ||
| lastSavedAt: new Date(Date.now() - 20 * 24 * 3600_000).toISOString(), | ||
| lastError: null, | ||
| dirtySince: Date.now() - 3 * 3600_000, | ||
| stale: true, | ||
| }), | ||
| ); | ||
| expect(res.status).toBe("degraded"); | ||
| expect(res.alerts.some((a) => a.startsWith("index_persist_stale_"))).toBe(true); | ||
| }); | ||
|
|
||
| it("degrades when the last save failed", () => { | ||
| const res = evaluateHealth( | ||
| snapshot({ | ||
| bm25Docs: 100, | ||
| vectors: 0, | ||
| lastSavedAt: null, | ||
| lastError: "TIMEOUT: invocation timed out after 180000ms", | ||
| dirtySince: Date.now(), | ||
| stale: false, | ||
| }), | ||
| ); | ||
| expect(res.status).toBe("degraded"); | ||
| expect(res.alerts).toContain("index_persist_error"); | ||
| }); | ||
|
|
||
| it("says nothing when the snapshot carries no index information", () => { | ||
| const res = evaluateHealth(snapshot(undefined)); | ||
| expect(res.status).toBe("healthy"); | ||
| expect(res.notes.join(" ")).not.toContain("index_"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("index persistence status tracking", () => { | ||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| it("starts clean, marks dirty on scheduleSave and reports stale only past the window", async () => { | ||
| const { getIndexPersistenceStatus, resetIndexPersistenceStatus, IndexPersistence } = | ||
| await import("../src/state/index-persistence.js"); | ||
| const { SearchIndex } = await import("../src/state/search-index.js"); | ||
| resetIndexPersistenceStatus(); | ||
|
|
||
| expect(getIndexPersistenceStatus().dirtySince).toBeNull(); | ||
| expect(getIndexPersistenceStatus().stale).toBe(false); | ||
|
|
||
| const kv = { | ||
| get: async () => null, | ||
| set: async () => {}, | ||
| delete: async () => {}, | ||
| list: async () => [], | ||
| }; | ||
| const persistence = new IndexPersistence(kv as never, new SearchIndex(), null); | ||
| persistence.scheduleSave(); | ||
|
|
||
| const dirty = getIndexPersistenceStatus(); | ||
| expect(dirty.dirtySince).not.toBeNull(); | ||
| expect(dirty.stale).toBe(false); // just went dirty, well inside the window | ||
|
|
||
| persistence.stop(); | ||
| }); | ||
| }); |
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Keep dirty state for updates scheduled during an active save.
If
scheduleSave()runs while an earliersave()awaits KV writes, the earlier save clearsdirtySinceat Line 150. The newer update can still be unsaved. Health then reports a clean index until the newer save completes.Track a revision for each scheduled update. Clear
dirtySinceonly if no later revision was scheduled.Proposed fix
let status: IndexPersistenceStatus = { // ... }; +let dirtyRevision = 0; export function resetIndexPersistenceStatus(): void { status = { // ... }; + dirtyRevision = 0; } scheduleSave(): void { if (status.dirtySince === null) status.dirtySince = Date.now(); + dirtyRevision += 1; // ... } async save(): Promise<void> { + const saveRevision = dirtyRevision; // ... try { // ... status.lastSavedAt = new Date().toISOString(); status.lastError = null; - status.dirtySince = null; + if (dirtyRevision === saveRevision) status.dirtySince = null;Also applies to: 146-150
🤖 Prompt for AI Agents