@scramble-cloud/lurus-code-cli 2.7.1 → 2.7.3

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.
@@ -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;
245
- }
246
- setIgnoreScanNoticeHandler(handler) {
247
- this.ignoreScanNotice = handler;
228
+ this.ignoreMatchers.clear();
229
+ this.ignoreOverride = null;
248
230
  }
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)) {
543
+ return { kind: "hard-deny" };
544
+ }
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") {
575
549
  return { kind: "hard-deny" };
576
550
  }
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)) {
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 {
@@ -1061,7 +1008,7 @@ function isIndexingWorkspaceAllowed(workspaceRoot, options2 = {}) {
1061
1008
 
1062
1009
  // src/services/code-index.service.ts
1063
1010
  import { readdir as readdir4, stat as stat3, mkdir as mkdir4, rm as rm2, unlink as unlink4 } from "node:fs/promises";
1064
- import { existsSync as existsSync4 } from "node:fs";
1011
+ import { existsSync as existsSync3 } from "node:fs";
1065
1012
 
1066
1013
  // src/services/index-runtime-lease.ts
1067
1014
  import { mkdir } from "node:fs/promises";
@@ -1319,7 +1266,7 @@ import { createHash as createHash3, randomBytes as randomBytes5 } from "node:cry
1319
1266
 
1320
1267
  // src/services/index-source-reader.ts
1321
1268
  import { open as open2, stat, rename, unlink as unlink2, readdir as readdir2 } from "node:fs/promises";
1322
- import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
1269
+ import { existsSync, readFileSync as readFileSync3 } from "node:fs";
1323
1270
  import { createHash, randomBytes as randomBytes2 } from "node:crypto";
1324
1271
  import { isDeepStrictEqual } from "node:util";
1325
1272
  import { join as join5, relative as relative2 } from "node:path";
@@ -1496,7 +1443,7 @@ async function loadIndexIgnorePatterns(workspaceRoot, policy) {
1496
1443
  const loadMatcher = async (fileName) => {
1497
1444
  const matcher = ignore3();
1498
1445
  const filePath = join5(workspaceRoot, fileName);
1499
- if (!existsSync2(filePath)) return matcher;
1446
+ if (!existsSync(filePath)) return matcher;
1500
1447
  if (!await policy.isReadAllowedAsync(filePath)) {
1501
1448
  if (fileName === ".lurusignore")
1502
1449
  throw new Error("Index ignore policy could not be read.");
@@ -1840,7 +1787,7 @@ var IndexEncryptionService = class {
1840
1787
 
1841
1788
  // src/services/embedding-cache.service.ts
1842
1789
  import { randomBytes as randomBytes4 } from "node:crypto";
1843
- import { existsSync as existsSync3 } from "node:fs";
1790
+ import { existsSync as existsSync2 } from "node:fs";
1844
1791
  import {
1845
1792
  mkdir as mkdir3,
1846
1793
  readFile as readFile3,
@@ -1867,7 +1814,7 @@ var EmbeddingCacheService = class {
1867
1814
  pendingCheckpoint;
1868
1815
  async load() {
1869
1816
  const manifestPath = join7(this.cacheDir, CACHE_MANIFEST);
1870
- if (!existsSync3(manifestPath)) return null;
1817
+ if (!existsSync2(manifestPath)) return null;
1871
1818
  try {
1872
1819
  const manifest = JSON.parse(
1873
1820
  await readFile3(manifestPath, "utf-8")
@@ -2025,7 +1972,7 @@ var EmbeddingCacheService = class {
2025
1972
  }
2026
1973
  async clear() {
2027
1974
  this.writeRevision++;
2028
- if (!existsSync3(this.cacheDir)) return;
1975
+ if (!existsSync2(this.cacheDir)) return;
2029
1976
  const entries = await readdir3(this.cacheDir).catch(() => []);
2030
1977
  await Promise.all(
2031
1978
  entries.map(
@@ -2824,7 +2771,7 @@ var CodeIndexService = class {
2824
2771
  if (!this.isWorkspaceAllowed()) return null;
2825
2772
  this.lastLoadIndexFailure = null;
2826
2773
  const manifestPath = join8(this.indexDir, INDEX_MANIFEST_FILE);
2827
- if (!existsSync4(manifestPath)) return null;
2774
+ if (!existsSync3(manifestPath)) return null;
2828
2775
  try {
2829
2776
  await this.encryption.ensureKey();
2830
2777
  const manifestFile = await readStableIndexFile(
@@ -3528,11 +3475,11 @@ var CodeIndexService = class {
3528
3475
  }
3529
3476
  selectLegacyArtifact(encryptedName, plaintextName) {
3530
3477
  const encryptedPath = join8(this.indexDir, encryptedName);
3531
- if (existsSync4(encryptedPath)) {
3478
+ if (existsSync3(encryptedPath)) {
3532
3479
  return { path: encryptedPath, encrypted: true };
3533
3480
  }
3534
3481
  const plaintextPath = join8(this.indexDir, plaintextName);
3535
- if (existsSync4(plaintextPath)) {
3482
+ if (existsSync3(plaintextPath)) {
3536
3483
  return { path: plaintextPath, encrypted: false };
3537
3484
  }
3538
3485
  throw new Error("Legacy index artifact is missing");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scramble-cloud/lurus-code-cli",
3
- "version": "2.7.1",
3
+ "version": "2.7.3",
4
4
  "description": "Lurus Code — Agentic Coding CLI",
5
5
  "keywords": [
6
6
  "lurus",
@@ -59,10 +59,12 @@
59
59
  "release": "node scripts/release.mjs",
60
60
  "dev": "tsx src/cli.ts",
61
61
  "start": "node dist/cli.js",
62
- "lint": "eslint \"src/**/*.{ts,tsx}\"",
63
- "lint:rules": "node eslint-rules/no-live-system-prompt-access.test.cjs",
64
- "lint:fix": "eslint \"src/**/*.{ts,tsx}\" --fix",
62
+ "lint": "node scripts/lint-source.mjs",
63
+ "lint:files": "eslint",
64
+ "lint:rules": "node eslint-rules/no-live-system-prompt-access.test.cjs && node --test scripts/lint-source.test.mjs",
65
+ "lint:fix": "node scripts/lint-source.mjs --fix",
65
66
  "format": "prettier --write \"src/**/*.{ts,tsx}\"",
67
+ "format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
66
68
  "prettier": "prettier --write",
67
69
  "test": "vitest run",
68
70
  "verify": "npm run lint && npm run lint:rules && npm test",