opencode-rag-plugin 2.0.0 → 2.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/dist/index.d.ts CHANGED
@@ -3,7 +3,10 @@
3
3
  * low-level building blocks (chunkers, embedders, vector stores, retrieval),
4
4
  * high-level convenience API (search, index, getContext), and plugin entry.
5
5
  */
6
- export { chunkFile, getChunker, registerChunker } from "./chunker/factory.js";
6
+ export { chunkFile, getChunker, registerChunker, getRegisteredLanguages, getExtensionsForLanguage, validateParserOverrides } from "./chunker/factory.js";
7
+ export { extractSkeleton } from "./chunker/skeleton.js";
8
+ export { normalizeParserOverrides } from "./core/parser-overrides.js";
9
+ export type { ParserOverrides } from "./core/parser-overrides.js";
7
10
  export { createEmbedder, embedBatch } from "./embedder/factory.js";
8
11
  export { createDescriptionProvider } from "./describer/factory.js";
9
12
  export { createVectorStore } from "./vectorstore/factory.js";
package/dist/index.js CHANGED
@@ -3,7 +3,9 @@
3
3
  * low-level building blocks (chunkers, embedders, vector stores, retrieval),
4
4
  * high-level convenience API (search, index, getContext), and plugin entry.
5
5
  */
6
- export { chunkFile, getChunker, registerChunker } from "./chunker/factory.js";
6
+ export { chunkFile, getChunker, registerChunker, getRegisteredLanguages, getExtensionsForLanguage, validateParserOverrides } from "./chunker/factory.js";
7
+ export { extractSkeleton } from "./chunker/skeleton.js";
8
+ export { normalizeParserOverrides } from "./core/parser-overrides.js";
7
9
  export { createEmbedder, embedBatch } from "./embedder/factory.js";
8
10
  export { createDescriptionProvider } from "./describer/factory.js";
9
11
  export { createVectorStore } from "./vectorstore/factory.js";
@@ -10,6 +10,7 @@ import { DescriptionCache } from "../core/desc-cache.js";
10
10
  import { embedBatch, probeEmbeddingDimension } from "../embedder/factory.js";
11
11
  import { createVectorStore } from "../vectorstore/factory.js";
12
12
  import { swapStoreDirectories } from "../vectorstore/lancedb.js";
13
+ import { reconcileQuirks } from "../quirks/quirk-store.js";
13
14
  import { createIndexStats } from "./stats.js";
14
15
  import { prepareFile, buildTextsToEmbed } from "./worker.js";
15
16
  import { tagChunksRole } from "../core/chunk-role.js";
@@ -1027,6 +1028,31 @@ async function runIndexPassInner(options, logger) {
1027
1028
  logger.warn("Rebuild did not complete — keeping the previous manifest and keyword index.");
1028
1029
  }
