@liendev/lien 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3708,8 +3708,7 @@ import {
3708
3708
  DEFAULT_EMBEDDING_BATCH_SIZE,
3709
3709
  DEFAULT_CHUNK_SIZE,
3710
3710
  DEFAULT_CHUNK_OVERLAP,
3711
- DEFAULT_GIT_POLL_INTERVAL_MS,
3712
- DEFAULT_DEBOUNCE_MS
3711
+ DEFAULT_GIT_POLL_INTERVAL_MS
3713
3712
  } from "@liendev/core";
3714
3713
  async function statusCommand() {
3715
3714
  const rootDir = process.cwd();
@@ -3766,7 +3765,7 @@ async function statusCommand() {
3766
3765
  console.log(chalk3.dim("Git detection:"), chalk3.yellow("Not a git repo"));
3767
3766
  }
3768
3767
  console.log(chalk3.dim("File watching:"), chalk3.green("\u2713 Enabled (default)"));
3769
- console.log(chalk3.dim(" Debounce:"), `${DEFAULT_DEBOUNCE_MS}ms`);
3768
+ console.log(chalk3.dim(" Batch window:"), "500ms (collects rapid changes, force-flush after 5s)");
3770
3769
  console.log(chalk3.dim(" Disable with:"), chalk3.bold("lien serve --no-watch"));
3771
3770
  console.log(chalk3.bold("\nIndexing Settings (defaults):"));
3772
3771
  console.log(chalk3.dim("Concurrency:"), DEFAULT_CONCURRENCY);
@@ -3993,7 +3992,7 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3993
3992
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3994
3993
  import { createRequire as createRequire2 } from "module";
3995
3994
  import { fileURLToPath as fileURLToPath2 } from "url";
3996
- import { dirname as dirname2, join as join2 } from "path";
3995
+ import { dirname as dirname2, join as join2, resolve } from "path";
3997
3996
  import {
3998
3997
  LocalEmbeddings,
3999
3998
  GitStateTracker,
@@ -4004,18 +4003,35 @@ import {
4004
4003
  isGitRepo as isGitRepo2,
4005
4004
  VERSION_CHECK_INTERVAL_MS,
4006
4005
  DEFAULT_GIT_POLL_INTERVAL_MS as DEFAULT_GIT_POLL_INTERVAL_MS2,
4007
- createVectorDB
4006
+ createVectorDB,
4007
+ computeContentHash,
4008
+ normalizeToRelativePath
4008
4009
  } from "@liendev/core";
4009
4010
 
4010
4011
  // src/watcher/index.ts
4011
4012
  import chokidar from "chokidar";
4012
4013
  import path3 from "path";
4013
- import { detectAllFrameworks, getFrameworkDetector, DEFAULT_DEBOUNCE_MS as DEFAULT_DEBOUNCE_MS2 } from "@liendev/core";
4014
+ import { detectAllFrameworks, getFrameworkDetector } from "@liendev/core";
4014
4015
  var FileWatcher = class {
4015
4016
  watcher = null;
4016
- debounceTimers = /* @__PURE__ */ new Map();
4017
4017
  rootDir;
4018
4018
  onChangeHandler = null;
4019
+ // Batch state for aggregating rapid changes
4020
+ pendingChanges = /* @__PURE__ */ new Map();
4021
+ batchTimer = null;
4022
+ batchInProgress = false;
4023
+ // Track if handler is currently processing a batch
4024
+ BATCH_WINDOW_MS = 500;
4025
+ // Collect changes for 500ms before processing
4026
+ MAX_BATCH_WAIT_MS = 5e3;
4027
+ // Force flush after 5s even if changes keep coming
4028
+ firstChangeTimestamp = null;
4029
+ // Track when batch started
4030
+ // Git watching state
4031
+ gitChangeTimer = null;
4032
+ gitChangeHandler = null;
4033
+ GIT_DEBOUNCE_MS = 1e3;
4034
+ // Git operations touch multiple files
4019
4035
  constructor(rootDir) {
4020
4036
  this.rootDir = rootDir;
4021
4037
  }
@@ -4102,7 +4118,11 @@ var FileWatcher = class {
4102
4118
  return;
4103
4119
  }
4104
4120
  this.watcher.on("add", (filepath) => this.handleChange("add", filepath)).on("change", (filepath) => this.handleChange("change", filepath)).on("unlink", (filepath) => this.handleChange("unlink", filepath)).on("error", (error) => {
4105
- console.error(`[Lien] File watcher error: ${error}`);
4121
+ try {
4122
+ const message = "[FileWatcher] Error: " + (error instanceof Error ? error.stack || error.message : String(error)) + "\n";
4123
+ process.stderr.write(message);
4124
+ } catch {
4125
+ }
4106
4126
  });
4107
4127
  }
4108
4128
  /**
@@ -4114,17 +4134,17 @@ var FileWatcher = class {
4114
4134
  }
4115
4135
  let readyFired = false;
4116
4136
  await Promise.race([
4117
- new Promise((resolve) => {
4137
+ new Promise((resolve2) => {
4118
4138
  const readyHandler = () => {
4119
4139
  readyFired = true;
4120
- resolve();
4140
+ resolve2();
4121
4141
  };
4122
4142
  this.watcher.once("ready", readyHandler);
4123
4143
  }),
4124
- new Promise((resolve) => {
4144
+ new Promise((resolve2) => {
4125
4145
  setTimeout(() => {
4126
4146
  if (!readyFired) {
4127
- resolve();
4147
+ resolve2();
4128
4148
  }
4129
4149
  }, 1e3);
4130
4150
  })
@@ -4146,34 +4166,200 @@ var FileWatcher = class {
4146
4166
  await this.waitForReady();
4147
4167
  }
4148
4168
  /**
4149
- * Handles a file change event with debouncing.
4150
- * Debouncing prevents rapid reindexing when files are saved multiple times quickly.
4169
+ * Enable watching .git directory for git operations.
4170
+ * Call this after start() to enable event-driven git detection.
4171
+ *
4172
+ * @param onGitChange - Callback invoked when git operations detected
4173
+ */
4174
+ watchGit(onGitChange) {
4175
+ if (!this.watcher) {
4176
+ throw new Error("Cannot watch git - watcher not started");
4177
+ }
4178
+ this.gitChangeHandler = onGitChange;
4179
+ this.watcher.add([
4180
+ path3.join(this.rootDir, ".git/HEAD"),
4181
+ path3.join(this.rootDir, ".git/index"),
4182
+ path3.join(this.rootDir, ".git/refs/**"),
4183
+ path3.join(this.rootDir, ".git/MERGE_HEAD"),
4184
+ path3.join(this.rootDir, ".git/REBASE_HEAD"),
4185
+ path3.join(this.rootDir, ".git/CHERRY_PICK_HEAD"),
4186
+ path3.join(this.rootDir, ".git/logs/refs/stash")
4187
+ // git stash operations
4188
+ ]);
4189
+ }
4190
+ /**
4191
+ * Check if a filepath is a git-related change
4192
+ */
4193
+ isGitChange(filepath) {
4194
+ const normalized = filepath.replace(/\\/g, "/");
4195
+ return normalized.includes(".git/");
4196
+ }
4197
+ /**
4198
+ * Handle git-related file changes with debouncing
4199
+ */
4200
+ handleGitChange() {
4201
+ if (this.gitChangeTimer) {
4202
+ clearTimeout(this.gitChangeTimer);
4203
+ }
4204
+ this.gitChangeTimer = setTimeout(async () => {
4205
+ try {
4206
+ await this.gitChangeHandler?.();
4207
+ } catch (error) {
4208
+ }
4209
+ this.gitChangeTimer = null;
4210
+ }, this.GIT_DEBOUNCE_MS);
4211
+ }
4212
+ /**
4213
+ * Handles a file change event with smart batching.
4214
+ * Collects rapid changes across multiple files and processes them together.
4215
+ * Forces flush after MAX_BATCH_WAIT_MS even if changes keep arriving.
4216
+ *
4217
+ * If a batch is currently being processed by an async handler, waits for completion
4218
+ * before starting a new batch to prevent race conditions.
4151
4219
  */
4152
4220
  handleChange(type, filepath) {
4153
- const existingTimer = this.debounceTimers.get(filepath);
4154
- if (existingTimer) {
4155
- clearTimeout(existingTimer);
4156
- }
4157
- const timer = setTimeout(() => {
4158
- this.debounceTimers.delete(filepath);
4159
- if (this.onChangeHandler) {
4160
- const absolutePath = path3.isAbsolute(filepath) ? filepath : path3.join(this.rootDir, filepath);
4161
- try {
4162
- const result = this.onChangeHandler({
4163
- type,
4164
- filepath: absolutePath
4165
- });
4166
- if (result instanceof Promise) {
4167
- result.catch((error) => {
4168
- console.error(`[Lien] Error handling file change: ${error}`);
4169
- });
4170
- }
4171
- } catch (error) {
4172
- console.error(`[Lien] Error handling file change: ${error}`);
4173
- }
4221
+ if (this.gitChangeHandler && this.isGitChange(filepath)) {
4222
+ this.handleGitChange();
4223
+ return;
4224
+ }
4225
+ if (!this.onChangeHandler) {
4226
+ return;
4227
+ }
4228
+ if (this.pendingChanges.size === 0) {
4229
+ this.firstChangeTimestamp = Date.now();
4230
+ }
4231
+ this.pendingChanges.set(filepath, type);
4232
+ const now = Date.now();
4233
+ const elapsed = now - this.firstChangeTimestamp;
4234
+ if (elapsed >= this.MAX_BATCH_WAIT_MS) {
4235
+ if (this.batchTimer) {
4236
+ clearTimeout(this.batchTimer);
4237
+ this.batchTimer = null;
4238
+ }
4239
+ this.flushBatch();
4240
+ return;
4241
+ }
4242
+ if (this.batchTimer) {
4243
+ clearTimeout(this.batchTimer);
4244
+ }
4245
+ if (!this.batchInProgress) {
4246
+ this.batchTimer = setTimeout(() => {
4247
+ this.flushBatch();
4248
+ }, this.BATCH_WINDOW_MS);
4249
+ }
4250
+ }
4251
+ /**
4252
+ * Group pending changes by type and convert to absolute paths.
4253
+ * Returns arrays of added, modified, and deleted files.
4254
+ */
4255
+ groupPendingChanges(changes) {
4256
+ const added = [];
4257
+ const modified = [];
4258
+ const deleted = [];
4259
+ for (const [filepath, type] of changes) {
4260
+ const absolutePath = path3.isAbsolute(filepath) ? filepath : path3.join(this.rootDir, filepath);
4261
+ switch (type) {
4262
+ case "add":
4263
+ added.push(absolutePath);
4264
+ break;
4265
+ case "change":
4266
+ modified.push(absolutePath);
4267
+ break;
4268
+ case "unlink":
4269
+ deleted.push(absolutePath);
4270
+ break;
4271
+ }
4272
+ }
4273
+ return { added, modified, deleted };
4274
+ }
4275
+ /**
4276
+ * Handle completion of async batch handler.
4277
+ * Triggers flush of accumulated changes if any.
4278
+ */
4279
+ handleBatchComplete() {
4280
+ this.batchInProgress = false;
4281
+ if (this.pendingChanges.size > 0 && !this.batchTimer) {
4282
+ this.batchTimer = setTimeout(() => {
4283
+ this.flushBatch();
4284
+ }, this.BATCH_WINDOW_MS);
4285
+ }
4286
+ }
4287
+ /**
4288
+ * Dispatch batch event to handler and track async state.
4289
+ * Caller must ensure at least one of added/modified/deleted is non-empty.
4290
+ */
4291
+ dispatchBatch(added, modified, deleted) {
4292
+ if (!this.onChangeHandler) return;
4293
+ const allFiles = [...added, ...modified];
4294
+ const firstFile = allFiles.length > 0 ? allFiles[0] : deleted[0];
4295
+ if (!firstFile) {
4296
+ return;
4297
+ }
4298
+ try {
4299
+ this.batchInProgress = true;
4300
+ const result = this.onChangeHandler({
4301
+ type: "batch",
4302
+ filepath: firstFile,
4303
+ added,
4304
+ modified,
4305
+ deleted
4306
+ });
4307
+ if (result instanceof Promise) {
4308
+ result.catch(() => {
4309
+ }).finally(() => this.handleBatchComplete());
4310
+ } else {
4311
+ this.handleBatchComplete();
4174
4312
  }
4175
- }, DEFAULT_DEBOUNCE_MS2);
4176
- this.debounceTimers.set(filepath, timer);
4313
+ } catch (error) {
4314
+ this.handleBatchComplete();
4315
+ }
4316
+ }
4317
+ /**
4318
+ * Flush pending changes and dispatch batch event.
4319
+ * Tracks async handler state to prevent race conditions.
4320
+ */
4321
+ flushBatch() {
4322
+ if (this.batchTimer) {
4323
+ clearTimeout(this.batchTimer);
4324
+ this.batchTimer = null;
4325
+ }
4326
+ if (this.pendingChanges.size === 0) return;
4327
+ const changes = new Map(this.pendingChanges);
4328
+ this.pendingChanges.clear();
4329
+ this.firstChangeTimestamp = null;
4330
+ const { added, modified, deleted } = this.groupPendingChanges(changes);
4331
+ if (added.length === 0 && modified.length === 0 && deleted.length === 0) {
4332
+ return;
4333
+ }
4334
+ this.dispatchBatch(added, modified, deleted);
4335
+ }
4336
+ /**
4337
+ * Flush final batch during shutdown.
4338
+ * Handles edge case where watcher is stopped while batch is pending.
4339
+ */
4340
+ async flushFinalBatch(handler) {
4341
+ if (this.pendingChanges.size === 0) return;
4342
+ const changes = new Map(this.pendingChanges);
4343
+ this.pendingChanges.clear();
4344
+ const { added, modified, deleted } = this.groupPendingChanges(changes);
4345
+ if (added.length === 0 && modified.length === 0 && deleted.length === 0) {
4346
+ return;
4347
+ }
4348
+ try {
4349
+ const allFiles = [...added, ...modified];
4350
+ const firstFile = allFiles.length > 0 ? allFiles[0] : deleted[0];
4351
+ if (!firstFile) {
4352
+ return;
4353
+ }
4354
+ await handler({
4355
+ type: "batch",
4356
+ filepath: firstFile,
4357
+ added,
4358
+ modified,
4359
+ deleted
4360
+ });
4361
+ } catch (error) {
4362
+ }
4177
4363
  }
4178
4364
  /**
4179
4365
  * Stops the file watcher and cleans up resources.
@@ -4182,13 +4368,25 @@ var FileWatcher = class {
4182
4368
  if (!this.watcher) {
4183
4369
  return;
4184
4370
  }
4185
- for (const timer of this.debounceTimers.values()) {
4186
- clearTimeout(timer);
4371
+ const handler = this.onChangeHandler;
4372
+ this.onChangeHandler = null;
4373
+ this.gitChangeHandler = null;
4374
+ if (this.gitChangeTimer) {
4375
+ clearTimeout(this.gitChangeTimer);
4376
+ this.gitChangeTimer = null;
4377
+ }
4378
+ while (this.batchInProgress) {
4379
+ await new Promise((resolve2) => setTimeout(resolve2, 50));
4380
+ }
4381
+ if (this.batchTimer) {
4382
+ clearTimeout(this.batchTimer);
4383
+ this.batchTimer = null;
4384
+ }
4385
+ if (handler && this.pendingChanges.size > 0) {
4386
+ await this.flushFinalBatch(handler);
4187
4387
  }
4188
- this.debounceTimers.clear();
4189
4388
  await this.watcher.close();
4190
4389
  this.watcher = null;
4191
- this.onChangeHandler = null;
4192
4390
  }
4193
4391
  /**
4194
4392
  * Gets the list of files currently being watched.
@@ -9579,6 +9777,134 @@ function registerMCPHandlers(server, toolContext, log) {
9579
9777
  });
9580
9778
  }
9581
9779
 
9780
+ // src/mcp/reindex-state-manager.ts
9781
+ function checkForStuckState(inProgress, lastStateChangeTimestamp, activeOperations, pendingFilesCount) {
9782
+ if (!inProgress) return;
9783
+ const STUCK_STATE_THRESHOLD_MS = 5 * 60 * 1e3;
9784
+ const stuckDuration = Date.now() - lastStateChangeTimestamp;
9785
+ if (stuckDuration > STUCK_STATE_THRESHOLD_MS) {
9786
+ console.warn(
9787
+ `[Lien] HEALTH CHECK: Reindex stuck in progress for ${Math.round(stuckDuration / 1e3)}s. This indicates an operation crashed without cleanup. Active operations: ${activeOperations}, Pending files: ${pendingFilesCount}. Consider using resetIfStuck() to recover.`
9788
+ );
9789
+ }
9790
+ }
9791
+ function mergePendingFiles(pendingFiles, newFiles) {
9792
+ const existing = new Set(pendingFiles);
9793
+ for (const file of newFiles) {
9794
+ if (!existing.has(file)) {
9795
+ pendingFiles.push(file);
9796
+ }
9797
+ }
9798
+ }
9799
+ function createReindexStateManager() {
9800
+ let state = {
9801
+ inProgress: false,
9802
+ pendingFiles: [],
9803
+ lastReindexTimestamp: null,
9804
+ lastReindexDurationMs: null
9805
+ };
9806
+ let activeOperations = 0;
9807
+ let lastStateChangeTimestamp = Date.now();
9808
+ return {
9809
+ /**
9810
+ * Get a copy of the current reindex state.
9811
+ * Returns a deep copy to prevent external mutation of nested arrays.
9812
+ */
9813
+ getState: () => {
9814
+ checkForStuckState(
9815
+ state.inProgress,
9816
+ lastStateChangeTimestamp,
9817
+ activeOperations,
9818
+ state.pendingFiles.length
9819
+ );
9820
+ return { ...state, pendingFiles: [...state.pendingFiles] };
9821
+ },
9822
+ /**
9823
+ * Start a new reindex operation.
9824
+ *
9825
+ * **Important**: Silently ignores empty or null file arrays without incrementing
9826
+ * activeOperations. This is intentional - if there's no work to do, no operation
9827
+ * is started. Callers should check for empty arrays before calling if they need
9828
+ * to track "attempted" operations.
9829
+ *
9830
+ * @param files - Array of file paths to reindex. Empty/null arrays are ignored.
9831
+ */
9832
+ startReindex: (files) => {
9833
+ if (!files || files.length === 0) {
9834
+ return;
9835
+ }
9836
+ activeOperations += 1;
9837
+ state.inProgress = true;
9838
+ lastStateChangeTimestamp = Date.now();
9839
+ mergePendingFiles(state.pendingFiles, files);
9840
+ },
9841
+ /**
9842
+ * Mark a reindex operation as complete.
9843
+ *
9844
+ * Logs a warning if called without a matching startReindex.
9845
+ * Only clears state when all concurrent operations finish.
9846
+ *
9847
+ * @param durationMs - Duration of the reindex operation in milliseconds
9848
+ */
9849
+ completeReindex: (durationMs) => {
9850
+ if (activeOperations === 0) {
9851
+ console.warn("[Lien] completeReindex called without matching startReindex");
9852
+ return;
9853
+ }
9854
+ activeOperations -= 1;
9855
+ if (activeOperations === 0) {
9856
+ state.inProgress = false;
9857
+ state.pendingFiles = [];
9858
+ state.lastReindexTimestamp = Date.now();
9859
+ state.lastReindexDurationMs = durationMs;
9860
+ lastStateChangeTimestamp = Date.now();
9861
+ }
9862
+ },
9863
+ /**
9864
+ * Mark a reindex operation as failed.
9865
+ *
9866
+ * Logs a warning if called without a matching startReindex.
9867
+ * Only clears state when all concurrent operations finish/fail.
9868
+ */
9869
+ failReindex: () => {
9870
+ if (activeOperations === 0) {
9871
+ console.warn("[Lien] failReindex called without matching startReindex");
9872
+ return;
9873
+ }
9874
+ activeOperations -= 1;
9875
+ if (activeOperations === 0) {
9876
+ state.inProgress = false;
9877
+ state.pendingFiles = [];
9878
+ lastStateChangeTimestamp = Date.now();
9879
+ }
9880
+ },
9881
+ /**
9882
+ * Manually reset state if it's stuck.
9883
+ *
9884
+ * **WARNING**: Only use this if you're certain operations have crashed without cleanup.
9885
+ * This will forcibly clear the inProgress flag and reset activeOperations counter.
9886
+ *
9887
+ * Use this when getState() health check detects stuck state and you've verified
9888
+ * no legitimate operations are running.
9889
+ *
9890
+ * @returns true if state was reset, false if state was already clean
9891
+ */
9892
+ resetIfStuck: () => {
9893
+ if (state.inProgress && activeOperations > 0) {
9894
+ console.warn(
9895
+ `[Lien] Manually resetting stuck reindex state. Active operations: ${activeOperations}, Pending files: ${state.pendingFiles.length}`
9896
+ );
9897
+ activeOperations = 0;
9898
+ state.inProgress = false;
9899
+ state.pendingFiles = [];
9900
+ lastStateChangeTimestamp = Date.now();
9901
+ return true;
9902
+ }
9903
+ return false;
9904
+ }
9905
+ };
9906
+ }
9907
+
9582
9908
  // src/mcp/server.ts
9583
9909
  var __filename2 = fileURLToPath2(import.meta.url);
9584
9910
  var __dirname2 = dirname2(__filename2);
@@ -9589,6 +9915,9 @@ try {
9589
9915
  } catch {
9590
9916
  packageJson2 = require3(join2(__dirname2, "../../package.json"));
9591
9917
  }
9918
+ function getRootDirFromDbPath(dbPath) {
9919
+ return resolve(dbPath, "../../..");
9920
+ }
9592
9921
  async function initializeDatabase(rootDir, log) {
9593
9922
  const embeddings = new LocalEmbeddings();
9594
9923
  log("Creating vector database...");
@@ -9621,7 +9950,96 @@ async function handleAutoIndexing(vectorDB, rootDir, log) {
9621
9950
  }
9622
9951
  }
9623
9952
  }
