little-coder 1.8.0 → 1.8.1

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.
@@ -0,0 +1,89 @@
1
+ import { describe, it, expect, beforeAll, afterAll } from "vitest";
2
+ import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { globFiles, renderGlobOutcome, DEFAULT_HEAVY_DIRS } from "./glob.ts";
6
+
7
+ let dir: string;
8
+
9
+ beforeAll(() => {
10
+ dir = mkdtempSync(join(tmpdir(), "glob-test-"));
11
+ // real source we want to find
12
+ mkdirSync(join(dir, "src", "sub"), { recursive: true });
13
+ writeFileSync(join(dir, "src", "a.py"), "");
14
+ writeFileSync(join(dir, "src", "sub", "b.py"), "");
15
+ writeFileSync(join(dir, "README.md"), "");
16
+ // heavy dirs that must be pruned (with files matching the pattern inside)
17
+ mkdirSync(join(dir, "node_modules", "pkg", "deep"), { recursive: true });
18
+ writeFileSync(join(dir, "node_modules", "pkg", "deep", "x.py"), "");
19
+ mkdirSync(join(dir, ".git", "objects"), { recursive: true });
20
+ writeFileSync(join(dir, ".git", "objects", "y.py"), "");
21
+ mkdirSync(join(dir, "dist"), { recursive: true });
22
+ writeFileSync(join(dir, "dist", "z.py"), "");
23
+ });
24
+
25
+ afterAll(() => rmSync(dir, { recursive: true, force: true }));
26
+
27
+ describe("globFiles", () => {
28
+ it("matches real files and prunes heavy dirs (node_modules/.git/dist)", async () => {
29
+ const { matches, scanTruncated, matchTruncated } = await globFiles("**/*.py", { base: dir });
30
+ const rel = matches.map((m) => m.slice(dir.length + 1)).sort();
31
+ expect(rel).toEqual(["src/a.py", "src/sub/b.py"]);
32
+ expect(matches.some((m) => m.includes("node_modules"))).toBe(false);
33
+ expect(matches.some((m) => m.includes(".git"))).toBe(false);
34
+ expect(matches.some((m) => m.includes("/dist/"))).toBe(false);
35
+ expect(scanTruncated).toBe(false);
36
+ expect(matchTruncated).toBe(false);
37
+ });
38
+
39
+ it("caps matches at maxMatches and flags matchTruncated", async () => {
40
+ const many = mkdtempSync(join(tmpdir(), "glob-many-"));
41
+ for (let i = 0; i < 50; i++) writeFileSync(join(many, `f${i}.txt`), "");
42
+ try {
43
+ const { matches, matchTruncated } = await globFiles("*.txt", { base: many, maxMatches: 10 });
44
+ expect(matches.length).toBe(10);
45
+ expect(matchTruncated).toBe(true);
46
+ } finally {
47
+ rmSync(many, { recursive: true, force: true });
48
+ }
49
+ });
50
+
51
+ it("stops the walk at maxScan and flags scanTruncated (memory bound)", async () => {
52
+ // A low budget must halt the walk regardless of how many entries exist.
53
+ const { scanned, scanTruncated } = await globFiles("**/*", { base: dir, maxScan: 3 });
54
+ expect(scanTruncated).toBe(true);
55
+ expect(scanned).toBeLessThanOrEqual(5); // a couple over the budget, not unbounded
56
+ });
57
+
58
+ it("the heavy-dir set covers the usual offenders", () => {
59
+ for (const d of ["node_modules", ".git", "dist", ".cache", "Library", "venv", "target"]) {
60
+ expect(DEFAULT_HEAVY_DIRS.has(d)).toBe(true);
61
+ }
62
+ });
63
+ });
64
+
65
+ describe("renderGlobOutcome", () => {
66
+ it("reports no matches plainly", () => {
67
+ expect(renderGlobOutcome({ matches: [], scanned: 5, scanTruncated: false, matchTruncated: false }))
68
+ .toBe("No files matched");
69
+ });
70
+ it("notes scan truncation when nothing matched", () => {
71
+ expect(renderGlobOutcome({ matches: [], scanned: 9, scanTruncated: true, matchTruncated: false }, 200000))
72
+ .toMatch(/stopped after scanning 200000 entries/);
73
+ });
74
+ it("appends a match-cap note", () => {
75
+ const text = renderGlobOutcome(
76
+ { matches: ["/a", "/b"], scanned: 2, scanTruncated: false, matchTruncated: true },
77
+ 200000,
78
+ 500,
79
+ );
80
+ expect(text).toMatch(/stopped at 500 matches/);
81
+ });
82
+ it("appends a scan-cap note when there were partial matches", () => {
83
+ const text = renderGlobOutcome(
84
+ { matches: ["/a"], scanned: 9, scanTruncated: true, matchTruncated: false },
85
+ 200000,
86
+ );
87
+ expect(text).toMatch(/results may be incomplete/);
88
+ });
89
+ });
@@ -0,0 +1,102 @@
1
+ import { glob as fsGlob } from "node:fs/promises";
2
+
3
+ // Bounded file globbing. The naive `for await (…glob…) { if (len>=500) break }`
4
+ // only caps MATCHES — it does nothing about the WALK. Run from a huge root
5
+ // (e.g. a home directory with macOS Library / caches / node_modules), fs.glob
6
+ // recursively descends everything, and its internal traversal state grows until
7
+ // the Node process OOMs (heap, not the model's context) — long before 500
8
+ // matches are found if matches are sparse. fs.glob exposes no signal/abort and
9
+ // no depth/scan cap, so we bound it through the one hook it does call for every
10
+ // entry: `exclude`. We use it to (a) prune heavy/irrelevant directories so they
11
+ // are never descended, and (b) meter total entries scanned — once the budget is
12
+ // hit, exclude everything, which winds the walk down.
13
+
14
+ /** Directories never worth descending for a file search — pruned at the dir
15
+ * level (returning true from `exclude` on a directory stops descent), which is
16
+ * what keeps a home-directory glob from exhausting memory. */
17
+ export const DEFAULT_HEAVY_DIRS: ReadonlySet<string> = new Set([
18
+ // version control
19
+ ".git", ".hg", ".svn",
20
+ // dependencies / language caches
21
+ "node_modules", ".venv", "venv", "__pycache__", ".tox", ".mypy_cache",
22
+ ".pytest_cache", ".gradle", ".cargo", "vendor", "Pods",
23
+ // build output
24
+ "dist", "build", "out", "target", ".next", ".nuxt", ".output", ".svelte-kit",
25
+ // tool caches
26
+ ".cache", ".npm", ".pnpm-store", ".yarn", ".turbo",
27
+ // macOS / system heavies that blow up a home-dir walk
28
+ "Library", "Applications", ".Trash", "Photos Library.photoslibrary",
29
+ ]);
30
+
31
+ export interface GlobOptions {
32
+ base: string;
33
+ maxScan?: number;
34
+ maxMatches?: number;
35
+ heavyDirs?: ReadonlySet<string>;
36
+ }
37
+
38
+ export interface GlobOutcome {
39
+ matches: string[];
40
+ scanned: number;
41
+ /** the walk was cut short at maxScan entries (results may be incomplete) */
42
+ scanTruncated: boolean;
43
+ /** matches were capped at maxMatches */
44
+ matchTruncated: boolean;
45
+ }
46
+
47
+ export const DEFAULT_MAX_SCAN = 200_000;
48
+ export const DEFAULT_MAX_MATCHES = 500;
49
+
50
+ export async function globFiles(pattern: string, opts: GlobOptions): Promise<GlobOutcome> {
51
+ const maxScan = opts.maxScan ?? DEFAULT_MAX_SCAN;
52
+ const maxMatches = opts.maxMatches ?? DEFAULT_MAX_MATCHES;
53
+ const heavy = opts.heavyDirs ?? DEFAULT_HEAVY_DIRS;
54
+
55
+ const matches: string[] = [];
56
+ let scanned = 0;
57
+ let scanTruncated = false;
58
+ let matchTruncated = false;
59
+
60
+ // Called for every entry the walk visits (files AND directories). Pruning a
61
+ // directory here stops descent into it. Also our scan meter: once the budget
62
+ // is spent, exclude everything so fs.glob stops adding work and ends.
63
+ const exclude = (entry: unknown): boolean => {
64
+ scanned++;
65
+ if (scanned > maxScan) {
66
+ scanTruncated = true;
67
+ return true;
68
+ }
69
+ const name = typeof entry === "string" ? entry : String((entry as { name?: string })?.name ?? entry);
70
+ return name.split(/[\\/]/).some((seg) => heavy.has(seg));
71
+ };
72
+
73
+ // `exclude` as a predicate isn't in every @types/node version's fs.glob
74
+ // signature, but it's supported at runtime (Node 22+); cast to pass it through.
75
+ for await (const m of fsGlob(pattern, { cwd: opts.base, exclude } as Parameters<typeof fsGlob>[1])) {
76
+ matches.push(`${opts.base}/${m}`);
77
+ if (matches.length >= maxMatches) {
78
+ matchTruncated = true;
79
+ break;
80
+ }
81
+ }
82
+
83
+ matches.sort();
84
+ return { matches, scanned, scanTruncated, matchTruncated };
85
+ }
86
+
87
+ /** Render a globFiles outcome as the tool's text output, with a one-line note
88
+ * when results were cut short so the model knows to narrow its search. */
89
+ export function renderGlobOutcome(o: GlobOutcome, maxScan = DEFAULT_MAX_SCAN, maxMatches = DEFAULT_MAX_MATCHES): string {
90
+ if (o.matches.length === 0) {
91
+ return o.scanTruncated
92
+ ? `No files matched (search stopped after scanning ${maxScan} entries — narrow the base path; build/dependency/cache dirs are skipped automatically).`
93
+ : "No files matched";
94
+ }
95
+ let text = o.matches.join("\n");
96
+ if (o.matchTruncated) {
97
+ text += `\n… (stopped at ${maxMatches} matches — narrow the pattern for the rest)`;
98
+ } else if (o.scanTruncated) {
99
+ text += `\n… (search stopped after scanning ${maxScan} entries — results may be incomplete; narrow the base path)`;
100
+ }
101
+ return text;
102
+ }
@@ -1,6 +1,6 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { Type } from "@sinclair/typebox";
3
- import { glob as globSync } from "node:fs/promises";
3
+ import { globFiles, renderGlobOutcome } from "./glob.ts";
4
4
 