1029
1030
  else {
1031
+ // Reconcile quirks.jsonl into the store + keyword index BEFORE the
1032
+ // pass-end save. Two failure modes this heals:
1033
+ // (a) rebuild swaps rebuild the table from workspace files only — the
1034
+ // preserved quirks.jsonl was never re-embedded, so recall silently
1035
+ // missed every quirk added before the rebuild;
1036
+ // (b) saving our in-memory keyword index without the jsonl quirks
1037
+ // clobbers entries that other processes (plugin/CLI) added since we
1038
+ // loaded it.
1039
+ // Best-effort: a reconcile failure must never fail the pass.
1040
+ try {
1041
+ const rec = await reconcileQuirks({
1042
+ embedder: options.embedder,
1043
+ store: options.store,
1044
+ keywordIndex: options.keywordIndex,
1045
+ cfg: options.config,
1046
+ storePath: options.storePath,
1047
+ });
1048
+ if (rec.restoredToStore > 0 || rec.addedToKeywordIndex > 0 || rec.removedOrphans > 0) {
1049
+ logger.info(`Quirk reconcile: restored ${rec.restoredToStore} to store, ` +
1050
+ `added ${rec.addedToKeywordIndex} to keyword index, removed ${rec.removedOrphans} orphans`);
1051
+ }
1052
+ }
1053
+ catch (err) {
1054
+ logger.warn(`Quirk reconcile failed: ${err.message}`);
1055
+ }
1030
1056
  // Save manifest and keyword index (always to the real store path — after
1031
1057
  // a successful swap this points to the new data).
1032
1058
  await saveManifest(options.storePath, manifest);
@@ -114,6 +114,7 @@ export declare function prepareFile(file: WorkspaceFile, cwd: string, previous:
114
114
  };
115
115
  chunking?: {
116
116
  nodeTypes?: Record<string, string[]>;
117
+ parsers?: Record<string, string>;
117
118
  };
118
119
  description?: {
119
120
  maxContentChars?: number;
@@ -163,6 +164,7 @@ export declare function processFile(file: WorkspaceFile, cwd: string, previous:
163
164
  };
164
165
  chunking?: {
165
166
  nodeTypes?: Record<string, string[]>;
167
+ parsers?: Record<string, string>;
166
168
  };
167
169
  description?: {
168
170
  maxContentChars?: number;
@@ -119,6 +119,7 @@ export async function prepareFile(file, cwd, previous, config, keywordIndex, des
119
119
  maxSvgSizeBytes: config.indexing?.maxSvgSizeBytes,
120
120
  maxChunkSize: config.indexing?.maxChunkSize,
121
121
  chunkOverlap: config.indexing?.chunkOverlap,
122
+ languageByExtension: config.chunking?.parsers,
122
123
  }).catch((err) => {
123
124
  logger.warn(` ${fileLabel} (chunking failed: ${err.message})`);
124
125
  return null;
@@ -5,6 +5,7 @@ import { type EmbeddingProvider, type VectorStore, type KeywordIndex, type Searc
5
5
  import type { RagConfig } from "../core/config.js";
6
6
  import { type ImageVisionProvider } from "../chunker/image.js";
7
7
  import { retrieve } from "../retriever/retriever.js";
8
+ import type { ParserOverrides } from "../core/parser-overrides.js";
8
9
  /**
9
10
  * Resolve a user-supplied file path against the worktree root, refusing to
10
11
  * escape the worktree. Absolute paths are accepted only if they already reside
@@ -56,7 +57,7 @@ export interface FileSkeletonResult {
56
57
  summary: string;
57
58
  }
58
59
  /** Extract the structural skeleton (functions, classes, interfaces) from a source file. */
59
- export declare function handleFileSkeleton(params: FileSkeletonParams, worktree: string): Promise<FileSkeletonResult>;
60
+ export declare function handleFileSkeleton(params: FileSkeletonParams, worktree: string, languageByExtension?: ParserOverrides): Promise<FileSkeletonResult>;
60
61
  /** Parameters for the find_usages MCP tool. */
61
62
  export interface FindUsagesParams {
62
63
  /** Symbol name to search for. */
@@ -6,8 +6,7 @@ import { normalizeFileExtensions } from "../core/filters.js";
6
6
  import { SUPPORTED_IMAGE_EXTENSIONS } from "../chunker/image.js";
7
7
  import { retrieve } from "../retriever/retriever.js";
8
8
  import { optimizeContext, DEFAULT_CONTEXT_OPTIMIZATION } from "../retriever/context-optimizer.js";
9
- import { Parser } from "web-tree-sitter";
10
- import { initParser, loadLanguage, walkTree } from "../chunker/grammar.js";
9
+ import { extractSkeleton, getExtension } from "../chunker/skeleton.js";
11
10
  import { readFileSync } from "node:fs";
12
11
  import { resolve, isAbsolute, relative, sep as pathSep } from "node:path";
13
12
  /**
@@ -31,105 +30,8 @@ export function resolveFilePath(filePath, worktree) {
31
30
  }
32
31
  return candidate;
33
32
  }
34
- const SKELETON_CONFIGS = {
35
- ".ts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
36
- ".tsx": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration", "arrow_function"] },
37
- ".mts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
38
- ".cts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
39
- ".js": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
40
- ".jsx": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
41
- ".mjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
42
- ".cjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
43
- ".py": { grammarName: "python", nodeTypes: ["function_definition", "class_definition", "decorated_definition"] },
44
- ".java": { grammarName: "java", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration"] },
45
- ".go": { grammarName: "go", nodeTypes: ["function_declaration", "method_declaration", "type_declaration", "type_spec"] },
46
- ".rs": { grammarName: "rust", nodeTypes: ["function_item", "struct_item", "enum_item", "impl_item", "trait_item", "type_item"] },
47
- ".c": { grammarName: "c", nodeTypes: ["function_definition", "struct_specifier", "enum_specifier"] },
48
- ".cpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
49
- ".cxx": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
50
- ".h": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
51
- ".hpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
52
- ".cs": { grammarName: "c-sharp", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration", "struct_declaration"] },
53
- ".rb": { grammarName: "ruby", nodeTypes: ["method", "class", "module"] },
54
- ".swift": { grammarName: "swift", nodeTypes: ["function_declaration", "class_declaration", "struct_declaration", "enum_declaration", "protocol_declaration"] },
55
- ".kt": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
56
- ".kts": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
57
- ".css": { grammarName: "css", nodeTypes: ["rule_set"] },
58
- ".md": { grammarName: "markdown", nodeTypes: ["atx_heading"] },
59
- };
60
- const REGEX_SKELETON = {
61
- ".json": [/^(\s*)"(\w+)":/gm],
62
- ".yaml": [/^(\w+):/gm],
63
- ".yml": [/^(\w+):/gm],
64
- ".toml": [/^\[(\w+)\]/gm],
65
- ".sh": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
66
- ".bash": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
67
- ".zsh": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
68
- ".sql": [/^(CREATE|ALTER|DROP|SELECT|INSERT|UPDATE|DELETE)\s/gim],
69
- };
70
- function getExtension(filePath) {
71
- const dot = filePath.lastIndexOf(".");
72
- return dot >= 0 ? filePath.slice(dot).toLowerCase() : "";
73
- }
74
- async function extractSkeleton(content, ext) {
75
- const config = SKELETON_CONFIGS[ext];
76
- if (!config) {
77
- const patterns = REGEX_SKELETON[ext];
78
- if (!patterns) {
79
- const lines = content.split("\n");
80
- return [{ type: "file", name: `${lines.length} lines`, startLine: 1, endLine: lines.length }];
81
- }
82
- const items = [];
83
- for (const pattern of patterns) {
84
- let match;
85
- pattern.lastIndex = 0;
86
- while ((match = pattern.exec(content)) !== null) {
87
- const name = match[1] ?? match[0].trim();
88
- const lineNum = content.slice(0, match.index).split("\n").length;
89
- items.push({ type: ext.slice(1), name, startLine: lineNum, endLine: lineNum });
90
- }
91
- }
92
- const seen = new Set();
93
- return items.filter((item) => {
94
- const key = `${item.type}:${item.name}`;
95
- if (seen.has(key))
96
- return false;
97
- seen.add(key);
98
- return true;
99
- });
100
- }
101
- await initParser();
102
- const lang = await loadLanguage(config.grammarName);
103
- const parser = new Parser();
104
- parser.setLanguage(lang);
105
- const tree = parser.parse(content);
106
- if (!tree)
107
- return [];
108
- const nodeTypes = new Set(config.nodeTypes);
109
- const astNodes = walkTree(tree.rootNode, nodeTypes, content, 15);
110
- tree.delete();
111
- parser.delete();
112
- return astNodes.map((node) => ({
113
- type: node.type,
114
- name: extractNodeName(node.text, node.type),
115
- startLine: node.startLine,
116
- endLine: node.endLine,
117
- }));
118
- }
119
- function extractNodeName(text, _nodeType) {
120
- const firstLine = text.split("\n")[0] ?? text;
121
- const nameMatch = firstLine.match(/^(?:export\s+)?(?:async\s+)?(?:function\s+)?(?:\w+\s+)?(?:(\w+))\s*(?:[<(]|$)/);
122
- if (nameMatch) {
123
- const keywordSet = new Set(["export", "async", "function", "class", "interface", "type", "enum", "struct", "impl", "trait", "def", "fun", "fn"]);
124
- if (!keywordSet.has(nameMatch[1])) {
125
- return nameMatch[1];
126
- }
127
- const secondMatch = firstLine.match(/^(?:export\s+)?(?:async\s+)?(?:function|class|interface|type|enum|struct|trait|impl|def|fun|fn)\s+(\w+)/);
128
- if (secondMatch)
129
- return secondMatch[1];
130
- }
131
- return firstLine.length > 80 ? firstLine.slice(0, 77) + "..." : firstLine;
132
- }
33
+ // Skeleton configuration and extraction live in ../chunker/skeleton.ts —
34
+ // shared with the OpenCode plugin tools so both honor `chunking.parsers`.
133
35
  function typeIcon(type) {
134
36
  if (type.includes("class") || type === "struct_specifier" || type === "struct_item" || type === "struct_declaration")
135
37
  return "□";
@@ -211,11 +113,11 @@ export async function handleSearchSemantic(params, embedder, store, cfg, keyword
211
113
  return { chunks: results, formatted };
212
114
  }
213
115
  /** Extract the structural skeleton (functions, classes, interfaces) from a source file. */
214
- export async function handleFileSkeleton(params, worktree) {
116
+ export async function handleFileSkeleton(params, worktree, languageByExtension) {
215
117
  const resolvedPath = resolveFilePath(params.filePath, worktree);
216
118
  const content = readFileSync(resolvedPath, "utf-8");
217
119
  const ext = getExtension(params.filePath);
218
- const skeleton = await extractSkeleton(content, ext);
120
+ const skeleton = await extractSkeleton(content, ext, { languageByExtension });
219
121
  const declCounts = new Map();
220
122
  for (const item of skeleton) {
221
123
  const baseType = item.type.replace(/_declaration$|_definition$|_specifier$|_item$/, "");
@@ -43,7 +43,7 @@ export async function createMcpServer(options) {
43
43
  filePath: z.string().min(1, "A file path is required."),
44
44
  }, async (args) => {
45
45
  try {
46
- const result = await handleFileSkeleton(args, cwd);
46
+ const result = await handleFileSkeleton(args, cwd, ctx.config.chunking?.parsers);
47
47
  return {
48
48
  content: [{ type: "text", text: result.formatted }],
49
49
  };
@@ -110,6 +110,7 @@ export function createRagReadTool(options) {
110
110
  ragChunks,
111
111
  relatedFiles,
112
112
  maxChars: maxReadOutputChars,
113
+ languageByExtension: config.chunking?.parsers,
113
114
  });
114
115
  // 6. Return
115
116
  return {
@@ -2,6 +2,7 @@
2
2
  * @fileoverview Formats RAG retrieval output, hybrid file-content output, file fallbacks, and related-file suggestions for the read tool.
3
3
  */
4
4
  import type { SearchResult } from "../core/interfaces.js";
5
+ import { type ParserOverrides } from "../core/parser-overrides.js";
5
6
  /**
6
7
  * Options for formatting read tool output.
7
8
  */
@@ -44,6 +45,8 @@ export interface FormatHybridReadOutputOptions {
44
45
  relatedFiles: RelatedFileEntry[];
45
46
  /** Maximum output character count. */
46
47
  maxChars: number;
48
+ /** Extension → parser overrides from `chunking.parsers` (for the code-fence language). */
49
+ languageByExtension?: ParserOverrides;
47
50
  }
48
51
  /**
49
52
  * Format hybrid read output: full file contents followed by optional RAG context.
@@ -76,6 +79,8 @@ export interface FormatFileFallbackOptions {
76
79
  reason: string;
77
80
  /** Maximum output character count. */
78
81
  maxChars?: number;
82
+ /** Extension → parser overrides from `chunking.parsers` (for the code-fence language). */
83
+ languageByExtension?: ParserOverrides;
79
84
  }
80
85
  /**
81
86
  * Format raw file contents as a fallback when no RAG chunks are available.
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @fileoverview Formats RAG retrieval output, hybrid file-content output, file fallbacks, and related-file suggestions for the read tool.
3
3
  */
4
+ import { normalizeParserOverrides } from "../core/parser-overrides.js";
4
5
  /**
5
6
  * Format RAG retrieval results for the search_semantic tool output.
6
7
  *
@@ -45,8 +46,8 @@ export function formatReadOutput(options) {
45
46
  * - Enforces maxChars limit (truncates RAG section first).
46
47
  */
47
48
  export function formatHybridReadOutput(options) {
48
- const { filePath, fileContent, startLine, endLine, ragChunks, relatedFiles, maxChars } = options;
49
- const lang = guessLanguage(filePath);
49
+ const { filePath, fileContent, startLine, endLine, ragChunks, relatedFiles, maxChars, languageByExtension } = options;
50
+ const lang = guessLanguage(filePath, languageByExtension);
50
51
  // Build the full file code block
51
52
  const lines = fileContent.split("\n");
52
53
  const sliceStart = startLine !== undefined ? startLine - 1 : 0;
@@ -139,12 +140,12 @@ function formatChunk(index, result) {
139
140
  * Applies optional line-range slicing and enforces maxChars limit.
140
141
  */
141
142
  export function formatFileFallback(options) {
142
- const { filePath, content, startLine, endLine, maxChars } = options;
143
+ const { filePath, content, startLine, endLine, maxChars, languageByExtension } = options;
143
144
  const lines = content.split("\n");
144
145
  const sliceStart = startLine !== undefined ? startLine - 1 : 0;
145
146
  const sliceEnd = endLine !== undefined ? endLine : lines.length;
146
147
  const sliced = lines.slice(sliceStart, sliceEnd);
147
- const lang = guessLanguage(filePath);
148
+ const lang = guessLanguage(filePath, languageByExtension);
148
149
  const codeBlock = "```" + lang + "\n" + sliced.join("\n") + "\n```";
149
150
  let output = codeBlock;
150
151
  if (maxChars && output.length > maxChars) {
@@ -152,8 +153,14 @@ export function formatFileFallback(options) {
152
153
  }
153
154
  return output;
154
155
  }
155
- function guessLanguage(filePath) {
156
+ function guessLanguage(filePath, languageByExtension) {
156
157
  const ext = filePath.split(".").pop()?.toLowerCase() ?? "";
158
+ const overrides = normalizeParserOverrides(languageByExtension);
159
+ const target = ext ? overrides[`.${ext}`] : undefined;
160
+ // A `chunking.parsers` override wins: it says which parser reads this file,
161
+ // so it is also the right label for the code fence.
162
+ if (target)
163
+ return target;
157
164
  const map = {
158
165
  ts: "typescript", tsx: "typescript", js: "javascript", jsx: "javascript",
159
166
  py: "python", rb: "ruby", go: "go", rs: "rust", java: "java",
@@ -6,9 +6,12 @@ import { type EmbeddingProvider, type VectorStore, type KeywordIndex } from "../
6
6
  import type { RagConfig } from "../core/config.js";
7
7
  import { type ImageVisionProvider } from "../chunker/image.js";
8
8
  import { retrieve } from "../retriever/retriever.js";
9
+ import type { ParserOverrides } from "../core/parser-overrides.js";
9
10
  /** Options for creating the `get_file_skeleton` tool. */
10
11
  export interface FileSkeletonToolOptions {
11
12
  worktree: string;
13
+ /** Extension → parser overrides from `chunking.parsers`. */
14
+ languageByExtension?: ParserOverrides;
12
15
  }
13
16
  /**
14
17
  * Create the `get_file_skeleton` tool.
@@ -17,137 +17,19 @@ import { CODE_SEARCH_FILTER } from "../core/interfaces.js";
17
17
  import { SUPPORTED_IMAGE_EXTENSIONS, createImageVisionProvider, resolveOnDemandImageConfig, getMimeType } from "../chunker/image.js";
18
18
  import { resizeImage } from "../content/image.js";
19
19
  import { retrieve } from "../retriever/retriever.js";
20
- import { Parser } from "web-tree-sitter";
21
- import { initParser, loadLanguage, walkTree } from "../chunker/grammar.js";
20
+ import { extractSkeleton, getExtension } from "../chunker/skeleton.js";
22
21
  import { readFileSync } from "node:fs";
23
22
  import { resolveWorkspacePath } from "./tool-args.js";
24
23
  import { addQuirk, updateQuirk, removeQuirk, recallQuirks } from "../quirks/quirk-store.js";
25
- const SKELETON_CONFIGS = {
26
- ".ts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
27
- ".tsx": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration", "arrow_function"] },
28
- ".mts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
29
- ".cts": { grammarName: "typescript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration"] },
30
- ".js": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
31
- ".jsx": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
32
- ".mjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
33
- ".cjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
34
- ".py": { grammarName: "python", nodeTypes: ["function_definition", "class_definition", "decorated_definition"] },
35
- ".java": { grammarName: "java", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration"] },
36
- ".go": { grammarName: "go", nodeTypes: ["function_declaration", "method_declaration", "type_declaration", "type_spec"] },
37
- ".rs": { grammarName: "rust", nodeTypes: ["function_item", "struct_item", "enum_item", "impl_item", "trait_item", "type_item"] },
38
- ".c": { grammarName: "c", nodeTypes: ["function_definition", "struct_specifier", "enum_specifier"] },
39
- ".cpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
40
- ".cxx": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
41
- ".h": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
42
- ".hpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
43
- ".cs": { grammarName: "c-sharp", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration", "struct_declaration"] },
44
- ".rb": { grammarName: "ruby", nodeTypes: ["method", "class", "module"] },
45
- ".swift": { grammarName: "swift", nodeTypes: ["function_declaration", "class_declaration", "struct_declaration", "enum_declaration", "protocol_declaration"] },
46
- ".kt": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
47
- ".kts": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
48
- ".css": { grammarName: "css", nodeTypes: ["rule_set"] },
49
- ".md": { grammarName: "markdown", nodeTypes: ["atx_heading"] },
50
- };
51
- /** Regex-based fallback for languages without tree-sitter WASM support. */
52
- const REGEX_SKELETON = {
53
- ".json": [/^(\s*)"(\w+)":/gm],
54
- ".yaml": [/^(\w+):/gm],
55
- ".yml": [/^(\w+):/gm],
56
- ".toml": [/^\[(\w+)\]/gm],
57
- ".sh": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
58
- ".bash": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
59
- ".zsh": [/^(function\s+\w+|^\w+\s*\(\)\s*\{)/gm],
60
- ".sql": [/^(CREATE|ALTER|DROP|SELECT|INSERT|UPDATE|DELETE)\s/gim],
61
- };
62
- /**
63
- * Return the file extension from a path (lowercase).
64
- */
65
- function getExtension(filePath) {
66
- const dot = filePath.lastIndexOf(".");
67
- return dot >= 0 ? filePath.slice(dot).toLowerCase() : "";
68
- }
24
+ // Skeleton configuration (extension → grammar + node types) and extraction
25
+ // live in ../chunker/skeleton.ts, shared with the MCP server so both surfaces
26
+ // honor `chunking.parsers` overrides identically.
69
27
  /**
70
28
  * Resolve a file path relative to the workspace root.
71
29
  */
72
30
  function resolveFilePath(filePath, worktree) {
73
31
  return resolveWorkspacePath(worktree, filePath);
74
32
  }
75
- /**
76
- * Extract structural outline from source code using tree-sitter.
77
- * Falls back to regex patterns for languages without WASM grammars.
78
- */
79
- async function extractSkeleton(content, ext) {
80
- const config = SKELETON_CONFIGS[ext];
81
- if (!config) {
82
- // Fallback: use regex patterns
83
- const patterns = REGEX_SKELETON[ext];
84
- if (!patterns) {
85
- // Last fallback: line count
86
- const lines = content.split("\n");
87
- return [{ type: "file", name: `${lines.length} lines`, startLine: 1, endLine: lines.length }];
88
- }
89
- const items = [];
90
- for (const pattern of patterns) {
91
- let match;
92
- // Reset lastIndex
93
- pattern.lastIndex = 0;
94
- while ((match = pattern.exec(content)) !== null) {
95
- const name = match[1] ?? match[0].trim();
96
- const lineNum = content.slice(0, match.index).split("\n").length;
97
- items.push({ type: ext.slice(1), name, startLine: lineNum, endLine: lineNum });
98
- }
99
- }
100
- const seen = new Set();
101
- return items.filter((item) => {
102
- const key = `${item.type}:${item.name}`;
103
- if (seen.has(key))
104
- return false;
105
- seen.add(key);
106
- return true;
107
- });
108
- }
109
- // Tree-sitter skeleton extraction
110
- await initParser();
111
- const lang = await loadLanguage(config.grammarName);
112
- const parser = new Parser();
113
- parser.setLanguage(lang);
114
- const tree = parser.parse(content);
115
- if (!tree)
116
- return [];
117
- const nodeTypes = new Set(config.nodeTypes);
118
- const astNodes = walkTree(tree.rootNode, nodeTypes, content, 15);
119
- tree.delete();
120
- parser.delete();
121
- return astNodes.map((node) => ({
122
- type: node.type,
123
- name: extractNodeName(node.text, node.type),
124
- startLine: node.startLine,
125
- endLine: node.endLine,
126
- }));
127
- }
128
- /**
129
- * Extract a human-readable name from a code node.
130
- * For declarations, this is typically the identifier after the keyword.
131
- */
132
- function extractNodeName(text, _nodeType) {
133
- // Try first line only for large nodes
134
- const firstLine = text.split("\n")[0] ?? text;
135
- // Common patterns: "function foo(...)" or "class Foo ..." or "foo(...)" (arrow, method)
136
- const nameMatch = firstLine.match(/^(?:export\s+)?(?:async\s+)?(?:function\s+)?(?:\w+\s+)?(?:(\w+))\s*(?:[<(]|$)/);
137
- if (nameMatch) {
138
- // Skip keywords
139
- const keywordSet = new Set(["export", "async", "function", "class", "interface", "type", "enum", "struct", "impl", "trait", "def", "fun", "fn"]);
140
- if (!keywordSet.has(nameMatch[1])) {
141
- return nameMatch[1];
142
- }
143
- // Try next word
144
- const secondMatch = firstLine.match(/^(?:export\s+)?(?:async\s+)?(?:function|class|interface|type|enum|struct|trait|impl|def|fun|fn)\s+(\w+)/);
145
- if (secondMatch)
146
- return secondMatch[1];
147
- }
148
- // Last resort: return first 80 chars of first line
149
- return firstLine.length > 80 ? firstLine.slice(0, 77) + "..." : firstLine;
150
- }
151
33
  /**
152
34
  * Format a skeleton outline as a compact markdown string.
153
35
  */
@@ -193,7 +75,7 @@ function typeIcon(type) {
193
75
  * with line numbers, using tree-sitter for parsing.
194
76
  */
195
77
  export function createFileSkeletonTool(options) {
196
- const { worktree } = options;
78
+ const { worktree, languageByExtension } = options;
197
79
  return tool({
198
80
  description: "Get a quick structural overview of a source file — functions, classes, " +
199
81
  "interfaces, methods, and other top-level declarations with their line numbers. " +
@@ -207,7 +89,7 @@ export function createFileSkeletonTool(options) {
207
89
  const resolvedPath = resolveFilePath(args.filePath, worktree);
208
90
  const content = readFileSync(resolvedPath, "utf-8");
209
91
  const ext = getExtension(args.filePath);
210
- const skeleton = await extractSkeleton(content, ext);
92
+ const skeleton = await extractSkeleton(content, ext, { languageByExtension });
211
93
  const declCounts = new Map();
212
94
  for (const item of skeleton) {
213
95
  const baseType = item.type.replace(/_declaration$|_definition$|_specifier$|_item$/, "");
package/dist/plugin.js CHANGED
@@ -28,7 +28,7 @@ import { countTokens } from "./eval/token-counter.js";
28
28
  import { checkForUpdate, getCurrentVersion, installLatestUpdate } from "./core/version-check.js";
29
29
  import { loadAutoUpdateState, saveAutoUpdateState, shouldAttemptInstall } from "./core/auto-update-state.js";
30
30
  import { destroyAllPooledConnections } from "./embedder/http.js";
31
- import { listQuirks, lintQuirks, recallQuirks, sharedWords } from "./quirks/quirk-store.js";
31
+ import { listQuirks, lintQuirks, recallQuirks, sharedWords, reconcileQuirks } from "./quirks/quirk-store.js";
32
32
  import { autoCaptureQuirks } from "./quirks/auto-capture.js";
33
33
  import { buildSystemGuidanceLines } from "./opencode/system-guidance.js";
34
34
  import { existsSync, readFileSync, readdirSync, unlinkSync } from "node:fs";
@@ -580,6 +580,7 @@ export function createRagHooks(options) {
580
580
  try {
581
581
  const fileSkeletonTool = createFileSkeletonTool({
582
582
  worktree: options.worktree,
583
+ languageByExtension: effectiveCfg.chunking?.parsers,
583
584
  });
584
585
  tools["get_file_skeleton"] = fileSkeletonTool;
585
586
  }
@@ -1452,6 +1453,26 @@ export const ragPlugin = async (input, _options) => {
1452
1453
  }
1453
1454
  // Load or create keyword index for hybrid search
1454
1455
  const keywordIndex = await loadKeywordIndex(storePath, logFilePath, logLevel);
1456
+ // Restore quirks that are in quirks.jsonl but missing from the vector store
1457
+ // or keyword index (full rebuilds drop quirk chunks — see reconcileQuirks).
1458
+ // Fire-and-forget: must never block plugin startup, and the embedder may be
1459
+ // offline. The background watcher re-runs this at every pass end.
1460
+ void reconcileQuirks({ embedder, store, keywordIndex, cfg: effectiveCfg, storePath })
1461
+ .then((r) => {
1462
+ if (r.restoredToStore > 0 || r.addedToKeywordIndex > 0 || r.removedOrphans > 0) {
1463
+ appendDebugLog(logFilePath, {
1464
+ scope: "plugin",
1465
+ message: `Quirk reconcile: restored ${r.restoredToStore} to store, added ${r.addedToKeywordIndex} to keyword index, removed ${r.removedOrphans} orphans`,
1466
+ }, logLevel);
1467
+ }
1468
+ })
1469
+ .catch((err) => {
1470
+ appendDebugLog(logFilePath, {
1471
+ scope: "plugin",
1472
+ message: "Quirk reconcile failed",
1473
+ error: err,
1474
+ }, logLevel);
1475
+ });
1455
1476
  // Create description provider (enabled by default)
1456
1477
  const descriptionConfig = effectiveCfg.description ?? { enabled: true, provider: "ollama", baseUrl: "http://127.0.0.1:11434/api", model: "qwen2.5:3b", systemPrompt: "" };
1457
1478
  const descriptionProvider = descriptionConfig.enabled
@@ -28,6 +28,51 @@ export declare function addQuirk(deps: QuirkStoreDeps, input: QuirkInput): Promi
28
28
  export declare function removeQuirk(deps: QuirkStoreDeps, id: string): Promise<void>;
29
29
  /** List all quirks sorted by lastObserved descending. */
30
30
  export declare function listQuirks(deps: QuirkStoreDeps): Promise<Quirk[]>;
31
+ /** Dependencies for {@link reconcileQuirks} — like {@link QuirkStoreDeps}, but
32
+ * the keyword index is optional (index passes may run without one). */
33
+ export interface QuirkReconcileDeps extends Omit<QuirkStoreDeps, "keywordIndex"> {
34
+ keywordIndex?: KeywordIndex;
35
+ }
36
+ /** Outcome of a {@link reconcileQuirks} run. Zeroes mean "already consistent". */
37
+ export interface QuirkReconcileResult {
38
+ /** Quirks re-embedded and restored into the vector store (were in quirks.jsonl but not the table). */
39
+ restoredToStore: number;
40
+ /** Quirks added to the in-memory keyword index (lexical only — no embedding involved). */
41
+ addedToKeywordIndex: number;
42
+ /** Store quirk chunks deleted because their quirks.jsonl entry is gone (past the grace window). */
43
+ removedOrphans: number;
44
+ }
45
+ /**
46
+ * Reconcile `quirks.jsonl` (the source of truth for quirk memory) into the
47
+ * vector store and the keyword index, in both directions.
48
+ *
49
+ * Why this exists: full store rebuilds rebuild the table from **workspace
50
+ * files only** — `swapStoreDirectories` carries `quirks.jsonl` across the swap,
51
+ * but nothing ever re-embeds those entries, so every quirk added before a
52
+ * rebuild silently vanishes from recall while `quirk list`/`lint` (which read
53
+ * the jsonl) still show it. The keyword index has the same gap: it only gains
54
+ * quirks via in-process `addQuirk`, and any process that saves its in-memory
55
+ * index clobbers entries added elsewhere.
56
+ *
57
+ * Behavior:
58
+ * - Restores jsonl quirks missing from the store (one batched embed call).
59
+ * - Adds jsonl quirks missing from the keyword index (no embedding needed).
60
+ * - Deletes store quirk chunks whose jsonl entry is gone — only when the jsonl
61
+ * exists and parses non-empty (a missing/unreadable file means "unknown",
62
+ * never "empty") and only after the orphan was observed twice across
63
+ * {@link QUIRK_ORPHAN_GRACE_MS} (see QUIRK_ORPHAN_STATE_FILE — the table has
64
+ * no row timestamps, and addQuirk writes the store before the jsonl append).
65
+ * - Persists the keyword index when it changed (best-effort).
66
+ *
67
+ * Idempotent and safe under concurrent runs: `store.addChunks` dedups by id and
68
+ * `KeywordIndex.addChunks` overwrites by id. Never applied to memory stores
69
+ * (per-process lifetime, nothing on disk to drift). Failures propagate to the
70
+ * caller, which is expected to treat reconcile as best-effort.
71
+ *
72
+ * @param deps - Embedder, store, optional keyword index, config, store path.
73
+ * @returns Counts of what changed (all zero when already consistent).
74
+ */
75
+ export declare function reconcileQuirks(deps: QuirkReconcileDeps): Promise<QuirkReconcileResult>;
31
76
  /** Recall quirks matching a query, with confidence re-weighting. */
32
77
  export declare function recallQuirks(deps: QuirkStoreDeps, query: string, options?: {
33
78
  topK?: number;