@fabersep/atlas 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/README.md +139 -2
- package/dist/packs/csharp/calls.d.ts +4 -0
- package/dist/packs/csharp/calls.js +167 -0
- package/dist/packs/csharp/declarations.d.ts +4 -0
- package/dist/packs/csharp/declarations.js +142 -0
- package/dist/packs/csharp/pack.d.ts +2 -0
- package/dist/packs/csharp/pack.js +39 -0
- package/dist/packs/csharp/resolve.d.ts +2 -0
- package/dist/packs/csharp/resolve.js +35 -0
- package/dist/packs/csharp/signals.d.ts +7 -0
- package/dist/packs/csharp/signals.js +143 -0
- package/dist/packs/dart/calls.d.ts +4 -0
- package/dist/packs/dart/calls.js +130 -0
- package/dist/packs/dart/declarations.d.ts +11 -0
- package/dist/packs/dart/declarations.js +79 -0
- package/dist/packs/dart/members.d.ts +4 -0
- package/dist/packs/dart/members.js +62 -0
- package/dist/packs/dart/pack.d.ts +2 -0
- package/dist/packs/dart/pack.js +37 -0
- package/dist/packs/dart/resolve.d.ts +2 -0
- package/dist/packs/dart/resolve.js +32 -0
- package/dist/packs/index.d.ts +3 -0
- package/dist/packs/index.js +8 -0
- package/dist/packs/json/pack.d.ts +2 -0
- package/dist/packs/json/pack.js +27 -0
- package/dist/packs/msbuild/pack.d.ts +2 -0
- package/dist/packs/msbuild/pack.js +75 -0
- package/dist/packs/msbuild/xml.d.ts +16 -0
- package/dist/packs/msbuild/xml.js +123 -0
- package/dist/packs/typescript/calls.d.ts +4 -0
- package/dist/packs/typescript/calls.js +201 -0
- package/dist/packs/typescript/declarations.d.ts +9 -0
- package/dist/packs/typescript/declarations.js +74 -0
- package/dist/packs/typescript/members.d.ts +4 -0
- package/dist/packs/typescript/members.js +92 -0
- package/dist/packs/typescript/pack.d.ts +2 -0
- package/dist/packs/typescript/pack.js +53 -0
- package/dist/packs/typescript/resolve.d.ts +2 -0
- package/dist/packs/typescript/resolve.js +68 -0
- package/dist/packs/typescript/signals.d.ts +7 -0
- package/dist/packs/typescript/signals.js +186 -0
- package/dist/packs/yaml/pack.d.ts +2 -0
- package/dist/packs/yaml/pack.js +35 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +371 -0
- package/dist/src/graph/build.d.ts +17 -0
- package/dist/src/graph/build.js +41 -0
- package/dist/src/graph/metrics.d.ts +12 -0
- package/dist/src/graph/metrics.js +75 -0
- package/dist/src/graph/topology.d.ts +5 -0
- package/dist/src/graph/topology.js +54 -0
- package/dist/src/health/compare.d.ts +2 -0
- package/dist/src/health/compare.js +38 -0
- package/dist/src/health/duplicates.d.ts +11 -0
- package/dist/src/health/duplicates.js +66 -0
- package/dist/src/health/health.d.ts +12 -0
- package/dist/src/health/health.js +94 -0
- package/dist/src/health/types.d.ts +101 -0
- package/dist/src/health/types.js +5 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +20 -0
- package/dist/src/init/init.d.ts +73 -0
- package/dist/src/init/init.js +305 -0
- package/dist/src/init/scout.d.ts +45 -0
- package/dist/src/init/scout.js +208 -0
- package/dist/src/jsonc.d.ts +5 -0
- package/dist/src/jsonc.js +34 -0
- package/dist/src/mcp/server.d.ts +266 -0
- package/dist/src/mcp/server.js +414 -0
- package/dist/src/metrics/doctor.d.ts +15 -0
- package/dist/src/metrics/doctor.js +125 -0
- package/dist/src/metrics/log.d.ts +43 -0
- package/dist/src/metrics/log.js +101 -0
- package/dist/src/metrics/stats.d.ts +43 -0
- package/dist/src/metrics/stats.js +172 -0
- package/dist/src/model.d.ts +178 -0
- package/dist/src/model.js +2 -0
- package/dist/src/outputs.d.ts +16 -0
- package/dist/src/outputs.js +48 -0
- package/dist/src/packs/calls.d.ts +9 -0
- package/dist/src/packs/calls.js +119 -0
- package/dist/src/packs/dataShape.d.ts +6 -0
- package/dist/src/packs/dataShape.js +54 -0
- package/dist/src/packs/pack.d.ts +51 -0
- package/dist/src/packs/pack.js +3 -0
- package/dist/src/packs/packFiles.d.ts +5 -0
- package/dist/src/packs/packFiles.js +12 -0
- package/dist/src/packs/symbols.d.ts +39 -0
- package/dist/src/packs/symbols.js +98 -0
- package/dist/src/packs/treeSitter.d.ts +8 -0
- package/dist/src/packs/treeSitter.js +107 -0
- package/dist/src/queries/flow.d.ts +42 -0
- package/dist/src/queries/flow.js +391 -0
- package/dist/src/queries/map.d.ts +28 -0
- package/dist/src/queries/map.js +102 -0
- package/dist/src/queries/neighborhood.d.ts +35 -0
- package/dist/src/queries/neighborhood.js +60 -0
- package/dist/src/scan/audit.d.ts +2 -0
- package/dist/src/scan/audit.js +41 -0
- package/dist/src/scan/records.d.ts +12 -0
- package/dist/src/scan/records.js +42 -0
- package/dist/src/scan/resolveImports.d.ts +3 -0
- package/dist/src/scan/resolveImports.js +47 -0
- package/dist/src/scan/roles.d.ts +5 -0
- package/dist/src/scan/roles.js +87 -0
- package/dist/src/scan/scan.d.ts +24 -0
- package/dist/src/scan/scan.js +72 -0
- package/dist/src/text/briefs.d.ts +13 -0
- package/dist/src/text/briefs.js +43 -0
- package/dist/src/text/collapse.d.ts +10 -0
- package/dist/src/text/collapse.js +58 -0
- package/dist/src/text/declarations.d.ts +12 -0
- package/dist/src/text/declarations.js +120 -0
- package/dist/src/text/structure.d.ts +2 -0
- package/dist/src/text/structure.js +15 -0
- package/dist/src/text/tree.d.ts +20 -0
- package/dist/src/text/tree.js +49 -0
- package/dist/src/tokens.d.ts +4 -0
- package/dist/src/tokens.js +9 -0
- package/dist/src/version.d.ts +10 -0
- package/dist/src/version.js +11 -0
- package/dist/src/walk/ignoreRules.d.ts +17 -0
- package/dist/src/walk/ignoreRules.js +72 -0
- package/dist/src/walk/walk.d.ts +11 -0
- package/dist/src/walk/walk.js +29 -0
- package/grammars/LICENSE-tree-sitter-dart +20 -0
- package/grammars/README.md +11 -0
- package/grammars/tree-sitter-dart.wasm +0 -0
- package/package.json +68 -4
- package/packs/csharp/README.md +9 -0
- package/packs/csharp/imports.scm +3 -0
- package/packs/csharp/symbols.scm +17 -0
- package/packs/dart/imports.scm +5 -0
- package/packs/dart/symbols.scm +13 -0
- package/packs/msbuild/README.md +11 -0
- package/packs/typescript/exports.scm +16 -0
- package/packs/typescript/imports.scm +9 -0
- package/packs/typescript/symbols.scm +19 -0
- package/schema/ast_code_map.schema.json +144 -0
- package/schema/schema-lock.json +7 -0
- package/schema/topology.schema.json +87 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { antigravityRulesPath, claudeScoutFilePath, defaultScoutEffort, defaultScoutModel, removeScoutBlock, renderAntigravityScoutBlock, renderClaudeScoutAgent, upsertScoutBlock, } from './scout.js';
|
|
5
|
+
export const BLOCK_START = '<!-- faber-atlas:start -->';
|
|
6
|
+
export const BLOCK_END = '<!-- faber-atlas:end -->';
|
|
7
|
+
export function getEngineSpecs() {
|
|
8
|
+
return {
|
|
9
|
+
'claude-code': {
|
|
10
|
+
name: 'claude-code',
|
|
11
|
+
gap: false,
|
|
12
|
+
mcpConfigFile: (home) => path.join(home, '.claude.json'),
|
|
13
|
+
},
|
|
14
|
+
'claude-desktop': {
|
|
15
|
+
name: 'claude-desktop',
|
|
16
|
+
gap: false,
|
|
17
|
+
needsRoot: true,
|
|
18
|
+
mcpConfigFile: (home) => process.platform === 'darwin'
|
|
19
|
+
? path.join(home, 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json')
|
|
20
|
+
: path.join(home, '.config', 'Claude', 'claude_desktop_config.json'),
|
|
21
|
+
},
|
|
22
|
+
antigravity: {
|
|
23
|
+
name: 'antigravity',
|
|
24
|
+
gap: false,
|
|
25
|
+
mcpConfigFile: (home) => path.join(home, '.gemini', 'config', 'mcp_config.json'),
|
|
26
|
+
},
|
|
27
|
+
cursor: {
|
|
28
|
+
name: 'cursor',
|
|
29
|
+
gap: false,
|
|
30
|
+
mcpConfigFile: (home) => path.join(home, '.cursor', 'mcp.json'),
|
|
31
|
+
},
|
|
32
|
+
codex: {
|
|
33
|
+
name: 'codex',
|
|
34
|
+
gap: true,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function detectTargetEngines(userHome, requested) {
|
|
39
|
+
if (requested && requested.length > 0)
|
|
40
|
+
return requested;
|
|
41
|
+
const detected = [];
|
|
42
|
+
if (fs.existsSync(path.join(userHome, '.claude')))
|
|
43
|
+
detected.push('claude-code');
|
|
44
|
+
if (fs.existsSync(path.join(userHome, '.gemini')))
|
|
45
|
+
detected.push('antigravity');
|
|
46
|
+
if (fs.existsSync(path.join(userHome, '.cursor')))
|
|
47
|
+
detected.push('cursor');
|
|
48
|
+
if (detected.length === 0)
|
|
49
|
+
detected.push('claude-code');
|
|
50
|
+
return detected;
|
|
51
|
+
}
|
|
52
|
+
export function renderAgentsBlock() {
|
|
53
|
+
return [
|
|
54
|
+
BLOCK_START,
|
|
55
|
+
'## Code Navigation (FaberSEP Atlas)',
|
|
56
|
+
'',
|
|
57
|
+
'When exploring this repository, prefer the `faber-atlas` MCP tools before broad grep/file reads:',
|
|
58
|
+
'- Start with `map(zoom=0)` to locate modules, then `map(folder=..., zoom=1)` for files and top-level symbols.',
|
|
59
|
+
'- Use `flow(symbol)` / `impact(symbol)` to trace call chains, implementations, and events, and `brief` / `neighborhood` for token-budgeted signatures.',
|
|
60
|
+
BLOCK_END,
|
|
61
|
+
].join('\n');
|
|
62
|
+
}
|
|
63
|
+
function escapeRegex(s) {
|
|
64
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
65
|
+
}
|
|
66
|
+
export function upsertManagedBlock(existing, block) {
|
|
67
|
+
const regex = new RegExp(`${escapeRegex(BLOCK_START)}[\\s\\S]*?${escapeRegex(BLOCK_END)}`, 'g');
|
|
68
|
+
if (regex.test(existing)) {
|
|
69
|
+
let first = true;
|
|
70
|
+
const replaced = existing.replace(regex, () => {
|
|
71
|
+
if (first) {
|
|
72
|
+
first = false;
|
|
73
|
+
return block;
|
|
74
|
+
}
|
|
75
|
+
return '';
|
|
76
|
+
});
|
|
77
|
+
return `${replaced.replace(/\n{3,}/g, '\n\n').trimEnd()}\n`;
|
|
78
|
+
}
|
|
79
|
+
const trimmed = existing.trimEnd();
|
|
80
|
+
return trimmed ? `${trimmed}\n\n${block}\n` : `${block}\n`;
|
|
81
|
+
}
|
|
82
|
+
export function removeManagedBlock(existing) {
|
|
83
|
+
const regex = new RegExp(`\\n?${escapeRegex(BLOCK_START)}[\\s\\S]*?${escapeRegex(BLOCK_END)}\\n?`, 'g');
|
|
84
|
+
const next = existing.replace(regex, '\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
85
|
+
return next ? `${next}\n` : '';
|
|
86
|
+
}
|
|
87
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
88
|
+
/** Parses a config file; `null` when it is not a JSON object (comments, a half-written file), so it is never rewritten. */
|
|
89
|
+
export function parseConfig(raw) {
|
|
90
|
+
if (raw === null || !raw.trim())
|
|
91
|
+
return {};
|
|
92
|
+
try {
|
|
93
|
+
const obj = JSON.parse(raw);
|
|
94
|
+
return isObject(obj) ? obj : null;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function serverTable(holder, create) {
|
|
101
|
+
if (!isObject(holder.mcpServers)) {
|
|
102
|
+
if (!create)
|
|
103
|
+
return undefined;
|
|
104
|
+
holder.mcpServers = {};
|
|
105
|
+
}
|
|
106
|
+
return holder.mcpServers;
|
|
107
|
+
}
|
|
108
|
+
/** The project root an existing `faber-atlas` entry is pinned to, if any. */
|
|
109
|
+
export function entryRoot(entry) {
|
|
110
|
+
if (!isObject(entry) || !Array.isArray(entry.args))
|
|
111
|
+
return undefined;
|
|
112
|
+
const i = entry.args.indexOf('--root');
|
|
113
|
+
return i >= 0 && typeof entry.args[i + 1] === 'string' ? entry.args[i + 1] : undefined;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Per-project `faber-atlas` entries an earlier build wrote into `~/.claude.json` (`projects[<root>].mcpServers`).
|
|
117
|
+
* One global entry now serves every project, so these are removed. Returns whether any was found.
|
|
118
|
+
*/
|
|
119
|
+
function removeProjectEntries(obj) {
|
|
120
|
+
if (!isObject(obj.projects))
|
|
121
|
+
return false;
|
|
122
|
+
let found = false;
|
|
123
|
+
for (const project of Object.values(obj.projects)) {
|
|
124
|
+
if (isObject(project) && isObject(project.mcpServers) && 'faber-atlas' in project.mcpServers) {
|
|
125
|
+
delete project.mcpServers['faber-atlas'];
|
|
126
|
+
found = true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return found;
|
|
130
|
+
}
|
|
131
|
+
export function atlasEntry(engine, root) {
|
|
132
|
+
return {
|
|
133
|
+
...(engine.name === 'claude-code' ? { type: 'stdio' } : {}),
|
|
134
|
+
command: 'npx',
|
|
135
|
+
args: ['-y', '@fabersep/atlas', 'mcp', ...(engine.needsRoot ? ['--root', root] : [])],
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function upsertMcpJson(obj, entry) {
|
|
139
|
+
removeProjectEntries(obj);
|
|
140
|
+
serverTable(obj, true)['faber-atlas'] = entry;
|
|
141
|
+
return `${JSON.stringify(obj, null, 2)}\n`;
|
|
142
|
+
}
|
|
143
|
+
/** The file without Atlas's entries; `before` itself when there is none, so an untouched file is never reformatted. */
|
|
144
|
+
function removeMcpJson(before, obj) {
|
|
145
|
+
const servers = serverTable(obj, false);
|
|
146
|
+
const top = !!servers && 'faber-atlas' in servers;
|
|
147
|
+
if (top)
|
|
148
|
+
delete servers['faber-atlas'];
|
|
149
|
+
const projects = removeProjectEntries(obj);
|
|
150
|
+
return top || projects ? `${JSON.stringify(obj, null, 2)}\n` : before;
|
|
151
|
+
}
|
|
152
|
+
/** Backups get Atlas's own suffix, so they never collide with another tool's `.bak`, and keep the original's permissions. */
|
|
153
|
+
export const BACKUP_SUFFIX = '.faber-atlas.bak';
|
|
154
|
+
function writeBackup(filePath, content) {
|
|
155
|
+
const bakPath = `${filePath}${BACKUP_SUFFIX}`;
|
|
156
|
+
if (fs.existsSync(bakPath))
|
|
157
|
+
return undefined;
|
|
158
|
+
const mode = fs.statSync(filePath).mode & 0o777;
|
|
159
|
+
fs.writeFileSync(bakPath, content, { encoding: 'utf8', mode });
|
|
160
|
+
fs.chmodSync(bakPath, mode);
|
|
161
|
+
return bakPath;
|
|
162
|
+
}
|
|
163
|
+
function readOrNull(file) {
|
|
164
|
+
return fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : null;
|
|
165
|
+
}
|
|
166
|
+
/** A managed Markdown block added to or removed from a file (Antigravity's global rules, a project's AGENTS.md). */
|
|
167
|
+
function blockPlan(filePath, kind, engine, undo, edit) {
|
|
168
|
+
const before = readOrNull(filePath);
|
|
169
|
+
const after = undo && before === null ? '' : edit(before ?? '');
|
|
170
|
+
return { filePath, kind, engine, before, after, changed: (before ?? '') !== after && !(undo && before === null) };
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* One-time setup (FR-01): every tool gets one global entry, the Scout goes to the user's own folders, and nothing is
|
|
174
|
+
* written into a project unless `--agents-md` asks for it. Running it again in any folder changes nothing.
|
|
175
|
+
*/
|
|
176
|
+
export function planOrApplyInit(options = {}) {
|
|
177
|
+
// Only Claude Desktop's entry and --agents-md use a project folder.
|
|
178
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
179
|
+
const userHome = options.userHome ?? process.env.FABER_USER_HOME ?? os.homedir();
|
|
180
|
+
const preview = options.preview === true;
|
|
181
|
+
const undo = options.undo === true;
|
|
182
|
+
const shouldWrite = !preview;
|
|
183
|
+
const includeScout = options.scout !== false;
|
|
184
|
+
const specs = getEngineSpecs();
|
|
185
|
+
const engines = detectTargetEngines(userHome, options.engines);
|
|
186
|
+
const plans = [];
|
|
187
|
+
const backups = [];
|
|
188
|
+
const notes = [];
|
|
189
|
+
for (const engineName of engines) {
|
|
190
|
+
const spec = specs[engineName];
|
|
191
|
+
if (!spec) {
|
|
192
|
+
notes.push(`Unknown engine "${engineName}" skipped.`);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (spec.gap || !spec.mcpConfigFile) {
|
|
196
|
+
notes.push(`Engine "${engineName}" requires manual MCP wiring (GAP-02). Run: npx -y @fabersep/atlas mcp`);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const filePath = spec.mcpConfigFile(userHome);
|
|
200
|
+
const entry = atlasEntry(spec, root);
|
|
201
|
+
const before = readOrNull(filePath);
|
|
202
|
+
const parsed = parseConfig(before);
|
|
203
|
+
if (parsed === null) {
|
|
204
|
+
notes.push(`${filePath} is not plain JSON (comments or a half-written file?), so it was left unchanged. ` +
|
|
205
|
+
`Add the server by hand: "faber-atlas": ${JSON.stringify(entry)}`);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
if (spec.needsRoot && !undo) {
|
|
209
|
+
const previous = entryRoot(serverTable(parsed, false)?.['faber-atlas']);
|
|
210
|
+
notes.push(previous && previous !== root
|
|
211
|
+
? `${engineName} has no project folder: Atlas now maps ${root} (it mapped ${previous}).`
|
|
212
|
+
: `${engineName} has no project folder: Atlas maps ${root}. Run "init --engine ${engineName} --root <folder>" to change it.`);
|
|
213
|
+
}
|
|
214
|
+
const after = undo ? (before !== null ? removeMcpJson(before, parsed) : '') : upsertMcpJson(parsed, entry);
|
|
215
|
+
const changed = (before ?? '') !== after && !(undo && before === null);
|
|
216
|
+
plans.push({ filePath, kind: 'mcp-config', engine: engineName, before, after, changed });
|
|
217
|
+
}
|
|
218
|
+
if (includeScout && engineName === 'claude-code') {
|
|
219
|
+
const scoutFile = claudeScoutFilePath(userHome);
|
|
220
|
+
const sBefore = readOrNull(scoutFile);
|
|
221
|
+
const sAfter = undo
|
|
222
|
+
? ''
|
|
223
|
+
: renderClaudeScoutAgent(options.scoutModel ?? defaultScoutModel('claude-code'), options.scoutEffort ?? defaultScoutEffort('claude-code') ?? 'low');
|
|
224
|
+
plans.push({
|
|
225
|
+
filePath: scoutFile,
|
|
226
|
+
kind: 'scout-agent',
|
|
227
|
+
engine: 'claude-code',
|
|
228
|
+
before: sBefore,
|
|
229
|
+
after: sAfter,
|
|
230
|
+
changed: undo ? sBefore !== null : (sBefore ?? '') !== sAfter,
|
|
231
|
+
...(undo && sBefore !== null ? { deleted: true } : {}),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (includeScout && engineName === 'antigravity') {
|
|
235
|
+
const block = renderAntigravityScoutBlock(options.scoutModel ?? defaultScoutModel('antigravity'));
|
|
236
|
+
plans.push(blockPlan(antigravityRulesPath(userHome), 'scout-rules', 'antigravity', undo, (text) => undo ? removeScoutBlock(text) : upsertScoutBlock(text, block)));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// Opt-in, per project: a short navigation block in the project's AGENTS.md.
|
|
240
|
+
if (options.agentsMd) {
|
|
241
|
+
plans.push(blockPlan(path.join(root, 'AGENTS.md'), 'agents-md', 'project', undo, (text) => undo ? removeManagedBlock(text) : upsertManagedBlock(text, renderAgentsBlock())));
|
|
242
|
+
}
|
|
243
|
+
if (shouldWrite) {
|
|
244
|
+
for (const p of plans) {
|
|
245
|
+
if (!p.changed)
|
|
246
|
+
continue;
|
|
247
|
+
if (undo && p.before === null)
|
|
248
|
+
continue;
|
|
249
|
+
if (undo && p.deleted) {
|
|
250
|
+
if (fs.existsSync(p.filePath))
|
|
251
|
+
fs.unlinkSync(p.filePath);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
fs.mkdirSync(path.dirname(p.filePath), { recursive: true });
|
|
255
|
+
if (!undo && p.before !== null) {
|
|
256
|
+
const bakPath = writeBackup(p.filePath, p.before);
|
|
257
|
+
if (bakPath) {
|
|
258
|
+
p.backupPath = bakPath;
|
|
259
|
+
backups.push(bakPath);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
fs.writeFileSync(p.filePath, p.after, 'utf8');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return { root, applied: shouldWrite && !undo, preview, undo, plans, backups, notes };
|
|
266
|
+
}
|
|
267
|
+
export function formatInitPlan(result) {
|
|
268
|
+
const mode = result.undo
|
|
269
|
+
? 'UNDO COMPLETE'
|
|
270
|
+
: result.preview
|
|
271
|
+
? 'PREVIEW (no files modified)'
|
|
272
|
+
: 'READY (applied)';
|
|
273
|
+
const lines = [`FaberSEP Atlas init — ${mode}`, `Project root: ${result.root}`, ''];
|
|
274
|
+
for (const plan of result.plans) {
|
|
275
|
+
lines.push(`[${plan.engine}] ${plan.kind} — ${plan.filePath}`);
|
|
276
|
+
if (!plan.changed) {
|
|
277
|
+
lines.push(' (already up to date)');
|
|
278
|
+
}
|
|
279
|
+
else if (plan.deleted) {
|
|
280
|
+
lines.push(' - (removed file)');
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const beforeLines = (plan.before ?? '').split('\n');
|
|
284
|
+
const afterLines = plan.after.split('\n');
|
|
285
|
+
const removed = beforeLines.filter((l) => l && !afterLines.includes(l));
|
|
286
|
+
const added = afterLines.filter((l) => l && !beforeLines.includes(l));
|
|
287
|
+
for (const l of removed)
|
|
288
|
+
lines.push(` - ${l}`);
|
|
289
|
+
for (const l of added)
|
|
290
|
+
lines.push(` + ${l}`);
|
|
291
|
+
if (plan.backupPath)
|
|
292
|
+
lines.push(` backup: ${plan.backupPath}`);
|
|
293
|
+
}
|
|
294
|
+
lines.push('');
|
|
295
|
+
}
|
|
296
|
+
for (const note of result.notes)
|
|
297
|
+
lines.push(`Note: ${note}`);
|
|
298
|
+
if (result.preview) {
|
|
299
|
+
lines.push('Run "faber-atlas init" (without --preview) to apply these changes.');
|
|
300
|
+
}
|
|
301
|
+
else if (!result.undo) {
|
|
302
|
+
lines.push('Next: change Scout model/effort anytime with "faber-atlas scout", or revert with "faber-atlas init --undo".');
|
|
303
|
+
}
|
|
304
|
+
return lines.join('\n').trimEnd();
|
|
305
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export declare const SCOUT_BLOCK_START = "<!-- faber-atlas-scout:start -->";
|
|
2
|
+
export declare const SCOUT_BLOCK_END = "<!-- faber-atlas-scout:end -->";
|
|
3
|
+
export type ScoutEngine = 'claude-code' | 'antigravity';
|
|
4
|
+
export type EffortLevel = 'low' | 'medium' | 'high';
|
|
5
|
+
export interface ScoutTierOption {
|
|
6
|
+
id: string;
|
|
7
|
+
label: string;
|
|
8
|
+
recommended?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare const CLAUDE_SCOUT_TIERS: readonly ScoutTierOption[];
|
|
11
|
+
export declare const ANTIGRAVITY_SCOUT_TIERS: readonly ScoutTierOption[];
|
|
12
|
+
export declare const EFFORT_OPTIONS: readonly EffortLevel[];
|
|
13
|
+
export interface ScoutConfig {
|
|
14
|
+
engine: ScoutEngine;
|
|
15
|
+
model: string;
|
|
16
|
+
effort?: EffortLevel;
|
|
17
|
+
filePath: string;
|
|
18
|
+
installed: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare function defaultScoutModel(engine: ScoutEngine): string;
|
|
21
|
+
export declare function defaultScoutEffort(engine: ScoutEngine): EffortLevel | undefined;
|
|
22
|
+
/** The Scout lives in the user's own folders, so one setup serves every project. */
|
|
23
|
+
export declare function claudeScoutFilePath(userHome: string): string;
|
|
24
|
+
/** Antigravity's global rules file, read in every workspace. */
|
|
25
|
+
export declare function antigravityRulesPath(userHome: string): string;
|
|
26
|
+
export declare const defaultUserHome: () => string;
|
|
27
|
+
export declare function renderClaudeScoutAgent(model?: string, effort?: EffortLevel): string;
|
|
28
|
+
export declare function renderAntigravityScoutBlock(model?: string): string;
|
|
29
|
+
export declare function upsertScoutBlock(existing: string, block: string): string;
|
|
30
|
+
export declare function removeScoutBlock(existing: string): string;
|
|
31
|
+
export declare function readCurrentScoutConfigs(userHome?: string): ScoutConfig[];
|
|
32
|
+
export declare function setScoutConfig(options: {
|
|
33
|
+
userHome?: string;
|
|
34
|
+
engine: ScoutEngine;
|
|
35
|
+
model: string;
|
|
36
|
+
effort?: EffortLevel;
|
|
37
|
+
}): ScoutConfig;
|
|
38
|
+
/** Optional live model discovery for Antigravity (`agy models`), with safe offline fallback. */
|
|
39
|
+
export declare function discoverLiveAgyModels(timeoutMs?: number): {
|
|
40
|
+
ok: boolean;
|
|
41
|
+
lines: string[];
|
|
42
|
+
note?: string;
|
|
43
|
+
};
|
|
44
|
+
export declare function formatScoutStatus(configs: ScoutConfig[]): string;
|
|
45
|
+
export declare function runInteractiveScout(userHome: string, preferredEngine?: ScoutEngine): Promise<ScoutConfig>;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import readline from 'node:readline/promises';
|
|
6
|
+
import { stdin as input, stdout as output } from 'node:process';
|
|
7
|
+
export const SCOUT_BLOCK_START = '<!-- faber-atlas-scout:start -->';
|
|
8
|
+
export const SCOUT_BLOCK_END = '<!-- faber-atlas-scout:end -->';
|
|
9
|
+
export const CLAUDE_SCOUT_TIERS = [
|
|
10
|
+
{ id: 'haiku', label: 'haiku — fastest & cheapest (tracks latest Haiku)', recommended: true },
|
|
11
|
+
{ id: 'sonnet', label: 'sonnet — balanced reasoning for complex repos' },
|
|
12
|
+
{ id: 'opus', label: 'opus — maximum capability (highest cost)' },
|
|
13
|
+
{ id: 'inherit', label: 'inherit — match the primary session model' },
|
|
14
|
+
];
|
|
15
|
+
export const ANTIGRAVITY_SCOUT_TIERS = [
|
|
16
|
+
{ id: 'flash', label: 'flash — fast & cheap read-only explorer', recommended: true },
|
|
17
|
+
{ id: 'flash_lite', label: 'flash_lite — lightest & fastest for simple lookups' },
|
|
18
|
+
{ id: 'pro', label: 'pro — larger model for dense architectures' },
|
|
19
|
+
{ id: 'inherit', label: 'inherit — match the primary session model' },
|
|
20
|
+
];
|
|
21
|
+
export const EFFORT_OPTIONS = ['low', 'medium', 'high'];
|
|
22
|
+
export function defaultScoutModel(engine) {
|
|
23
|
+
return engine === 'claude-code' ? 'haiku' : 'flash';
|
|
24
|
+
}
|
|
25
|
+
export function defaultScoutEffort(engine) {
|
|
26
|
+
return engine === 'claude-code' ? 'low' : undefined;
|
|
27
|
+
}
|
|
28
|
+
/** The Scout lives in the user's own folders, so one setup serves every project. */
|
|
29
|
+
export function claudeScoutFilePath(userHome) {
|
|
30
|
+
return path.join(userHome, '.claude', 'agents', 'atlas-scout.md');
|
|
31
|
+
}
|
|
32
|
+
/** Antigravity's global rules file, read in every workspace. */
|
|
33
|
+
export function antigravityRulesPath(userHome) {
|
|
34
|
+
return path.join(userHome, '.gemini', 'GEMINI.md');
|
|
35
|
+
}
|
|
36
|
+
export const defaultUserHome = () => process.env.FABER_USER_HOME ?? os.homedir();
|
|
37
|
+
export function renderClaudeScoutAgent(model = 'haiku', effort = 'low') {
|
|
38
|
+
return [
|
|
39
|
+
'---',
|
|
40
|
+
'name: atlas-scout',
|
|
41
|
+
'description: Fast, read-only codebase explorer powered by FaberSEP Atlas MCP. Delegate code discovery, call-chain tracing, and impact analysis here before editing.',
|
|
42
|
+
`model: ${model}`,
|
|
43
|
+
`effort: ${effort}`,
|
|
44
|
+
'tools: mcp__faber-atlas__map, mcp__faber-atlas__brief, mcp__faber-atlas__neighborhood, mcp__faber-atlas__flow, mcp__faber-atlas__impact, mcp__faber-atlas__hubs, mcp__faber-atlas__health, Read',
|
|
45
|
+
'---',
|
|
46
|
+
'',
|
|
47
|
+
'You are **Atlas Scout**, a read-only codebase explorer. Your job is to locate exact `file:line` targets, call chains, and affected callers for the primary model using minimal tokens.',
|
|
48
|
+
'',
|
|
49
|
+
'## Protocol',
|
|
50
|
+
'1. Start with `map(zoom=0)` to identify relevant modules (~200 tokens).',
|
|
51
|
+
'2. Narrow down with `map(folder="<dir>", zoom=1)` to find exact files and top-level symbols.',
|
|
52
|
+
'3. Trace execution with `flow(symbol)` (downstream) or `impact(symbol)` (upstream callers & entrypoints).',
|
|
53
|
+
'4. Fetch signatures via `brief(files)` or `neighborhood(files, budget=...)` before reading raw files.',
|
|
54
|
+
'5. Return a concise structured summary: verified `file:line` pointers, relevant flow/impact chains, and any test files that must be updated. Never edit files.',
|
|
55
|
+
'',
|
|
56
|
+
].join('\n');
|
|
57
|
+
}
|
|
58
|
+
export function renderAntigravityScoutBlock(model = 'flash') {
|
|
59
|
+
return [
|
|
60
|
+
SCOUT_BLOCK_START,
|
|
61
|
+
'## Atlas Scout (Code Discovery)',
|
|
62
|
+
'',
|
|
63
|
+
`For codebase exploration, call tracing, or impact analysis, delegate a read-only subagent with \`Model: "${model}"\` using the \`faber-atlas\` MCP tools:`,
|
|
64
|
+
'1. Start with `map(zoom=0)` -> `map(folder=..., zoom=1)` to locate target files and symbols.',
|
|
65
|
+
'2. Use `flow(symbol)` / `impact(symbol)` to trace calls, implementations, and async events/jobs, and `brief` / `neighborhood` for signatures.',
|
|
66
|
+
'3. Return only verified `file:line` pointers, flow summaries, and affected tests to the primary session.',
|
|
67
|
+
SCOUT_BLOCK_END,
|
|
68
|
+
].join('\n');
|
|
69
|
+
}
|
|
70
|
+
function escapeRegex(s) {
|
|
71
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
72
|
+
}
|
|
73
|
+
export function upsertScoutBlock(existing, block) {
|
|
74
|
+
const regex = new RegExp(`${escapeRegex(SCOUT_BLOCK_START)}[\\s\\S]*?${escapeRegex(SCOUT_BLOCK_END)}`, 'g');
|
|
75
|
+
if (regex.test(existing)) {
|
|
76
|
+
let first = true;
|
|
77
|
+
const replaced = existing.replace(regex, () => {
|
|
78
|
+
if (first) {
|
|
79
|
+
first = false;
|
|
80
|
+
return block;
|
|
81
|
+
}
|
|
82
|
+
return '';
|
|
83
|
+
});
|
|
84
|
+
return `${replaced.replace(/\n{3,}/g, '\n\n').trimEnd()}\n`;
|
|
85
|
+
}
|
|
86
|
+
const trimmed = existing.trimEnd();
|
|
87
|
+
return trimmed ? `${trimmed}\n\n${block}\n` : `${block}\n`;
|
|
88
|
+
}
|
|
89
|
+
export function removeScoutBlock(existing) {
|
|
90
|
+
const regex = new RegExp(`\\n?${escapeRegex(SCOUT_BLOCK_START)}[\\s\\S]*?${escapeRegex(SCOUT_BLOCK_END)}\\n?`, 'g');
|
|
91
|
+
const next = existing.replace(regex, '\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
92
|
+
return next ? `${next}\n` : '';
|
|
93
|
+
}
|
|
94
|
+
export function readCurrentScoutConfigs(userHome = defaultUserHome()) {
|
|
95
|
+
const results = [];
|
|
96
|
+
// Claude Code
|
|
97
|
+
const claudeFile = claudeScoutFilePath(userHome);
|
|
98
|
+
if (fs.existsSync(claudeFile)) {
|
|
99
|
+
const raw = fs.readFileSync(claudeFile, 'utf8');
|
|
100
|
+
const model = raw.match(/^model:\s*(.+)$/m)?.[1]?.trim() ?? defaultScoutModel('claude-code');
|
|
101
|
+
const effortRaw = raw.match(/^effort:\s*(low|medium|high)$/m)?.[1];
|
|
102
|
+
results.push({ engine: 'claude-code', model, effort: effortRaw ?? 'low', filePath: claudeFile, installed: true });
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
results.push({ engine: 'claude-code', model: defaultScoutModel('claude-code'), effort: 'low', filePath: claudeFile, installed: false });
|
|
106
|
+
}
|
|
107
|
+
// Antigravity (managed block in its global rules)
|
|
108
|
+
const agentsFile = antigravityRulesPath(userHome);
|
|
109
|
+
if (fs.existsSync(agentsFile)) {
|
|
110
|
+
const raw = fs.readFileSync(agentsFile, 'utf8');
|
|
111
|
+
const hasBlock = raw.includes(SCOUT_BLOCK_START);
|
|
112
|
+
const block = raw.slice(raw.indexOf(SCOUT_BLOCK_START));
|
|
113
|
+
const model = (hasBlock ? block.match(/Model:\s*"([^"]+)"/)?.[1]?.trim() : undefined) ?? defaultScoutModel('antigravity');
|
|
114
|
+
results.push({ engine: 'antigravity', model, filePath: agentsFile, installed: hasBlock });
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
results.push({ engine: 'antigravity', model: defaultScoutModel('antigravity'), filePath: agentsFile, installed: false });
|
|
118
|
+
}
|
|
119
|
+
return results;
|
|
120
|
+
}
|
|
121
|
+
export function setScoutConfig(options) {
|
|
122
|
+
const userHome = options.userHome ?? defaultUserHome();
|
|
123
|
+
if (options.engine === 'claude-code') {
|
|
124
|
+
const filePath = claudeScoutFilePath(userHome);
|
|
125
|
+
const effort = options.effort ?? 'low';
|
|
126
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
127
|
+
fs.writeFileSync(filePath, renderClaudeScoutAgent(options.model, effort), 'utf8');
|
|
128
|
+
return { engine: 'claude-code', model: options.model, effort, filePath, installed: true };
|
|
129
|
+
}
|
|
130
|
+
const filePath = antigravityRulesPath(userHome);
|
|
131
|
+
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';
|
|
132
|
+
const updated = upsertScoutBlock(existing, renderAntigravityScoutBlock(options.model));
|
|
133
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
134
|
+
fs.writeFileSync(filePath, updated, 'utf8');
|
|
135
|
+
return { engine: 'antigravity', model: options.model, filePath, installed: true };
|
|
136
|
+
}
|
|
137
|
+
/** Optional live model discovery for Antigravity (`agy models`), with safe offline fallback. */
|
|
138
|
+
export function discoverLiveAgyModels(timeoutMs = 4000) {
|
|
139
|
+
try {
|
|
140
|
+
const out = execFileSync('agy', ['models'], { encoding: 'utf8', timeout: timeoutMs, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
141
|
+
const lines = out.split('\n').map((l) => l.trim()).filter(Boolean);
|
|
142
|
+
return { ok: true, lines };
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
146
|
+
return {
|
|
147
|
+
ok: false,
|
|
148
|
+
lines: [],
|
|
149
|
+
note: `Could not query "agy models" (${msg.split('\n')[0]}). Using stable tier aliases instead.`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export function formatScoutStatus(configs) {
|
|
154
|
+
const lines = ['FaberSEP Atlas — Scout Configuration', ''];
|
|
155
|
+
for (const cfg of configs) {
|
|
156
|
+
const state = cfg.installed ? 'configured' : 'not configured';
|
|
157
|
+
const effortStr = cfg.engine === 'claude-code'
|
|
158
|
+
? ` | effort: ${cfg.effort ?? 'low'}`
|
|
159
|
+
: ' | effort: n/a (subagent uses tier alias only)';
|
|
160
|
+
lines.push(` [${cfg.engine}] ${state} — model: ${cfg.model}${effortStr}`);
|
|
161
|
+
lines.push(` file: ${cfg.filePath}`);
|
|
162
|
+
}
|
|
163
|
+
lines.push('');
|
|
164
|
+
lines.push('Change anytime:');
|
|
165
|
+
lines.push(' faber-atlas scout --engine claude-code --model haiku --effort low');
|
|
166
|
+
lines.push(' faber-atlas scout --engine antigravity --model flash');
|
|
167
|
+
return lines.join('\n');
|
|
168
|
+
}
|
|
169
|
+
export async function runInteractiveScout(userHome, preferredEngine) {
|
|
170
|
+
const rl = readline.createInterface({ input, output });
|
|
171
|
+
try {
|
|
172
|
+
let engine = preferredEngine;
|
|
173
|
+
if (!engine) {
|
|
174
|
+
output.write('\nSelect target environment:\n 1) claude-code (subagent: .claude/agents/atlas-scout.md)\n 2) antigravity (Scout rules in ~/.gemini/GEMINI.md)\n');
|
|
175
|
+
const ans = (await rl.question('Choice [1]: ')).trim();
|
|
176
|
+
engine = ans === '2' ? 'antigravity' : 'claude-code';
|
|
177
|
+
}
|
|
178
|
+
const tiers = engine === 'claude-code' ? CLAUDE_SCOUT_TIERS : ANTIGRAVITY_SCOUT_TIERS;
|
|
179
|
+
output.write(`\nAvailable Scout models for ${engine}:\n`);
|
|
180
|
+
tiers.forEach((t, i) => {
|
|
181
|
+
output.write(` ${i + 1}) ${t.label}${t.recommended ? ' [recommended]' : ''}\n`);
|
|
182
|
+
});
|
|
183
|
+
output.write(` ${tiers.length + 1}) Custom model name\n`);
|
|
184
|
+
const modelChoice = (await rl.question('Choice [1]: ')).trim();
|
|
185
|
+
const idx = modelChoice ? Number(modelChoice) - 1 : 0;
|
|
186
|
+
let model = tiers[0].id;
|
|
187
|
+
if (idx >= 0 && idx < tiers.length) {
|
|
188
|
+
model = tiers[idx].id;
|
|
189
|
+
}
|
|
190
|
+
else if (idx === tiers.length) {
|
|
191
|
+
const custom = (await rl.question('Enter model name: ')).trim();
|
|
192
|
+
if (custom)
|
|
193
|
+
model = custom;
|
|
194
|
+
}
|
|
195
|
+
let effort;
|
|
196
|
+
if (engine === 'claude-code') {
|
|
197
|
+
output.write('\nSelect reasoning effort (low is fastest and recommended for Scout):\n 1) low [recommended]\n 2) medium\n 3) high\n');
|
|
198
|
+
const effChoice = (await rl.question('Choice [1]: ')).trim();
|
|
199
|
+
effort = effChoice === '2' ? 'medium' : effChoice === '3' ? 'high' : 'low';
|
|
200
|
+
}
|
|
201
|
+
const saved = setScoutConfig({ userHome, engine, model, effort });
|
|
202
|
+
output.write(`\nSaved ${saved.engine} Scout -> model: ${saved.model}${saved.effort ? `, effort: ${saved.effort}` : ''} (${saved.filePath})\n`);
|
|
203
|
+
return saved;
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
rl.close();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses JSON that may hold comments and trailing commas (tsconfig.json, VS Code settings). Strings are copied as
|
|
3
|
+
* they are, so a `//` inside a string is kept. Invalid JSON throws, as JSON.parse does.
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseJsonc(text: string): unknown;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses JSON that may hold comments and trailing commas (tsconfig.json, VS Code settings). Strings are copied as
|
|
3
|
+
* they are, so a `//` inside a string is kept. Invalid JSON throws, as JSON.parse does.
|
|
4
|
+
*/
|
|
5
|
+
export function parseJsonc(text) {
|
|
6
|
+
let out = '';
|
|
7
|
+
for (let i = 0; i < text.length; i++) {
|
|
8
|
+
const ch = text[i];
|
|
9
|
+
if (ch === '"') {
|
|
10
|
+
const end = stringEnd(text, i);
|
|
11
|
+
out += text.slice(i, end);
|
|
12
|
+
i = end - 1;
|
|
13
|
+
}
|
|
14
|
+
else if (ch === '/' && text[i + 1] === '/') {
|
|
15
|
+
while (i < text.length && text[i] !== '\n')
|
|
16
|
+
i++;
|
|
17
|
+
out += '\n';
|
|
18
|
+
}
|
|
19
|
+
else if (ch === '/' && text[i + 1] === '*') {
|
|
20
|
+
const close = text.indexOf('*/', i + 2);
|
|
21
|
+
i = close === -1 ? text.length : close + 1;
|
|
22
|
+
}
|
|
23
|
+
else if (ch !== ',' || !/^\s*[}\]]/.test(text.slice(i + 1, i + 200)))
|
|
24
|
+
out += ch;
|
|
25
|
+
}
|
|
26
|
+
return JSON.parse(out);
|
|
27
|
+
}
|
|
28
|
+
/** The index just after the string that starts at `start`. */
|
|
29
|
+
function stringEnd(text, start) {
|
|
30
|
+
let i = start + 1;
|
|
31
|
+
while (i < text.length && text[i] !== '"')
|
|
32
|
+
i += text[i] === '\\' ? 2 : 1;
|
|
33
|
+
return i + 1;
|
|
34
|
+
}
|