dsh-plugin-codegraph-tree-sitter 0.1.7 → 0.2.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/lib/cache.d.ts +65 -0
- package/lib/cache.js +142 -0
- package/lib/index.d.ts +31 -7
- package/lib/index.js +32 -7
- package/lib/schema.d.ts +2 -0
- package/lib/schema.js +27 -2
- package/lib/walk.d.ts +25 -2
- package/lib/walk.js +82 -25
- package/lib/watcher.d.ts +106 -41
- package/lib/watcher.js +307 -163
- package/package.json +3 -2
package/lib/cache.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse-result cache: the extraction this package computed for each file on the previous run, so a
|
|
3
|
+
* repeat index of an unchanged workspace can skip the read, the parse, and the tree walk for every
|
|
4
|
+
* file whose content did not change.
|
|
5
|
+
*
|
|
6
|
+
* The cache is an accelerator with no authority. It is read only as a hint — anything missing,
|
|
7
|
+
* unreadable, corrupt, written by another version, or shaped unexpectedly is discarded and the file
|
|
8
|
+
* is parsed again, so losing it costs time and never correctness. That is what lets
|
|
9
|
+
* {@link readParseCache} swallow every failure and {@link writeParseCache} tolerate a workspace this
|
|
10
|
+
* process may not write to.
|
|
11
|
+
* @module dsh-plugin-codegraph-tree-sitter/cache
|
|
12
|
+
*/
|
|
13
|
+
import type { FileExtraction } from './extract.ts';
|
|
14
|
+
import type { ExtractedFile } from './resolve.ts';
|
|
15
|
+
/**
|
|
16
|
+
* Version of the extraction semantics this cache records. Bump it by hand whenever `extract.ts` (or
|
|
17
|
+
* a grammar that feeds it) starts producing a different result for the same source text: a stale
|
|
18
|
+
* entry would otherwise be reused as if it were current, and the graph would silently keep the old
|
|
19
|
+
* shape.
|
|
20
|
+
*/
|
|
21
|
+
export declare const EXTRACTION_CACHE_VERSION = 1;
|
|
22
|
+
/** Path of the parse cache, relative to a project root. */
|
|
23
|
+
export declare const EXTRACTION_CACHE_RELATIVE_PATH = ".codegraph/cache/extractions.json";
|
|
24
|
+
/** One file's cached extraction, together with the identity it was computed from. */
|
|
25
|
+
export interface CachedFile {
|
|
26
|
+
/** Epoch milliseconds of the file's modification time when its extraction was produced. */
|
|
27
|
+
readonly modifiedAt: number;
|
|
28
|
+
/** File size in bytes at that same moment. */
|
|
29
|
+
readonly size: number;
|
|
30
|
+
/** SHA-256 of the content the extraction was produced from, hex-encoded. */
|
|
31
|
+
readonly contentHash: string;
|
|
32
|
+
/** Line count of that content, recorded so a reused file still gets a correct span. */
|
|
33
|
+
readonly lineCount: number;
|
|
34
|
+
/** Seam language label the extraction was produced for. */
|
|
35
|
+
readonly language: string;
|
|
36
|
+
/** The extraction itself. */
|
|
37
|
+
readonly extraction: FileExtraction;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Load the previous run's extractions for `projectRoot`.
|
|
41
|
+
*
|
|
42
|
+
* Never throws: a cache that cannot be read, parsed, or trusted yields an empty map, which makes the
|
|
43
|
+
* caller parse everything. A single malformed entry discards the whole file — an entry is either a
|
|
44
|
+
* complete record this package wrote or evidence the document was damaged, and reasoning about
|
|
45
|
+
* partial reuse is not worth the risk of a graph built from a half-written entry.
|
|
46
|
+
* @param projectRoot - absolute path of the workspace whose cache to load.
|
|
47
|
+
* @returns the cached extractions keyed by project-relative path, empty when none can be trusted.
|
|
48
|
+
*/
|
|
49
|
+
export declare function readParseCache(projectRoot: string): Promise<Map<string, CachedFile>>;
|
|
50
|
+
/**
|
|
51
|
+
* Record this run's extractions as the next run's cache.
|
|
52
|
+
*
|
|
53
|
+
* Written through a sibling temporary name and `rename`d into place, the same way `./schema.ts`
|
|
54
|
+
* replaces its status sidecar, so a reader never sees half a document. The temporary name is fixed
|
|
55
|
+
* rather than unique: a write interrupted before its rename is overwritten by the next attempt
|
|
56
|
+
* instead of accumulating, and a torn leftover is rejected by {@link readParseCache} at the cost of
|
|
57
|
+
* one run's reuse.
|
|
58
|
+
*
|
|
59
|
+
* A failure is swallowed on purpose. A read-only workspace, a full disk, or a locked directory must
|
|
60
|
+
* still be indexable; the cache is the optimization, never the product.
|
|
61
|
+
* @param projectRoot - absolute path of the workspace to record the cache for.
|
|
62
|
+
* @param files - every file this run produced an extraction for.
|
|
63
|
+
*/
|
|
64
|
+
export declare function writeParseCache(projectRoot: string, files: readonly ExtractedFile[]): Promise<void>;
|
|
65
|
+
//# sourceMappingURL=cache.d.ts.map
|
package/lib/cache.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse-result cache: the extraction this package computed for each file on the previous run, so a
|
|
3
|
+
* repeat index of an unchanged workspace can skip the read, the parse, and the tree walk for every
|
|
4
|
+
* file whose content did not change.
|
|
5
|
+
*
|
|
6
|
+
* The cache is an accelerator with no authority. It is read only as a hint — anything missing,
|
|
7
|
+
* unreadable, corrupt, written by another version, or shaped unexpectedly is discarded and the file
|
|
8
|
+
* is parsed again, so losing it costs time and never correctness. That is what lets
|
|
9
|
+
* {@link readParseCache} swallow every failure and {@link writeParseCache} tolerate a workspace this
|
|
10
|
+
* process may not write to.
|
|
11
|
+
* @module dsh-plugin-codegraph-tree-sitter/cache
|
|
12
|
+
*/
|
|
13
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { dirname, join } from 'node:path';
|
|
15
|
+
/**
|
|
16
|
+
* Version of the extraction semantics this cache records. Bump it by hand whenever `extract.ts` (or
|
|
17
|
+
* a grammar that feeds it) starts producing a different result for the same source text: a stale
|
|
18
|
+
* entry would otherwise be reused as if it were current, and the graph would silently keep the old
|
|
19
|
+
* shape.
|
|
20
|
+
*/
|
|
21
|
+
export const EXTRACTION_CACHE_VERSION = 1;
|
|
22
|
+
/** Path of the parse cache, relative to a project root. */
|
|
23
|
+
export const EXTRACTION_CACHE_RELATIVE_PATH = '.codegraph/cache/extractions.json';
|
|
24
|
+
/**
|
|
25
|
+
* Whether `value` is a plain JSON object. Arrays are deliberately included: every caller still
|
|
26
|
+
* validates the fields it needs, and an array simply carries none of them.
|
|
27
|
+
* @param value - the parsed JSON value to test.
|
|
28
|
+
* @returns whether it is a non-null object.
|
|
29
|
+
*/
|
|
30
|
+
function isRecord(value) {
|
|
31
|
+
return typeof value === 'object' && value !== null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Whether `value` is a usable extraction. Deliberately shallow past the four arrays: those are what
|
|
35
|
+
* `resolveWorkspace` indexes into, and the only expected producer is this package's own writer.
|
|
36
|
+
* @param value - the value to test.
|
|
37
|
+
* @returns whether it has the four collections an extraction consists of.
|
|
38
|
+
*/
|
|
39
|
+
function isFileExtraction(value) {
|
|
40
|
+
return isRecord(value)
|
|
41
|
+
&& Array.isArray(value.definitions)
|
|
42
|
+
&& Array.isArray(value.calls)
|
|
43
|
+
&& Array.isArray(value.imports)
|
|
44
|
+
&& Array.isArray(value.heritage);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether `value` is a complete cache entry.
|
|
48
|
+
* @param value - the value to test.
|
|
49
|
+
* @returns whether every field is present and of the type a reader will use it as.
|
|
50
|
+
*/
|
|
51
|
+
function isCachedFile(value) {
|
|
52
|
+
if (!isRecord(value))
|
|
53
|
+
return false;
|
|
54
|
+
return typeof value.modifiedAt === 'number'
|
|
55
|
+
&& typeof value.size === 'number'
|
|
56
|
+
&& typeof value.contentHash === 'string'
|
|
57
|
+
&& typeof value.lineCount === 'number'
|
|
58
|
+
&& typeof value.language === 'string'
|
|
59
|
+
&& isFileExtraction(value.extraction);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Load the previous run's extractions for `projectRoot`.
|
|
63
|
+
*
|
|
64
|
+
* Never throws: a cache that cannot be read, parsed, or trusted yields an empty map, which makes the
|
|
65
|
+
* caller parse everything. A single malformed entry discards the whole file — an entry is either a
|
|
66
|
+
* complete record this package wrote or evidence the document was damaged, and reasoning about
|
|
67
|
+
* partial reuse is not worth the risk of a graph built from a half-written entry.
|
|
68
|
+
* @param projectRoot - absolute path of the workspace whose cache to load.
|
|
69
|
+
* @returns the cached extractions keyed by project-relative path, empty when none can be trusted.
|
|
70
|
+
*/
|
|
71
|
+
export async function readParseCache(projectRoot) {
|
|
72
|
+
const cachePath = join(projectRoot, EXTRACTION_CACHE_RELATIVE_PATH);
|
|
73
|
+
let raw;
|
|
74
|
+
try {
|
|
75
|
+
raw = await readFile(cachePath, 'utf8');
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// The ordinary first run has no cache file yet; an unreadable one is the same answer to the
|
|
79
|
+
// caller — nothing to reuse — and must not fail an index that can still parse the workspace.
|
|
80
|
+
return new Map();
|
|
81
|
+
}
|
|
82
|
+
let document;
|
|
83
|
+
try {
|
|
84
|
+
document = JSON.parse(raw);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return new Map();
|
|
88
|
+
}
|
|
89
|
+
if (!isRecord(document))
|
|
90
|
+
return new Map();
|
|
91
|
+
if (document.cacheVersion !== EXTRACTION_CACHE_VERSION)
|
|
92
|
+
return new Map();
|
|
93
|
+
const entries = document.files;
|
|
94
|
+
if (!isRecord(entries))
|
|
95
|
+
return new Map();
|
|
96
|
+
const cache = new Map();
|
|
97
|
+
for (const [path, entry] of Object.entries(entries)) {
|
|
98
|
+
if (!isCachedFile(entry))
|
|
99
|
+
return new Map();
|
|
100
|
+
cache.set(path, entry);
|
|
101
|
+
}
|
|
102
|
+
return cache;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Record this run's extractions as the next run's cache.
|
|
106
|
+
*
|
|
107
|
+
* Written through a sibling temporary name and `rename`d into place, the same way `./schema.ts`
|
|
108
|
+
* replaces its status sidecar, so a reader never sees half a document. The temporary name is fixed
|
|
109
|
+
* rather than unique: a write interrupted before its rename is overwritten by the next attempt
|
|
110
|
+
* instead of accumulating, and a torn leftover is rejected by {@link readParseCache} at the cost of
|
|
111
|
+
* one run's reuse.
|
|
112
|
+
*
|
|
113
|
+
* A failure is swallowed on purpose. A read-only workspace, a full disk, or a locked directory must
|
|
114
|
+
* still be indexable; the cache is the optimization, never the product.
|
|
115
|
+
* @param projectRoot - absolute path of the workspace to record the cache for.
|
|
116
|
+
* @param files - every file this run produced an extraction for.
|
|
117
|
+
*/
|
|
118
|
+
export async function writeParseCache(projectRoot, files) {
|
|
119
|
+
const cachePath = join(projectRoot, EXTRACTION_CACHE_RELATIVE_PATH);
|
|
120
|
+
const tempPath = `${cachePath}.tmp`;
|
|
121
|
+
try {
|
|
122
|
+
const entries = {};
|
|
123
|
+
for (const file of files) {
|
|
124
|
+
entries[file.path] = {
|
|
125
|
+
modifiedAt: file.modifiedAt,
|
|
126
|
+
size: file.size,
|
|
127
|
+
contentHash: file.contentHash,
|
|
128
|
+
lineCount: file.lineCount,
|
|
129
|
+
language: file.language,
|
|
130
|
+
extraction: file.extraction,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const document = { cacheVersion: EXTRACTION_CACHE_VERSION, files: entries };
|
|
134
|
+
await mkdir(dirname(cachePath), { recursive: true });
|
|
135
|
+
await writeFile(tempPath, JSON.stringify(document));
|
|
136
|
+
await rename(tempPath, cachePath);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// See above: an unusable cache location only means the next run parses everything again.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=cache.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
* runs implicitly from a query — it is a caller-initiated, potentially multi-minute operation, kept
|
|
11
11
|
* out of the seam's read-only query path on purpose.
|
|
12
12
|
*
|
|
13
|
+
* Watching a root runs through `ctx.fs` (see `./watcher.ts`): the package observes each in-scope
|
|
14
|
+
* directory through the host's filesystem seam rather than opening paths itself, so a workspace a
|
|
15
|
+
* backend serves from another execution world is watched where it actually lives, and a backend that
|
|
16
|
+
* cannot watch at all degrades to explicit indexing instead of failing the plugin.
|
|
17
|
+
*
|
|
13
18
|
* Namespace plugin (named exports, no default export). Grammars load lazily, one per language, on
|
|
14
19
|
* first sight of a matching file, and stay cached for the process — never eagerly for every grammar
|
|
15
20
|
* this package ships with.
|
|
@@ -28,10 +33,12 @@ export type { ExtractedFile, GraphEdge, GraphNode, ResolvedGraph, UnresolvedRef
|
|
|
28
33
|
export { isMinifiedName, looksMinified, measureText, MINIFIED_AVERAGE_LINE_LENGTH } from './minified.ts';
|
|
29
34
|
export type { TextShape } from './minified.ts';
|
|
30
35
|
export { SCHEMA_VERSION, writeGraph } from './schema.ts';
|
|
36
|
+
export { EXTRACTION_CACHE_RELATIVE_PATH, EXTRACTION_CACHE_VERSION, readParseCache, writeParseCache } from './cache.ts';
|
|
37
|
+
export type { CachedFile } from './cache.ts';
|
|
31
38
|
export { isExcluded, walkAndExtract } from './walk.ts';
|
|
32
39
|
export type { WalkConfig, WalkResult } from './walk.ts';
|
|
33
|
-
export { createWatcher
|
|
34
|
-
export type { DegradeReason, WatchConfig,
|
|
40
|
+
export { createWatcher } from './watcher.ts';
|
|
41
|
+
export type { DegradeReason, WatchConfig, WatchDirEntry, WatchFileSystem, WatchTarget, Watcher, } from './watcher.ts';
|
|
35
42
|
export { decideWatch, readProcVersion } from './watch-policy.ts';
|
|
36
43
|
export type { WatchDecision, WatchPolicyInput } from './watch-policy.ts';
|
|
37
44
|
export { detectWorktree, execGit } from './worktree.ts';
|
|
@@ -60,7 +67,7 @@ export declare const DEFAULT_MAX_FILES = 50000;
|
|
|
60
67
|
export declare const DEFAULT_CONCURRENCY = 4;
|
|
61
68
|
/** Default quiet period after the last change before a watch-triggered refresh runs. */
|
|
62
69
|
export declare const DEFAULT_WATCH_DEBOUNCE_MS = 2000;
|
|
63
|
-
/** Default cap on directories watched individually,
|
|
70
|
+
/** Default cap on directories watched individually, one watch each, by the fs seam. */
|
|
64
71
|
export declare const DEFAULT_MAX_WATCHED_DIRECTORIES = 4000;
|
|
65
72
|
/** Plugin configuration: the indexer's identity and every deployment-varying bound on a run. */
|
|
66
73
|
export interface Config {
|
|
@@ -100,6 +107,22 @@ export interface Config {
|
|
|
100
107
|
maxFiles?: number;
|
|
101
108
|
/** Files parsed concurrently (default 4). */
|
|
102
109
|
concurrency?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Reuse the extraction a previous run cached for every file whose content is unchanged (default
|
|
112
|
+
* true), so a repeat index — or a watch rebuild after one edit — parses only what actually changed
|
|
113
|
+
* instead of the whole workspace. The cache lives at
|
|
114
|
+
* {@link EXTRACTION_CACHE_RELATIVE_PATH} and is rewritten after each successful run. Set false to
|
|
115
|
+
* parse every file every time, at the cost of the full parse each run.
|
|
116
|
+
*/
|
|
117
|
+
reuseParsedCache?: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* How a cached file's unchanged content is confirmed before its extraction is reused (default
|
|
120
|
+
* `'stat'`): `stat` compares the modification time and size the cache recorded and reads nothing;
|
|
121
|
+
* `hash` re-reads the file and compares a SHA-256, which also catches a rewrite that preserved both
|
|
122
|
+
* mtime and size (a checkout restoring an older file over a newer one). Hash mode still skips the
|
|
123
|
+
* parse and the tree walk, which dominate the per-file cost.
|
|
124
|
+
*/
|
|
125
|
+
cacheVerify?: 'stat' | 'hash';
|
|
103
126
|
/**
|
|
104
127
|
* Watch the workspace for file changes and refresh the index automatically after a successful
|
|
105
128
|
* `index()` call establishes a baseline (default true). Set `watch: false` to keep indexing purely
|
|
@@ -114,16 +137,17 @@ export interface Config {
|
|
|
114
137
|
/** Quiet period after the last change before a watch-triggered refresh runs, in ms (default 2000). */
|
|
115
138
|
watchDebounceMs?: number;
|
|
116
139
|
/**
|
|
117
|
-
* Hard cap on directories watched individually (default 4000).
|
|
118
|
-
*
|
|
119
|
-
*
|
|
140
|
+
* Hard cap on directories watched individually (default 4000). Watching goes through the host's
|
|
141
|
+
* filesystem seam, whose `watch` covers one directory's direct entries, so a workspace costs one
|
|
142
|
+
* watch per in-scope directory; exceeding this cap degrades that root's watcher rather than
|
|
143
|
+
* covering only part of the tree.
|
|
120
144
|
*/
|
|
121
145
|
maxWatchedDirectories?: number;
|
|
122
146
|
}
|
|
123
147
|
export declare const Config: z<Config>;
|
|
124
148
|
/**
|
|
125
149
|
* Register the tree-sitter indexer.
|
|
126
|
-
* @param ctx - the plugin context (must inject `codegraph`).
|
|
150
|
+
* @param ctx - the plugin context (must inject `codegraph` and `fs`).
|
|
127
151
|
* @param config - the resolved plugin configuration.
|
|
128
152
|
*/
|
|
129
153
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
* runs implicitly from a query — it is a caller-initiated, potentially multi-minute operation, kept
|
|
11
11
|
* out of the seam's read-only query path on purpose.
|
|
12
12
|
*
|
|
13
|
+
* Watching a root runs through `ctx.fs` (see `./watcher.ts`): the package observes each in-scope
|
|
14
|
+
* directory through the host's filesystem seam rather than opening paths itself, so a workspace a
|
|
15
|
+
* backend serves from another execution world is watched where it actually lives, and a backend that
|
|
16
|
+
* cannot watch at all degrades to explicit indexing instead of failing the plugin.
|
|
17
|
+
*
|
|
13
18
|
* Namespace plugin (named exports, no default export). Grammars load lazily, one per language, on
|
|
14
19
|
* first sight of a matching file, and stay cached for the process — never eagerly for every grammar
|
|
15
20
|
* this package ships with.
|
|
@@ -21,6 +26,7 @@ import z from '@deepseek-ai/schemastery';
|
|
|
21
26
|
import { CodegraphIndexerId } from 'dsh-plugin-codegraph-service';
|
|
22
27
|
import { LANGUAGE_TABLE } from "./languages.js";
|
|
23
28
|
import { walkAndExtract } from "./walk.js";
|
|
29
|
+
import { writeParseCache } from "./cache.js";
|
|
24
30
|
import { resolveWorkspace } from "./resolve.js";
|
|
25
31
|
import { writeGraph } from "./schema.js";
|
|
26
32
|
import { createWatcher } from "./watcher.js";
|
|
@@ -31,15 +37,16 @@ export { loadGitignore, matchesGitignore, parseGitignore } from "./gitignore.js"
|
|
|
31
37
|
export { resolveWorkspace } from "./resolve.js";
|
|
32
38
|
export { isMinifiedName, looksMinified, measureText, MINIFIED_AVERAGE_LINE_LENGTH } from "./minified.js";
|
|
33
39
|
export { SCHEMA_VERSION, writeGraph } from "./schema.js";
|
|
40
|
+
export { EXTRACTION_CACHE_RELATIVE_PATH, EXTRACTION_CACHE_VERSION, readParseCache, writeParseCache } from "./cache.js";
|
|
34
41
|
export { isExcluded, walkAndExtract } from "./walk.js";
|
|
35
|
-
export { createWatcher
|
|
42
|
+
export { createWatcher } from "./watcher.js";
|
|
36
43
|
export { decideWatch, readProcVersion } from "./watch-policy.js";
|
|
37
44
|
export { detectWorktree, execGit } from "./worktree.js";
|
|
38
45
|
export { HOOK_MARKER_BEGIN, HOOK_MARKER_END, installGitHooks, uninstallGitHooks } from "./git-hooks.js";
|
|
39
46
|
/** Cordis plugin name for loader diagnostics. */
|
|
40
47
|
export const name = 'codegraph-tree-sitter';
|
|
41
48
|
/** Services required by this plugin. */
|
|
42
|
-
export const inject = ['codegraph'];
|
|
49
|
+
export const inject = ['codegraph', 'fs'];
|
|
43
50
|
/** Path of the on-disk graph this package writes, relative to a project root. */
|
|
44
51
|
export const DATABASE_RELATIVE_PATH = '.codegraph/codegraph.db';
|
|
45
52
|
/** Default branded identity this indexer reserves on the seam. */
|
|
@@ -58,7 +65,7 @@ export const DEFAULT_MAX_FILES = 50_000;
|
|
|
58
65
|
export const DEFAULT_CONCURRENCY = 4;
|
|
59
66
|
/** Default quiet period after the last change before a watch-triggered refresh runs. */
|
|
60
67
|
export const DEFAULT_WATCH_DEBOUNCE_MS = 2_000;
|
|
61
|
-
/** Default cap on directories watched individually,
|
|
68
|
+
/** Default cap on directories watched individually, one watch each, by the fs seam. */
|
|
62
69
|
export const DEFAULT_MAX_WATCHED_DIRECTORIES = 4_000;
|
|
63
70
|
export const Config = z.object({
|
|
64
71
|
indexerId: z.string().default(DEFAULT_INDEXER_ID),
|
|
@@ -69,13 +76,15 @@ export const Config = z.object({
|
|
|
69
76
|
maxFileBytes: z.number().default(DEFAULT_MAX_FILE_BYTES),
|
|
70
77
|
maxFiles: z.number().default(DEFAULT_MAX_FILES),
|
|
71
78
|
concurrency: z.number().default(DEFAULT_CONCURRENCY),
|
|
79
|
+
reuseParsedCache: z.boolean().default(true),
|
|
80
|
+
cacheVerify: z.union([z.const('stat'), z.const('hash')]).default('stat'),
|
|
72
81
|
watch: z.boolean().default(true),
|
|
73
82
|
watchDebounceMs: z.number().default(DEFAULT_WATCH_DEBOUNCE_MS),
|
|
74
83
|
maxWatchedDirectories: z.number().default(DEFAULT_MAX_WATCHED_DIRECTORIES),
|
|
75
84
|
});
|
|
76
85
|
/**
|
|
77
86
|
* Register the tree-sitter indexer.
|
|
78
|
-
* @param ctx - the plugin context (must inject `codegraph`).
|
|
87
|
+
* @param ctx - the plugin context (must inject `codegraph` and `fs`).
|
|
79
88
|
* @param config - the resolved plugin configuration.
|
|
80
89
|
*/
|
|
81
90
|
export function apply(ctx, config) {
|
|
@@ -85,6 +94,13 @@ export function apply(ctx, config) {
|
|
|
85
94
|
assertPositiveInteger('concurrency', resolved.concurrency);
|
|
86
95
|
assertPositiveInteger('watchDebounceMs', resolved.watchDebounceMs);
|
|
87
96
|
assertPositiveInteger('maxWatchedDirectories', resolved.maxWatchedDirectories);
|
|
97
|
+
// Watching goes through the host's filesystem seam rather than `node:fs`, so it follows the same
|
|
98
|
+
// execution world the rest of the harness reads: a remote or confined backend watches its own paths.
|
|
99
|
+
// The `fs` service is a declared dependency (see `inject`), so it is present whenever `apply` runs.
|
|
100
|
+
// It is read structurally instead of importing `@deepseek-ai/dsh-fs`: that package is not a
|
|
101
|
+
// dependency here, and its `watch` method only exists from host 0.2 onward, while this package
|
|
102
|
+
// supports older peers that have no such member to import.
|
|
103
|
+
const fileSystem = ctx.fs;
|
|
88
104
|
// Keyed by project root, private to this plugin instance: never registered on `ctx`, so there is no
|
|
89
105
|
// cross-plugin relation here for an invariant to describe — just process-local bookkeeping this
|
|
90
106
|
// effect's cleanup below tears down on unload.
|
|
@@ -115,7 +131,7 @@ export function apply(ctx, config) {
|
|
|
115
131
|
console.error(`codegraph-tree-sitter: file watching for "${projectRoot}" stopped permanently (${reason.code}); ` +
|
|
116
132
|
'the index will no longer refresh on its own — run codegraph_index manually to update it.');
|
|
117
133
|
},
|
|
118
|
-
});
|
|
134
|
+
}, fileSystem);
|
|
119
135
|
watchers.set(projectRoot, watcher);
|
|
120
136
|
watcher.start();
|
|
121
137
|
}
|
|
@@ -169,7 +185,7 @@ async function runSync(projectRoot, config) {
|
|
|
169
185
|
* @returns the run's report.
|
|
170
186
|
*/
|
|
171
187
|
async function runIndex(projectRoot, config, signal) {
|
|
172
|
-
const { files, filesSkipped, filesMinified } = await walkAndExtract(projectRoot, {
|
|
188
|
+
const { files, filesSkipped, filesMinified, filesReused } = await walkAndExtract(projectRoot, {
|
|
173
189
|
exclude: config.exclude,
|
|
174
190
|
respectGitignore: config.respectGitignore,
|
|
175
191
|
skipMinified: config.skipMinified,
|
|
@@ -177,13 +193,21 @@ async function runIndex(projectRoot, config, signal) {
|
|
|
177
193
|
maxFiles: config.maxFiles,
|
|
178
194
|
concurrency: config.concurrency,
|
|
179
195
|
languages: config.languages,
|
|
196
|
+
reuseParsedCache: config.reuseParsedCache,
|
|
197
|
+
cacheVerify: config.cacheVerify,
|
|
180
198
|
}, signal);
|
|
181
199
|
signal?.throwIfAborted();
|
|
182
200
|
const indexedAt = Date.now();
|
|
183
201
|
const graph = resolveWorkspace(files, indexedAt);
|
|
184
202
|
signal?.throwIfAborted();
|
|
185
203
|
const databasePath = join(projectRoot, DATABASE_RELATIVE_PATH);
|
|
186
|
-
await writeGraph(databasePath, {
|
|
204
|
+
await writeGraph(databasePath, {
|
|
205
|
+
files, nodes: graph.nodes, edges: graph.edges, unresolved: graph.unresolved, filesMinified, indexedAt,
|
|
206
|
+
});
|
|
207
|
+
// Only after the graph is in place: the cache records the extractions this run actually committed,
|
|
208
|
+
// so a run that failed before writing its graph leaves the previous cache — and the mtimes it was
|
|
209
|
+
// built from — alone instead of vouching for a graph that was never written.
|
|
210
|
+
await writeParseCache(projectRoot, files);
|
|
187
211
|
const languageCounts = new Map();
|
|
188
212
|
for (const file of files)
|
|
189
213
|
languageCounts.set(file.language, (languageCounts.get(file.language) ?? 0) + 1);
|
|
@@ -195,6 +219,7 @@ async function runIndex(projectRoot, config, signal) {
|
|
|
195
219
|
filesIndexed: files.length,
|
|
196
220
|
filesSkipped,
|
|
197
221
|
filesMinified,
|
|
222
|
+
filesReused,
|
|
198
223
|
nodeCount: graph.nodes.length,
|
|
199
224
|
edgeCount: graph.edges.length,
|
|
200
225
|
unresolvedCount: graph.unresolved.length,
|
package/lib/schema.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export interface WriteInput {
|
|
|
19
19
|
readonly nodes: readonly GraphNode[];
|
|
20
20
|
readonly edges: readonly GraphEdge[];
|
|
21
21
|
readonly unresolved: readonly UnresolvedRef[];
|
|
22
|
+
/** Generated artifacts this run left out of the graph, carried into the status sidecar. */
|
|
23
|
+
readonly filesMinified: number;
|
|
22
24
|
readonly indexedAt: number;
|
|
23
25
|
}
|
|
24
26
|
/**
|
package/lib/schema.js
CHANGED
|
@@ -10,10 +10,11 @@
|
|
|
10
10
|
* naive "delete then recreate" leaves open between `CREATE TABLE` and the first `COMMIT`.
|
|
11
11
|
* @module dsh-plugin-codegraph-tree-sitter/schema
|
|
12
12
|
*/
|
|
13
|
-
import { mkdir, rename, rm } from 'node:fs/promises';
|
|
14
|
-
import { dirname } from 'node:path';
|
|
13
|
+
import { mkdir, rename, rm, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { basename, dirname, join } from 'node:path';
|
|
15
15
|
import { DatabaseSync } from 'node:sqlite';
|
|
16
16
|
import { randomUUID } from 'node:crypto';
|
|
17
|
+
import { INDEX_STATUS_RELATIVE_PATH } from 'dsh-plugin-codegraph-service';
|
|
17
18
|
/** The schema version this package writes, matching the format `dsh-codegraph-sqlite` reads. */
|
|
18
19
|
export const SCHEMA_VERSION = 4;
|
|
19
20
|
/**
|
|
@@ -122,5 +123,29 @@ export async function writeGraph(databasePath, input) {
|
|
|
122
123
|
// rename() only moves the main file; the temp name's own sidecars (should the driver have left any)
|
|
123
124
|
// never travel with it and would otherwise linger under the temp name forever.
|
|
124
125
|
await removeGraphFiles(tempPath);
|
|
126
|
+
await writeStatusFile(dirname(databasePath), {
|
|
127
|
+
indexedAt: input.indexedAt,
|
|
128
|
+
filesIndexed: input.files.length,
|
|
129
|
+
filesMinified: input.filesMinified,
|
|
130
|
+
nodeCount: input.nodes.length,
|
|
131
|
+
edgeCount: input.edges.length,
|
|
132
|
+
unresolvedCount: input.unresolved.length,
|
|
133
|
+
unresolvedLikelyInternalCount: input.unresolved.filter(ref => !ref.likelyExternal).length,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Write the prompt-time status sidecar beside the graph, replacing it atomically.
|
|
138
|
+
*
|
|
139
|
+
* Deliberately after the graph's own rename: a reader that sees this file can trust that the graph it
|
|
140
|
+
* summarizes is already in place, which is what lets a prompt line report "336 files, built 12s ago"
|
|
141
|
+
* without opening SQLite on every model request.
|
|
142
|
+
* @param directory - the graph's directory (`.codegraph`).
|
|
143
|
+
* @param status - this run's summary.
|
|
144
|
+
*/
|
|
145
|
+
async function writeStatusFile(directory, status) {
|
|
146
|
+
const target = join(directory, basename(INDEX_STATUS_RELATIVE_PATH));
|
|
147
|
+
const temp = `${target}.tmp`;
|
|
148
|
+
await writeFile(temp, JSON.stringify(status));
|
|
149
|
+
await rename(temp, target);
|
|
125
150
|
}
|
|
126
151
|
//# sourceMappingURL=schema.js.map
|
package/lib/walk.d.ts
CHANGED
|
@@ -46,6 +46,21 @@ export interface WalkConfig {
|
|
|
46
46
|
* workspace-wide name wins" call resolution would pick between them arbitrarily.
|
|
47
47
|
*/
|
|
48
48
|
readonly respectGitignore: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Reuse the extraction a previous run cached for every file whose content is unchanged, instead of
|
|
51
|
+
* reading and parsing it again. The cache lives under `<projectRoot>/.codegraph/cache` and is
|
|
52
|
+
* written by the indexer after a run succeeds; a missing or unusable cache simply means every file
|
|
53
|
+
* is parsed.
|
|
54
|
+
*/
|
|
55
|
+
readonly reuseParsedCache: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* How a cached file is confirmed unchanged before its extraction is reused. `'stat'` compares the
|
|
58
|
+
* modification time and size the cache recorded — no read at all, and enough for a watcher rebuild,
|
|
59
|
+
* whose changes always move mtime. `'hash'` reads the file and compares a SHA-256, which also
|
|
60
|
+
* catches a rewrite that preserved both mtime and size (a checkout restoring an old file) at the
|
|
61
|
+
* cost of the read it would otherwise save.
|
|
62
|
+
*/
|
|
63
|
+
readonly cacheVerify: 'stat' | 'hash';
|
|
49
64
|
}
|
|
50
65
|
/** What the walk produced. */
|
|
51
66
|
export interface WalkResult {
|
|
@@ -55,13 +70,21 @@ export interface WalkResult {
|
|
|
55
70
|
readonly filesSkipped: number;
|
|
56
71
|
/** Candidates skipped because they are generated output — see {@link WalkConfig.skipMinified}. */
|
|
57
72
|
readonly filesMinified: number;
|
|
73
|
+
/**
|
|
74
|
+
* Files whose extraction came from the parse cache instead of a fresh parse. These are still
|
|
75
|
+
* present in {@link files} and still indexed — the count says how much work the cache saved, not
|
|
76
|
+
* how much of the workspace was left out.
|
|
77
|
+
*/
|
|
78
|
+
readonly filesReused: number;
|
|
58
79
|
}
|
|
59
80
|
/**
|
|
60
|
-
* Walk `projectRoot` and parse every candidate file it finds
|
|
81
|
+
* Walk `projectRoot` and parse every candidate file it finds — or reuse the extraction a previous
|
|
82
|
+
* run cached for it.
|
|
61
83
|
* @param projectRoot - absolute path of the workspace to index.
|
|
62
84
|
* @param config - the walk's bounds.
|
|
63
85
|
* @param signal - aborts the walk.
|
|
64
|
-
* @returns the parsed files
|
|
86
|
+
* @returns the parsed files, how many candidates were skipped (over size, or past `maxFiles`), and
|
|
87
|
+
* how many files reused a cached extraction.
|
|
65
88
|
*/
|
|
66
89
|
export declare function walkAndExtract(projectRoot: string, config: WalkConfig, signal?: AbortSignal): Promise<WalkResult>;
|
|
67
90
|
//# sourceMappingURL=walk.d.ts.map
|