@scramble-cloud/lurus-code-cli 2.7.0 → 2.7.2

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.
@@ -1,7 +1,7 @@
1
1
  // src/services/index-worker.ts
2
2
  import { parentPort, workerData } from "node:worker_threads";
3
- import { basename as basename6 } from "node:path";
4
- import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { basename as basename7 } from "node:path";
4
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
5
5
 
6
6
  // src/services/index-diagnostics.ts
7
7
  function createIndexDiagnostics() {
@@ -69,9 +69,16 @@ function canonicalizeSync(absPath) {
69
69
 
70
70
  // src/services/sandbox.service.ts
71
71
  import { resolve as resolve3, basename as basename2, dirname as dirname2, join as join2, sep as sep2 } from "node:path";
72
- import { realpathSync as realpathSync2, readdirSync } from "node:fs";
72
+ import {
73
+ closeSync,
74
+ constants,
75
+ fstatSync,
76
+ lstatSync,
77
+ openSync,
78
+ readFileSync,
79
+ realpathSync as realpathSync2
80
+ } from "node:fs";
73
81
  import { homedir as homedir2 } from "node:os";
74
- import { existsSync, readFileSync } from "node:fs";
75
82
  import { readFile, realpath } from "node:fs/promises";
76
83
  import ignore from "ignore";
77
84
 
@@ -170,27 +177,11 @@ function blockedPaths(homeDir) {
170
177
  ];
171
178
  }
172
179
  var BLOCKED_PATHS = blockedPaths(homedir2());
173
- var IGNORE_SCAN_SKIP_DIRS = /* @__PURE__ */ new Set([
174
- "node_modules",
175
- "dist",
176
- "build",
177
- "out",
178
- "coverage",
179
- ".git",
180
- ".svn",
181
- ".hg",
182
- ".next",
183
- ".nuxt",
184
- ".cache",
185
- ".turbo",
186
- ".venv",
187
- "venv",
188
- "__pycache__",
189
- "target"
190
- // rust / java
191
- ]);
192
- var IGNORE_SCAN_MAX_DEPTH = 8;
193
- var MAX_IGNORE_FILES = 200;
180
+ var MAX_CACHED_IGNORE_MATCHERS = 512;
181
+ var IGNORE_FILES = [
182
+ [".lurusignore", "lurusignore"],
183
+ [".gitignore", "gitignore"]
184
+ ];
194
185
  var SandboxService = class {
195
186
  constructor(workspaceRoot, ignoreReadAllowed) {
196
187
  this.ignoreReadAllowed = ignoreReadAllowed;
@@ -230,27 +221,13 @@ var SandboxService = class {
230
221
  ownedReadPaths = /* @__PURE__ */ new Map();
231
222
  ownedWritePaths = /* @__PURE__ */ new Map();
232
223
  ownedExactReadPaths = /* @__PURE__ */ new Map();
233
- /**
234
- * Ignore rules keyed by the directory that contains the originating
235
- * `.lurusignore` / `.gitignore`. Patterns are interpreted relative to
236
- * that directory \u2014 the same semantics git itself uses for nested
237
- * `.gitignore` files. Loaded recursively at construction so a project
238
- * living in a subdirectory of `cwd` still has its ignore rules honoured.
239
- */
240
- ignoreSources = null;
224
+ ignoreMatchers = /* @__PURE__ */ new Map();
225
+ ignoreOverride = null;
241
226
  fileReadScopeResolver = null;
242
- ignoreScanNotice = null;
243
227
  refreshIgnoreSources() {
244
- this.ignoreSources = null;
228
+ this.ignoreMatchers.clear();
229
+ this.ignoreOverride = null;
245
230
  }
246
- setIgnoreScanNoticeHandler(handler) {
247
- this.ignoreScanNotice = handler;
248
- }
249
- /**
250
- * Replace all ignore sources with a single in-memory source rooted at
251
- * the workspace root. Kept for tests and programmatic callers that
252
- * want to set patterns without writing a file.
253
- */
254
231
  /**
255
232
  * Replace all ignore sources with a single in-memory source rooted at
256
233
  * the workspace root. Kept for tests and programmatic callers that
@@ -260,13 +237,7 @@ var SandboxService = class {
260
237
  * `setIgnorePatternsOfKind` instead.
261
238
  */
262
239
  setIgnorePatterns(patterns) {
263
- this.ignoreSources = [
264
- {
265
- basePath: this.workspaceRoot,
266
- kind: "lurusignore",
267
- matcher: ignore().add(patterns)
268
- }
269
- ];
240
+ this.setIgnorePatternsOfKind("lurusignore", patterns);
270
241
  }
271
242
  /**
272
243
  * Replace all ignore sources with a single in-memory source of a
@@ -274,13 +245,7 @@ var SandboxService = class {
274
245
  * path without touching the filesystem.
275
246
  */
276
247
  setIgnorePatternsOfKind(kind, patterns) {
277
- this.ignoreSources = [
278
- {
279
- basePath: this.workspaceRoot,
280
- kind,
281
- matcher: ignore().add(patterns)
282
- }
283
- ];
248
+ this.ignoreOverride = { kind, matcher: ignore().add(patterns) };
284
249
  }
285
250
  /**
286
251
  * Match a path against all loaded ignore files and report WHICH kind
@@ -558,9 +523,12 @@ var SandboxService = class {
558
523
  classifyGrepDirectoryRoot(logicalPath, physicalPath) {
559
524
  const absoluteLogicalPath = resolve3(logicalPath);
560
525
  const absolutePhysicalPath = resolve3(physicalPath);
526
+ if (this.checkBlockedPaths(absoluteLogicalPath) || this.checkBlockedPaths(absolutePhysicalPath)) {
527
+ return { kind: "hard-deny" };
528
+ }
561
529
  const logicalIgnore = this.checkIgnoredPath(absoluteLogicalPath, true);
562
- const physicalIgnore = this.checkIgnoredPath(absolutePhysicalPath, true);
563
- if (logicalIgnore?.kind === "lurusignore" || physicalIgnore?.kind === "lurusignore" || this.isBlockedPath(absoluteLogicalPath) || this.isBlockedPath(absolutePhysicalPath)) {
530
+ const physicalIgnore = absoluteLogicalPath === absolutePhysicalPath ? logicalIgnore : this.checkIgnoredPath(absolutePhysicalPath, true);
531
+ if (logicalIgnore?.kind === "lurusignore" || physicalIgnore?.kind === "lurusignore") {
564
532
  return { kind: "hard-deny" };
565
533
  }
566
534
  return {
@@ -571,12 +539,16 @@ var SandboxService = class {
571
539
  classifyGrepCandidate(logicalPath, physicalPath, scope) {
572
540
  const absoluteLogicalPath = resolve3(logicalPath);
573
541
  const absolutePhysicalPath = resolve3(physicalPath);
574
- if (this.isLurusIgnored(absoluteLogicalPath) || this.isLurusIgnored(absolutePhysicalPath) || this.isBlockedPath(absoluteLogicalPath) || this.isBlockedPath(absolutePhysicalPath)) {
542
+ if (this.checkBlockedPaths(absoluteLogicalPath) || this.checkBlockedPaths(absolutePhysicalPath)) {
575
543
  return { kind: "hard-deny" };
576
544
  }
577
- const checkSensitive = scope !== "explicit-file";
578
- const checkGitIgnore = scope === "broad-directory" && !(this.hasFileReadScope(absoluteLogicalPath) && this.hasFileReadScope(absolutePhysicalPath));
579
- if (checkGitIgnore && (this.isGitIgnored(absoluteLogicalPath) || this.isGitIgnored(absolutePhysicalPath)) || checkSensitive && (this.matchesSensitivePattern(absoluteLogicalPath) !== null || this.matchesSensitivePattern(absolutePhysicalPath) !== null)) {
545
+ const logicalIgnore = this.checkIgnoredPath(absoluteLogicalPath, false);
546
+ const samePath = absoluteLogicalPath === absolutePhysicalPath;
547
+ const physicalIgnore = samePath ? logicalIgnore : this.checkIgnoredPath(absolutePhysicalPath, false);
548
+ if (logicalIgnore?.kind === "lurusignore" || physicalIgnore?.kind === "lurusignore") {
549
+ return { kind: "hard-deny" };
550
+ }
551
+ if (scope === "broad-directory" && (logicalIgnore?.kind === "gitignore" || physicalIgnore?.kind === "gitignore") && !(this.hasFileReadScope(absoluteLogicalPath) && (samePath || this.hasFileReadScope(absolutePhysicalPath))) || scope !== "explicit-file" && (this.checkSensitiveFile(absoluteLogicalPath) || !samePath && this.checkSensitiveFile(absolutePhysicalPath))) {
580
552
  return { kind: "soft-deny" };
581
553
  }
582
554
  return { kind: "allow" };
@@ -588,28 +560,6 @@ var SandboxService = class {
588
560
  getWorkspaceRoot() {
589
561
  return this.workspaceRoot;
590
562
  }
591
- /**
592
- * Walk `workspaceRoot` recursively and collect every `.lurusignore` and
593
- * `.gitignore` file. Each file's patterns are stored alongside its
594
- * containing directory so matching can be relative to the right base.
595
- *
596
- * Rationale: the previous single-file loader only looked at
597
- * `<workspaceRoot>/.lurusignore`. When the CLI runs from a parent
598
- * directory of the actual project (e.g. `cwd = /home/me/repos/`, project
599
- * `/home/me/repos/my-app/.lurusignore`), the ignore rules were silently
600
- * dropped and `isIgnored()` returned `false` for paths the user had
601
- * explicitly excluded. Nested `.gitignore` support matches git's own
602
- * behaviour so the agent respects the same rules the developer sees.
603
- */
604
- /**
605
- * Walk `workspaceRoot` (and `physicalRoot` if it differs) recursively and
606
- * collect every `.lurusignore` and `.gitignore` file. Each file's
607
- * patterns are stored alongside its containing directory so matching can
608
- * be relative to the right base.
609
- *
610
- * Loaded lazily and cached so sandbox construction stays fast even when
611
- * `lurus` is started in a directory with thousands of nested files.
612
- */
613
563
  replaceOwnedPaths(ownedPaths, owner, paths) {
614
564
  const normalized = new Set(paths.map((path2) => resolve3(path2)));
615
565
  if (normalized.size === 0) {
@@ -636,82 +586,79 @@ var SandboxService = class {
636
586
  return false;
637
587
  }
638
588
  checkIgnoredPath(filePath, directory) {
639
- const sources = this.getIgnoreSources();
640
- if (sources.length === 0) return null;
641
589
  const absolute = resolve3(filePath);
590
+ const suffix = directory ? "/" : "";
591
+ if (this.ignoreOverride) {
592
+ const relative3 = relativePathWithinRoot(this.workspaceRoot, absolute);
593
+ return relative3 && this.ignoreOverride.matcher.ignores(
594
+ relative3.split(sep2).join("/") + suffix
595
+ ) ? { kind: this.ignoreOverride.kind } : null;
596
+ }
642
597
  let softHit = null;
643
- for (const source of sources) {
644
- const relativePath = relativePathWithinRoot(source.basePath, absolute);
645
- if (!relativePath) continue;
646
- const normalized = sep2 === "/" ? relativePath : relativePath.split(sep2).join("/");
647
- const candidate = directory ? `${normalized.replace(/\/$/, "")}/` : normalized;
648
- if (!source.matcher.ignores(candidate)) continue;
649
- if (source.kind === "lurusignore") return { kind: "lurusignore" };
650
- softHit = { kind: "gitignore" };
598
+ for (const root of this.workspaceRoots) {
599
+ const relative3 = relativePathWithinRoot(root, absolute);
600
+ if (!relative3) continue;
601
+ const parts = relative3.split(sep2);
602
+ let base = root;
603
+ for (let index2 = 0; index2 < parts.length; index2++) {
604
+ const candidate = parts.slice(index2).join("/") + suffix;
605
+ if (this.checkBlockedPaths(base)) break;
606
+ for (const [fileName, kind] of IGNORE_FILES) {
607
+ if (kind === "gitignore" && softHit) continue;
608
+ try {
609
+ const matcher = this.readIgnoreMatcher(join2(base, fileName));
610
+ if (!matcher?.ignores(candidate)) continue;
611
+ if (kind === "lurusignore") return { kind };
612
+ softHit = { kind };
613
+ } catch {
614
+ if (kind === "lurusignore") return { kind };
615
+ }
616
+ }
617
+ base = join2(base, parts[index2]);
618
+ }
651
619
  }
652
620
  return softHit;
653
621
  }
654
- getIgnoreSources() {
655
- if (this.ignoreSources !== null) return this.ignoreSources;
656
- const sources = [];
657
- const seenDirs = /* @__PURE__ */ new Set();
658
- const counter = { count: 0, capped: false };
659
- for (const root of this.workspaceRoots) {
660
- this.scanIgnoreFiles(root, 0, sources, seenDirs, counter);
661
- }
662
- if (counter.capped) {
663
- this.ignoreScanNotice?.(
664
- `Ignore-file scan reached its ${MAX_IGNORE_FILES}-file limit. Some nested ignore rules could not be loaded.`
665
- );
622
+ readIgnoreMatcher(filePath) {
623
+ let info = lstatSync(filePath, { bigint: true, throwIfNoEntry: false });
624
+ if (!info) {
625
+ this.ignoreMatchers.delete(filePath);
626
+ return null;
666
627
  }
667
- this.ignoreSources = sources;
668
- return sources;
669
- }
670
- scanIgnoreFiles(dir, depth, out, seenDirs, counter) {
671
- if (depth > IGNORE_SCAN_MAX_DEPTH) return;
672
- if (counter.capped) return;
673
- const canonical = this.resolvePhysicalPath(dir);
674
- if (seenDirs.has(canonical)) return;
675
- seenDirs.add(canonical);
676
- for (const fileName of [".lurusignore", ".gitignore"]) {
677
- if (counter.count >= MAX_IGNORE_FILES) {
678
- counter.capped = true;
679
- return;
680
- }
681
- const ignorePath = join2(dir, fileName);
682
- if (!existsSync(ignorePath) || this.ignoreReadAllowed?.(ignorePath) === false)
683
- continue;
684
- try {
685
- const content = readFileSync(ignorePath, "utf-8");
686
- const patterns = content.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#"));
687
- if (patterns.length > 0) {
688
- out.push({
689
- basePath: dir,
690
- kind: fileName === ".lurusignore" ? "lurusignore" : "gitignore",
691
- matcher: ignore().add(patterns)
692
- });
693
- counter.count++;
694
- }
695
- } catch (error) {
696
- if (this.ignoreReadAllowed && fileName === ".lurusignore" && error.code !== "ENOENT") {
697
- throw new Error("Index ignore policy could not be read.", {
698
- cause: error
699
- });
700
- }
701
- }
628
+ if (this.ignoreReadAllowed?.(filePath) === false) return null;
629
+ const physicalDirectory = realpathSync2(dirname2(filePath));
630
+ if (!this.isWithinWorkspace(physicalDirectory) || this.checkBlockedPaths(physicalDirectory)) {
631
+ return null;
702
632
  }
703
- let entries;
633
+ let physicalPath = join2(physicalDirectory, basename2(filePath));
634
+ if (info.isSymbolicLink()) {
635
+ physicalPath = realpathSync2(physicalPath);
636
+ if (!this.isWithinWorkspace(physicalPath) || this.checkBlockedPaths(physicalPath) || this.checkSensitiveFile(physicalPath) || this.ignoreReadAllowed?.(physicalPath) === false)
637
+ throw new Error("Ignore policy points to an unauthorized file.");
638
+ info = lstatSync(physicalPath, { bigint: true });
639
+ }
640
+ if (!info.isFile()) throw new Error("Ignore policy is not a regular file.");
641
+ const version = `${physicalPath}:${info.dev}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}`;
642
+ const cached = this.ignoreMatchers.get(filePath);
643
+ if (cached?.version === version) return cached.matcher;
644
+ const flags = process.platform === "win32" ? constants.O_RDONLY : constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW;
645
+ const fd = openSync(physicalPath, flags);
646
+ let matcher;
704
647
  try {
705
- entries = readdirSync(dir, { withFileTypes: true });
706
- } catch {
707
- return;
648
+ const current = fstatSync(fd, { bigint: true });
649
+ if (!current.isFile() || current.dev !== info.dev || current.ino !== info.ino)
650
+ throw new Error("Ignore policy changed while opening it.");
651
+ const patterns = readFileSync(fd, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
652
+ matcher = ignore().add(patterns);
653
+ } finally {
654
+ closeSync(fd);
708
655
  }
709
- for (const entry of entries) {
710
- if (!entry.isDirectory()) continue;
711
- const name = entry.name;
712
- if (IGNORE_SCAN_SKIP_DIRS.has(name)) continue;
713
- this.scanIgnoreFiles(join2(dir, name), depth + 1, out, seenDirs, counter);
656
+ if (!cached && this.ignoreMatchers.size >= MAX_CACHED_IGNORE_MATCHERS) {
657
+ const oldest = this.ignoreMatchers.keys().next().value;
658
+ if (oldest !== void 0) this.ignoreMatchers.delete(oldest);
714
659
  }
660
+ this.ignoreMatchers.set(filePath, { version, matcher });
661
+ return matcher;
715
662
  }
716
663
  resolvePhysicalPath(absolutePath) {
717
664
  try {
@@ -812,11 +759,11 @@ function isIndexFailureRetryable(failure) {
812
759
  );
813
760
  }
814
761
  function indexRetryDelay(failures, failure, baseDelayMs = 3e4) {
815
- const delay = Math.min(
762
+ const delay2 = Math.min(
816
763
  baseDelayMs * 2 ** Math.min(Math.max(failures - 1, 0), 4),
817
764
  baseDelayMs * 10
818
765
  );
819
- return Math.max(delay, failure?.retryAfterMs ?? 0);
766
+ return Math.max(delay2, failure?.retryAfterMs ?? 0);
820
767
  }
821
768
  function indexHttpFailure(status, headers, now = Date.now()) {
822
769
  if (status === 429) {
@@ -1060,27 +1007,269 @@ function isIndexingWorkspaceAllowed(workspaceRoot, options2 = {}) {
1060
1007
  }
1061
1008
 
1062
1009
  // src/services/code-index.service.ts
1010
+ import { readdir as readdir4, stat as stat3, mkdir as mkdir4, rm as rm2, unlink as unlink4 } from "node:fs/promises";
1011
+ import { existsSync as existsSync3 } from "node:fs";
1012
+
1013
+ // src/services/index-runtime-lease.ts
1014
+ import { mkdir } from "node:fs/promises";
1015
+ import { join as join4 } from "node:path";
1016
+
1017
+ // src/utils/permission-store-lock.ts
1018
+ import { randomBytes } from "node:crypto";
1063
1019
  import {
1064
- readdir as readdir3,
1065
- readFile as readFile4,
1066
- stat as stat3,
1067
- writeFile as writeFile2,
1068
- mkdir as mkdir3,
1069
- rm,
1070
- unlink as unlink3,
1020
+ link,
1071
1021
  lstat,
1072
- link
1022
+ open,
1023
+ readdir,
1024
+ realpath as realpath2,
1025
+ unlink
1073
1026
  } from "node:fs/promises";
1074
- import { existsSync as existsSync5 } from "node:fs";
1075
- import { join as join6, extname as extname2, basename as basename5, resolve as resolve4, isAbsolute as isAbsolute2 } from "node:path";
1076
- import { createHash as createHash2, randomBytes as randomBytes4 } from "node:crypto";
1027
+ import { basename as basename3, dirname as dirname3, join as join3, resolve as resolve4 } from "node:path";
1028
+ import { setTimeout as delay } from "node:timers/promises";
1029
+ var PermissionStoreLockError = class extends Error {
1030
+ constructor(code, options2) {
1031
+ super(
1032
+ code === "lock-contention" ? "Permission storage lock is occupied or its owner cannot be verified. Stop all CLI writers before inspecting or quarantining an ownerless legacy lock; preserve the data document." : "Permission storage lock could not be updated safely.",
1033
+ options2
1034
+ );
1035
+ this.code = code;
1036
+ this.name = "PermissionStoreLockError";
1037
+ }
1038
+ code;
1039
+ };
1040
+ function sameFile(left, right) {
1041
+ return left.dev === right.dev && left.ino === right.ino;
1042
+ }
1043
+ function processIsAlive(pid) {
1044
+ try {
1045
+ process.kill(pid, 0);
1046
+ return true;
1047
+ } catch (error) {
1048
+ return error.code !== "ESRCH";
1049
+ }
1050
+ }
1051
+ function parseData(raw) {
1052
+ try {
1053
+ const value = JSON.parse(raw);
1054
+ if (!value || typeof value !== "object" || Array.isArray(value))
1055
+ return null;
1056
+ const data = value;
1057
+ if (Object.keys(data).length !== 2 || typeof data.token !== "string" || !/^[a-f0-9]{48}$/.test(data.token) || !Number.isSafeInteger(data.pid) || data.pid <= 0)
1058
+ return null;
1059
+ return { token: data.token, pid: data.pid };
1060
+ } catch {
1061
+ return null;
1062
+ }
1063
+ }
1064
+ async function readData(handle) {
1065
+ const bytes = Buffer.alloc(4097);
1066
+ const { bytesRead } = await handle.read(bytes, 0, bytes.length, 0);
1067
+ return bytesRead <= 4096 ? parseData(bytes.subarray(0, bytesRead).toString("utf8")) : null;
1068
+ }
1069
+ var PermissionStoreLock = class {
1070
+ constructor(path2, options2 = {}) {
1071
+ this.path = path2;
1072
+ this.options = options2;
1073
+ this.root = dirname3(path2);
1074
+ this.attempts = options2.attempts ?? 50;
1075
+ this.retryDelayMs = options2.retryDelayMs ?? 20;
1076
+ this.isAlive = options2.isProcessAlive ?? processIsAlive;
1077
+ }
1078
+ path;
1079
+ options;
1080
+ attempts;
1081
+ retryDelayMs;
1082
+ isAlive;
1083
+ root;
1084
+ async acquire() {
1085
+ await this.assertRoot();
1086
+ for (let attempt = 0; attempt < this.attempts; attempt++) {
1087
+ const token = randomBytes(24).toString("hex");
1088
+ const temp = this.alias("publish");
1089
+ let handle;
1090
+ let lease;
1091
+ let created = false;
1092
+ try {
1093
+ handle = await open(temp, "wx", 384);
1094
+ created = true;
1095
+ await handle.writeFile(JSON.stringify({ token, pid: process.pid }));
1096
+ await handle.sync();
1097
+ const stat4 = await handle.stat({ bigint: true });
1098
+ await handle.close();
1099
+ handle = void 0;
1100
+ await link(temp, this.path);
1101
+ lease = { path: this.path, token, dev: stat4.dev, ino: stat4.ino };
1102
+ await this.phase("published");
1103
+ await unlink(temp);
1104
+ created = false;
1105
+ await this.sync();
1106
+ return lease;
1107
+ } catch (error) {
1108
+ await handle?.close().catch(() => void 0);
1109
+ if (created) await unlink(temp).catch(() => void 0);
1110
+ if (lease) await this.release(lease).catch(() => void 0);
1111
+ if (error.code !== "EEXIST")
1112
+ throw new PermissionStoreLockError("persistence-failed", {
1113
+ cause: error
1114
+ });
1115
+ }
1116
+ if (await this.recover()) continue;
1117
+ await delay(this.retryDelayMs);
1118
+ }
1119
+ throw new PermissionStoreLockError("lock-contention");
1120
+ }
1121
+ async release(lease) {
1122
+ if (lease.path !== this.path)
1123
+ throw new PermissionStoreLockError("persistence-failed");
1124
+ for (let attempt = 0; attempt < this.attempts; attempt++) {
1125
+ if (await this.remove(lease, false)) return;
1126
+ const current = await this.observe();
1127
+ if (current?.token !== lease.token || !sameFile(current, lease)) return;
1128
+ await delay(this.retryDelayMs);
1129
+ }
1130
+ throw new PermissionStoreLockError("lock-contention");
1131
+ }
1132
+ async assertRoot() {
1133
+ const stat4 = await lstat(this.root);
1134
+ if (!stat4.isDirectory() || stat4.isSymbolicLink() || await realpath2(this.root) !== resolve4(this.root))
1135
+ throw new PermissionStoreLockError("persistence-failed");
1136
+ }
1137
+ alias(kind) {
1138
+ return join3(
1139
+ this.root,
1140
+ `.${basename3(this.path)}.${kind}.${process.pid}.${randomBytes(8).toString("hex")}.tmp`
1141
+ );
1142
+ }
1143
+ phase(phase) {
1144
+ return this.options.onPhase?.(phase) ?? Promise.resolve();
1145
+ }
1146
+ sync() {
1147
+ return this.options.syncDirectory?.() ?? Promise.resolve();
1148
+ }
1149
+ async observe() {
1150
+ let handle;
1151
+ try {
1152
+ const stat4 = await lstat(this.path, { bigint: true });
1153
+ if (!stat4.isFile() || stat4.isSymbolicLink()) return null;
1154
+ handle = await open(this.path, "r");
1155
+ const pinned = await handle.stat({ bigint: true });
1156
+ if (!pinned.isFile() || !sameFile(stat4, pinned)) return null;
1157
+ const data = await readData(handle);
1158
+ return data ? { path: this.path, ...data, dev: pinned.dev, ino: pinned.ino } : null;
1159
+ } catch (error) {
1160
+ if (error.code === "ENOENT") return null;
1161
+ throw new PermissionStoreLockError("persistence-failed", {
1162
+ cause: error
1163
+ });
1164
+ } finally {
1165
+ await handle?.close();
1166
+ }
1167
+ }
1168
+ async recover() {
1169
+ const lease = await this.observe();
1170
+ if (!lease || this.isAlive(lease.pid)) return false;
1171
+ return this.remove(lease, true);
1172
+ }
1173
+ async deadAliasCount(claim, pinned, data) {
1174
+ const prefix = `.${basename3(this.path)}.`;
1175
+ let count = 0n;
1176
+ for (const name of await readdir(this.root)) {
1177
+ if (!name.startsWith(prefix)) continue;
1178
+ const path2 = join3(this.root, name);
1179
+ if (path2 === claim) continue;
1180
+ let stat4;
1181
+ try {
1182
+ stat4 = await lstat(path2, { bigint: true });
1183
+ } catch (error) {
1184
+ if (error.code === "ENOENT") continue;
1185
+ throw error;
1186
+ }
1187
+ if (!sameFile(stat4, pinned)) continue;
1188
+ const match = /^(publish|claim)\.([1-9][0-9]*)\.[a-f0-9]{16}\.tmp$/.exec(
1189
+ name.slice(prefix.length)
1190
+ );
1191
+ if (!stat4.isFile() || stat4.isSymbolicLink() || !match) return null;
1192
+ const pid = Number(match[2]);
1193
+ if (!Number.isSafeInteger(pid) || this.isAlive(pid) || match[1] === "publish" && pid !== data.pid)
1194
+ return null;
1195
+ count++;
1196
+ }
1197
+ return count;
1198
+ }
1199
+ async remove(lease, deadRequired) {
1200
+ const claim = this.alias("claim");
1201
+ let created = false;
1202
+ let handle;
1203
+ try {
1204
+ await this.assertRoot();
1205
+ const observed = await lstat(this.path, { bigint: true });
1206
+ if (!observed.isFile() || observed.isSymbolicLink() || !sameFile(observed, lease))
1207
+ return false;
1208
+ await link(this.path, claim);
1209
+ created = true;
1210
+ handle = await open(claim, "r");
1211
+ const pinned = await handle.stat({ bigint: true });
1212
+ const data = await readData(handle);
1213
+ if (!sameFile(pinned, lease) || data?.token !== lease.token || deadRequired && this.isAlive(data.pid))
1214
+ return false;
1215
+ await this.phase("claimed");
1216
+ const deadAliases = await this.deadAliasCount(claim, pinned, data);
1217
+ if (deadAliases === null) return false;
1218
+ const final = await handle.stat({ bigint: true });
1219
+ if (final.nlink !== 2n + deadAliases) return false;
1220
+ await this.phase("links-verified");
1221
+ const current = await lstat(this.path, { bigint: true });
1222
+ if (!current.isFile() || current.isSymbolicLink() || !sameFile(current, pinned) || (await readData(handle))?.token !== lease.token)
1223
+ return false;
1224
+ await this.phase("identity-verified");
1225
+ await unlink(this.path);
1226
+ await this.phase("unlinked");
1227
+ await this.sync();
1228
+ return true;
1229
+ } catch (error) {
1230
+ if (error.code === "ENOENT") return false;
1231
+ throw new PermissionStoreLockError("persistence-failed", {
1232
+ cause: error
1233
+ });
1234
+ } finally {
1235
+ await handle?.close();
1236
+ if (created) await unlink(claim).catch(() => void 0);
1237
+ }
1238
+ }
1239
+ };
1240
+
1241
+ // src/services/index-runtime-lease.ts
1242
+ function indexLock(workspaceRoot) {
1243
+ return new PermissionStoreLock(
1244
+ join4(workspaceRoot, ".lurus", "index-refresh.lock"),
1245
+ { attempts: 2, retryDelayMs: 1 }
1246
+ );
1247
+ }
1248
+ async function acquireIndexLease(workspaceRoot) {
1249
+ await mkdir(join4(workspaceRoot, ".lurus"), { recursive: true });
1250
+ try {
1251
+ const proof = await indexLock(workspaceRoot).acquire();
1252
+ return { pid: process.pid, ownerToken: proof.token, proof };
1253
+ } catch (error) {
1254
+ if (error instanceof PermissionStoreLockError && error.code === "lock-contention")
1255
+ return null;
1256
+ throw error;
1257
+ }
1258
+ }
1259
+ async function releaseIndexLease(workspaceRoot, lease) {
1260
+ await indexLock(workspaceRoot).release(lease.proof);
1261
+ }
1262
+
1263
+ // src/services/code-index.service.ts
1264
+ import { join as join8, extname as extname2, basename as basename6, resolve as resolve5, isAbsolute as isAbsolute2 } from "node:path";
1265
+ import { createHash as createHash3, randomBytes as randomBytes5 } from "node:crypto";
1077
1266
 
1078
1267
  // src/services/index-source-reader.ts
1079
- import { open, stat, rename, unlink, readdir } from "node:fs/promises";
1080
- import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
1081
- import { createHash, randomBytes } from "node:crypto";
1268
+ import { open as open2, stat, rename, unlink as unlink2, readdir as readdir2 } from "node:fs/promises";
1269
+ import { existsSync, readFileSync as readFileSync3 } from "node:fs";
1270
+ import { createHash, randomBytes as randomBytes2 } from "node:crypto";
1082
1271
  import { isDeepStrictEqual } from "node:util";
1083
- import { join as join3, relative as relative2 } from "node:path";
1272
+ import { join as join5, relative as relative2 } from "node:path";
1084
1273
  import ignore3 from "ignore";
1085
1274
  async function scanIndexSourcePaths(workspaceRoot, policy, directoryAllowed, fileAllowed, diagnostics, onProgress) {
1086
1275
  const candidates = [];
@@ -1098,7 +1287,7 @@ async function scanIndexSourcePaths(workspaceRoot, policy, directoryAllowed, fil
1098
1287
  diagnostics.directories++;
1099
1288
  return {
1100
1289
  directory,
1101
- entries: await readdir(directory, { withFileTypes: true })
1290
+ entries: await readdir2(directory, { withFileTypes: true })
1102
1291
  };
1103
1292
  } catch (error) {
1104
1293
  if (directory === workspaceRoot) throw error;
@@ -1110,7 +1299,7 @@ async function scanIndexSourcePaths(workspaceRoot, policy, directoryAllowed, fil
1110
1299
  );
1111
1300
  const targets = listings.flatMap(
1112
1301
  ({ directory, entries }) => entries.flatMap((entry) => {
1113
- const absPath = join3(directory, entry.name);
1302
+ const absPath = join5(directory, entry.name);
1114
1303
  const relPath = indexPath(absPath);
1115
1304
  return (entry.isDirectory() ? directoryAllowed(entry.name, relPath) : entry.isFile() && fileAllowed(relPath)) ? [{ absPath, relPath, directory: entry.isDirectory() }] : [];
1116
1305
  })
@@ -1148,11 +1337,11 @@ async function scanIndexSourcePaths(workspaceRoot, policy, directoryAllowed, fil
1148
1337
  return { candidates, unavailablePaths };
1149
1338
  }
1150
1339
  async function writeIndexFile(filePath, content, cancelled, beforeCommit) {
1151
- const temporaryPath = `${filePath}.${randomBytes(4).toString("hex")}.tmp`;
1340
+ const temporaryPath = `${filePath}.${randomBytes2(4).toString("hex")}.tmp`;
1152
1341
  let handle;
1153
1342
  let owned = false;
1154
1343
  try {
1155
- handle = await open(temporaryPath, "wx");
1344
+ handle = await open2(temporaryPath, "wx");
1156
1345
  owned = true;
1157
1346
  await handle.writeFile(content);
1158
1347
  await handle.close();
@@ -1163,7 +1352,7 @@ async function writeIndexFile(filePath, content, cancelled, beforeCommit) {
1163
1352
  await rename(temporaryPath, filePath);
1164
1353
  } finally {
1165
1354
  await handle?.close().catch(() => void 0);
1166
- if (owned) await unlink(temporaryPath).catch(() => void 0);
1355
+ if (owned) await unlink2(temporaryPath).catch(() => void 0);
1167
1356
  }
1168
1357
  }
1169
1358
  async function indexManifestMatches(filePath, maxBytes, current, project) {
@@ -1174,7 +1363,7 @@ async function indexManifestMatches(filePath, maxBytes, current, project) {
1174
1363
  );
1175
1364
  }
1176
1365
  async function readStableIndexFile(filePath, maxBytes, minimumMtimeMs) {
1177
- const handle = await open(filePath, "r");
1366
+ const handle = await open2(filePath, "r");
1178
1367
  try {
1179
1368
  const before = await handle.stat();
1180
1369
  if (!before.isFile() || !Number.isSafeInteger(before.size) || before.size < 0 || before.size > maxBytes || minimumMtimeMs !== void 0 && before.mtimeMs < minimumMtimeMs) {
@@ -1205,7 +1394,7 @@ async function readStableIndexFile(filePath, maxBytes, minimumMtimeMs) {
1205
1394
  async function readIndexSourceFile(file, policy, maxBytes, isReadDenied) {
1206
1395
  const allowed = async () => await policy.validate([file.absPath]) && !isReadDenied?.(file.absPath) && !isReadDenied?.(file.relPath);
1207
1396
  if (!await allowed()) return null;
1208
- const handle = await open(file.absPath, "r");
1397
+ const handle = await open2(file.absPath, "r");
1209
1398
  try {
1210
1399
  const before = await handle.stat();
1211
1400
  if (!before.isFile() || before.size <= 0 || before.size > maxBytes)
@@ -1253,8 +1442,8 @@ function hashContent(content) {
1253
1442
  async function loadIndexIgnorePatterns(workspaceRoot, policy) {
1254
1443
  const loadMatcher = async (fileName) => {
1255
1444
  const matcher = ignore3();
1256
- const filePath = join3(workspaceRoot, fileName);
1257
- if (!existsSync2(filePath)) return matcher;
1445
+ const filePath = join5(workspaceRoot, fileName);
1446
+ if (!existsSync(filePath)) return matcher;
1258
1447
  if (!await policy.isReadAllowedAsync(filePath)) {
1259
1448
  if (fileName === ".lurusignore")
1260
1449
  throw new Error("Index ignore policy could not be read.");
@@ -1416,8 +1605,15 @@ var MAX_INDEX_EMBEDDING_RESPONSE_BYTES = 8 * 1024 * 1024;
1416
1605
 
1417
1606
  // src/utils/output.ts
1418
1607
  import pc from "picocolors";
1608
+ import { AsyncLocalStorage } from "node:async_hooks";
1609
+ var outputSinks = new AsyncLocalStorage();
1419
1610
  var useStderr = false;
1420
- function write(message) {
1611
+ function write(message, level = "log") {
1612
+ const sink = outputSinks.getStore();
1613
+ if (sink && !useStderr) {
1614
+ sink(message, level);
1615
+ return;
1616
+ }
1421
1617
  if (useStderr) {
1422
1618
  process.stderr.write(message + "\n");
1423
1619
  } else {
@@ -1426,20 +1622,40 @@ function write(message) {
1426
1622
  }
1427
1623
  var output = {
1428
1624
  log: (message) => write(message),
1429
- info: (message) => write(pc.blue(message)),
1430
- success: (message) => write(pc.green("\u2713 ") + message),
1431
- warn: (message) => console.warn(pc.yellow("\u26A0 ") + message),
1432
- error: (message) => console.error(pc.red("\u2717 ") + message),
1433
- dim: (message) => write(pc.dim(message)),
1434
- bold: (message) => write(pc.bold(message)),
1625
+ info: (message) => write(pc.blue(message), "info"),
1626
+ success: (message) => write(pc.green("\u2713 ") + message, "success"),
1627
+ warn: (message) => {
1628
+ const sink = outputSinks.getStore();
1629
+ if (sink && !useStderr) sink(message, "warn");
1630
+ else console.warn(pc.yellow("\u26A0 ") + message);
1631
+ },
1632
+ error: (message) => {
1633
+ const sink = outputSinks.getStore();
1634
+ if (sink && !useStderr) sink(message, "error");
1635
+ else console.error(pc.red("\u2717 ") + message);
1636
+ },
1637
+ dim: (message) => write(pc.dim(message), "dim"),
1638
+ bold: (message) => write(pc.bold(message), "bold"),
1435
1639
  blank: () => write("")
1436
1640
  };
1437
1641
 
1438
1642
  // src/services/index-encryption.service.ts
1439
- import { randomBytes as randomBytes2, createCipheriv, createDecipheriv } from "node:crypto";
1440
- import { readFile as readFile2, writeFile, mkdir } from "node:fs/promises";
1441
- import { existsSync as existsSync3, readFileSync as readFileSync4 } from "node:fs";
1442
- import { join as join4, dirname as dirname3 } from "node:path";
1643
+ import {
1644
+ randomBytes as randomBytes3,
1645
+ createCipheriv,
1646
+ createDecipheriv,
1647
+ createHash as createHash2
1648
+ } from "node:crypto";
1649
+ import {
1650
+ readFile as readFile2,
1651
+ writeFile,
1652
+ mkdir as mkdir2,
1653
+ open as open3,
1654
+ link as link2,
1655
+ rm,
1656
+ lstat as lstat2
1657
+ } from "node:fs/promises";
1658
+ import { join as join6, dirname as dirname4 } from "node:path";
1443
1659
  import { homedir as homedir4 } from "node:os";
1444
1660
  var ALGORITHM = "aes-256-gcm";
1445
1661
  var IV_LENGTH = 16;
@@ -1448,33 +1664,53 @@ var KEY_LENGTH = 32;
1448
1664
  var IndexEncryptionService = class {
1449
1665
  key = null;
1450
1666
  keyPath;
1667
+ legacyKeyPath;
1451
1668
  constructor(workspaceRoot, homeDir = homedir4()) {
1452
- const projectHash = Buffer.from(workspaceRoot).toString("base64url").slice(0, 16);
1453
- this.keyPath = join4(homeDir, ".lurus", "keys", `${projectHash}.json`);
1669
+ const projectHash = createHash2("sha256").update(workspaceRoot).digest("hex");
1670
+ const legacyHash = Buffer.from(workspaceRoot).toString("base64url").slice(0, 16);
1671
+ const root = join6(homeDir, ".lurus", "keys");
1672
+ this.keyPath = join6(root, `${projectHash}.json`);
1673
+ this.legacyKeyPath = join6(root, `${legacyHash}.json`);
1454
1674
  }
1455
1675
  async ensureKey() {
1456
1676
  if (this.key) return;
1457
- if (existsSync3(this.keyPath)) {
1677
+ const existing = await this.readKey(this.keyPath);
1678
+ if (existing) {
1679
+ this.key = existing;
1680
+ return;
1681
+ }
1682
+ const candidate = await this.readKey(this.legacyKeyPath) ?? randomBytes3(KEY_LENGTH);
1683
+ await mkdir2(dirname4(this.keyPath), { recursive: true, mode: 448 });
1684
+ const temporary = `${this.keyPath}.${randomBytes3(12).toString("hex")}.tmp`;
1685
+ const handle = await open3(temporary, "wx", 384);
1686
+ try {
1458
1687
  try {
1459
- const raw = readFileSync4(this.keyPath, "utf-8");
1460
- const keyFile2 = JSON.parse(raw);
1461
- this.key = Buffer.from(keyFile2.indexEncryptionKey, "base64");
1462
- if (this.key.length === KEY_LENGTH) return;
1463
- } catch {
1688
+ await handle.writeFile(
1689
+ JSON.stringify({ indexEncryptionKey: candidate.toString("base64") })
1690
+ );
1691
+ await handle.sync();
1692
+ } finally {
1693
+ await handle.close();
1694
+ }
1695
+ try {
1696
+ await link2(temporary, this.keyPath);
1697
+ this.key = candidate;
1698
+ } catch (error) {
1699
+ if (error.code !== "EEXIST") throw error;
1700
+ const published = await this.readKey(this.keyPath);
1701
+ if (!published)
1702
+ throw new Error("Index key publication failed. Retry indexing.", {
1703
+ cause: error
1704
+ });
1705
+ this.key = published;
1464
1706
  }
1707
+ } finally {
1708
+ await rm(temporary, { force: true }).catch(() => void 0);
1465
1709
  }
1466
- this.key = randomBytes2(KEY_LENGTH);
1467
- const keyFile = {
1468
- indexEncryptionKey: this.key.toString("base64")
1469
- };
1470
- await mkdir(dirname3(this.keyPath), { recursive: true });
1471
- await writeFile(this.keyPath, JSON.stringify(keyFile, null, 2), {
1472
- mode: 384
1473
- });
1474
1710
  }
1475
1711
  encrypt(data) {
1476
1712
  if (!this.key) throw new Error("Encryption key not initialized");
1477
- const iv = randomBytes2(IV_LENGTH);
1713
+ const iv = randomBytes3(IV_LENGTH);
1478
1714
  const cipher = createCipheriv(ALGORITHM, this.key, iv, {
1479
1715
  authTagLength: AUTH_TAG_LENGTH
1480
1716
  });
@@ -1504,7 +1740,7 @@ var IndexEncryptionService = class {
1504
1740
  }
1505
1741
  async encryptFile(plaintext, outputPath) {
1506
1742
  const encrypted = this.encrypt(plaintext);
1507
- await mkdir(dirname3(outputPath), { recursive: true });
1743
+ await mkdir2(dirname4(outputPath), { recursive: true });
1508
1744
  await writeFile(outputPath, encrypted);
1509
1745
  }
1510
1746
  async decryptFile(inputPath) {
@@ -1514,21 +1750,54 @@ var IndexEncryptionService = class {
1514
1750
  isAvailable() {
1515
1751
  return this.key !== null;
1516
1752
  }
1753
+ async readKey(path2) {
1754
+ let bytes;
1755
+ try {
1756
+ bytes = await readFile2(path2, "utf8");
1757
+ } catch (error) {
1758
+ if (error.code !== "ENOENT") throw error;
1759
+ try {
1760
+ await lstat2(path2);
1761
+ } catch (entryError) {
1762
+ if (entryError.code === "ENOENT")
1763
+ return null;
1764
+ throw entryError;
1765
+ }
1766
+ throw new Error(
1767
+ "The existing index key is unavailable. Restore access to the original key.",
1768
+ { cause: error }
1769
+ );
1770
+ }
1771
+ let value;
1772
+ try {
1773
+ value = JSON.parse(bytes);
1774
+ } catch (cause) {
1775
+ throw new Error("Invalid index key. The original key was not changed.", {
1776
+ cause
1777
+ });
1778
+ }
1779
+ if (!value || typeof value !== "object" || !("indexEncryptionKey" in value) || typeof value.indexEncryptionKey !== "string")
1780
+ throw new Error("Invalid index key. The original key was not changed.");
1781
+ const key = Buffer.from(value.indexEncryptionKey, "base64");
1782
+ if (key.length !== KEY_LENGTH || key.toString("base64") !== value.indexEncryptionKey)
1783
+ throw new Error("Invalid index key. The original key was not changed.");
1784
+ return key;
1785
+ }
1517
1786
  };
1518
1787
 
1519
1788
  // src/services/embedding-cache.service.ts
1520
- import { randomBytes as randomBytes3 } from "node:crypto";
1521
- import { existsSync as existsSync4 } from "node:fs";
1789
+ import { randomBytes as randomBytes4 } from "node:crypto";
1790
+ import { existsSync as existsSync2 } from "node:fs";
1522
1791
  import {
1523
- mkdir as mkdir2,
1792
+ mkdir as mkdir3,
1524
1793
  readFile as readFile3,
1525
- readdir as readdir2,
1794
+ readdir as readdir3,
1526
1795
  rename as rename2,
1527
- unlink as unlink2,
1528
- open as open2,
1796
+ unlink as unlink3,
1797
+ open as open4,
1529
1798
  stat as stat2
1530
1799
  } from "node:fs/promises";
1531
- import { basename as basename3, join as join5 } from "node:path";
1800
+ import { basename as basename4, join as join7 } from "node:path";
1532
1801
  var CACHE_VERSION = 1;
1533
1802
  var CACHE_MANIFEST = "manifest.json";
1534
1803
  var CACHE_FILE_PATTERN = /^(metadata|vectors)\.[a-f0-9]+\.(enc|bin)$/;
@@ -1536,7 +1805,7 @@ var EmbeddingCacheService = class {
1536
1805
  constructor(indexDir, encryption, maxVectorBytes) {
1537
1806
  this.encryption = encryption;
1538
1807
  this.maxVectorBytes = maxVectorBytes;
1539
- this.cacheDir = join5(indexDir, "cache");
1808
+ this.cacheDir = join7(indexDir, "cache");
1540
1809
  }
1541
1810
  encryption;
1542
1811
  maxVectorBytes;
@@ -1544,8 +1813,8 @@ var EmbeddingCacheService = class {
1544
1813
  writeRevision = 0;
1545
1814
  pendingCheckpoint;
1546
1815
  async load() {
1547
- const manifestPath = join5(this.cacheDir, CACHE_MANIFEST);
1548
- if (!existsSync4(manifestPath)) return null;
1816
+ const manifestPath = join7(this.cacheDir, CACHE_MANIFEST);
1817
+ if (!existsSync2(manifestPath)) return null;
1549
1818
  try {
1550
1819
  const manifest = JSON.parse(
1551
1820
  await readFile3(manifestPath, "utf-8")
@@ -1556,12 +1825,12 @@ var EmbeddingCacheService = class {
1556
1825
  const expectedBytes = manifest.entryCount * manifest.dimensions * Float32Array.BYTES_PER_ELEMENT;
1557
1826
  if (!Number.isSafeInteger(expectedBytes) || expectedBytes > this.maxVectorBytes)
1558
1827
  return null;
1559
- const vectorPath = join5(this.cacheDir, manifest.vectorsFile);
1828
+ const vectorPath = join7(this.cacheDir, manifest.vectorsFile);
1560
1829
  const before = await stat2(vectorPath);
1561
1830
  if (!before.isFile() || before.size !== expectedBytes) return null;
1562
1831
  const metadata = JSON.parse(
1563
1832
  await this.encryption.decryptFile(
1564
- join5(this.cacheDir, manifest.metadataFile)
1833
+ join7(this.cacheDir, manifest.metadataFile)
1565
1834
  )
1566
1835
  );
1567
1836
  if (!Array.isArray(metadata.keys) || metadata.keys.length !== manifest.entryCount || new Set(metadata.keys).size !== metadata.keys.length) {
@@ -1613,8 +1882,8 @@ var EmbeddingCacheService = class {
1613
1882
  try {
1614
1883
  await Promise.race([
1615
1884
  save,
1616
- new Promise((resolve5) => {
1617
- timer = setTimeout(resolve5, 500);
1885
+ new Promise((resolve6) => {
1886
+ timer = setTimeout(resolve6, 500);
1618
1887
  })
1619
1888
  ]);
1620
1889
  } finally {
@@ -1656,7 +1925,7 @@ var EmbeddingCacheService = class {
1656
1925
  ).equals(Buffer.from(old.buffer, old.byteOffset, old.byteLength));
1657
1926
  })) {
1658
1927
  const manifest2 = await readFile3(
1659
- join5(this.cacheDir, CACHE_MANIFEST),
1928
+ join7(this.cacheDir, CACHE_MANIFEST),
1660
1929
  "utf-8"
1661
1930
  ).then((text) => JSON.parse(text)).catch(() => null);
1662
1931
  if (manifest2?.generation === previous.generation && current()) return;
@@ -1667,8 +1936,8 @@ var EmbeddingCacheService = class {
1667
1936
  const vector = selectedEntries.get(key);
1668
1937
  if (vector) vectors.set(vector, index2 * dimensions);
1669
1938
  });
1670
- await mkdir2(this.cacheDir, { recursive: true });
1671
- const generation = randomBytes3(8).toString("hex");
1939
+ await mkdir3(this.cacheDir, { recursive: true });
1940
+ const generation = randomBytes4(8).toString("hex");
1672
1941
  const metadataFile = `metadata.${generation}.enc`;
1673
1942
  const vectorsFile = `vectors.${generation}.bin`;
1674
1943
  const metadataBuffer = this.encryption.encrypt(
@@ -1691,7 +1960,7 @@ var EmbeddingCacheService = class {
1691
1960
  vectorsFile
1692
1961
  };
1693
1962
  await this.atomicWrite(
1694
- join5(this.cacheDir, CACHE_MANIFEST),
1963
+ join7(this.cacheDir, CACHE_MANIFEST),
1695
1964
  JSON.stringify(manifest),
1696
1965
  current
1697
1966
  );
@@ -1703,30 +1972,30 @@ var EmbeddingCacheService = class {
1703
1972
  }
1704
1973
  async clear() {
1705
1974
  this.writeRevision++;
1706
- if (!existsSync4(this.cacheDir)) return;
1707
- const entries = await readdir2(this.cacheDir).catch(() => []);
1975
+ if (!existsSync2(this.cacheDir)) return;
1976
+ const entries = await readdir3(this.cacheDir).catch(() => []);
1708
1977
  await Promise.all(
1709
1978
  entries.map(
1710
- (entry) => unlink2(join5(this.cacheDir, entry)).catch(() => void 0)
1979
+ (entry) => unlink3(join7(this.cacheDir, entry)).catch(() => void 0)
1711
1980
  )
1712
1981
  );
1713
1982
  }
1714
1983
  isSafeArtifactName(fileName) {
1715
- return basename3(fileName) === fileName && CACHE_FILE_PATTERN.test(fileName);
1984
+ return basename4(fileName) === fileName && CACHE_FILE_PATTERN.test(fileName);
1716
1985
  }
1717
1986
  async writeArtifact(fileName, content, isCurrent) {
1718
1987
  if (!this.isSafeArtifactName(fileName)) {
1719
1988
  throw new Error("Invalid embedding cache artifact name");
1720
1989
  }
1721
- await this.atomicWrite(join5(this.cacheDir, fileName), content, isCurrent);
1990
+ await this.atomicWrite(join7(this.cacheDir, fileName), content, isCurrent);
1722
1991
  }
1723
1992
  async atomicWrite(filePath, content, isCurrent) {
1724
- const temporaryPath = `${filePath}.${randomBytes3(4).toString("hex")}.tmp`;
1993
+ const temporaryPath = `${filePath}.${randomBytes4(4).toString("hex")}.tmp`;
1725
1994
  let handle;
1726
1995
  let owned = false;
1727
1996
  try {
1728
1997
  if (!isCurrent()) return;
1729
- handle = await open2(temporaryPath, "wx");
1998
+ handle = await open4(temporaryPath, "wx");
1730
1999
  owned = true;
1731
2000
  await handle.writeFile(content);
1732
2001
  await handle.close();
@@ -1735,24 +2004,24 @@ var EmbeddingCacheService = class {
1735
2004
  await rename2(temporaryPath, filePath);
1736
2005
  } finally {
1737
2006
  await handle?.close().catch(() => void 0);
1738
- if (owned) await unlink2(temporaryPath).catch(() => void 0);
2007
+ if (owned) await unlink3(temporaryPath).catch(() => void 0);
1739
2008
  }
1740
2009
  }
1741
2010
  async removeUnreferencedArtifacts(referenced, isCurrent) {
1742
- const files = await readdir2(this.cacheDir).catch(() => []);
2011
+ const files = await readdir3(this.cacheDir).catch(() => []);
1743
2012
  if (!isCurrent()) return;
1744
2013
  await Promise.all(
1745
2014
  files.filter(
1746
2015
  (file) => CACHE_FILE_PATTERN.test(file) && !referenced.has(file)
1747
2016
  ).map(
1748
- (file) => unlink2(join5(this.cacheDir, file)).catch(() => void 0)
2017
+ (file) => unlink3(join7(this.cacheDir, file)).catch(() => void 0)
1749
2018
  )
1750
2019
  );
1751
2020
  }
1752
2021
  };
1753
2022
 
1754
2023
  // src/services/source-structure.service.ts
1755
- import { basename as basename4, extname } from "node:path";
2024
+ import { basename as basename5, extname } from "node:path";
1756
2025
  import ts from "./vendor/typescript.cjs";
1757
2026
  var LANGUAGE_MAP = {
1758
2027
  ".ts": "TypeScript",
@@ -1899,7 +2168,7 @@ var CODE_INDEX_BOUNDARY_LANGUAGES = /* @__PURE__ */ new Set([
1899
2168
  ]);
1900
2169
  function detectSourceLanguage(filePath) {
1901
2170
  const extension = extname(filePath).toLowerCase();
1902
- return LANGUAGE_LOOKUP[extension] ?? basename4(filePath);
2171
+ return LANGUAGE_LOOKUP[extension] ?? basename5(filePath);
1903
2172
  }
1904
2173
  function hasCodeIndexBoundarySupport(language) {
1905
2174
  return CODE_INDEX_BOUNDARY_LANGUAGES.has(language.split(" ")[0]);
@@ -1995,7 +2264,6 @@ var INDEX_VERSION = 3;
1995
2264
  var LEGACY_INDEX_VERSION = 2;
1996
2265
  var INDEX_DIR = ".lurus/index";
1997
2266
  var INDEX_MANIFEST_FILE = "index.json";
1998
- var INDEX_REFRESH_LEASE_FILE = "index-refresh.lock";
1999
2267
  var LEASE_POLL_INTERVAL_MS = 100;
2000
2268
  var LEASE_WAIT_TIMEOUT_MS = 3e4;
2001
2269
  var CANONICAL_EMBEDDING_MODEL = "Qwen3-Embedding-8B";
@@ -2199,7 +2467,7 @@ var CodeIndexService = class {
2199
2467
  this.httpClient = httpClient;
2200
2468
  this.now = now;
2201
2469
  this.workspaceRoot = workspaceRoot || process.cwd();
2202
- this.indexDir = join6(this.workspaceRoot, INDEX_DIR);
2470
+ this.indexDir = join8(this.workspaceRoot, INDEX_DIR);
2203
2471
  this.sourcePolicy = new IndexSourcePolicy(this.workspaceRoot);
2204
2472
  this.encryption = new IndexEncryptionService(
2205
2473
  this.workspaceRoot,
@@ -2242,6 +2510,7 @@ var CodeIndexService = class {
2242
2510
  limits;
2243
2511
  sourcePolicy;
2244
2512
  leaseListener;
2513
+ leaseBroker;
2245
2514
  getDiagnostics() {
2246
2515
  return { ...this.diagnostics };
2247
2516
  }
@@ -2251,6 +2520,9 @@ var CodeIndexService = class {
2251
2520
  onLease(listener) {
2252
2521
  this.leaseListener = listener;
2253
2522
  }
2523
+ setLeaseBroker(broker2) {
2524
+ this.leaseBroker = broker2;
2525
+ }
2254
2526
  isWorkspaceAllowed() {
2255
2527
  return isIndexingWorkspaceAllowed(this.workspaceRoot);
2256
2528
  }
@@ -2482,7 +2754,7 @@ var CodeIndexService = class {
2482
2754
  throw new Error("Timed out waiting for the workspace index lease.");
2483
2755
  }
2484
2756
  try {
2485
- await rm(this.indexDir, { recursive: true, force: true });
2757
+ await rm2(this.indexDir, { recursive: true, force: true });
2486
2758
  this.invalidationGeneration++;
2487
2759
  this.refreshAbortController?.abort();
2488
2760
  this.dirtyFiles.clear();
@@ -2498,8 +2770,8 @@ var CodeIndexService = class {
2498
2770
  async loadIndex(options2 = {}) {
2499
2771
  if (!this.isWorkspaceAllowed()) return null;
2500
2772
  this.lastLoadIndexFailure = null;
2501
- const manifestPath = join6(this.indexDir, INDEX_MANIFEST_FILE);
2502
- if (!existsSync5(manifestPath)) return null;
2773
+ const manifestPath = join8(this.indexDir, INDEX_MANIFEST_FILE);
2774
+ if (!existsSync3(manifestPath)) return null;
2503
2775
  try {
2504
2776
  await this.encryption.ensureKey();
2505
2777
  const manifestFile = await readStableIndexFile(
@@ -2515,7 +2787,22 @@ var CodeIndexService = class {
2515
2787
  }
2516
2788
  if (rawMeta["version"] === INDEX_VERSION) {
2517
2789
  const meta = this.projectIndexMeta(rawMeta, INDEX_VERSION);
2518
- const loaded = await this.loadV3Index(meta);
2790
+ let loaded;
2791
+ try {
2792
+ loaded = await this.loadV3Index(meta);
2793
+ } catch (error) {
2794
+ const latest = await readStableIndexFile(
2795
+ manifestPath,
2796
+ this.limits.maxSourceBytes
2797
+ );
2798
+ if (latest.buffer.equals(manifestFile.buffer)) throw error;
2799
+ loaded = await this.loadV3Index(
2800
+ this.projectIndexMeta(
2801
+ JSON.parse(latest.buffer.toString("utf8")),
2802
+ INDEX_VERSION
2803
+ )
2804
+ );
2805
+ }
2519
2806
  if (options2.migrateLegacy === true) {
2520
2807
  await this.removeLegacyV2Artifacts();
2521
2808
  }
@@ -2669,7 +2956,7 @@ var CodeIndexService = class {
2669
2956
  lease = acquisition.lease;
2670
2957
  let rebased = false;
2671
2958
  if (this.activeIndex?.meta.version === INDEX_VERSION && !await indexManifestMatches(
2672
- join6(this.indexDir, INDEX_MANIFEST_FILE),
2959
+ join8(this.indexDir, INDEX_MANIFEST_FILE),
2673
2960
  this.limits.maxSourceBytes,
2674
2961
  this.activeIndex.meta,
2675
2962
  (value) => this.projectIndexMeta(value, INDEX_VERSION)
@@ -2851,42 +3138,23 @@ var CodeIndexService = class {
2851
3138
  listener({ ...this.refreshState });
2852
3139
  }
2853
3140
  }
2854
- get leasePath() {
2855
- return join6(this.workspaceRoot, ".lurus", INDEX_REFRESH_LEASE_FILE);
2856
- }
2857
3141
  async acquireLease(signal, wait, timeoutMs = LEASE_WAIT_TIMEOUT_MS) {
2858
3142
  if (!this.isWorkspaceAllowed()) return { status: "cancelled" };
2859
- const leaseDirectory = join6(this.workspaceRoot, ".lurus");
2860
- await mkdir3(leaseDirectory, { recursive: true });
2861
3143
  const deadline = Date.now() + timeoutMs;
2862
3144
  while (!signal.aborted) {
2863
- const lease = {
2864
- pid: process.pid,
2865
- ownerToken: randomBytes4(16).toString("hex")
2866
- };
2867
- const temporaryPath = join6(
2868
- leaseDirectory,
2869
- `${INDEX_REFRESH_LEASE_FILE}.${lease.ownerToken}.tmp`
2870
- );
2871
- try {
2872
- await writeFile2(temporaryPath, JSON.stringify(lease), {
2873
- flag: "wx",
2874
- mode: 384
2875
- });
3145
+ const lease = await (this.leaseBroker ? this.leaseBroker.acquire() : acquireIndexLease(this.workspaceRoot));
3146
+ if (lease) {
2876
3147
  try {
2877
3148
  await this.leaseListener?.(lease.ownerToken);
2878
- await link(temporaryPath, this.leasePath);
3149
+ if (signal.aborted) {
3150
+ await this.releaseLease(lease);
3151
+ return { status: "cancelled" };
3152
+ }
2879
3153
  return { status: "acquired", lease };
2880
3154
  } catch (error) {
2881
- if (!this.isFileExistsError(error)) throw error;
3155
+ await this.releaseLease(lease);
3156
+ throw error;
2882
3157
  }
2883
- } finally {
2884
- await unlink3(temporaryPath).catch(() => void 0);
2885
- }
2886
- const inspection = await this.inspectLease();
2887
- if (inspection.status === "missing") continue;
2888
- if (inspection.status === "dead") {
2889
- if (await this.removeLeaseIfMatches(inspection.lease)) continue;
2890
3158
  }
2891
3159
  if (!wait) return { status: "contended" };
2892
3160
  if (Date.now() >= deadline) return { status: "timed_out" };
@@ -2894,51 +3162,6 @@ var CodeIndexService = class {
2894
3162
  }
2895
3163
  return { status: "cancelled" };
2896
3164
  }
2897
- async inspectLease() {
2898
- try {
2899
- const info = await lstat(this.leasePath);
2900
- if (!info.isFile() || info.isSymbolicLink()) {
2901
- return { status: "occupied" };
2902
- }
2903
- const parsed = JSON.parse(
2904
- await readFile4(this.leasePath, "utf-8")
2905
- );
2906
- const lease = this.projectLease(parsed);
2907
- if (!lease) return { status: "occupied" };
2908
- try {
2909
- process.kill(lease.pid, 0);
2910
- return { status: "active", lease };
2911
- } catch (error) {
2912
- return this.isProcessMissingError(error) ? { status: "dead", lease } : { status: "active", lease };
2913
- }
2914
- } catch (error) {
2915
- return this.isFileMissingError(error) ? { status: "missing" } : { status: "occupied" };
2916
- }
2917
- }
2918
- projectLease(value) {
2919
- if (!this.isRecord(value)) return null;
2920
- const pid = value["pid"];
2921
- const ownerToken = value["ownerToken"];
2922
- if (!Number.isSafeInteger(pid) || pid <= 0 || typeof ownerToken !== "string" || !/^[a-f0-9]{32}$/.test(ownerToken)) {
2923
- return null;
2924
- }
2925
- return { pid, ownerToken };
2926
- }
2927
- async removeLeaseIfMatches(expected) {
2928
- try {
2929
- const parsed = JSON.parse(
2930
- await readFile4(this.leasePath, "utf-8")
2931
- );
2932
- const current = this.projectLease(parsed);
2933
- if (current?.pid !== expected.pid || current?.ownerToken !== expected.ownerToken) {
2934
- return false;
2935
- }
2936
- await unlink3(this.leasePath);
2937
- return true;
2938
- } catch {
2939
- return false;
2940
- }
2941
- }
2942
3165
  async delayLeasePoll(signal, deadline = Number.POSITIVE_INFINITY) {
2943
3166
  const delayMs = Math.max(
2944
3167
  0,
@@ -2956,18 +3179,13 @@ var CodeIndexService = class {
2956
3179
  });
2957
3180
  }
2958
3181
  async releaseLease(lease) {
2959
- if (await this.removeLeaseIfMatches(lease))
2960
- await this.leaseListener?.(null);
2961
- }
2962
- isFileExistsError(error) {
2963
- return error instanceof Error && "code" in error && error.code === "EEXIST";
3182
+ if (this.leaseBroker) await this.leaseBroker.release(lease);
3183
+ else await releaseIndexLease(this.workspaceRoot, lease);
3184
+ await this.leaseListener?.(null);
2964
3185
  }
2965
3186
  isFileMissingError(error) {
2966
3187
  return error instanceof Error && "code" in error && error.code === "ENOENT";
2967
3188
  }
2968
- isProcessMissingError(error) {
2969
- return error instanceof Error && "code" in error && error.code === "ESRCH";
2970
- }
2971
3189
  async loadV3Index(meta, verifiedVectors) {
2972
3190
  if (!meta.artifacts || !this.validateArtifacts(meta.artifacts)) {
2973
3191
  throw new Error("Saved artifact manifest is invalid");
@@ -2975,15 +3193,15 @@ var CodeIndexService = class {
2975
3193
  const expectedVectorBytes = this.getExpectedVectorBytes(meta);
2976
3194
  const [vectorFile, metadataFile, keywordsFile] = await Promise.all([
2977
3195
  verifiedVectors ?? readStableIndexFile(
2978
- join6(this.indexDir, meta.artifacts.vectors),
3196
+ join8(this.indexDir, meta.artifacts.vectors),
2979
3197
  expectedVectorBytes
2980
3198
  ),
2981
3199
  readStableIndexFile(
2982
- join6(this.indexDir, meta.artifacts.metadata),
3200
+ join8(this.indexDir, meta.artifacts.metadata),
2983
3201
  this.limits.maxSourceBytes
2984
3202
  ),
2985
3203
  readStableIndexFile(
2986
- join6(this.indexDir, meta.artifacts.keywords),
3204
+ join8(this.indexDir, meta.artifacts.keywords),
2987
3205
  this.limits.maxSourceBytes
2988
3206
  )
2989
3207
  ]);
@@ -3016,7 +3234,7 @@ var CodeIndexService = class {
3016
3234
  );
3017
3235
  const [vectorFile, metadataFile, keywordsFile] = await Promise.all([
3018
3236
  readStableIndexFile(
3019
- join6(this.indexDir, "vectors.bin"),
3237
+ join8(this.indexDir, "vectors.bin"),
3020
3238
  this.limits.maxVectorBytes,
3021
3239
  manifestMtimeMs
3022
3240
  ),
@@ -3256,12 +3474,12 @@ var CodeIndexService = class {
3256
3474
  return bytes;
3257
3475
  }
3258
3476
  selectLegacyArtifact(encryptedName, plaintextName) {
3259
- const encryptedPath = join6(this.indexDir, encryptedName);
3260
- if (existsSync5(encryptedPath)) {
3477
+ const encryptedPath = join8(this.indexDir, encryptedName);
3478
+ if (existsSync3(encryptedPath)) {
3261
3479
  return { path: encryptedPath, encrypted: true };
3262
3480
  }
3263
- const plaintextPath = join6(this.indexDir, plaintextName);
3264
- if (existsSync5(plaintextPath)) {
3481
+ const plaintextPath = join8(this.indexDir, plaintextName);
3482
+ if (existsSync3(plaintextPath)) {
3265
3483
  return { path: plaintextPath, encrypted: false };
3266
3484
  }
3267
3485
  throw new Error("Legacy index artifact is missing");
@@ -3353,7 +3571,7 @@ var CodeIndexService = class {
3353
3571
  }
3354
3572
  if (scanResult.files.length === 0) {
3355
3573
  if (signal?.aborted) return { kind: "cancelled", index: null };
3356
- await rm(join6(this.indexDir, INDEX_MANIFEST_FILE), { force: true });
3574
+ await rm2(join8(this.indexDir, INDEX_MANIFEST_FILE), { force: true });
3357
3575
  const message = "No files to index";
3358
3576
  onProgress?.({ status: "complete", message });
3359
3577
  return { kind: "empty", message };
@@ -3369,7 +3587,7 @@ var CodeIndexService = class {
3369
3587
  }
3370
3588
  if (chunks.length === 0) {
3371
3589
  if (signal?.aborted) return { kind: "cancelled", index: null };
3372
- await rm(join6(this.indexDir, INDEX_MANIFEST_FILE), { force: true });
3590
+ await rm2(join8(this.indexDir, INDEX_MANIFEST_FILE), { force: true });
3373
3591
  const message = "No code chunks found";
3374
3592
  onProgress?.({ status: "complete", message });
3375
3593
  return { kind: "empty", message };
@@ -3807,8 +4025,8 @@ var CodeIndexService = class {
3807
4025
  normalizeWorkspacePath(filePath) {
3808
4026
  const trimmed = filePath.trim();
3809
4027
  if (!trimmed) return null;
3810
- const workspaceRoot = resolve4(this.workspaceRoot);
3811
- const absPath = isAbsolute2(trimmed) ? resolve4(trimmed) : resolve4(workspaceRoot, trimmed);
4028
+ const workspaceRoot = resolve5(this.workspaceRoot);
4029
+ const absPath = isAbsolute2(trimmed) ? resolve5(trimmed) : resolve5(workspaceRoot, trimmed);
3812
4030
  const relativePath = relativePathWithinRoot(workspaceRoot, absPath);
3813
4031
  if (!relativePath) return null;
3814
4032
  return { absPath, relPath: this.toIndexPath(relativePath) };
@@ -3829,7 +4047,7 @@ var CodeIndexService = class {
3829
4047
  const normalizedRelPath = this.toIndexPath(relPath);
3830
4048
  const segments = normalizedRelPath.split("/");
3831
4049
  if (segments.some((segment) => IGNORED_DIRS.has(segment))) return false;
3832
- const fileName = basename5(normalizedRelPath);
4050
+ const fileName = basename6(normalizedRelPath);
3833
4051
  const extension = extname2(normalizedRelPath).toLowerCase();
3834
4052
  if (!CODE_EXTENSIONS.has(extension) && !CODE_FILENAMES.has(fileName)) {
3835
4053
  return false;
@@ -4124,7 +4342,7 @@ var CodeIndexService = class {
4124
4342
  };
4125
4343
  }
4126
4344
  createEmbeddingCacheKey(input, model) {
4127
- return createHash2("sha256").update(
4345
+ return createHash3("sha256").update(
4128
4346
  `${EMBEDDING_INPUT_REVISION}\0${model}\0${hashContentFull(input)}`
4129
4347
  ).digest("hex");
4130
4348
  }
@@ -4142,8 +4360,8 @@ var CodeIndexService = class {
4142
4360
  this.diagnostics.cacheMs += performance.now() - startedAt;
4143
4361
  }
4144
4362
  createProjectIdentifiers() {
4145
- const name = basename5(this.workspaceRoot);
4146
- const hash = createHash2("sha256").update(this.workspaceRoot).digest("hex").slice(0, 16);
4363
+ const name = basename6(this.workspaceRoot);
4364
+ const hash = createHash3("sha256").update(this.workspaceRoot).digest("hex").slice(0, 16);
4147
4365
  return { projectHash: hash, projectName: name };
4148
4366
  }
4149
4367
  // ─── Keyword Index ──────────────────────────────────────────────────────
@@ -4201,7 +4419,7 @@ var CodeIndexService = class {
4201
4419
  try {
4202
4420
  candidate = await this.writeV3Candidate(data);
4203
4421
  const writtenVectors = await readStableIndexFile(
4204
- join6(this.indexDir, candidate.artifacts.vectors),
4422
+ join8(this.indexDir, candidate.artifacts.vectors),
4205
4423
  this.getExpectedVectorBytes(candidate.meta)
4206
4424
  );
4207
4425
  const expected = expectedVectorBuffer ?? candidate.vectorBuffer;
@@ -4228,8 +4446,8 @@ var CodeIndexService = class {
4228
4446
  }
4229
4447
  async writeV3Candidate(data) {
4230
4448
  await this.encryption.ensureKey();
4231
- await mkdir3(this.indexDir, { recursive: true });
4232
- const generation = randomBytes4(8).toString("hex");
4449
+ await mkdir4(this.indexDir, { recursive: true });
4450
+ const generation = randomBytes5(8).toString("hex");
4233
4451
  const artifacts = {
4234
4452
  vectors: `vectors.${generation}.bin`,
4235
4453
  metadata: `meta.${generation}.enc`,
@@ -4253,15 +4471,15 @@ var CodeIndexService = class {
4253
4471
  const candidate = { meta, artifacts, vectorBuffer };
4254
4472
  try {
4255
4473
  await this.atomicWrite(
4256
- join6(this.indexDir, artifacts.vectors),
4474
+ join8(this.indexDir, artifacts.vectors),
4257
4475
  vectorBuffer
4258
4476
  );
4259
4477
  await this.atomicWrite(
4260
- join6(this.indexDir, artifacts.metadata),
4478
+ join8(this.indexDir, artifacts.metadata),
4261
4479
  metadataBuffer
4262
4480
  );
4263
4481
  await this.atomicWrite(
4264
- join6(this.indexDir, artifacts.keywords),
4482
+ join8(this.indexDir, artifacts.keywords),
4265
4483
  keywordsBuffer
4266
4484
  );
4267
4485
  } catch (error) {
@@ -4274,9 +4492,9 @@ var CodeIndexService = class {
4274
4492
  async saveCommitManifest(meta, signal) {
4275
4493
  const projectedMeta = this.projectIndexMeta(meta, INDEX_VERSION);
4276
4494
  const manifest = JSON.stringify(projectedMeta, null, 2);
4277
- await mkdir3(this.indexDir, { recursive: true });
4495
+ await mkdir4(this.indexDir, { recursive: true });
4278
4496
  await this.atomicWrite(
4279
- join6(this.indexDir, INDEX_MANIFEST_FILE),
4497
+ join8(this.indexDir, INDEX_MANIFEST_FILE),
4280
4498
  manifest,
4281
4499
  signal,
4282
4500
  async () => {
@@ -4287,7 +4505,7 @@ var CodeIndexService = class {
4287
4505
  this.diagnostics.metadataBytesWritten += Buffer.byteLength(manifest);
4288
4506
  }
4289
4507
  validateArtifacts(artifacts) {
4290
- return /^vectors\.[a-f0-9]+\.bin$/.test(artifacts.vectors) && /^meta\.[a-f0-9]+\.enc$/.test(artifacts.metadata) && /^keywords\.[a-f0-9]+\.enc$/.test(artifacts.keywords) && basename5(artifacts.vectors) === artifacts.vectors && basename5(artifacts.metadata) === artifacts.metadata && basename5(artifacts.keywords) === artifacts.keywords;
4508
+ return /^vectors\.[a-f0-9]+\.bin$/.test(artifacts.vectors) && /^meta\.[a-f0-9]+\.enc$/.test(artifacts.metadata) && /^keywords\.[a-f0-9]+\.enc$/.test(artifacts.keywords) && basename6(artifacts.vectors) === artifacts.vectors && basename6(artifacts.metadata) === artifacts.metadata && basename6(artifacts.keywords) === artifacts.keywords;
4291
4509
  }
4292
4510
  async cleanupCommittedIndex(artifacts) {
4293
4511
  await Promise.allSettled([
@@ -4298,27 +4516,27 @@ var CodeIndexService = class {
4298
4516
  async removeCandidateArtifacts(artifacts) {
4299
4517
  await Promise.all(
4300
4518
  Object.values(artifacts).map(
4301
- (file) => unlink3(join6(this.indexDir, file)).catch(() => void 0)
4519
+ (file) => unlink4(join8(this.indexDir, file)).catch(() => void 0)
4302
4520
  )
4303
4521
  );
4304
4522
  }
4305
4523
  async removeUnreferencedSnapshotArtifacts(artifacts) {
4306
4524
  const referenced = new Set(Object.values(artifacts));
4307
- const files = await readdir3(this.indexDir).catch(() => []);
4525
+ const files = await readdir4(this.indexDir).catch(() => []);
4308
4526
  await Promise.all(
4309
4527
  files.filter(
4310
4528
  (file) => /^(vectors\.[a-f0-9]+\.bin|meta\.[a-f0-9]+\.enc|keywords\.[a-f0-9]+\.enc)$/.test(
4311
4529
  file
4312
4530
  ) && !referenced.has(file)
4313
4531
  ).map(
4314
- (file) => unlink3(join6(this.indexDir, file)).catch(() => void 0)
4532
+ (file) => unlink4(join8(this.indexDir, file)).catch(() => void 0)
4315
4533
  )
4316
4534
  );
4317
4535
  }
4318
4536
  async removeLegacyV2Artifacts() {
4319
4537
  await Promise.all(
4320
4538
  LEGACY_INDEX_ARTIFACTS.map(
4321
- (file) => unlink3(join6(this.indexDir, file)).catch(() => void 0)
4539
+ (file) => unlink4(join8(this.indexDir, file)).catch(() => void 0)
4322
4540
  )
4323
4541
  );
4324
4542
  }
@@ -4384,7 +4602,7 @@ var CodeIndexService = class {
4384
4602
  };
4385
4603
 
4386
4604
  // src/services/retrieval.service.ts
4387
- import { readdir as readdir4 } from "node:fs/promises";
4605
+ import { readdir as readdir5 } from "node:fs/promises";
4388
4606
  import { posix, win32 } from "node:path";
4389
4607
  var TOP_K = 10;
4390
4608
  var MAX_PER_FILE = 3;
@@ -4725,7 +4943,7 @@ var RetrievalService = class {
4725
4943
  // ─── Path Hint Extraction ──────────────────────────────────────────
4726
4944
  async extractPathHints(query) {
4727
4945
  try {
4728
- const entries = await readdir4(this.workspaceRoot, {
4946
+ const entries = await readdir5(this.workspaceRoot, {
4729
4947
  withFileTypes: true
4730
4948
  });
4731
4949
  const dirs = entries.filter((e) => e.isDirectory() && !e.name.startsWith(".")).map((e) => e.name);
@@ -4989,7 +5207,7 @@ var IndexDirtyBatch = class {
4989
5207
  // src/services/index-worker.ts
4990
5208
  var port = parentPort;
4991
5209
  var options = workerData;
4992
- var searchContext = new AsyncLocalStorage();
5210
+ var searchContext = new AsyncLocalStorage2();
4993
5211
  var sequence = 0;
4994
5212
  var stopped = false;
4995
5213
  var index = null;
@@ -5027,7 +5245,7 @@ function summary() {
5027
5245
  }
5028
5246
  function broker(event, signal) {
5029
5247
  const requestId = ++sequence;
5030
- return new Promise((resolve5, reject) => {
5248
+ return new Promise((resolve6, reject) => {
5031
5249
  const timer = setTimeout(
5032
5250
  () => {
5033
5251
  pending.delete(requestId);
@@ -5046,7 +5264,7 @@ function broker(event, signal) {
5046
5264
  pending.set(requestId, {
5047
5265
  resolve: (value) => {
5048
5266
  signal?.removeEventListener("abort", abort);
5049
- resolve5(value);
5267
+ resolve6(value);
5050
5268
  },
5051
5269
  reject: (error) => {
5052
5270
  signal?.removeEventListener("abort", abort);
@@ -5092,14 +5310,15 @@ var authorize = async (paths) => !stopped && await broker({
5092
5310
  }) === true;
5093
5311
  engine.setAuthorization(authorize);
5094
5312
  retrieval.setAuthorization(authorize);
5095
- engine.onLease(async (ownerToken) => {
5096
- if (!stopped) await broker({ type: "lease", ownerToken });
5097
- else
5098
- port.postMessage({
5099
- type: "lease",
5100
- requestId: ++sequence,
5101
- ownerToken
5102
- });
5313
+ engine.setLeaseBroker({
5314
+ async acquire() {
5315
+ if (stopped) return null;
5316
+ return await broker({ type: "lease-acquire" });
5317
+ },
5318
+ async release(lease) {
5319
+ if (!stopped)
5320
+ await broker({ type: "lease-release", ownerToken: lease.ownerToken });
5321
+ }
5103
5322
  });
5104
5323
  function publishIndex(state = engine.getRefreshState()) {
5105
5324
  send({
@@ -5241,7 +5460,7 @@ function scheduleDirty(recovery = false) {
5241
5460
  if (!recovery && captured.length === 0) return;
5242
5461
  engine.replaceDirtyFiles(captured.map(({ path: path2 }) => path2));
5243
5462
  const policyChanged = captured.some(
5244
- ({ path: path2 }) => [".lurusignore", ".gitignore"].includes(basename6(path2))
5463
+ ({ path: path2 }) => [".lurusignore", ".gitignore"].includes(basename7(path2))
5245
5464
  );
5246
5465
  operation = recovery ? recoveryOperation : !index ? "startup" : policyChanged ? "refresh" : "dirty";
5247
5466
  if (!recovery) engine.resetAutomaticRefreshBlock();