opencode-rag-plugin 2.11.0 → 2.11.2

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.
@@ -90,6 +90,9 @@ export declare function buildByteOffsetMap(source: string): Map<number, number>
90
90
  * @param source - Full source text (used to slice node text and comments).
91
91
  * @param maxDepth - Maximum recursion depth (default: 10).
92
92
  * @param depth - Current recursion depth (internal, start at 0).
93
+ * @param descend - When true, keep walking into a matched node's children so
94
+ * nested members are returned too. The chunker keeps the default (false) so
95
+ * one matched declaration stays one chunk; the skeleton outline opts in.
93
96
  * @returns An array of {@link AstNode} objects for matching declarations.
94
97
  */
95
- export declare function walkTree(node: Node, nodeTypes: Set<string>, source: string, maxDepth?: number, depth?: number, offsetMap?: Map<number, number> | null): AstNode[];
98
+ export declare function walkTree(node: Node, nodeTypes: Set<string>, source: string, maxDepth?: number, depth?: number, offsetMap?: Map<number, number> | null, descend?: boolean): AstNode[];
@@ -249,9 +249,12 @@ function extractDocstringFromBody(node, source, offsetMap) {
249
249
  * @param source - Full source text (used to slice node text and comments).
250
250
  * @param maxDepth - Maximum recursion depth (default: 10).
251
251
  * @param depth - Current recursion depth (internal, start at 0).
252
+ * @param descend - When true, keep walking into a matched node's children so
253
+ * nested members are returned too. The chunker keeps the default (false) so
254
+ * one matched declaration stays one chunk; the skeleton outline opts in.
252
255
  * @returns An array of {@link AstNode} objects for matching declarations.
253
256
  */
254
- export function walkTree(node, nodeTypes, source, maxDepth = 10, depth = 0, offsetMap) {
257
+ export function walkTree(node, nodeTypes, source, maxDepth = 10, depth = 0, offsetMap, descend = false) {
255
258
  const results = [];
256
259
  if (nodeTypes.has(node.type) && depth > 0) {
257
260
  const leadingComments = extractLeadingComments(node, source, offsetMap);
@@ -268,11 +271,14 @@ export function walkTree(node, nodeTypes, source, maxDepth = 10, depth = 0, offs
268
271
  type: node.type,
269
272
  leadingDoc: leadingDoc.length > 0 ? leadingDoc : undefined,
270
273
  });
271
- return results;
274
+ // The chunker stops at the first matched declaration so that one
275
+ // declaration produces one chunk. The skeleton opts into descending.
276
+ if (!descend)
277
+ return results;
272
278
  }
273
279
  if (depth < maxDepth) {
274
280
  for (const child of node.children) {
275
- results.push(...walkTree(child, nodeTypes, source, maxDepth, depth + 1, offsetMap));
281
+ results.push(...walkTree(child, nodeTypes, source, maxDepth, depth + 1, offsetMap, descend));
276
282
  }
277
283
  }
278
284
  return results;
@@ -21,16 +21,16 @@ export const SKELETON_CONFIGS = {
21
21
  ".jsx": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
22
22
  ".mjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
23
23
  ".cjs": { grammarName: "javascript", nodeTypes: ["function_declaration", "method_definition", "class_declaration", "arrow_function"] },
24
- ".py": { grammarName: "python", nodeTypes: ["function_definition", "class_definition", "decorated_definition"] },
24
+ ".py": { grammarName: "python", nodeTypes: ["function_definition", "class_definition"] },
25
25
  ".java": { grammarName: "java", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration"] },
26
- ".go": { grammarName: "go", nodeTypes: ["function_declaration", "method_declaration", "type_declaration", "type_spec"] },
26
+ ".go": { grammarName: "go", nodeTypes: ["function_declaration", "method_declaration", "type_spec", "type_alias"] },
27
27
  ".rs": { grammarName: "rust", nodeTypes: ["function_item", "struct_item", "enum_item", "impl_item", "trait_item", "type_item"] },
28
28
  ".c": { grammarName: "c", nodeTypes: ["function_definition", "struct_specifier", "enum_specifier"] },
29
29
  ".cpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
30
30
  ".cxx": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
31
31
  ".h": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
32
32
  ".hpp": { grammarName: "cpp", nodeTypes: ["function_definition", "class_specifier", "struct_specifier", "enum_specifier"] },
33
- ".cs": { grammarName: "c_sharp", nodeTypes: ["class_declaration", "method_declaration", "interface_declaration", "enum_declaration", "struct_declaration"] },
33
+ ".cs": { grammarName: "c_sharp", nodeTypes: ["class_declaration", "struct_declaration", "interface_declaration", "enum_declaration", "record_declaration", "delegate_declaration", "constructor_declaration", "destructor_declaration", "method_declaration", "property_declaration", "indexer_declaration", "event_field_declaration", "event_declaration", "operator_declaration", "conversion_operator_declaration"] },
34
34
  ".rb": { grammarName: "ruby", nodeTypes: ["method", "class", "module"] },
35
35
  ".swift": { grammarName: "swift", nodeTypes: ["function_declaration", "class_declaration", "struct_declaration", "enum_declaration", "protocol_declaration"] },
36
36
  ".kt": { grammarName: "kotlin", nodeTypes: ["function_declaration", "class_declaration", "interface_declaration", "object_declaration"] },
@@ -99,6 +99,61 @@ function resolveSkeletonKey(ext, overrides) {
99
99
  // line-count fallback instead of using it.
100
100
  return hit ?? NO_SKELETON;
101
101
  }
102
+ /**
103
+ * Node types that represent executable bodies. A callable nested inside
104
+ * another callable is an implementation detail (callback, local helper), not
105
+ * a member of the file's outline, so descent drops it. A callable nested in a
106
+ * type (a method, constructor, …) is kept.
107
+ */
108
+ const CALLABLE_NODE_TYPES = new Set([
109
+ "function_declaration",
110
+ "function_definition",
111
+ "function_item",
112
+ "function",
113
+ "method_declaration",
114
+ "method_definition",
115
+ "method",
116
+ "constructor_declaration",
117
+ "destructor_declaration",
118
+ "operator_declaration",
119
+ "conversion_operator_declaration",
120
+ "arrow_function",
121
+ "generator_function_declaration",
122
+ "generator_function",
123
+ "lambda",
124
+ "lambda_expression",
125
+ "lambda_literal",
126
+ "func_literal",
127
+ "closure_expression",
128
+ "anonymous_function",
129
+ ]);
130
+ /**
131
+ * Drop a callable whose nearest outlined ancestor is also a callable, keeping
132
+ * the outer one. Types and their members are unaffected: only body-level
133
+ * nesting is removed, so a method of a local class still appears.
134
+ */
135
+ function dropNestedCallables(nodes) {
136
+ return nodes.filter((node) => {
137
+ if (!CALLABLE_NODE_TYPES.has(node.type))
138
+ return true;
139
+ let nearest;
140
+ for (const other of nodes) {
141
+ if (other === node)
142
+ continue;
143
+ const contains = other.startIndex <= node.startIndex &&
144
+ other.endIndex >= node.endIndex &&
145
+ (other.startIndex < node.startIndex || other.endIndex > node.endIndex);
146
+ if (!contains)
147
+ continue;
148
+ if (!nearest ||
149
+ other.startIndex > nearest.startIndex ||
150
+ (other.startIndex === nearest.startIndex && other.endIndex < nearest.endIndex)) {
151
+ nearest = other;
152
+ }
153
+ }
154
+ return !nearest || !CALLABLE_NODE_TYPES.has(nearest.type);
155
+ });
156
+ }
102
157
  /**
103
158
  * Extract structural outline from source code using tree-sitter.
104
159
  * Falls back to regex patterns for languages without WASM grammars, and to a
@@ -150,7 +205,9 @@ export async function extractSkeleton(content, ext, options) {
150
205
  return [];
151
206
  try {
152
207
  const nodeTypes = new Set(config.nodeTypes);
153
- const astNodes = walkTree(tree.rootNode, nodeTypes, content, 15);
208
+ // Descend into matched declarations: a skeleton describes the members
209
+ // inside a type, whereas the chunker intentionally stops at the type.
210
+ const astNodes = dropNestedCallables(walkTree(tree.rootNode, nodeTypes, content, 15, 0, undefined, true));
154
211
  return astNodes.map((node) => ({
155
212
  type: node.type,
156
213
  name: extractNodeName(node.text, node.type),
package/dist/plugin.d.ts CHANGED
@@ -9,6 +9,19 @@ import { type RagConfig } from "./core/config.js";
9
9
  import { createEmbedder } from "./embedder/factory.js";
10
10
  import { StateSync } from "./core/state-sync.js";
11
11
  import { retrieve } from "./retriever/retriever.js";
12
+ /**
13
+ * Describe where a workspace's effective config is read from.
14
+ *
15
+ * Slash-command gates (`/doc`, `/wiki`, `/quirk`) report "not enabled" from a
16
+ * config object, and without provenance the report is not actionable: a
17
+ * higher-precedence layer (`.opencode/opencode-rag.json`) silently shadows the
18
+ * root file, and a plugin that booted before an edit keeps serving its startup
19
+ * snapshot. Printed in the command's response and in the plugin log.
20
+ *
21
+ * @param directory - Workspace directory the plugin was loaded for.
22
+ * @returns One line naming the directory and both config layers (`none` when absent).
23
+ */
24
+ export declare function describeConfigSource(directory: string): string;
12
25
  /**
13
26
  * Load the RAG configuration for a directory: DEFAULT_CONFIG, then the global
14
27
  * layer (`~/.config/opencode/opencode-rag.json`), then the first workspace
package/dist/plugin.js CHANGED
@@ -242,6 +242,22 @@ function configStamp(paths) {
242
242
  paths.workspace ? `${paths.workspace}:${configFileMtime(paths.workspace)}` : "",
243
243
  ].join("|");
244
244
  }
245
+ /**
246
+ * Describe where a workspace's effective config is read from.
247
+ *
248
+ * Slash-command gates (`/doc`, `/wiki`, `/quirk`) report "not enabled" from a
249
+ * config object, and without provenance the report is not actionable: a
250
+ * higher-precedence layer (`.opencode/opencode-rag.json`) silently shadows the
251
+ * root file, and a plugin that booted before an edit keeps serving its startup
252
+ * snapshot. Printed in the command's response and in the plugin log.
253
+ *
254
+ * @param directory - Workspace directory the plugin was loaded for.
255
+ * @returns One line naming the directory and both config layers (`none` when absent).
256
+ */
257
+ export function describeConfigSource(directory) {
258
+ const paths = resolveConfigPaths(directory);
259
+ return `directory: ${directory}; workspace: ${paths.workspace ?? "none"}; global: ${paths.global ?? "none"}`;
260
+ }
245
261
  /**
246
262
  * Load the RAG configuration for a directory: DEFAULT_CONFIG, then the global
247
263
  * layer (`~/.config/opencode/opencode-rag.json`), then the first workspace
@@ -670,6 +686,31 @@ export function createRagHooks(options) {
670
686
  lastEffectiveCfgTime = Date.now();
671
687
  return lastEffectiveCfg;
672
688
  }
689
+ /**
690
+ * Config used by the slash-command gates (`/doc`, `/wiki`, `/quirk`).
691
+ *
692
+ * These are cold paths, so read the workspace config straight from disk
693
+ * instead of trusting the cached startup snapshot: the gate must agree with
694
+ * the file the user just edited, even when the plugin started before that
695
+ * edit. This matters because OpenCode V2 runs plugins inside a shared
696
+ * background service that survives TUI restarts, so "restart OpenCode" does
697
+ * not necessarily reload the plugin.
698
+ *
699
+ * Falls back to the effective config when the workspace has no config file
700
+ * or the file cannot be parsed (the last known config keeps serving).
701
+ */
702
+ function slashCommandConfig() {
703
+ try {
704
+ const paths = resolveConfigPaths(options.worktree);
705
+ if (paths.workspace || paths.global) {
706
+ return applyRuntimeOverrides(loadWorkspaceConfig(options.worktree, false), cachedOverrides);
707
+ }
708
+ }
709
+ catch {
710
+ // Unreadable/malformed config — fall through to the last known config.
711
+ }
712
+ return getEffectiveCfg();
713
+ }
673
714
  // The embedder follows the live configuration: when the config file changes
674
715
  // (new API key, baseUrl or model) the provider is rebuilt instead of
675
716
  // keeping the one built at plugin setup.
@@ -1592,13 +1633,26 @@ export function createRagHooks(options) {
1592
1633
  boundedSet(sessionLastMessage, input.sessionID, text, MAX_SESSION_MAP_SIZE);
1593
1634
  // Handle /doc slash command
1594
1635
  if (text.startsWith("/doc")) {
1595
- const docMode = getEffectiveCfg().documentationMode;
1636
+ // Read the config from disk (cold path): the gate must agree with the
1637
+ // file the user just edited, not with a startup snapshot.
1638
+ const docMode = slashCommandConfig().documentationMode;
1596
1639
  if (!docMode?.enabled) {
1640
+ const configSource = describeConfigSource(options.worktree);
1641
+ const configHint = resolveConfigPaths(options.worktree).workspace
1642
+ ?? path.join(options.worktree, ".opencode", "opencode-rag.json");
1643
+ appendDebugLog(options.logFilePath, {
1644
+ scope: "chat.message",
1645
+ message: `Slash command /doc blocked: documentationMode.enabled is not true — ${configSource}`,
1646
+ }, options.logLevel);
1597
1647
  const parts = output?.parts ?? output?.message?.parts;
1598
1648
  if (Array.isArray(parts) && parts.length > 0) {
1599
1649
  const first = parts[0];
1600
1650
  if (typeof first.text === "string") {
1601
- parts[0] = { ...first, text: "Documentation mode is not enabled. Set `documentationMode.enabled` to `true` in .opencode/opencode-rag.json." };
1651
+ parts[0] = { ...first, text: "Documentation mode is not enabled.\n\n" +
1652
+ `Set \`documentationMode.enabled\` to \`true\` in \`${configHint}\`.\n\n` +
1653
+ `Config resolution — ${configSource}.\n\n` +
1654
+ "If the file already says `true`, the running plugin is using a stale config — " +
1655
+ "restart the OpenCode service (`opencode service restart`) so the plugin reloads." };
1602
1656
  }
1603
1657
  }
1604
1658
  return;
@@ -1676,13 +1730,13 @@ export function createRagHooks(options) {
1676
1730
  }
1677
1731
  // Handle /wiki slash command
1678
1732
  if (text.startsWith("/wiki")) {
1679
- const wikiMode = getEffectiveCfg().wikiMode;
1733
+ const wikiMode = slashCommandConfig().wikiMode;
1680
1734
  if (!wikiMode?.enabled) {
1681
1735
  const parts = output?.parts ?? output?.message?.parts;
1682
1736
  if (Array.isArray(parts) && parts.length > 0) {
1683
1737
  const first = parts[0];
1684
1738
  if (typeof first.text === "string") {
1685
- parts[0] = { ...first, text: "Wiki mode is not enabled. Set `wikiMode.enabled` to `true` in .opencode/opencode-rag.json." };
1739
+ parts[0] = { ...first, text: `Wiki mode is not enabled. Set \`wikiMode.enabled\` to \`true\` in opencode-rag.json. Config resolution — ${describeConfigSource(options.worktree)}.` };
1686
1740
  }
1687
1741
  }
1688
1742
  return;
@@ -1734,13 +1788,13 @@ export function createRagHooks(options) {
1734
1788
  }
1735
1789
  // Handle /quirk slash command
1736
1790
  if (text.startsWith("/quirk")) {
1737
- const memoryCfg = getEffectiveCfg().memory;
1791
+ const memoryCfg = slashCommandConfig().memory;
1738
1792
  if (!memoryCfg?.enabled) {
1739
1793
  const parts = output?.parts ?? output?.message?.parts;
1740
1794
  if (Array.isArray(parts) && parts.length > 0) {
1741
1795
  const first = parts[0];
1742
1796
  if (typeof first.text === "string") {
1743
- parts[0] = { ...first, text: "Quirk memory is not enabled. Set `memory.enabled` to `true` in .opencode/opencode-rag.json." };
1797
+ parts[0] = { ...first, text: `Quirk memory is not enabled. Set \`memory.enabled\` to \`true\` in opencode-rag.json. Config resolution — ${describeConfigSource(options.worktree)}.` };
1744
1798
  }
1745
1799
  }
1746
1800
  return;
@@ -1996,6 +2050,16 @@ export const ragPlugin = async (input, _options) => {
1996
2050
  if (!cfg.openCode.enabled) {
1997
2051
  return {};
1998
2052
  }
2053
+ // Record WHICH config file produced this instance. Without it, a report like
2054
+ // "the config says documentationMode.enabled = true but /doc says it is off"
2055
+ // cannot be diagnosed from the log: the answer is either a different file
2056
+ // (layer shadowing) or a stale snapshot (plugin never reloaded).
2057
+ appendDebugLog(logFilePath, {
2058
+ scope: "config",
2059
+ message: `Config: ${describeConfigSource(input.directory)}; ` +
2060
+ `documentationMode.enabled=${cfg.documentationMode?.enabled === true}; ` +
2061
+ `wikiMode.enabled=${cfg.wikiMode?.enabled === true}`,
2062
+ }, logLevel);
1999
2063
  const storePath = path.resolve(input.directory, cfg.vectorStore.path);
2000
2064
  // Apply runtime overrides before creating services
2001
2065
  const overrides = loadRuntimeOverrides(storePath);
@@ -2048,8 +2112,13 @@ export const ragPlugin = async (input, _options) => {
2048
2112
  }
2049
2113
  ragStores.delete(input.directory);
2050
2114
  }
2051
- // Clean up stale config cache and pending update info for this directory
2052
- configCache.delete(input.directory);
2115
+ // Clean up pending update info for this directory.
2116
+ //
2117
+ // Do NOT drop the config cache entry here: `getConfig` above just stored it,
2118
+ // and that entry is what makes `refreshConfig` re-read the file on every
2119
+ // later mtime change. Deleting it pinned the whole plugin to its startup
2120
+ // snapshot for the lifetime of the (long-lived, shared) OpenCode service —
2121
+ // a config edit was then never picked up, not even by `getEffectiveCfg()`.
2053
2122
  pendingUpdateInfo.delete(input.directory);
2054
2123
  pendingRestartNotice.delete(input.directory);
2055
2124
  notifiedUpdateDirs.delete(input.directory);