@causyne/agent 1.20.94 → 1.20.96
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agentLoop.js +44 -49
- package/src/agentLoop.ts +45 -46
- package/src/budget/budgetPolicy.js +92 -0
- package/src/budget/budgetPolicy.ts +107 -0
- package/src/providers/antigravity.js +27 -43
- package/src/providers/antigravity.ts +35 -45
- package/src/tokenTracker.js +24 -0
- package/src/tokenTracker.ts +25 -0
- package/src/tui/completion.js +29 -88
- package/src/tui/prompt.js +23 -1
- package/src/tui/workspaceFileIndex.js +183 -0
- package/src/tui/workspaceFileIndex.ts +179 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne: bounded, asynchronous, workspace-scoped index for @ references.
|
|
3
|
+
* This module never follows symlinks or invokes external binaries.
|
|
4
|
+
* It indexes nested project paths once, not on each keystroke.
|
|
5
|
+
*/
|
|
6
|
+
import { readdir } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
const SKIP = new Set([
|
|
10
|
+
'.git', '.causyne', 'node_modules', '.next', '.nuxt', '.svelte-kit',
|
|
11
|
+
'.venv', 'venv', '__pycache__', '.gradle', '.idea', '.cache',
|
|
12
|
+
'coverage', 'vendor', 'Pods', 'dist', 'build', 'out', 'target'
|
|
13
|
+
]);
|
|
14
|
+
const DEFAULT_MAX_ENTRIES = 100_000;
|
|
15
|
+
const DEFAULT_MAX_DIRS = 20_000;
|
|
16
|
+
const DEFAULT_MAX_DEPTH = 32;
|
|
17
|
+
const DEFAULT_REFRESH_MS = 45_000;
|
|
18
|
+
const YIELD_EVERY = 48;
|
|
19
|
+
|
|
20
|
+
export interface WorkspaceFileIndexOptions {
|
|
21
|
+
maxEntries?: number;
|
|
22
|
+
maxDirs?: number;
|
|
23
|
+
maxDepth?: number;
|
|
24
|
+
refreshMs?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function finiteInt(value: number | undefined, fallback: number, minimum = 1): number {
|
|
28
|
+
return Number.isSafeInteger(value) && value !== undefined && value >= minimum ? value : fallback;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Terminal control codes must never be inserted into a selectable path. */
|
|
32
|
+
function safeRelative(relative: string): boolean {
|
|
33
|
+
return relative.length > 0 && relative.length <= 4096
|
|
34
|
+
&& !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/u.test(relative);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface IndexedPath {
|
|
38
|
+
value: string;
|
|
39
|
+
folded: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class WorkspaceFileIndex {
|
|
43
|
+
readonly root: string;
|
|
44
|
+
entries: string[] | null = null;
|
|
45
|
+
truncated = false;
|
|
46
|
+
private indexed: IndexedPath[] = [];
|
|
47
|
+
private listeners = new Set<() => void>();
|
|
48
|
+
private building: Promise<string[]> | null = null;
|
|
49
|
+
private generation = 0;
|
|
50
|
+
private completedAt = 0;
|
|
51
|
+
private disposed = false;
|
|
52
|
+
private readonly maxEntries: number;
|
|
53
|
+
private readonly maxDirs: number;
|
|
54
|
+
private readonly maxDepth: number;
|
|
55
|
+
private readonly refreshMs: number;
|
|
56
|
+
|
|
57
|
+
constructor(root: string, options: WorkspaceFileIndexOptions = {}) {
|
|
58
|
+
this.root = path.resolve(root);
|
|
59
|
+
this.maxEntries = finiteInt(options.maxEntries, DEFAULT_MAX_ENTRIES);
|
|
60
|
+
this.maxDirs = finiteInt(options.maxDirs, DEFAULT_MAX_DIRS);
|
|
61
|
+
this.maxDepth = finiteInt(options.maxDepth, DEFAULT_MAX_DEPTH);
|
|
62
|
+
this.refreshMs = finiteInt(options.refreshMs, DEFAULT_REFRESH_MS, 0);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get isReady(): boolean { return this.entries !== null; }
|
|
66
|
+
get isBuilding(): boolean { return this.building !== null; }
|
|
67
|
+
|
|
68
|
+
subscribe(listener: () => void): () => void {
|
|
69
|
+
if (this.disposed) return () => {};
|
|
70
|
+
this.listeners.add(listener);
|
|
71
|
+
return () => { this.listeners.delete(listener); };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
invalidate(): void { this.completedAt = 0; }
|
|
75
|
+
|
|
76
|
+
dispose(): void {
|
|
77
|
+
this.disposed = true;
|
|
78
|
+
this.generation++;
|
|
79
|
+
this.listeners.clear();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Starts a scan only when required. Existing results remain available while
|
|
84
|
+
* a TTL refresh runs. Consumers receive one event when the snapshot changes.
|
|
85
|
+
*/
|
|
86
|
+
ensure(force = false): Promise<string[]> {
|
|
87
|
+
if (this.disposed) return Promise.resolve(this.entries ?? []);
|
|
88
|
+
if (this.building) return this.building;
|
|
89
|
+
if (!force && this.entries && Date.now() - this.completedAt < this.refreshMs) {
|
|
90
|
+
return Promise.resolve(this.entries);
|
|
91
|
+
}
|
|
92
|
+
const generation = ++this.generation;
|
|
93
|
+
this.building = this.scan(generation)
|
|
94
|
+
.then(({ entries, truncated }) => {
|
|
95
|
+
if (this.disposed || generation !== this.generation) return this.entries ?? [];
|
|
96
|
+
this.entries = entries;
|
|
97
|
+
this.indexed = entries.map(value => ({ value, folded: value.toLocaleLowerCase() }));
|
|
98
|
+
this.truncated = truncated;
|
|
99
|
+
this.completedAt = Date.now();
|
|
100
|
+
for (const listener of [...this.listeners]) {
|
|
101
|
+
try { listener(); } catch { /* UI listeners cannot fail the index */ }
|
|
102
|
+
}
|
|
103
|
+
return entries;
|
|
104
|
+
})
|
|
105
|
+
.catch(() => {
|
|
106
|
+
// Keep any previous snapshot if the root becomes unreadable.
|
|
107
|
+
if (!this.disposed && generation === this.generation && this.entries === null) {
|
|
108
|
+
this.entries = [];
|
|
109
|
+
this.indexed = [];
|
|
110
|
+
this.completedAt = Date.now();
|
|
111
|
+
for (const listener of [...this.listeners]) {
|
|
112
|
+
try { listener(); } catch { /* never propagate to keyboard handling */ }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return this.entries ?? [];
|
|
116
|
+
})
|
|
117
|
+
.finally(() => { if (generation === this.generation) this.building = null; });
|
|
118
|
+
return this.building;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Contiguous matches take priority; fuzzy matching remains the JS fallback. */
|
|
122
|
+
candidates(query: string): string[] {
|
|
123
|
+
if (!query) return this.entries ?? [];
|
|
124
|
+
const needle = query.toLocaleLowerCase();
|
|
125
|
+
const direct = this.indexed.filter(entry => entry.folded.includes(needle));
|
|
126
|
+
return (direct.length > 0 ? direct.map(entry => entry.value) : this.entries ?? []);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async scan(generation: number): Promise<{ entries: string[]; truncated: boolean }> {
|
|
130
|
+
const found: string[] = [];
|
|
131
|
+
const queue: Array<{ absolute: string; relative: string; depth: number }> = [
|
|
132
|
+
{ absolute: this.root, relative: '', depth: 0 }
|
|
133
|
+
];
|
|
134
|
+
let cursor = 0;
|
|
135
|
+
let truncated = false;
|
|
136
|
+
|
|
137
|
+
while (cursor < queue.length) {
|
|
138
|
+
if (this.disposed || generation !== this.generation) break;
|
|
139
|
+
if (cursor >= this.maxDirs || found.length >= this.maxEntries) {
|
|
140
|
+
truncated = true;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
const current = queue[cursor++];
|
|
144
|
+
if (!current) break;
|
|
145
|
+
let listing;
|
|
146
|
+
try { listing = await readdir(current.absolute, { withFileTypes: true }); }
|
|
147
|
+
catch { continue; }
|
|
148
|
+
listing.sort((a, b) => a.name.localeCompare(b.name));
|
|
149
|
+
|
|
150
|
+
for (const entry of listing) {
|
|
151
|
+
if (this.disposed || generation !== this.generation) break;
|
|
152
|
+
if (entry.isSymbolicLink()) continue;
|
|
153
|
+
if (!entry.isFile() && !entry.isDirectory()) continue;
|
|
154
|
+
const relative = current.relative ? `${current.relative}/${entry.name}` : entry.name;
|
|
155
|
+
if (!safeRelative(relative)) continue;
|
|
156
|
+
|
|
157
|
+
if (entry.isDirectory()) {
|
|
158
|
+
if (SKIP.has(entry.name)) continue;
|
|
159
|
+
if (current.depth < this.maxDepth) {
|
|
160
|
+
if (queue.length >= this.maxDirs) truncated = true;
|
|
161
|
+
else queue.push({
|
|
162
|
+
absolute: path.join(current.absolute, entry.name),
|
|
163
|
+
relative, depth: current.depth + 1
|
|
164
|
+
});
|
|
165
|
+
} else truncated = true;
|
|
166
|
+
found.push(`${relative}/`);
|
|
167
|
+
} else {
|
|
168
|
+
found.push(relative);
|
|
169
|
+
}
|
|
170
|
+
if (found.length >= this.maxEntries) { truncated = true; break; }
|
|
171
|
+
}
|
|
172
|
+
// Periodically yield: typing, rendering and cancellation must remain responsive.
|
|
173
|
+
if (cursor % YIELD_EVERY === 0) {
|
|
174
|
+
await new Promise<void>(resolve => setImmediate(resolve));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { entries: found, truncated };
|
|
178
|
+
}
|
|
179
|
+
}
|