Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

fix(search): preserve verified chat index across clean restarts

BlueSkyID6660ef021005461
diff --git a/src/main/features/search/indexer.ts b/src/main/features/search/indexer.ts
index a101893f..575e2aae 100644
--- a/src/main/features/search/indexer.ts
+++ b/src/main/features/search/indexer.ts
@@ -280,6 +280,7 @@ export async function flushAll(): Promise<void> {
for (const uid of _chatRepairTasks.keys()) cancelChatIndexRepair(uid);
await Promise.allSettled([..._chatReconcileRuns.values()]);
await drainDeferredChatWrites();
+ const trustedStamps = await _trustedChatSourceStamps();
await Promise.all(Array.from(_cache.keys()).map((p) => flushOne(p).catch((err) => {
log.warn('flushAll entry failed', { error: logErrorSummary(err) });
})));
@@ -288,10 +289,46 @@ export async function flushAll(): Promise<void> {
// per-test workspace teardown and an in-place repair.
await Promise.allSettled([..._chatWorkers.values()].map(worker => worker.close()));
_chatWorkers.clear();
+ _persistTrustedChatSourceStamps(trustedStamps);
chatStore.closeAllChatStores();
_closing = false;
}
+/** The stored source stamp is otherwise written only by a completed full
+ * reconcile, while ordinary app writes move the conversation catalog. Every
+ * restart after normal use would then distrust a current index and wait for
+ * an idle repair. A clean quit records the catalog the process still trusts;
+ * a crash, a later write that moves the catalog, or any invalidation leaves
+ * the next start on the existing repair path. */
+function _canPersistChatTrust(uid: string): boolean {
+ return _currentChatIndexes.has(uid) && !_invalidatedChats.has(uid)
+ && !_chatReconcileRuns.has(uid) && !_migratingChats.has(uid)
+ && !(_pendingChatWrites.get(uid) || 0) && !_dirtyChats.get(uid)?.size;
+}
+
+async function _trustedChatSourceStamps(): Promise<Array<{ uid: string; revision: number; stamp: string }>> {
+ const out: Array<{ uid: string; revision: number; stamp: string }> = [];
+ for (const uid of [..._currentChatIndexes]) {
+ if (!_canPersistChatTrust(uid)) continue;
+ const revision = _chatRevisions.get(uid) || 0;
+ try { out.push({ uid, revision, stamp: await _chatSourceStamp(uid) }); }
+ catch (err) { log.warn('chat trust stamp read failed', { error: logErrorSummary(err) }); }
+ }
+ return out;
+}
+
+function _persistTrustedChatSourceStamps(
+ stamps: Array<{ uid: string; revision: number; stamp: string }>,
+): void {
+ for (const { uid, revision, stamp } of stamps) {
+ // Recheck synchronously: a write admitted while workers were closing
+ // either still counts as pending or already invalidated trust.
+ if (!_canPersistChatTrust(uid) || revision !== (_chatRevisions.get(uid) || 0)) continue;
+ try { chatStore.writeSourceStamp(uid, stamp); }
+ catch (err) { log.warn('chat trust stamp write failed', { error: logErrorSummary(err) }); }
+ }
+}
+
/** Chain live writes per account so receipts reach the writer in admission
* order and a reader can await the work queued before its query. Gaps leave
* the file unverified until source reconciliation fills the missing history. */
diff --git a/test/main/features/search/indexer.test.ts b/test/main/features/search/indexer.test.ts
index 0f63dbfc..fccb7807 100644
--- a/test/main/features/search/indexer.test.ts
+++ b/test/main/features/search/indexer.test.ts
@@ -598,3 +598,92 @@ describe('partial chat index deletion', () => {
expect(Object.values(idx.docs).some(doc => doc.fileKey === 'removed')).toBe(false);
});
});
+
+// Scenario: the user searches after restarting the app. Ordinary use moves the
+// conversation catalog (activity bumps rewrite _index.json) while the process
+// keeps a verified index current through incremental writes. A clean quit must
+// carry that trust into the next start instead of showing "still preparing"
+// until an idle full repair runs. Anything the quitting process could not
+// vouch for must still send the next start down the repair path.
+describe('search/indexer › chat index trust across a clean restart', () => {
+ const catalog = () => path.join(tmpDir, 'u1', 'cloud', 'chats', '_index.json');
+ const moveCatalog = (title: string) => {
+ fs.writeFileSync(catalog(), JSON.stringify([{ conversation_id: 'c1', title }]));
+ const later = new Date(Date.now() + 5_000);
+ fs.utimesSync(catalog(), later, later);
+ };
+ async function restart() {
+ await (await loadIndexer()).flushAll();
+ vi.resetModules();
+ return loadIndexer();
+ }
+
+ it('trusts the index on the next start after normal writes and a clean quit', async () => {
+ writeChat('u1', 'c1', [{ from: 'user', ts: 't1', text: 'first message' }]);
+ let ix = await loadIndexer();
+ await ix.reconcileChatsIndex('u1');
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(true);
+
+ const reply = { from: 'commander', ts: 't2', text: 'wombat migration plan' };
+ appendChat('u1', 'c1', reply);
+ await ix.indexChatMessage('u1', 'c1', 1, reply);
+ moveCatalog('activity bumped');
+ expect(ix.isChatsIndexTrusted('u1')).toBe(true);
+
+ ix = await restart();
+ expect(ix.isChatsIndexTrusted('u1')).toBe(false);
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(true);
+ const search = await import('../../../../src/main/features/search/index');
+ const page = await search.searchChatsWithStatus('u1', 'wombat');
+ expect(page.indexComplete).toBe(true);
+ expect(page.results.map((hit) => hit.msg_index)).toEqual([1]);
+ });
+
+ it('keeps an index the quitting process no longer trusted on the repair path', async () => {
+ writeChat('u1', 'c1', [{ from: 'user', ts: 't1', text: 'first message' }]);
+ let ix = await loadIndexer();
+ await ix.reconcileChatsIndex('u1');
+ // Message 1 never reached the index, so message 2 cannot certify the file.
+ appendChat('u1', 'c1', { from: 'commander', ts: 't2', text: 'lost porcupine message' });
+ appendChat('u1', 'c1', { from: 'user', ts: 't3', text: 'later message' });
+ await ix.indexChatMessage('u1', 'c1', 2, { from: 'user', ts: 't3', text: 'later message' });
+ moveCatalog('activity bumped');
+ expect(ix.isChatsIndexTrusted('u1')).toBe(false);
+
+ ix = await restart();
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(false);
+ await expect(ix.reconcileChatsIndex('u1')).resolves.toMatchObject({ complete: true, updated: 1 });
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(true);
+ });
+
+ it('distrusts the index when the catalog moved after the clean quit', async () => {
+ writeChat('u1', 'c1', [{ from: 'user', ts: 't1', text: 'first message' }]);
+ let ix = await loadIndexer();
+ await ix.reconcileChatsIndex('u1');
+ moveCatalog('before quit');
+ ix = await restart();
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(true);
+
+ // A sync pull or another writer changed history while the app was closed.
+ await ix.flushAll();
+ vi.resetModules();
+ const later = new Date(Date.now() + 10_000);
+ fs.writeFileSync(catalog(), JSON.stringify([{ conversation_id: 'c1', title: 'synced while closed' }]));
+ fs.utimesSync(catalog(), later, later);
+ ix = await loadIndexer();
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(false);
+ });
+
+ it('does not carry trust for an account whose index was never verified this run', async () => {
+ writeChat('u1', 'c1', [{ from: 'user', ts: 't1', text: 'first message' }]);
+ let ix = await loadIndexer();
+ await ix.reconcileChatsIndex('u1');
+ moveCatalog('activity before restart');
+ ix = await restart();
+ // This run never checked the index; quitting must not certify the moved catalog.
+ expect(ix.isChatsIndexTrusted('u1')).toBe(false);
+ moveCatalog('activity in unverified run');
+ ix = await restart();
+ expect(await ix.isChatsIndexCurrent('u1')).toBe(false);
+ });
+});