Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(switchdash): drop file results from search and remove the file indexer (CHOO-2009) (#153)
Christian McDermott17483b3c14f1
diff --git a/dash/apps/switchdash-desktop/src/main/core/fs/controller.ts b/dash/apps/switchdash-desktop/src/main/core/fs/controller.tsindex a0e0fd24d..0d4a9d305 100644--- a/dash/apps/switchdash-desktop/src/main/core/fs/controller.ts+++ b/dash/apps/switchdash-desktop/src/main/core/fs/controller.ts@@ -1,5 +1,4 @@import { err, ok } from '@switchdash/shared';-import { fsEvents } from '@main/core/fs/fs-events';import { resolveLocationRuntime } from '@main/core/locations/utils';import { events } from '@main/lib/events';import { fsWatchEventChannel } from '@shared/core/fs/fsEvents';@@ -35,9 +34,7 @@ export const filesController = createRPCController({existing.update(union);} else {const watcher = env.fs.watch((evts) => {- const event = { locationId, events: evts };- events.emit(fsWatchEventChannel, event);- fsEvents.emitWatchEvent(event);+ events.emit(fsWatchEventChannel, { locationId, events: evts });});watcher.update(union);watcherRegistry.set(locationId, watcher);diff --git a/dash/apps/switchdash-desktop/src/main/core/fs/fs-events.ts b/dash/apps/switchdash-desktop/src/main/core/fs/fs-events.tsdeleted file mode 100644index 33768010d..000000000--- a/dash/apps/switchdash-desktop/src/main/core/fs/fs-events.ts+++ /dev/null@@ -1,23 +0,0 @@-import { HookCore, type Hookable } from '@main/lib/hookable';-import { log } from '@main/lib/logger';-import type { FileWatchEvent } from '@shared/core/fs/fs';--export type FsHooks = {- 'watch:event': (event: { locationId: string; events: FileWatchEvent[] }) => void | Promise<void>;-};--class FsEvents implements Hookable<FsHooks> {- private readonly _core = new HookCore<FsHooks>((name, e) =>- log.error(`FsEvents: ${String(name)} hook error`, e)- );-- on<K extends keyof FsHooks>(name: K, handler: FsHooks[K]) {- return this._core.on(name, handler);- }-- emitWatchEvent(event: { locationId: string; events: FileWatchEvent[] }): void {- this._core.callHookBackground('watch:event', event);- }-}--export const fsEvents = new FsEvents();diff --git a/dash/apps/switchdash-desktop/src/main/core/locations/location-runtime-factory.ts b/dash/apps/switchdash-desktop/src/main/core/locations/location-runtime-factory.tsindex a54bfcf90..175c05410 100644--- a/dash/apps/switchdash-desktop/src/main/core/locations/location-runtime-factory.ts+++ b/dash/apps/switchdash-desktop/src/main/core/locations/location-runtime-factory.ts@@ -10,7 +10,6 @@ import type { FileSystemProvider } from '@main/core/fs/types';import { LifecycleScriptService } from '@main/core/locations/lifecycle-service';import type { LocationRuntime } from '@main/core/locations/location-runtime';import { type LocationRuntimeFactoryResult } from '@main/core/locations/location-runtime-registry';-import { locationFileIndexService } from '@main/core/search/location-file-index-service';import { preflightRemoteSession } from '@main/core/sessions/remote-session-preflight';import { appSettingsService } from '@main/core/settings/settings-service';import { ensureSshConnected } from '@main/core/ssh/connect/connect-agent-ssh';@@ -127,7 +126,6 @@ export function createLocationRuntimeFactory(runtime,onCreateSideEffect: (rt) => {- void locationFileIndexService.onRuntimeCreated(locationId, rt);void (async () => {if (scripts?.setup && (locationSettings.autoRunSetupScriptOnSessionCreation ?? true)) {const setupResult = await runLifecycleScriptWithPolicy({@@ -173,7 +171,6 @@ export function createLocationRuntimeFactory(onCreate: context.extraHooks?.onCreate,onDestroy: async (rt) => {- locationFileIndexService.onRuntimeDestroyed(locationId);const latestSessionSettings = await getEffectiveSessionSettings({locationSettings: context.settings,sessionFs: rt.fs,diff --git a/dash/apps/switchdash-desktop/src/main/core/search/location-file-index-service.ts b/dash/apps/switchdash-desktop/src/main/core/search/location-file-index-service.tsdeleted file mode 100644index ddcf741d7..000000000--- a/dash/apps/switchdash-desktop/src/main/core/search/location-file-index-service.ts+++ /dev/null@@ -1,209 +0,0 @@-import { basename } from 'node:path';-import { fsEvents } from '@main/core/fs/fs-events';-import type { LocationRuntime } from '@main/core/locations/location-runtime';-import { locationRuntimeRegistry } from '@main/core/locations/location-runtime-registry';-import { sqlite } from '@main/db/client';-import { log } from '@main/lib/logger';--const STALE_DAYS = 14;-const MAX_FILES = 50_000;-const CRAWL_TIMEOUT_MS = 30_000;-const REINDEX_DEBOUNCE_MS = 3_000;--const CRAWL_IGNORED_DIRS = new Set([- 'node_modules',- '.git',- '.svn',- '.hg',- 'dist',- 'build',- '.next',- '.nuxt',- 'coverage',- '.cache',- '.parcel-cache',- '__pycache__',- '.pytest_cache',- 'venv',- '.venv',- 'target',- '.terraform',- '.serverless',- 'worktrees',- '.switchdash',- '.conductor',- '.cursor',- '.claude',- '.amp',- '.codex',- '.aider',- '.continue',- '.cody',- '.windsurf',-]);--type FileHit = { path: string; filename: string };--class LocationFileIndexService {- private crawling = new Set<string>();- private debounceTimers = new Map<string, ReturnType<typeof setTimeout>>();-- initialize(): void {- this.evictStale();-- fsEvents.on('watch:event', ({ locationId }) => {- this.scheduleReindex(locationId);- });- }-- async onRuntimeCreated(locationId: string, runtime: LocationRuntime): Promise<void> {- const alreadyIndexed = sqlite- .prepare(`SELECT 1 FROM location_file_index_meta WHERE location_id = ?`)- .get(locationId);-- if (alreadyIndexed) {- this.touchMeta(locationId);- return;- }-- await this.crawl(locationId, runtime);- }-- onRuntimeDestroyed(_locationId: string): void {- // Intentionally a no-op: the index ages out 14 days after the last provision.- // Calling touchMeta here would reset the staleness clock on every destroy,- // preventing eviction of stale entries for frequently-cycled locations.- }-- deleteIndex(locationId: string): void {- try {- sqlite.transaction(() => {- sqlite.prepare(`DELETE FROM location_file_index WHERE location_id = ?`).run(locationId);- sqlite- .prepare(`DELETE FROM location_file_index_meta WHERE location_id = ?`)- .run(locationId);- })();- log.info('LocationFileIndexService: deleted index', { locationId });- } catch (e) {- log.warn('LocationFileIndexService: deleteIndex failed', { locationId, error: String(e) });- }- }-- search(locationId: string, query: string): FileHit[] {- const terms = query- .trim()- .split(/[\s\-_/]+/)- .filter((t) => t.length >= 3);-- if (terms.length === 0) return [];-- const ftsQuery = terms.map((t) => `"${t}"`).join(' AND ');- try {- return sqlite- .prepare(- `SELECT path, filename- FROM location_file_index- WHERE location_file_index MATCH ?- AND location_id = ?- ORDER BY bm25(location_file_index, 1.0, 2.0)- LIMIT 20`- )- .all(ftsQuery, locationId) as FileHit[];- } catch (e) {- log.warn('LocationFileIndexService: search failed', { locationId, error: String(e) });- return [];- }- }-- private async crawl(locationId: string, runtime: LocationRuntime): Promise<void> {- if (this.crawling.has(locationId)) return;- this.crawling.add(locationId);-- try {- const result = await runtime.fs.list('', {- recursive: true,- maxEntries: MAX_FILES,- timeBudgetMs: CRAWL_TIMEOUT_MS,- });-- const files = result.entries.filter(- (e) => e.type === 'file' && !e.path.split('/').some((seg) => CRAWL_IGNORED_DIRS.has(seg))- );-- sqlite.transaction(() => {- sqlite.prepare(`DELETE FROM location_file_index WHERE location_id = ?`).run(locationId);- const stmt = sqlite.prepare(- `INSERT INTO location_file_index(location_id, path, filename) VALUES (?, ?, ?)`- );- for (const f of files) {- stmt.run(locationId, f.path, basename(f.path));- }- })();-- this.touchMeta(locationId);- log.info('LocationFileIndexService: indexed runtime', {- locationId,- count: files.length,- truncated: result.truncated ?? false,- });- } catch (e) {- log.warn('LocationFileIndexService: crawl failed', { locationId, error: String(e) });- } finally {- this.crawling.delete(locationId);- }- }-- private scheduleReindex(locationId: string): void {- const existing = this.debounceTimers.get(locationId);- if (existing) clearTimeout(existing);-- this.debounceTimers.set(- locationId,- setTimeout(() => {- this.debounceTimers.delete(locationId);- const ws = locationRuntimeRegistry.get(locationId);- if (ws) void this.crawl(locationId, ws);- }, REINDEX_DEBOUNCE_MS)- );- }-- private touchMeta(locationId: string): void {- try {- sqlite- .prepare(- `INSERT OR REPLACE INTO location_file_index_meta (location_id, indexed_at)- VALUES (?, unixepoch())`- )- .run(locationId);- } catch (e) {- log.warn('LocationFileIndexService: touchMeta failed', { locationId, error: String(e) });- }- }-- private evictStale(): void {- try {- const cutoff = Math.floor(Date.now() / 1000) - STALE_DAYS * 86400;- const stale = sqlite- .prepare(`SELECT location_id FROM location_file_index_meta WHERE indexed_at < ?`)- .all(cutoff) as Array<{ location_id: string }>;-- if (stale.length > 0) {- sqlite.transaction(() => {- const delIndex = sqlite.prepare(`DELETE FROM location_file_index WHERE location_id = ?`);- const delMeta = sqlite.prepare(- `DELETE FROM location_file_index_meta WHERE location_id = ?`- );- for (const row of stale) {- delIndex.run(row.location_id);- delMeta.run(row.location_id);- }- })();- log.info('LocationFileIndexService: evicted stale indexes', { count: stale.length });- }- } catch (e) {- log.warn('LocationFileIndexService: evictStale failed', { error: String(e) });- }- }-}--export const locationFileIndexService = new LocationFileIndexService();diff --git a/dash/apps/switchdash-desktop/src/main/core/search/search-service.db.test.ts b/dash/apps/switchdash-desktop/src/main/core/search/search-service.db.test.tsindex 4dbdfc57a..bffdb712d 100644--- a/dash/apps/switchdash-desktop/src/main/core/search/search-service.db.test.ts+++ b/dash/apps/switchdash-desktop/src/main/core/search/search-service.db.test.ts@@ -20,12 +20,6 @@ vi.mock('@main/db/client', () => ({},}));-// The file index is a separate FTS table with its own indexing lifecycle; these-// tests are about the item index, so it contributes nothing.-vi.mock('./location-file-index-service', () => ({- locationFileIndexService: { search: () => [] },-}));-const { searchService } = await import('./search-service');const { agentEvents } = await import('../agents/agent-events');@@ -81,6 +75,38 @@ describe('searchService', () => {mocks.sqlite = undefined;});+ // Search covers sessions, agents and commands. Filesystem hits were appended+ // to any search carrying a full session context — after the ranking cut, so+ // they bypassed both the tiers and the result cap.+ describe('result kinds', () => {+ it('returns no filesystem results when a full session context is supplied', () => {+ searchService.initialize();++ const result = searchService.search({+ query: 'reviewer',+ context: { locationId: 'loc-1', sessionId: 'session-1' },+ });++ expect(result.status).toBe('ok');+ expect(result.items.length).toBeGreaterThan(0);+ for (const item of result.items) {+ expect(['session', 'agent', 'command']).toContain(item.kind);+ }+ });++ it('returns the same results with a session context as without one', () => {+ searchService.initialize();++ const withoutContext = searchService.search({ query: 'reviewer' });+ const withContext = searchService.search({+ query: 'reviewer',+ context: { locationId: 'loc-1', sessionId: 'session-1' },+ });++ expect(titles(withContext.items)).toEqual(titles(withoutContext.items));+ });+ });+describe('indexing agents', () => {it('backfills existing agents so they are findable by name', () => {searchService.initialize();diff --git a/dash/apps/switchdash-desktop/src/main/core/search/search-service.ts b/dash/apps/switchdash-desktop/src/main/core/search/search-service.tsindex a8f8f7aad..671a87f3f 100644--- a/dash/apps/switchdash-desktop/src/main/core/search/search-service.ts+++ b/dash/apps/switchdash-desktop/src/main/core/search/search-service.ts@@ -15,7 +15,6 @@ import type { Session } from '@shared/core/sessions/sessions';import { agentEvents } from '../agents/agent-events';import { sessionHooks } from '../sessions/session-hooks';import { sessionService } from '../sessions/session-service';-import { locationFileIndexService } from './location-file-index-service';type FtsRow = {item_type: string;@@ -178,24 +177,6 @@ class SearchService {.sort((a, b) => a.score - b.score).slice(0, RESULT_LIMIT);- // Only offered when a session is in context: opening a file navigates to a- // session, so without one there is nothing to open and the hit would render- // as a row that silently does nothing when clicked.- if (context?.locationId && context.sessionId) {- const fileHits = locationFileIndexService.search(context.locationId, query);- for (const h of fileHits) {- results.push({- kind: 'file',- id: h.path,- locationId: context.locationId,- sessionId: context.sessionId,- title: h.filename,- subtitle: h.path,- score: 0,- });- }- }-return { items: results, status: 'ok' };}diff --git a/dash/apps/switchdash-desktop/src/main/db/initialize.ts b/dash/apps/switchdash-desktop/src/main/db/initialize.tsindex 9db94b50b..74a8d421e 100644--- a/dash/apps/switchdash-desktop/src/main/db/initialize.ts+++ b/dash/apps/switchdash-desktop/src/main/db/initialize.ts@@ -171,13 +171,14 @@ function ensureSearchIndex(connection: BetterSqlite3.Database): void {}/**- * Creates the FTS5 virtual table and companion meta table used for location- * file indexing. Managed outside Drizzle (same reason as ensureSearchIndex).- * Version-gated via `kv` so the tables can be dropped and recreated on schema- * changes without a full Drizzle migration.+ * Drops the location file index. It existed to back file results in the+ * command palette; those are gone, and nothing else ever read it. An existing+ * database still carries the tables and their rows, so they are removed here+ * rather than left behind as a write-only index. Version-gated via `kv` like+ * the tables were, so this runs once per database.*/-function ensureFileIndex(connection: BetterSqlite3.Database): void {- const FILE_INDEX_VERSION = '2';+function dropFileIndex(connection: BetterSqlite3.Database): void {+ const FILE_INDEX_VERSION = 'dropped';const row = connection.prepare(`SELECT value FROM kv WHERE key = 'file_index_version'`).get() as| { value: string }@@ -188,20 +189,6 @@ function ensureFileIndex(connection: BetterSqlite3.Database): void {connection.exec(`DROP TABLE IF EXISTS location_file_index`);connection.exec(`DROP TABLE IF EXISTS workspace_file_index_meta`);connection.exec(`DROP TABLE IF EXISTS location_file_index_meta`);- connection.exec(`- CREATE VIRTUAL TABLE location_file_index USING fts5(- location_id UNINDEXED,- path,- filename,- tokenize = 'trigram case_sensitive 0'- )- `);- connection.exec(`- CREATE TABLE location_file_index_meta (- location_id TEXT PRIMARY KEY,- indexed_at INTEGER NOT NULL- )- `);connection.prepare(`INSERT OR REPLACE INTO kv (key, value, updated_at) VALUES ('file_index_version', ?, unixepoch())`@@ -228,6 +215,6 @@ export async function initializeDatabase(const conn = connection ?? (await import('./client')).sqlite;runBundledMigrations(conn);ensureSearchIndex(conn);- ensureFileIndex(conn);+ dropFileIndex(conn);return conn;}diff --git a/dash/apps/switchdash-desktop/src/main/index.ts b/dash/apps/switchdash-desktop/src/main/index.tsindex 9f42d4b97..95d081939 100644--- a/dash/apps/switchdash-desktop/src/main/index.ts+++ b/dash/apps/switchdash-desktop/src/main/index.ts@@ -26,7 +26,6 @@ import {reconcileResourceSampler,stopResourceSampler,} from './core/resource-monitor/resource-sampler';-import { locationFileIndexService } from './core/search/location-file-index-service';import { searchService } from './core/search/search-service';import { appSettingsService } from './core/settings/settings-service';import { registerSidecarDiagnostics } from './core/sidecar/sidecar-diagnostics';@@ -104,7 +103,6 @@ void app.whenReady().then(async () => {try {await initializeDatabase();searchService.initialize();- locationFileIndexService.initialize();try {viewStateService.pruneOrphans();} catch (e: unknown) {diff --git a/dash/apps/switchdash-desktop/src/renderer/features/command-palette/command-palette-modal.tsx b/dash/apps/switchdash-desktop/src/renderer/features/command-palette/command-palette-modal.tsxindex 456878d96..b657fe3f7 100644--- a/dash/apps/switchdash-desktop/src/renderer/features/command-palette/command-palette-modal.tsx+++ b/dash/apps/switchdash-desktop/src/renderer/features/command-palette/command-palette-modal.tsx@@ -4,7 +4,6 @@ import {Activity,Bot,DoorOpen,- File,Globe,HardDrive,type LucideIcon,@@ -164,26 +163,6 @@ function PaletteItem({);}-function PaletteFileItem({- value,- item,- onSelect,-}: {- value: string;- item: SearchItem;- onSelect: () => void;-}) {- return (- <Command.Item value={value} onSelect={onSelect} className={PALETTE_ITEM_CLASS}>- <File className="size-3.5 shrink-0 text-foreground/60" aria-hidden="true" />- <span className="flex min-w-0 flex-1 items-baseline gap-2 overflow-hidden">- <span className="shrink-0">{item.title}</span>- <span className="truncate text-xs text-foreground/40">{item.subtitle}</span>- </span>- </Command.Item>- );-}-/** One group of renderer-matched results, or nothing when the kind had no hits. */function PaletteResultGroup({heading,@@ -351,12 +330,6 @@ export function CommandPaletteModal({);};- const handleOpenFile = (item: SearchItem) => {- if (!item.locationId || !item.sessionId) return;- handleClose();- navigate('session', { locationId: item.locationId, sessionId: item.sessionId });- };-/*** Opens the agent, matching `sidebar/agent-item.tsx` exactly.*@@ -394,7 +367,6 @@ export function CommandPaletteModal({const handleSelect = (item: SearchItem) => {if (item.kind === 'session') return handleNavigateToSession(item);- if (item.kind === 'file') return handleOpenFile(item);if (item.kind === 'agent') return handleNavigateToAgent(item);if (item.kind === 'room') return handleNavigateToRoom(item);if (item.kind === 'server') return handleNavigateToServer(item);@@ -460,7 +432,7 @@ export function CommandPaletteModal({)}{searchResult.status === 'query-too-short' && (<div className="px-3 py-6 text-center text-sm text-foreground/40">- Type at least 3 characters to search agents, sessions, files and commands.+ Type at least 3 characters to search agents, sessions and commands.{rendererMatchCount > 0 &&' Rooms, servers and hosts are matched from the first character.'}</div>@@ -538,16 +510,6 @@ export function CommandPaletteModal({);}}- if (item.kind === 'file') {- return (- <PaletteFileItem- key={`file:${item.id}`}- value={`file:${item.id}`}- item={item}- onSelect={() => handleOpenFile(item)}- />- );- }return (<PaletteItemkey={`${item.kind}:${item.id}`}diff --git a/dash/apps/switchdash-desktop/src/shared/core/search.ts b/dash/apps/switchdash-desktop/src/shared/core/search.tsindex ed5d5d200..18ca11c71 100644--- a/dash/apps/switchdash-desktop/src/shared/core/search.ts+++ b/dash/apps/switchdash-desktop/src/shared/core/search.ts@@ -1,7 +1,7 @@/*** Two populations, one item shape.*- * `'session' | 'command' | 'file' | 'agent'` come from the SQLite FTS index —+ * `'session' | 'command' | 'agent'` come from the SQLite FTS index —* high-cardinality local content worth indexing.** `'room' | 'server' | 'host'` are matched in the renderer against sets it has@@ -11,7 +11,7 @@* whose only job is to stop the index going stale. Matching a handful of rows* in the renderer is always fresh and has nothing to invalidate.*/-export type SearchItemKind = 'session' | 'command' | 'file' | 'agent' | 'room' | 'server' | 'host';+export type SearchItemKind = 'session' | 'command' | 'agent' | 'room' | 'server' | 'host';export interface SearchItem {kind: SearchItemKind;