@denizokcu/haze 1.7.10 → 1.7.12
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 +25 -0
- package/README.md +125 -99
- package/assets/graph-grammars/NOTICE.md +21 -0
- package/assets/graph-grammars/tree-sitter-javascript.wasm +0 -0
- package/assets/graph-grammars/tree-sitter-tsx.wasm +0 -0
- package/assets/graph-grammars/tree-sitter-typescript.wasm +0 -0
- package/assets/graph-grammars/web-tree-sitter.cjs +4116 -0
- package/assets/graph-grammars/web-tree-sitter.cjs.map +7 -0
- package/assets/graph-grammars/web-tree-sitter.js +4056 -0
- package/assets/graph-grammars/web-tree-sitter.js.map +7 -0
- package/assets/graph-grammars/web-tree-sitter.wasm +0 -0
- package/assets/graph-grammars/web-tree-sitter.wasm.map +55 -0
- package/dist/buildInfo.json +3 -3
- package/dist/cli/chat/BusyBar.d.ts +2 -1
- package/dist/cli/chat/BusyBar.js +2 -2
- package/dist/cli/chat/chatMetrics.d.ts +25 -0
- package/dist/cli/chat/chatMetrics.js +36 -0
- package/dist/cli/chat/inputSuggestions.js +1 -0
- package/dist/cli/chat/sessionLifecycle.d.ts +2 -0
- package/dist/cli/chat/sessionLifecycle.js +29 -19
- package/dist/cli/chat/sessionRecorder.d.ts +2 -1
- package/dist/cli/chat/sessionRecorder.js +29 -9
- package/dist/cli/chat/startupSequence.d.ts +2 -0
- package/dist/cli/chat/startupSequence.js +6 -0
- package/dist/cli/chat/transcriptPartition.js +10 -4
- package/dist/cli/chat/wizard/providerHandlers.js +57 -9
- package/dist/cli/chat/wizard/sessionThemeHandlers.js +5 -1
- package/dist/cli/commands/chat.js +44 -9
- package/dist/cli/commands/commandHelp.js +6 -3
- package/dist/cli/commands/commands.js +2 -0
- package/dist/cli/commands/formatters.js +28 -0
- package/dist/cli/commands/graphCommand.d.ts +2 -0
- package/dist/cli/commands/graphCommand.js +60 -0
- package/dist/cli/commands/initCommand.js +14 -0
- package/dist/cli/commands/providerWizard.js +9 -17
- package/dist/cli/commands/reasoningCommand.d.ts +1 -0
- package/dist/cli/commands/reasoningCommand.js +32 -4
- package/dist/cli/commands/runCommand.js +7 -1
- package/dist/cli/commands/streaming/agentAttempt.js +11 -1
- package/dist/cli/commands/streaming/attemptLifecycle.js +1 -0
- package/dist/cli/commands/streaming/attemptOutcome.js +6 -2
- package/dist/cli/commands/streaming/attemptSetup.d.ts +3 -0
- package/dist/cli/commands/streaming/attemptSetup.js +13 -6
- package/dist/cli/commands/streaming/goalSupervisor.d.ts +1 -1
- package/dist/cli/commands/streaming/goalSupervisor.js +22 -1
- package/dist/cli/commands/streaming/prepareStep.js +1 -0
- package/dist/cli/commands/streaming/streamLoop.d.ts +2 -0
- package/dist/cli/commands/streaming/streamLoop.js +14 -10
- package/dist/cli/commands/streaming/toolPartHandlers.js +7 -1
- package/dist/cli/commands/streaming.d.ts +2 -0
- package/dist/cli/commands/streaming.js +5 -0
- package/dist/cli/commands/wizardFlow.d.ts +1 -0
- package/dist/cli/commands/wizardFlow.js +10 -4
- package/dist/cli/index.js +5 -3
- package/dist/config/hazeBuild.d.ts +2 -2
- package/dist/config/hazeBuild.js +2 -2
- package/dist/config/modelDiscovery.d.ts +1 -1
- package/dist/config/modelDiscovery.js +8 -8
- package/dist/config/{modelCatalog.d.ts → providerModelFacts.d.ts} +13 -2
- package/dist/config/{modelCatalog.js → providerModelFacts.js} +145 -21
- package/dist/config/providerPresets.d.ts +8 -19
- package/dist/config/providerPresets.js +86 -555
- package/dist/config/providerReasoning.d.ts +16 -0
- package/dist/config/providerReasoning.js +56 -0
- package/dist/config/providers.d.ts +4 -0
- package/dist/config/providers.js +19 -2
- package/dist/config/settings.d.ts +14 -0
- package/dist/config/settings.js +5 -0
- package/dist/core/agent/compaction.d.ts +1 -0
- package/dist/core/agent/compaction.js +3 -0
- package/dist/core/agent/events.d.ts +1 -1
- package/dist/core/agent/providerTransition.d.ts +8 -0
- package/dist/core/agent/providerTransition.js +47 -0
- package/dist/core/agent/reasoningPolicy.d.ts +1 -1
- package/dist/core/agent/reasoningPolicy.js +4 -2
- package/dist/core/graph/analyze.d.ts +61 -0
- package/dist/core/graph/analyze.js +274 -0
- package/dist/core/graph/buildGraph.d.ts +44 -0
- package/dist/core/graph/buildGraph.js +290 -0
- package/dist/core/graph/digest.d.ts +2 -0
- package/dist/core/graph/digest.js +40 -0
- package/dist/core/graph/extract.d.ts +58 -0
- package/dist/core/graph/extract.js +394 -0
- package/dist/core/graph/model.d.ts +63 -0
- package/dist/core/graph/model.js +96 -0
- package/dist/core/graph/query.d.ts +69 -0
- package/dist/core/graph/query.js +391 -0
- package/dist/core/graph/store.d.ts +64 -0
- package/dist/core/graph/store.js +171 -0
- package/dist/core/limits.d.ts +0 -2
- package/dist/core/limits.js +0 -2
- package/dist/core/process/runBoundedProcess.js +12 -6
- package/dist/core/providerContracts.d.ts +9 -8
- package/dist/core/providerContracts.js +21 -1
- package/dist/core/safety/redaction.js +12 -2
- package/dist/core/safety/untrustedEnvelopes.d.ts +6 -0
- package/dist/core/safety/untrustedEnvelopes.js +17 -2
- package/dist/core/session/sessionSlimming.js +5 -59
- package/dist/core/session/sessionStore.d.ts +4 -0
- package/dist/core/session/sessionStore.js +149 -9
- package/dist/core/session/snapshotCodec.d.ts +5 -0
- package/dist/core/session/snapshotCodec.js +47 -0
- package/dist/core/shellOutput/registry.js +2 -46
- package/dist/core/subagent/executionProfiles.js +8 -5
- package/dist/core/tasks/taskStorage.js +11 -1
- package/dist/core/toolOutput/reduction.d.ts +55 -2
- package/dist/core/toolOutput/reduction.js +77 -2
- package/dist/core/validation/outputParser.js +16 -8
- package/dist/llm/client.d.ts +4 -2
- package/dist/llm/client.js +104 -55
- package/dist/llm/hazeTools.d.ts +2 -12426
- package/dist/llm/hazeTools.js +2 -0
- package/dist/llm/lspTools.d.ts +3 -12622
- package/dist/llm/mistralReasoning.d.ts +2 -0
- package/dist/llm/mistralReasoning.js +17 -0
- package/dist/llm/mistralVibeAuth.d.ts +5 -0
- package/dist/llm/mistralVibeAuth.js +63 -0
- package/dist/llm/requestContext.d.ts +3 -0
- package/dist/llm/requestContext.js +21 -11
- package/dist/llm/subagentRunner.d.ts +2 -308
- package/dist/llm/subagentRunner.js +15 -7
- package/dist/llm/systemPrompt.js +7 -4
- package/dist/llm/toolResultTypes.d.ts +1 -1
- package/dist/llm/toolResultTypes.js +7 -1
- package/dist/llm/tools/fetchTool.d.ts +2 -1221
- package/dist/llm/tools/fileTools.d.ts +2 -7768
- package/dist/llm/tools/graphAdvisory.d.ts +22 -0
- package/dist/llm/tools/graphAdvisory.js +68 -0
- package/dist/llm/tools/graphTools.d.ts +13 -0
- package/dist/llm/tools/graphTools.js +195 -0
- package/dist/llm/tools/outputCap.d.ts +2 -7
- package/dist/llm/tools/outputCap.js +3 -11
- package/dist/llm/tools/processTool.d.ts +2 -1081
- package/dist/llm/tools/replaceInFiles.js +5 -0
- package/dist/llm/tools/shellTool.d.ts +2 -1513
- package/dist/llm/tools/storedOutputTool.d.ts +2 -355
- package/dist/llm/tools/taskTool.d.ts +2 -489
- package/dist/llm/tools/toolContext.js +24 -9
- package/dist/llm/tools/toolContextState.d.ts +3 -0
- package/dist/llm/tools/toolContextState.js +2 -1
- package/dist/skills/skillTools.js +4 -1
- package/package.json +24 -15
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type FileExtraction } from './extract.js';
|
|
2
|
+
import { type CodeGraph, type GraphEdge, type GraphNode } from './model.js';
|
|
3
|
+
import { type GraphStore } from './store.js';
|
|
4
|
+
export interface GraphBuildOptions {
|
|
5
|
+
/** Permit intentional shrink (mass deletion); otherwise a near-empty rebuild of a healthy graph is refused. */
|
|
6
|
+
force?: boolean;
|
|
7
|
+
/** Progress callback per processed file (used by /graph for user feedback). */
|
|
8
|
+
onProgress?: (processed: number, total: number) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface GraphBuildResult {
|
|
11
|
+
ok: boolean;
|
|
12
|
+
store: GraphStore;
|
|
13
|
+
graph: CodeGraph;
|
|
14
|
+
files: number;
|
|
15
|
+
skippedLarge: number;
|
|
16
|
+
parsedFiles: number;
|
|
17
|
+
cachedFiles: number;
|
|
18
|
+
durationMs: number;
|
|
19
|
+
incremental: boolean;
|
|
20
|
+
capped: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One-shot, non-blocking graph ensure for startup: kick off a single
|
|
24
|
+
* incremental build in the background on every startup — creating the graph
|
|
25
|
+
* when none exists yet, refreshing it otherwise (content-hash cached, so an
|
|
26
|
+
* unchanged workspace only re-runs cheap cross-file resolution) — and return
|
|
27
|
+
* its promise; while one is running, return the same promise. Never rejects —
|
|
28
|
+
* on failure the graph tools still report `graph_not_built` with a rebuild
|
|
29
|
+
* hint. Not a daemon: one build per process, no watching.
|
|
30
|
+
*/
|
|
31
|
+
export declare function ensureGraphInBackground(deps?: {
|
|
32
|
+
build?: () => Promise<GraphBuildResult>;
|
|
33
|
+
onActivity?: (building: boolean) => void;
|
|
34
|
+
}): Promise<GraphBuildResult | undefined>;
|
|
35
|
+
export declare function buildWorkspaceGraph(options?: GraphBuildOptions): Promise<GraphBuildResult>;
|
|
36
|
+
/**
|
|
37
|
+
* Pure corpus resolution: per-file extractions → graph nodes and edges with
|
|
38
|
+
* cross-file bindings resolved (imports rewritten through NodeNext `.js`
|
|
39
|
+
* specifiers, calls/heritage bound to imported symbols as INFERRED edges).
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveGraph(extractions: Map<string, FileExtraction>): {
|
|
42
|
+
nodes: GraphNode[];
|
|
43
|
+
edges: GraphEdge[];
|
|
44
|
+
};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph build orchestration: walk the workspace, extract (cache-first), and
|
|
3
|
+
* resolve cross-file relationships into the final edge set.
|
|
4
|
+
*
|
|
5
|
+
* Safety: the walk reuses the file tools' own confinement — in-process
|
|
6
|
+
* gitignore evaluation, `.git`/`node_modules` skipping (walkDir), cwd
|
|
7
|
+
* confinement, and the shared secret-path refusals — so the graph never reads
|
|
8
|
+
* a file the file tools would refuse. Parsing is deterministic tree-sitter
|
|
9
|
+
* only: no LLM, no network, no shell.
|
|
10
|
+
*
|
|
11
|
+
* Incremental behavior: extraction results are content-hash cached, so a
|
|
12
|
+
* rebuild only re-parses new/changed files; removed files drop out naturally.
|
|
13
|
+
* Cross-file resolution always re-runs — it is cheap and corpus-wide.
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs/promises';
|
|
16
|
+
import os from 'node:os';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import { walkDir } from '../../utils/fs.js';
|
|
19
|
+
import { workspaceRoot } from '../../utils/path.js';
|
|
20
|
+
import { createIgnoreClassifier } from '../../llm/tools/gitIgnore.js';
|
|
21
|
+
import { isProtectedSecretPath } from '../safety/secretPaths.js';
|
|
22
|
+
import { contentHash, extractFile, isGraphSupportedExtension, EXTRACTOR_VERSION, MAX_SOURCE_BYTES } from './extract.js';
|
|
23
|
+
import { detectCommunities } from './analyze.js';
|
|
24
|
+
import { GRAPH_SCHEMA_VERSION, symbolNodeId } from './model.js';
|
|
25
|
+
import { graphStoreFor, pruneExtractionCache, readCachedExtraction, writeCachedExtraction, writeGraph } from './store.js';
|
|
26
|
+
/** Upper bound on files considered per build; larger workspaces get a visible partial notice, not a silent hang. */
|
|
27
|
+
const GRAPH_MAX_FILES = 20_000;
|
|
28
|
+
const EXTRACTION_CACHE_MAX_ENTRIES = 40_000;
|
|
29
|
+
let backgroundBuildInFlight;
|
|
30
|
+
/**
|
|
31
|
+
* One-shot, non-blocking graph ensure for startup: kick off a single
|
|
32
|
+
* incremental build in the background on every startup — creating the graph
|
|
33
|
+
* when none exists yet, refreshing it otherwise (content-hash cached, so an
|
|
34
|
+
* unchanged workspace only re-runs cheap cross-file resolution) — and return
|
|
35
|
+
* its promise; while one is running, return the same promise. Never rejects —
|
|
36
|
+
* on failure the graph tools still report `graph_not_built` with a rebuild
|
|
37
|
+
* hint. Not a daemon: one build per process, no watching.
|
|
38
|
+
*/
|
|
39
|
+
export function ensureGraphInBackground(deps = {}) {
|
|
40
|
+
if (backgroundBuildInFlight)
|
|
41
|
+
return backgroundBuildInFlight;
|
|
42
|
+
const build = deps.build ?? (() => buildWorkspaceGraph());
|
|
43
|
+
backgroundBuildInFlight = (async () => {
|
|
44
|
+
deps.onActivity?.(true);
|
|
45
|
+
try {
|
|
46
|
+
return await build();
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
deps.onActivity?.(false);
|
|
50
|
+
}
|
|
51
|
+
})().catch(() => undefined).finally(() => {
|
|
52
|
+
backgroundBuildInFlight = undefined;
|
|
53
|
+
});
|
|
54
|
+
return backgroundBuildInFlight;
|
|
55
|
+
}
|
|
56
|
+
/** Candidate path resolutions for a relative import specifier (extensionless, ESM `.js`-style rewriting, index files). */
|
|
57
|
+
function resolveImportSpecifier(fromFile, specifier, fileSet) {
|
|
58
|
+
if (!specifier.startsWith('.'))
|
|
59
|
+
return undefined; // package imports carry no workspace edge
|
|
60
|
+
const base = path.posix.normalize(path.posix.join(path.posix.dirname(fromFile), specifier));
|
|
61
|
+
if (fileSet.has(base))
|
|
62
|
+
return base;
|
|
63
|
+
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.mts', '.cts', '.mjs', '.cjs'];
|
|
64
|
+
// NodeNext ESM rewrites: `./foo.js` may name foo.ts; strip the emitted
|
|
65
|
+
// extension and try every source extension (including the extensionless file).
|
|
66
|
+
for (const emitted of ['.js', '.mjs', '.cjs', '.jsx']) {
|
|
67
|
+
if (base.endsWith(emitted)) {
|
|
68
|
+
const stem = base.slice(0, -emitted.length);
|
|
69
|
+
if (fileSet.has(stem))
|
|
70
|
+
return stem;
|
|
71
|
+
for (const extension of extensions) {
|
|
72
|
+
if (fileSet.has(`${stem}${extension}`))
|
|
73
|
+
return `${stem}${extension}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const extension of extensions) {
|
|
78
|
+
if (fileSet.has(`${base}${extension}`))
|
|
79
|
+
return `${base}${extension}`;
|
|
80
|
+
}
|
|
81
|
+
for (const index of ['/index.ts', '/index.tsx', '/index.js']) {
|
|
82
|
+
if (fileSet.has(`${base}${index}`))
|
|
83
|
+
return `${base}${index}`;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
/** Attribute a source line to the nearest preceding symbol (call/heritage site), else the file node. */
|
|
88
|
+
function enclosingSymbolId(extraction, line) {
|
|
89
|
+
let best;
|
|
90
|
+
for (const symbol of extraction.symbols) {
|
|
91
|
+
if (symbol.line > line)
|
|
92
|
+
continue;
|
|
93
|
+
if (!best || symbol.line > best.line)
|
|
94
|
+
best = symbol;
|
|
95
|
+
}
|
|
96
|
+
return best?.id;
|
|
97
|
+
}
|
|
98
|
+
/** Nearest class whose definition precedes the line — best-effort owner for `this.method()` calls. */
|
|
99
|
+
function enclosingClassName(extraction, line) {
|
|
100
|
+
let best;
|
|
101
|
+
for (const symbol of extraction.symbols) {
|
|
102
|
+
if (symbol.kind !== 'class' || symbol.line > line)
|
|
103
|
+
continue;
|
|
104
|
+
if (!best || symbol.line > best.line)
|
|
105
|
+
best = symbol;
|
|
106
|
+
}
|
|
107
|
+
return best?.name;
|
|
108
|
+
}
|
|
109
|
+
export async function buildWorkspaceGraph(options = {}) {
|
|
110
|
+
const started = Date.now();
|
|
111
|
+
const root = workspaceRoot();
|
|
112
|
+
const store = graphStoreFor(root);
|
|
113
|
+
// --- Walk -----------------------------------------------------------------
|
|
114
|
+
const ignoreClassifier = createIgnoreClassifier(root);
|
|
115
|
+
const ignoreBatch = (entries) => ignoreClassifier.classify(entries);
|
|
116
|
+
const walked = await walkDir(root, { recursive: true, maxEntries: GRAPH_MAX_FILES * 3, ignoreBatch });
|
|
117
|
+
const homeDir = os.homedir();
|
|
118
|
+
const sourceFiles = walked
|
|
119
|
+
.filter(entry => entry.isFile && isGraphSupportedExtension(path.extname(entry.name).toLowerCase()))
|
|
120
|
+
.filter(entry => !isProtectedSecretPath(entry.absolutePath, homeDir))
|
|
121
|
+
.map(entry => entry.path.replace(/\\/g, '/'))
|
|
122
|
+
.sort();
|
|
123
|
+
const capped = sourceFiles.length > GRAPH_MAX_FILES;
|
|
124
|
+
const files = capped ? sourceFiles.slice(0, GRAPH_MAX_FILES) : sourceFiles;
|
|
125
|
+
// --- Extract (cache-first) -------------------------------------------------
|
|
126
|
+
const extractions = new Map();
|
|
127
|
+
let parsedFiles = 0;
|
|
128
|
+
let cachedFiles = 0;
|
|
129
|
+
let skippedLarge = 0;
|
|
130
|
+
for (const [index, file] of files.entries()) {
|
|
131
|
+
options.onProgress?.(index + 1, files.length);
|
|
132
|
+
let content;
|
|
133
|
+
try {
|
|
134
|
+
const absolute = path.join(root, file);
|
|
135
|
+
const stat = await fs.stat(absolute);
|
|
136
|
+
if (stat.size > MAX_SOURCE_BYTES) {
|
|
137
|
+
skippedLarge += 1;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
content = await fs.readFile(absolute, 'utf8');
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
continue; // Unreadable file: skip honestly; staleness reports it, builds never fail on one file.
|
|
144
|
+
}
|
|
145
|
+
const hash = contentHash(content);
|
|
146
|
+
const cached = await readCachedExtraction(store, hash);
|
|
147
|
+
let extraction;
|
|
148
|
+
if (cached && cached.file === file) {
|
|
149
|
+
extraction = cached;
|
|
150
|
+
cachedFiles += 1;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
try {
|
|
154
|
+
extraction = await extractFile(file, content);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
continue; // e.g. grammar unavailable; keep building the rest.
|
|
158
|
+
}
|
|
159
|
+
parsedFiles += 1;
|
|
160
|
+
await writeCachedExtraction(store, hash, extraction);
|
|
161
|
+
}
|
|
162
|
+
extractions.set(file, extraction);
|
|
163
|
+
}
|
|
164
|
+
// --- Resolve + persist --------------------------------------------------------
|
|
165
|
+
const { nodes, edges } = resolveGraph(extractions);
|
|
166
|
+
const graph = {
|
|
167
|
+
schemaVersion: GRAPH_SCHEMA_VERSION,
|
|
168
|
+
extractorVersion: EXTRACTOR_VERSION,
|
|
169
|
+
builtAt: new Date().toISOString(),
|
|
170
|
+
fileCount: files.length,
|
|
171
|
+
nodes,
|
|
172
|
+
edges,
|
|
173
|
+
};
|
|
174
|
+
detectCommunities(graph);
|
|
175
|
+
const signatures = {};
|
|
176
|
+
for (const file of files) {
|
|
177
|
+
try {
|
|
178
|
+
const stat = await fs.stat(path.join(root, file));
|
|
179
|
+
signatures[file] = { size: stat.size, mtimeMs: stat.mtimeMs };
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
// Deleted between walk and stat: omit; the next build reports it as removed.
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
await writeGraph(store, graph, { extractorVersion: EXTRACTOR_VERSION, workspaceRoot: root, files: signatures }, { force: options.force });
|
|
186
|
+
await pruneExtractionCache(store, EXTRACTION_CACHE_MAX_ENTRIES);
|
|
187
|
+
return { ok: true, store, graph, files: files.length, skippedLarge, parsedFiles, cachedFiles, durationMs: Date.now() - started, incremental: cachedFiles > 0, capped };
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Pure corpus resolution: per-file extractions → graph nodes and edges with
|
|
191
|
+
* cross-file bindings resolved (imports rewritten through NodeNext `.js`
|
|
192
|
+
* specifiers, calls/heritage bound to imported symbols as INFERRED edges).
|
|
193
|
+
*/
|
|
194
|
+
export function resolveGraph(extractions) {
|
|
195
|
+
const nodes = [];
|
|
196
|
+
const edges = [];
|
|
197
|
+
const fileSet = new Set(extractions.keys());
|
|
198
|
+
for (const [file, extraction] of extractions) {
|
|
199
|
+
nodes.push({ id: file, label: path.posix.basename(file), kind: 'file', file, line: 1 });
|
|
200
|
+
const taken = new Set([file]);
|
|
201
|
+
for (const symbol of extraction.symbols) {
|
|
202
|
+
nodes.push({ id: symbolNodeId(file, symbol.name, symbol.line, taken), label: symbol.name, kind: symbol.kind, file, line: symbol.line });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Resolve an exported name in a file to its symbol id (one re-export hop). */
|
|
206
|
+
const resolveExportedSymbol = (targetFile, exportedName, depth = 0) => {
|
|
207
|
+
const target = extractions.get(targetFile);
|
|
208
|
+
if (!target || depth > 1)
|
|
209
|
+
return undefined;
|
|
210
|
+
const direct = target.symbols.find(symbol => symbol.name === exportedName);
|
|
211
|
+
if (direct)
|
|
212
|
+
return direct.id;
|
|
213
|
+
for (const reExport of target.imports) {
|
|
214
|
+
if (reExport.kind !== 'export-from')
|
|
215
|
+
continue;
|
|
216
|
+
const resolved = resolveImportSpecifier(targetFile, reExport.specifier, fileSet);
|
|
217
|
+
if (!resolved)
|
|
218
|
+
continue;
|
|
219
|
+
const binding = reExport.bindings.find(candidate => candidate.importedName === exportedName);
|
|
220
|
+
if (binding)
|
|
221
|
+
return resolveExportedSymbol(resolved, binding.importedName, depth + 1);
|
|
222
|
+
}
|
|
223
|
+
return undefined;
|
|
224
|
+
};
|
|
225
|
+
for (const [file, extraction] of extractions) {
|
|
226
|
+
const firstSymbolByName = new Map();
|
|
227
|
+
for (const symbol of extraction.symbols) {
|
|
228
|
+
if (!firstSymbolByName.has(symbol.name))
|
|
229
|
+
firstSymbolByName.set(symbol.name, { id: symbol.id, kind: symbol.kind });
|
|
230
|
+
}
|
|
231
|
+
// local binding name (imports included) -> symbol id in another file
|
|
232
|
+
const importedBindingToSymbol = new Map();
|
|
233
|
+
for (const entry of extraction.imports) {
|
|
234
|
+
if (!entry.specifier.startsWith('.'))
|
|
235
|
+
continue;
|
|
236
|
+
const targetFile = resolveImportSpecifier(file, entry.specifier, fileSet);
|
|
237
|
+
if (!targetFile)
|
|
238
|
+
continue;
|
|
239
|
+
edges.push({ source: file, target: targetFile, relation: 'imports', confidence: 'EXTRACTED', file, line: entry.line });
|
|
240
|
+
for (const binding of entry.bindings) {
|
|
241
|
+
const targetId = resolveExportedSymbol(targetFile, binding.importedName);
|
|
242
|
+
if (targetId)
|
|
243
|
+
importedBindingToSymbol.set(binding.localName, targetId);
|
|
244
|
+
}
|
|
245
|
+
if (entry.defaultImport) {
|
|
246
|
+
// Default imports bind to the target's single callable export when
|
|
247
|
+
// unambiguous; several candidates stay unresolved rather than guessed.
|
|
248
|
+
const target = extractions.get(targetFile);
|
|
249
|
+
const callables = target.symbols.filter(symbol => target.exports.includes(symbol.name) && (symbol.kind === 'function' || symbol.kind === 'class'));
|
|
250
|
+
const unique = callables.length === 1 ? callables[0] : undefined;
|
|
251
|
+
if (unique)
|
|
252
|
+
importedBindingToSymbol.set(entry.defaultImport, unique.id);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
for (const call of extraction.calls) {
|
|
256
|
+
const sourceId = enclosingSymbolId(extraction, call.line);
|
|
257
|
+
if (call.thisCall) {
|
|
258
|
+
const className = enclosingClassName(extraction, call.line);
|
|
259
|
+
const targetId = className
|
|
260
|
+
? extraction.symbols.find(symbol => symbol.name === call.name && symbol.owner === className)?.id
|
|
261
|
+
: undefined;
|
|
262
|
+
if (targetId)
|
|
263
|
+
edges.push({ source: sourceId ?? file, target: targetId, relation: 'calls', confidence: 'EXTRACTED', file, line: call.line });
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const importedId = importedBindingToSymbol.get(call.name);
|
|
267
|
+
if (importedId) {
|
|
268
|
+
edges.push({ source: sourceId ?? file, target: importedId, relation: 'calls', confidence: 'INFERRED', file, line: call.line });
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const local = firstSymbolByName.get(call.name);
|
|
272
|
+
if (local && (local.kind === 'function' || local.kind === 'method' || local.kind === 'class')) {
|
|
273
|
+
edges.push({ source: sourceId ?? file, target: local.id, relation: 'calls', confidence: 'EXTRACTED', file, line: call.line });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
for (const heritage of extraction.heritage) {
|
|
277
|
+
const sourceId = enclosingSymbolId(extraction, heritage.line);
|
|
278
|
+
const importedId = importedBindingToSymbol.get(heritage.name);
|
|
279
|
+
const localId = firstSymbolByName.get(heritage.name);
|
|
280
|
+
const target = importedId ?? (localId?.kind === 'class' ? localId.id : undefined);
|
|
281
|
+
if (target) {
|
|
282
|
+
edges.push({ source: sourceId ?? file, target, relation: 'inherits', confidence: importedId ? 'INFERRED' : 'EXTRACTED', file, line: heritage.line });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
for (const symbol of extraction.symbols) {
|
|
286
|
+
edges.push({ source: file, target: symbol.id, relation: 'defines', confidence: 'EXTRACTED', file, line: symbol.line });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return { nodes, edges };
|
|
290
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compact structure digest for compaction: a bounded, deterministic excerpt
|
|
3
|
+
* of the code graph (top hub symbols + community directory labels) handed to
|
|
4
|
+
* LLM-written compaction summaries so they reference named structure instead
|
|
5
|
+
* of re-describing it in prose. The graph itself persists across compaction —
|
|
6
|
+
* this only keeps its vocabulary alive in the summarized prefix.
|
|
7
|
+
*/
|
|
8
|
+
import { workspaceRoot } from '../../utils/path.js';
|
|
9
|
+
import { buildGraphIndex } from './model.js';
|
|
10
|
+
import { graphStoreFor, readGraph } from './store.js';
|
|
11
|
+
import { communitySummaries, godNodes } from './analyze.js';
|
|
12
|
+
const DIGEST_MAX_CHARS = 2_000;
|
|
13
|
+
/** Build the digest, or undefined when no graph exists / it cannot be read. Best-effort by contract. */
|
|
14
|
+
export async function buildCodeGraphDigest() {
|
|
15
|
+
try {
|
|
16
|
+
const graph = await readGraph(graphStoreFor(workspaceRoot()));
|
|
17
|
+
if (!graph || graph.nodes.length === 0)
|
|
18
|
+
return undefined;
|
|
19
|
+
const index = buildGraphIndex(graph);
|
|
20
|
+
const lines = [];
|
|
21
|
+
const gods = godNodes(index, 6);
|
|
22
|
+
if (gods.length > 0) {
|
|
23
|
+
lines.push('Hub symbols:');
|
|
24
|
+
for (const god of gods)
|
|
25
|
+
lines.push(`- ${god.label} (${god.kind}, ${god.file})`);
|
|
26
|
+
}
|
|
27
|
+
const communities = communitySummaries(index, 5).filter(community => community.size >= 10);
|
|
28
|
+
if (communities.length > 0) {
|
|
29
|
+
lines.push('Subsystems (communities):');
|
|
30
|
+
for (const community of communities) {
|
|
31
|
+
const members = community.members.slice(0, 3).map(member => member.label).join(', ');
|
|
32
|
+
lines.push(`- [${community.directories.join(', ') || 'misc'}] ${members}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return lines.length > 0 ? lines.slice(0, 20).join('\n').slice(0, DIGEST_MAX_CHARS) : undefined;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type GraphNodeKind } from './model.js';
|
|
2
|
+
export declare const EXTRACTOR_VERSION = 4;
|
|
3
|
+
/** Largest source file parsed; bigger files are reported as skipped instead. */
|
|
4
|
+
export declare const MAX_SOURCE_BYTES: number;
|
|
5
|
+
export declare function isGraphSupportedExtension(extension: string): boolean;
|
|
6
|
+
export interface ExtractedSymbol {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
kind: GraphNodeKind;
|
|
10
|
+
line: number;
|
|
11
|
+
/** Enclosing class name for methods/fields; absent on top-level symbols. */
|
|
12
|
+
owner?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ImportedBinding {
|
|
15
|
+
/** Name exported by the source module. */
|
|
16
|
+
importedName: string;
|
|
17
|
+
/** Local name after aliasing (`as`); equals importedName when not aliased. */
|
|
18
|
+
localName: string;
|
|
19
|
+
}
|
|
20
|
+
export interface FileImport {
|
|
21
|
+
/** Raw module specifier as written (./foo.js, ../bar, pkg). */
|
|
22
|
+
specifier: string;
|
|
23
|
+
line: number;
|
|
24
|
+
kind: 'import' | 'export-from' | 'require';
|
|
25
|
+
defaultImport?: string;
|
|
26
|
+
namespaceImport?: string;
|
|
27
|
+
bindings: ImportedBinding[];
|
|
28
|
+
}
|
|
29
|
+
export interface CallSite {
|
|
30
|
+
/** Called identifier, or property name for `this.method()` member calls. */
|
|
31
|
+
name: string;
|
|
32
|
+
line: number;
|
|
33
|
+
/** True for `this.x()` calls; resolvable only against the enclosing class. */
|
|
34
|
+
thisCall?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface HeritageEntry {
|
|
37
|
+
/** Superclass identifier as written. */
|
|
38
|
+
name: string;
|
|
39
|
+
line: number;
|
|
40
|
+
}
|
|
41
|
+
export interface FileExtraction {
|
|
42
|
+
version: number;
|
|
43
|
+
file: string;
|
|
44
|
+
symbols: ExtractedSymbol[];
|
|
45
|
+
imports: FileImport[];
|
|
46
|
+
calls: CallSite[];
|
|
47
|
+
heritage: HeritageEntry[];
|
|
48
|
+
/** Names exported for other files (declaration name or explicit export list). */
|
|
49
|
+
exports: string[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Extract one file's structure. `file` is the workspace-relative path used for
|
|
53
|
+
* node ids; `content` is the UTF-8 source. Throws on parser/grammar problems;
|
|
54
|
+
* syntax errors inside the source degrade gracefully (tree-sitter keeps
|
|
55
|
+
* parsing; partial results are still honest structure).
|
|
56
|
+
*/
|
|
57
|
+
export declare function extractFile(file: string, content: string): Promise<FileExtraction>;
|
|
58
|
+
export declare function contentHash(content: string): string;
|