9624
- async function setupGitDetection(rootDir, vectorDB, embeddings, verbose, log) {
9953
+ async function handleGitStartup(gitTracker, vectorDB, embeddings, _verbose, log, reindexStateManager) {
9954
+ log("Checking for git changes...");
9955
+ const changedFiles = await gitTracker.initialize();
9956
+ if (changedFiles && changedFiles.length > 0) {
9957
+ const startTime = Date.now();
9958
+ reindexStateManager.startReindex(changedFiles);
9959
+ log(`\u{1F33F} Git changes detected: ${changedFiles.length} files changed`);
9960
+ try {
9961
+ const count = await indexMultipleFiles(changedFiles, vectorDB, embeddings, { verbose: false });
9962
+ const duration = Date.now() - startTime;
9963
+ reindexStateManager.completeReindex(duration);
9964
+ log(`\u2713 Reindexed ${count} files in ${duration}ms`);
9965
+ } catch (error) {
9966
+ reindexStateManager.failReindex();
9967
+ throw error;
9968
+ }
9969
+ } else {
9970
+ log("\u2713 Index is up to date with git state");
9971
+ }
9972
+ }
9973
+ function createGitPollInterval(gitTracker, vectorDB, embeddings, _verbose, log, reindexStateManager) {
9974
+ return setInterval(async () => {
9975
+ try {
9976
+ const changedFiles = await gitTracker.detectChanges();
9977
+ if (changedFiles && changedFiles.length > 0) {
9978
+ const currentState = reindexStateManager.getState();
9979
+ if (currentState.inProgress) {
9980
+ log(
9981
+ `Background reindex already in progress (${currentState.pendingFiles.length} files pending), skipping git poll cycle`,
9982
+ "debug"
9983
+ );
9984
+ return;
9985
+ }
9986
+ const startTime = Date.now();
9987
+ reindexStateManager.startReindex(changedFiles);
9988
+ log(`\u{1F33F} Git change detected: ${changedFiles.length} files changed`);
9989
+ try {
9990
+ const count = await indexMultipleFiles(changedFiles, vectorDB, embeddings, { verbose: false });
9991
+ const duration = Date.now() - startTime;
9992
+ reindexStateManager.completeReindex(duration);
9993
+ log(`\u2713 Background reindex complete: ${count} files in ${duration}ms`);
9994
+ } catch (error) {
9995
+ reindexStateManager.failReindex();
9996
+ log(`Git background reindex failed: ${error}`, "warning");
9997
+ }
9998
+ }
9999
+ } catch (error) {
10000
+ log(`Git detection check failed: ${error}`, "warning");
10001
+ }
10002
+ }, DEFAULT_GIT_POLL_INTERVAL_MS2);
10003
+ }
10004
+ function createGitChangeHandler(gitTracker, vectorDB, embeddings, _verbose, log, reindexStateManager) {
10005
+ let gitReindexInProgress = false;
10006
+ let lastGitReindexTime = 0;
10007
+ const GIT_REINDEX_COOLDOWN_MS = 5e3;
10008
+ return async () => {
10009
+ const { inProgress: globalInProgress } = reindexStateManager.getState();
10010
+ if (gitReindexInProgress || globalInProgress) {
10011
+ log("Git reindex already in progress, skipping", "debug");
10012
+ return;
10013
+ }
10014
+ const timeSinceLastReindex = Date.now() - lastGitReindexTime;
10015
+ if (timeSinceLastReindex < GIT_REINDEX_COOLDOWN_MS) {
10016
+ log(`Git change ignored (cooldown: ${GIT_REINDEX_COOLDOWN_MS - timeSinceLastReindex}ms remaining)`, "debug");
10017
+ return;
10018
+ }
10019
+ log("\u{1F33F} Git change detected (event-driven)");
10020
+ const changedFiles = await gitTracker.detectChanges();
10021
+ if (!changedFiles || changedFiles.length === 0) {
10022
+ return;
10023
+ }
10024
+ gitReindexInProgress = true;
10025
+ const startTime = Date.now();
10026
+ reindexStateManager.startReindex(changedFiles);
10027
+ log(`Reindexing ${changedFiles.length} files from git change`);
10028
+ try {
10029
+ const count = await indexMultipleFiles(changedFiles, vectorDB, embeddings, { verbose: false });
10030
+ const duration = Date.now() - startTime;
10031
+ reindexStateManager.completeReindex(duration);
10032
+ log(`\u2713 Reindexed ${count} files in ${duration}ms`);
10033
+ lastGitReindexTime = Date.now();
10034
+ } catch (error) {
10035
+ reindexStateManager.failReindex();
10036
+ log(`Git reindex failed: ${error}`, "warning");
10037
+ } finally {
10038
+ gitReindexInProgress = false;
10039
+ }
10040
+ };
10041
+ }
10042
+ async function setupGitDetection(rootDir, vectorDB, embeddings, verbose, log, reindexStateManager, fileWatcher) {
9625
10043
  const gitAvailable = await isGitAvailable();
9626
10044
  const isRepo = await isGitRepo2(rootDir);
9627
10045
  if (!gitAvailable) {
@@ -9635,34 +10053,204 @@ async function setupGitDetection(rootDir, vectorDB, embeddings, verbose, log) {
9635
10053
  log("\u2713 Detected git repository");
9636
10054
  const gitTracker = new GitStateTracker(rootDir, vectorDB.dbPath);
9637
10055
  try {
9638
- log("Checking for git changes...");
9639
- const changedFiles = await gitTracker.initialize();
9640
- if (changedFiles && changedFiles.length > 0) {
9641
- log(`\u{1F33F} Git changes detected: ${changedFiles.length} files changed`);
9642
- const count = await indexMultipleFiles(changedFiles, vectorDB, embeddings, { verbose });
9643
- log(`\u2713 Reindexed ${count} files`);
9644
- } else {
9645
- log("\u2713 Index is up to date with git state");
9646
- }
10056
+ await handleGitStartup(gitTracker, vectorDB, embeddings, verbose, log, reindexStateManager);
9647
10057
  } catch (error) {
9648
10058
  log(`Failed to check git state on startup: ${error}`, "warning");
9649
10059
  }
10060
+ if (fileWatcher) {
10061
+ const gitChangeHandler = createGitChangeHandler(
10062
+ gitTracker,
10063
+ vectorDB,
10064
+ embeddings,
10065
+ verbose,
10066
+ log,
10067
+ reindexStateManager
10068
+ );
10069
+ fileWatcher.watchGit(gitChangeHandler);
10070
+ log("\u2713 Git detection enabled (event-driven via file watcher)");
10071
+ return { gitTracker, gitPollInterval: null };
10072
+ }
9650
10073
  const pollIntervalSeconds = DEFAULT_GIT_POLL_INTERVAL_MS2 / 1e3;
9651
- log(`\u2713 Git detection enabled (checking every ${pollIntervalSeconds}s)`);
9652
- const gitPollInterval = setInterval(async () => {
10074
+ log(`\u2713 Git detection enabled (polling fallback every ${pollIntervalSeconds}s)`);
10075
+ const gitPollInterval = createGitPollInterval(gitTracker, vectorDB, embeddings, verbose, log, reindexStateManager);
10076
+ return { gitTracker, gitPollInterval };
10077
+ }
10078
+ async function handleFileDeletion(filepath, vectorDB, log) {
10079
+ log(`\u{1F5D1}\uFE0F File deleted: ${filepath}`);
10080
+ const manifest = new ManifestManager(vectorDB.dbPath);
10081
+ try {
10082
+ await vectorDB.deleteByFile(filepath);
10083
+ await manifest.removeFile(filepath);
10084
+ log(`\u2713 Removed ${filepath} from index`);
10085
+ } catch (error) {
10086
+ log(`Failed to remove ${filepath}: ${error}`, "warning");
10087
+ throw error;
10088
+ }
10089
+ }
10090
+ async function handleSingleFileChange(filepath, type, vectorDB, embeddings, _verbose, log, reindexStateManager) {
10091
+ const action = type === "add" ? "added" : "changed";
10092
+ const rootDir = getRootDirFromDbPath(vectorDB.dbPath);
10093
+ if (type === "change") {
10094
+ const manifest = new ManifestManager(vectorDB.dbPath);
10095
+ const normalizedPath = normalizeToRelativePath(filepath, rootDir);
9653
10096
  try {
9654
- const changedFiles = await gitTracker.detectChanges();
9655
- if (changedFiles && changedFiles.length > 0) {
9656
- log(`\u{1F33F} Git change detected: ${changedFiles.length} files changed`);
9657
- indexMultipleFiles(changedFiles, vectorDB, embeddings, { verbose }).then((count) => log(`\u2713 Background reindex complete: ${count} files`)).catch((error) => log(`Background reindex failed: ${error}`, "warning"));
10097
+ const existingEntry = await manifest.transaction(async (manifestData) => {
10098
+ return manifestData.files[normalizedPath];
10099
+ });
10100
+ const { shouldReindex, newMtime } = await shouldReindexFile(filepath, existingEntry, log);
10101
+ if (!shouldReindex && newMtime && existingEntry) {
10102
+ const skipReindex = await manifest.transaction(async (manifestData) => {
10103
+ const entry = manifestData.files[normalizedPath];
10104
+ if (entry) {
10105
+ entry.lastModified = newMtime;
10106
+ return true;
10107
+ }
10108
+ return false;
10109
+ });
10110
+ if (skipReindex) {
10111
+ return;
10112
+ }
9658
10113
  }
9659
10114
  } catch (error) {
9660
- log(`Git detection check failed: ${error}`, "warning");
10115
+ log(`Content hash check failed, will reindex: ${error}`, "warning");
9661
10116
  }
9662
- }, DEFAULT_GIT_POLL_INTERVAL_MS2);
9663
- return { gitTracker, gitPollInterval };
10117
+ }
10118
+ const startTime = Date.now();
10119
+ reindexStateManager.startReindex([filepath]);
10120
+ log(`\u{1F4DD} File ${action}: ${filepath}`);
10121
+ try {
10122
+ await indexSingleFile(filepath, vectorDB, embeddings, { verbose: false, rootDir });
10123
+ const duration = Date.now() - startTime;
10124
+ reindexStateManager.completeReindex(duration);
10125
+ } catch (error) {
10126
+ reindexStateManager.failReindex();
10127
+ log(`Failed to reindex ${filepath}: ${error}`, "warning");
10128
+ }
10129
+ }
10130
+ async function shouldReindexFile(filepath, existingEntry, log) {
10131
+ if (!existingEntry?.contentHash) {
10132
+ return { shouldReindex: true };
10133
+ }
10134
+ const currentHash = await computeContentHash(filepath);
10135
+ if (currentHash && currentHash === existingEntry.contentHash) {
10136
+ log(`\u23ED\uFE0F File mtime changed but content unchanged: ${filepath}`, "debug");
10137
+ try {
10138
+ const fs5 = await import("fs/promises");
10139
+ const stats = await fs5.stat(filepath);
10140
+ return { shouldReindex: false, newMtime: stats.mtimeMs };
10141
+ } catch {
10142
+ return { shouldReindex: true };
10143
+ }
10144
+ }
10145
+ return { shouldReindex: true };
10146
+ }
10147
+ async function filterModifiedFilesByHash(modifiedFiles, vectorDB, log) {
10148
+ if (modifiedFiles.length === 0) {
10149
+ return [];
10150
+ }
10151
+ const manifest = new ManifestManager(vectorDB.dbPath);
10152
+ const rootDir = getRootDirFromDbPath(vectorDB.dbPath);
10153
+ const manifestData = await manifest.transaction(async (data) => data);
10154
+ if (!manifestData) {
10155
+ return modifiedFiles;
10156
+ }
10157
+ const checkResults = [];
10158
+ for (const filepath of modifiedFiles) {
10159
+ const normalizedPath = normalizeToRelativePath(filepath, rootDir);
10160
+ const existingEntry = manifestData.files[normalizedPath];
10161
+ const { shouldReindex, newMtime } = await shouldReindexFile(filepath, existingEntry, log);
10162
+ checkResults.push({
10163
+ filepath,
10164
+ normalizedPath,
10165
+ shouldReindex,
10166
+ newMtime
10167
+ });
10168
+ }
10169
+ await manifest.transaction(async (data) => {
10170
+ for (const result of checkResults) {
10171
+ if (!result.shouldReindex && result.newMtime) {
10172
+ const entry = data.files[result.normalizedPath];
10173
+ if (entry) {
10174
+ entry.lastModified = result.newMtime;
10175
+ }
10176
+ }
10177
+ }
10178
+ return null;
10179
+ });
10180
+ return checkResults.filter((r) => r.shouldReindex).map((r) => r.filepath);
10181
+ }
10182
+ async function prepareFilesForReindexing(event, vectorDB, log) {
10183
+ const addedFiles = event.added || [];
10184
+ const modifiedFiles = event.modified || [];
10185
+ const deletedFiles = event.deleted || [];
10186
+ let modifiedFilesToReindex = [];
10187
+ try {
10188
+ modifiedFilesToReindex = await filterModifiedFilesByHash(modifiedFiles, vectorDB, log);
10189
+ } catch (error) {
10190
+ log(`Hash-based filtering failed, will reindex all modified files: ${error}`, "warning");
10191
+ modifiedFilesToReindex = modifiedFiles;
10192
+ }
10193
+ const filesToIndex = [...addedFiles, ...modifiedFilesToReindex];
10194
+ return { filesToIndex, deletedFiles };
10195
+ }
10196
+ async function executeReindexOperations(filesToIndex, deletedFiles, vectorDB, embeddings, log) {
10197
+ const operations = [];
10198
+ if (filesToIndex.length > 0) {
10199
+ log(`\u{1F4C1} ${filesToIndex.length} file(s) changed, reindexing...`);
10200
+ operations.push(indexMultipleFiles(filesToIndex, vectorDB, embeddings, { verbose: false }));
10201
+ }
10202
+ if (deletedFiles.length > 0) {
10203
+ operations.push(
10204
+ Promise.all(
10205
+ deletedFiles.map((deleted) => handleFileDeletion(deleted, vectorDB, log))
10206
+ )
10207
+ );
10208
+ }
10209
+ await Promise.all(operations);
9664
10210
  }
9665
- async function setupFileWatching(watch, rootDir, vectorDB, embeddings, verbose, log) {
10211
+ async function handleBatchEvent(event, vectorDB, embeddings, _verbose, log, reindexStateManager) {
10212
+ const { filesToIndex, deletedFiles } = await prepareFilesForReindexing(event, vectorDB, log);
10213
+ const allFiles = [...filesToIndex, ...deletedFiles];
10214
+ if (allFiles.length === 0) {
10215
+ return;
10216
+ }
10217
+ const startTime = Date.now();
10218
+ reindexStateManager.startReindex(allFiles);
10219
+ try {
10220
+ await executeReindexOperations(filesToIndex, deletedFiles, vectorDB, embeddings, log);
10221
+ const duration = Date.now() - startTime;
10222
+ reindexStateManager.completeReindex(duration);
10223
+ log(`\u2713 Processed ${filesToIndex.length} file(s) + ${deletedFiles.length} deletion(s) in ${duration}ms`);
10224
+ } catch (error) {
10225
+ reindexStateManager.failReindex();
10226
+ log(`Batch reindex failed: ${error}`, "warning");
10227
+ }
10228
+ }
10229
+ async function handleUnlinkEvent(filepath, vectorDB, log, reindexStateManager) {
10230
+ const startTime = Date.now();
10231
+ reindexStateManager.startReindex([filepath]);
10232
+ try {
10233
+ await handleFileDeletion(filepath, vectorDB, log);
10234
+ const duration = Date.now() - startTime;
10235
+ reindexStateManager.completeReindex(duration);
10236
+ } catch (error) {
10237
+ reindexStateManager.failReindex();
10238
+ log(`Failed to process deletion for ${filepath}: ${error}`, "warning");
10239
+ }
10240
+ }
10241
+ function createFileChangeHandler(vectorDB, embeddings, verbose, log, reindexStateManager) {
10242
+ return async (event) => {
10243
+ const { type } = event;
10244
+ if (type === "batch") {
10245
+ await handleBatchEvent(event, vectorDB, embeddings, verbose, log, reindexStateManager);
10246
+ } else if (type === "unlink") {
10247
+ await handleUnlinkEvent(event.filepath, vectorDB, log, reindexStateManager);
10248
+ } else {
10249
+ await handleSingleFileChange(event.filepath, type, vectorDB, embeddings, verbose, log, reindexStateManager);
10250
+ }
10251
+ };
10252
+ }
10253
+ async function setupFileWatching(watch, rootDir, vectorDB, embeddings, verbose, log, reindexStateManager) {
9666
10254
  const fileWatchingEnabled = watch !== void 0 ? watch : true;
9667
10255
  if (!fileWatchingEnabled) {
9668
10256
  return null;
@@ -9670,24 +10258,8 @@ async function setupFileWatching(watch, rootDir, vectorDB, embeddings, verbose,
9670
10258
  log("\u{1F440} Starting file watcher...");
9671
10259
  const fileWatcher = new FileWatcher(rootDir);
9672
10260
  try {
9673
- await fileWatcher.start(async (event) => {
9674
- const { type, filepath } = event;
9675
- if (type === "unlink") {
9676
- log(`\u{1F5D1}\uFE0F File deleted: ${filepath}`);
9677
- try {
9678
- await vectorDB.deleteByFile(filepath);
9679
- const manifest = new ManifestManager(vectorDB.dbPath);
9680
- await manifest.removeFile(filepath);
9681
- log(`\u2713 Removed ${filepath} from index`);
9682
- } catch (error) {
9683
- log(`Failed to remove ${filepath}: ${error}`, "warning");
9684
- }
9685
- } else {
9686
- const action = type === "add" ? "added" : "changed";
9687
- log(`\u{1F4DD} File ${action}: ${filepath}`);
9688
- indexSingleFile(filepath, vectorDB, embeddings, { verbose }).catch((error) => log(`Failed to reindex ${filepath}: ${error}`, "warning"));
9689
- }
9690
- });
10261
+ const handler = createFileChangeHandler(vectorDB, embeddings, verbose, log, reindexStateManager);
10262
+ await fileWatcher.start(handler);
9691
10263
  log(`\u2713 File watching enabled (watching ${fileWatcher.getWatchedFiles().length} files)`);
9692
10264
  return fileWatcher;
9693
10265
  } catch (error) {
@@ -9714,7 +10286,7 @@ function setupCleanupHandlers(versionCheckInterval, gitPollInterval, fileWatcher
9714
10286
  process.exit(0);
9715
10287
  };
9716
10288
  }
9717
- function setupVersionChecking(vectorDB, log) {
10289
+ function setupVersionChecking(vectorDB, log, reindexStateManager) {
9718
10290
  const checkAndReconnect = async () => {
9719
10291
  try {
9720
10292
  if (await vectorDB.checkVersion()) {
@@ -9725,10 +10297,19 @@ function setupVersionChecking(vectorDB, log) {
9725
10297
  log(`Version check failed: ${error}`, "warning");
9726
10298
  }
9727
10299
  };
9728
- const getIndexMetadata = () => ({
9729
- indexVersion: vectorDB.getCurrentVersion(),
9730
- indexDate: vectorDB.getVersionDate()
9731
- });
10300
+ const getIndexMetadata = () => {
10301
+ const reindex = reindexStateManager.getState();
10302
+ return {
10303
+ indexVersion: vectorDB.getCurrentVersion(),
10304
+ indexDate: vectorDB.getVersionDate(),
10305
+ reindexInProgress: reindex.inProgress,
10306
+ pendingFileCount: reindex.pendingFiles.length,
10307
+ lastReindexDurationMs: reindex.lastReindexDurationMs,
10308
+ // Note: msSinceLastReindex is computed at call time, not cached.
10309
+ // This ensures AI assistants always get current freshness info.
10310
+ msSinceLastReindex: reindex.lastReindexTimestamp ? Date.now() - reindex.lastReindexTimestamp : null
10311
+ };
10312
+ };
9732
10313
  const interval = setInterval(checkAndReconnect, VERSION_CHECK_INTERVAL_MS);
9733
10314
  return { interval, checkAndReconnect, getIndexMetadata };
9734
10315
  }
@@ -9774,13 +10355,13 @@ function createMCPServer() {
9774
10355
  { capabilities: serverConfig.capabilities }
9775
10356
  );
9776
10357
  }
9777
- async function setupAndConnectServer(server, toolContext, log, versionCheckInterval, options) {
10358
+ async function setupAndConnectServer(server, toolContext, log, versionCheckInterval, reindexStateManager, options) {
9778
10359
  const { rootDir, verbose, watch } = options;
9779
10360
  const { vectorDB, embeddings } = toolContext;
9780
10361
  registerMCPHandlers(server, toolContext, log);
9781
10362
  await handleAutoIndexing(vectorDB, rootDir, log);
9782
- const { gitPollInterval } = await setupGitDetection(rootDir, vectorDB, embeddings, verbose, log);
9783
- const fileWatcher = await setupFileWatching(watch, rootDir, vectorDB, embeddings, verbose, log);
10363
+ const fileWatcher = await setupFileWatching(watch, rootDir, vectorDB, embeddings, verbose, log, reindexStateManager);
10364
+ const { gitPollInterval } = await setupGitDetection(rootDir, vectorDB, embeddings, verbose, log, reindexStateManager, fileWatcher);
9784
10365
  const cleanup = setupCleanupHandlers(versionCheckInterval, gitPollInterval, fileWatcher, log);
9785
10366
  process.on("SIGINT", cleanup);
9786
10367
  process.on("SIGTERM", cleanup);
@@ -9803,9 +10384,18 @@ async function startMCPServer(options) {
9803
10384
  const { embeddings, vectorDB } = await initializeComponents(rootDir, earlyLog);
9804
10385
  const server = createMCPServer();
9805
10386
  const log = createMCPLog(server, verbose);
9806
- const { interval: versionCheckInterval, checkAndReconnect, getIndexMetadata } = setupVersionChecking(vectorDB, log);
9807
- const toolContext = { vectorDB, embeddings, rootDir, log, checkAndReconnect, getIndexMetadata };
9808
- await setupAndConnectServer(server, toolContext, log, versionCheckInterval, { rootDir, verbose, watch });
10387
+ const reindexStateManager = createReindexStateManager();
10388
+ const { interval: versionCheckInterval, checkAndReconnect, getIndexMetadata } = setupVersionChecking(vectorDB, log, reindexStateManager);
10389
+ const toolContext = {
10390
+ vectorDB,
10391
+ embeddings,
10392
+ rootDir,
10393
+ log,
10394
+ checkAndReconnect,
10395
+ getIndexMetadata,
10396
+ getReindexState: () => reindexStateManager.getState()
10397
+ };
10398
+ await setupAndConnectServer(server, toolContext, log, versionCheckInterval, reindexStateManager, { rootDir, verbose, watch });
9809
10399
  }
9810
10400
 
9811
10401
  // src/cli/serve.ts