5
5
  // Ports of tools.py::_glob, _webfetch, _websearch. Pi ships its own grep/find,
6
6
  // so those are not re-registered here.
@@ -10,7 +10,9 @@ export default function (pi: ExtensionAPI) {
10
10
  name: "glob",
11
11
  label: "Glob",
12
12
  description:
13
- "Find files matching a glob pattern. Returns a sorted list of matching paths (up to 500).",
13
+ "Find files matching a glob pattern. Returns a sorted list of matching paths (up to 500). " +
14
+ "Common dependency/build/cache dirs (node_modules, .git, dist, …) are skipped, and the walk " +
15
+ "is bounded — for a focused search, pass a `path` rather than globbing a whole home directory.",
14
16
  parameters: Type.Object({
15
17
  pattern: Type.String({ description: "Glob pattern e.g. **/*.py" }),
16
18
  path: Type.Optional(Type.String({ description: "Base directory (default: cwd)" })),
@@ -18,16 +20,11 @@ export default function (pi: ExtensionAPI) {
18
20
  async execute(_id, { pattern, path }) {
19
21
  try {
20
22
  const base = path || process.cwd();
21
- const matches: string[] = [];
22
- // Node 22's fs/promises.glob returns an async iterator
23
- for await (const m of globSync(pattern, { cwd: base })) {
24
- matches.push(`${base}/${m}`);
25
- if (matches.length >= 500) break;
26
- }
27
- matches.sort();
28
- const text = matches.length === 0 ? "No files matched" : matches.join("\n");
23
+ // Bounded walk: prunes heavy dirs and caps total entries scanned so a
24
+ // recursive glob from a huge root can't exhaust the process heap.
25
+ const outcome = await globFiles(pattern, { base });
29
26
  return {
30
- content: [{ type: "text", text }],
27
+ content: [{ type: "text", text: renderGlobOutcome(outcome) }],
31
28
  details: {},
32
29
  };
33
30
  } catch (e) {
package/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  All notable changes to little-coder are documented here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and little-coder's public interface (CLI, providers, tools, skills) follows semver starting at `v0.0.1` post-rename.
4
4
 
5
+ ## [v1.8.1] — 2026-05-23
6
+
7
+ ### Fixed
8
+ - **`glob` no longer exhausts memory on a recursive search from a huge root.** The tool capped *matches* at 500 but never bounded the *walk*: run from a home directory (or any tree with macOS `Library`, caches, or `node_modules`), `fs.glob` recursively descended everything and its internal traversal state grew until the Node **process** ran out of heap — a host-memory crash (`Ineffective mark-compacts near heap limit`), entirely distinct from the model's *context window* (the read-guard / window machinery operates on tool *results* in tokens; this died mid-walk, before any result existed). The walk is now bounded two ways: heavy/irrelevant directories (`node_modules`, `.git`, `dist`, `.cache`, `Library`, `venv`, `target`, …) are **pruned** — never descended — and a hard scan budget (200 000 entries) halts the walk through the one hook `fs.glob` calls per entry (`exclude`), since it exposes no signal/abort. When results are cut short the output says so, so the model narrows its search. New unit-tested `globFiles` / `renderGlobOutcome` helpers (`.pi/extensions/extra-tools/glob.ts`), verified to prune `node_modules` (0 descent) and to halt at the scan budget.
9
+
10
+ ### Notes for upgraders
11
+ - For a focused search, pass a `path` (a project subdirectory) instead of globbing from a home directory. Hidden directories continue to be skipped by `fs.glob` as before.
12
+
13
+ ---
14
+
5
15
  ## [v1.8.0] — 2026-05-23
6
16
 
7
17
  little-coder now **auto-detects the llama.cpp server's live context window** at startup and registers the model with it, so a `llama-server -c 131072` shows 128k instead of the declared default — no config edit. This completes [v1.7.0](#v170--2026-05-23): the budget already *followed* the registered window; now the registered window itself comes from the running server.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "little-coder",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "A pi-based coding agent optimized for small local language models. Reproduces the whitepaper's scaffold-model-fit adaptations as pi extensions.",
5
5
  "homepage": "https://github.com/itayinbarr/little-coder",
6
6
  "repository": {