opencode-rag-plugin 2.3.1 → 2.4.1

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.
Files changed (62) hide show
  1. package/ReadMe.md +71 -8
  2. package/dist/chunker/skeleton.js +1 -1
  3. package/dist/cli/commands/decide.d.ts +25 -0
  4. package/dist/cli/commands/decide.js +160 -0
  5. package/dist/cli/commands/eval.d.ts +23 -0
  6. package/dist/cli/commands/eval.js +331 -1
  7. package/dist/cli/commands/index-command.js +28 -10
  8. package/dist/cli/commands/index.d.ts +2 -1
  9. package/dist/cli/commands/index.js +2 -1
  10. package/dist/cli/commands/init-helpers.d.ts +16 -1
  11. package/dist/cli/commands/init-helpers.js +84 -2
  12. package/dist/cli/commands/init.js +29 -4
  13. package/dist/cli/commands/status.js +14 -8
  14. package/dist/cli/format.d.ts +50 -0
  15. package/dist/cli/format.js +122 -0
  16. package/dist/cli/index.js +4 -1
  17. package/dist/cli/types.d.ts +2 -0
  18. package/dist/core/bootstrap.js +1 -0
  19. package/dist/core/config.d.ts +47 -0
  20. package/dist/core/config.js +34 -1
  21. package/dist/core/interfaces.d.ts +69 -0
  22. package/dist/decision/factory.d.ts +17 -0
  23. package/dist/decision/factory.js +17 -0
  24. package/dist/decision/format.d.ts +14 -0
  25. package/dist/decision/format.js +53 -0
  26. package/dist/decision/systemone.d.ts +76 -0
  27. package/dist/decision/systemone.js +234 -0
  28. package/dist/decision/validate.d.ts +26 -0
  29. package/dist/decision/validate.js +74 -0
  30. package/dist/eval/decide-calibration.d.ts +112 -0
  31. package/dist/eval/decide-calibration.js +185 -0
  32. package/dist/eval/golden-gate.d.ts +65 -0
  33. package/dist/eval/golden-gate.js +61 -0
  34. package/dist/eval/golden-set.d.ts +81 -0
  35. package/dist/eval/golden-set.js +155 -0
  36. package/dist/eval/rerank-eval.js +10 -125
  37. package/dist/index.d.ts +6 -2
  38. package/dist/index.js +4 -0
  39. package/dist/indexer/pipeline.d.ts +0 -10
  40. package/dist/indexer/pipeline.js +1 -0
  41. package/dist/mcp/handlers.d.ts +17 -1
  42. package/dist/mcp/handlers.js +30 -0
  43. package/dist/mcp/server.d.ts +9 -1
  44. package/dist/mcp/server.js +47 -3
  45. package/dist/opencode/system-guidance.d.ts +11 -0
  46. package/dist/opencode/system-guidance.js +53 -0
  47. package/dist/opencode/tools.d.ts +24 -1
  48. package/dist/opencode/tools.js +101 -0
  49. package/dist/plugin.js +21 -3
  50. package/dist/quirks/quirk-store.d.ts +2 -0
  51. package/dist/quirks/quirk-store.js +33 -4
  52. package/dist/tui.js +21 -2
  53. package/dist/v2/v2-adapter.d.ts +20 -0
  54. package/dist/v2/v2-adapter.js +36 -8
  55. package/dist/vectorstore/lancedb.js +54 -5
  56. package/dist/watcher.d.ts +26 -1
  57. package/dist/watcher.js +71 -9
  58. package/dist/web/ui/assets/index-CWcp0jNf.css +2 -0
  59. package/dist/web/ui/index.html +2 -2
  60. package/package.json +5 -2
  61. package/dist/web/ui/assets/index-Dp009QCQ.css +0 -1
  62. /package/dist/web/ui/assets/{index-C0QXKA-b.js → index-DbXFrn53.js} +0 -0
@@ -42,7 +42,7 @@
42
42
  * stage is being developed in parallel, and this harness must not depend on its
43
43
  * exact RetrieveOptions type.
44
44
  */
45
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
45
+ import { existsSync, writeFileSync } from "node:fs";
46
46
  import path from "node:path";
47
47
  import { DEFAULT_CONFIG, loadConfig } from "../core/config.js";
48
48
  import { createEmbedder } from "../embedder/factory.js";
@@ -52,6 +52,7 @@ import { retrieve } from "../retriever/retriever.js";
52
52
  import { KeywordIndex } from "../retriever/keyword-index.js";
53
53
  import { applyRuntimeOverrides, loadRuntimeOverrides } from "../core/runtime-overrides.js";
54
54
  import { resolveApiKey } from "../core/resolve-api-key.js";
55
+ import { evaluateGoldenResults, loadGoldenLabels, summarizeGoldenOutcomes, } from "./golden-set.js";
55
56
  const WORKTREE = process.cwd();
56
57
  const CONFIG_PATH = path.join(WORKTREE, "opencode-rag.json");
57
58
  const STORE_PATH = path.join(WORKTREE, ".opencode", "rag_db");
@@ -59,6 +60,13 @@ const DEFAULT_LABELS_PATH = path.join(WORKTREE, "src", "eval", "rerank-labels.js
59
60
  const DEFAULT_REPORT_PATH = path.join(WORKTREE, "doc", "rerank-eval-report.md");
60
61
  const RERANK_FACTORY_SPECIFIER = "../reranker/factory.js";
61
62
  const retrieveFn = retrieve;
63
+ // ────────────────────────────────────────────────────────────────────────────
64
+ // Labels
65
+ // ────────────────────────────────────────────────────────────────────────────
66
+ // Thin adapters over the shared golden-set module so this harness and the
67
+ // `eval:gate` command always compute identical metrics for the same labels.
68
+ const evaluateResults = (label, results) => evaluateGoldenResults(WORKTREE, label, results);
69
+ const summarize = summarizeGoldenOutcomes;
62
70
  const ARM_SPECS = [
63
71
  { name: "baseline", description: "current pipeline (reranking disabled)", reranked: false },
64
72
  { name: "rerank20", description: "rerank stage, candidates=20", reranked: true, overrides: { candidates: 20 } },
@@ -102,21 +110,6 @@ function parseOptions(argv) {
102
110
  embeddingBaseUrl: kv.get("embedding-base-url"),
103
111
  };
104
112
  }
105
- function loadLabels(labelsPath) {
106
- if (!existsSync(labelsPath)) {
107
- throw new Error(`Label file not found: ${labelsPath}`);
108
- }
109
- const parsed = JSON.parse(readFileSync(labelsPath, "utf-8"));
110
- if (!Array.isArray(parsed.labels) || parsed.labels.length === 0) {
111
- throw new Error(`Label file has no labels: ${labelsPath}`);
112
- }
113
- for (const label of parsed.labels) {
114
- if (!label.id || !label.query || !Array.isArray(label.files) || label.files.length === 0) {
115
- throw new Error(`Malformed label (needs id, query, files[]): ${JSON.stringify(label)}`);
116
- }
117
- }
118
- return parsed.labels;
119
- }
120
113
  async function bootstrap(opts) {
121
114
  let cfg;
122
115
  if (existsSync(CONFIG_PATH)) {
@@ -173,114 +166,6 @@ async function resolveRerankProvider(section) {
173
166
  return { error: `reranker module unavailable: ${err.message}` };
174
167
  }
175
168
  }
176
- function toRepoRelative(filePath) {
177
- let p = filePath.replace(/\\/g, "/");
178
- const root = WORKTREE.replace(/\\/g, "/").toLowerCase();
179
- const idx = p.toLowerCase().indexOf(root);
180
- if (idx >= 0)
181
- p = p.slice(idx + root.length);
182
- return p.replace(/^\/+/, "").toLowerCase();
183
- }
184
- function evaluateResults(label, results) {
185
- const expected = new Set(label.files.map((f) => f.toLowerCase()));
186
- const fileRank = new Map();
187
- for (const [i, r] of results.entries()) {
188
- const file = toRepoRelative(r.chunk.metadata.filePath);
189
- if (!fileRank.has(file))
190
- fileRank.set(file, i + 1);
191
- }
192
- let firstHitRank = null;
193
- for (const [file, rank] of fileRank) {
194
- if (!expected.has(file))
195
- continue;
196
- firstHitRank = firstHitRank === null ? rank : Math.min(firstHitRank, rank);
197
- }
198
- const foundInK = (k) => {
199
- let found = 0;
200
- for (const file of expected) {
201
- const rank = fileRank.get(file);
202
- if (rank !== undefined && rank <= k)
203
- found += 1;
204
- }
205
- return found;
206
- };
207
- const hitAt = (k) => (foundInK(k) > 0 ? 1 : 0);
208
- // nDCG@10 with binary gains, deduplicated per expected file.
209
- let dcg = 0;
210
- const gained = new Set();
211
- for (const [i, r] of results.slice(0, 10).entries()) {
212
- const file = toRepoRelative(r.chunk.metadata.filePath);
213
- if (expected.has(file) && !gained.has(file)) {
214
- gained.add(file);
215
- dcg += 1 / Math.log2(i + 2);
216
- }
217
- }
218
- const idealCount = Math.min(expected.size, 10);
219
- let idcg = 0;
220
- for (let i = 0; i < idealCount; i++)
221
- idcg += 1 / Math.log2(i + 2);
222
- const ndcg10 = idcg > 0 ? dcg / idcg : 0;
223
- let symbolHitRank = null;
224
- if (label.symbol) {
225
- for (const [i, r] of results.entries()) {
226
- const file = toRepoRelative(r.chunk.metadata.filePath);
227
- if (expected.has(file) && r.chunk.content.includes(label.symbol)) {
228
- symbolHitRank = i + 1;
229
- break;
230
- }
231
- }
232
- }
233
- // Did the rerank stage actually reorder this query? The stage sets
234
- // explanation.scoreBreakdown.rerankScore only on a successful rerank; when it
235
- // degrades (timeout/error/cooldown) the fusion order is kept and the field is absent.
236
- const rerankRan = results.some((r) => {
237
- const breakdown = r.explanation?.scoreBreakdown;
238
- return typeof breakdown?.rerankScore === "number";
239
- });
240
- return {
241
- firstHitRank,
242
- hitAt1: hitAt(1),
243
- hitAt3: hitAt(3),
244
- hitAt5: hitAt(5),
245
- hitAt10: hitAt(10),
246
- precision5: foundInK(5) / 5,
247
- recall10: foundInK(10) / Math.max(expected.size, 1),
248
- rr: firstHitRank !== null ? 1 / firstHitRank : 0,
249
- ndcg10,
250
- symbolHitRank,
251
- topFiles: results.slice(0, 5).map((r) => toRepoRelative(r.chunk.metadata.filePath)),
252
- rerankRan,
253
- };
254
- }
255
- function percentile(sorted, q) {
256
- if (sorted.length === 0)
257
- return 0;
258
- const idx = Math.min(sorted.length - 1, Math.floor(q * (sorted.length - 1)));
259
- return sorted[idx] ?? 0;
260
- }
261
- function mean(values) {
262
- if (values.length === 0)
263
- return 0;
264
- return values.reduce((s, v) => s + v, 0) / values.length;
265
- }
266
- function summarize(outcomes) {
267
- const latencies = outcomes.map((o) => o.latencyMs).sort((a, b) => a - b);
268
- const withSymbol = outcomes.filter((o) => o.label.symbol);
269
- return {
270
- hit1: mean(outcomes.map((o) => o.hitAt1)),
271
- hit3: mean(outcomes.map((o) => o.hitAt3)),
272
- hit5: mean(outcomes.map((o) => o.hitAt5)),
273
- hit10: mean(outcomes.map((o) => o.hitAt10)),
274
- precision5: mean(outcomes.map((o) => o.precision5)),
275
- recall10: mean(outcomes.map((o) => o.recall10)),
276
- mrr: mean(outcomes.map((o) => o.rr)),
277
- ndcg10: mean(outcomes.map((o) => o.ndcg10)),
278
- symbolRate: withSymbol.length > 0 ? mean(withSymbol.map((o) => (o.symbolHitRank !== null ? 1 : 0))) : 0,
279
- latencyAvg: mean(latencies),
280
- latencyP50: percentile(latencies, 0.5),
281
- latencyP95: percentile(latencies, 0.95),
282
- };
283
- }
284
169
  // ────────────────────────────────────────────────────────────────────────────
285
170
  // Run
286
171
  // ────────────────────────────────────────────────────────────────────────────
@@ -342,7 +227,7 @@ function top5Overlap(baseline, other) {
342
227
  }
343
228
  async function main() {
344
229
  const opts = parseOptions(process.argv.slice(2));
345
- const labels = loadLabels(opts.labelsPath)
230
+ const labels = loadGoldenLabels(opts.labelsPath)
346
231
  .filter((l) => (opts.category ? l.category === opts.category : true))
347
232
  .slice(0, opts.limit ?? Number.MAX_SAFE_INTEGER);
348
233
  if (labels.length === 0) {
package/dist/index.d.ts CHANGED
@@ -9,6 +9,10 @@ export { normalizeParserOverrides } from "./core/parser-overrides.js";
9
9
  export type { ParserOverrides } from "./core/parser-overrides.js";
10
10
  export { createEmbedder, embedBatch } from "./embedder/factory.js";
11
11
  export { createDescriptionProvider } from "./describer/factory.js";
12
+ export { createDecisionProvider } from "./decision/factory.js";
13
+ export { OllamaDecisionProvider, resolveSystemOneUrl, SYSTEMONE_MAX_QUESTIONS, SYSTEMONE_MIN_OPTIONS, SYSTEMONE_MAX_OPTIONS } from "./decision/systemone.js";
14
+ export { validateDecisionRequest } from "./decision/validate.js";
15
+ export { formatDecisionAnswers } from "./decision/format.js";
12
16
  export { createVectorStore } from "./vectorstore/factory.js";
13
17
  export { LanceDbStore } from "./vectorstore/lancedb.js";
14
18
  export { InMemoryVectorStore } from "./vectorstore/memory.js";
@@ -19,8 +23,8 @@ export { createBackgroundIndexer } from "./watcher.js";
19
23
  export { createWatchIgnore } from "./indexer.js";
20
24
  export { ImageChunker, createImageVisionProvider, resolveOnDemandImageConfig, getMimeType, SUPPORTED_IMAGE_EXTENSIONS } from "./chunker/image.js";
21
25
  export { DescriptionCache } from "./core/desc-cache.js";
22
- export type { RagConfig, DescriptionConfig, ImageDescriptionConfig, ImageDescriptionOnDemandConfig } from "./core/config.js";
23
- export type { Chunk, SearchResult, OptimizedSearchResult, Chunker, DescriptionProvider, EmbeddingProvider, VectorStore } from "./core/interfaces.js";
26
+ export type { RagConfig, DescriptionConfig, ImageDescriptionConfig, ImageDescriptionOnDemandConfig, DecisionConfig } from "./core/config.js";
27
+ export type { Chunk, SearchResult, OptimizedSearchResult, Chunker, DescriptionProvider, EmbeddingProvider, VectorStore, DecisionProvider, DecisionQuestion, DecisionQuestionType, DecisionAnswer, DecisionRequest, DecisionResult } from "./core/interfaces.js";
24
28
  export type { ContextOptimizationConfig, ContextOptimizationOptions } from "./retriever/context-optimizer.js";
25
29
  /**
26
30
  * High-level convenience API — search, index, and retrieve context in a single function call.
package/dist/index.js CHANGED
@@ -8,6 +8,10 @@ export { extractSkeleton } from "./chunker/skeleton.js";
8
8
  export { normalizeParserOverrides } from "./core/parser-overrides.js";
9
9
  export { createEmbedder, embedBatch } from "./embedder/factory.js";
10
10
  export { createDescriptionProvider } from "./describer/factory.js";
11
+ export { createDecisionProvider } from "./decision/factory.js";
12
+ export { OllamaDecisionProvider, resolveSystemOneUrl, SYSTEMONE_MAX_QUESTIONS, SYSTEMONE_MIN_OPTIONS, SYSTEMONE_MAX_OPTIONS } from "./decision/systemone.js";
13
+ export { validateDecisionRequest } from "./decision/validate.js";
14
+ export { formatDecisionAnswers } from "./decision/format.js";
11
15
  export { createVectorStore } from "./vectorstore/factory.js";
12
16
  export { LanceDbStore } from "./vectorstore/lancedb.js";
13
17
  export { InMemoryVectorStore } from "./vectorstore/memory.js";
@@ -30,16 +30,6 @@ export interface RunIndexPassOptions {
30
30
  abortSignal?: AbortSignal;
31
31
  /** Embedding vector dimension — needed when creating a temporary store for atomic rebuilds. */
32
32
  dimension?: number;
33
- /**
34
- * Caller-provided list of changed file paths (absolute). When set, only these
35
- * files are scanned — full directory walk is skipped.
36
- */
37
- filterPaths?: string[];
38
- /**
39
- * Caller-provided list of deleted file paths (absolute). When set, these
40
- * entries are removed from the index without comparing against the scanned set.
41
- */
42
- deletedPaths?: string[];
43
33
  /**
44
34
  * Optional injected image vision provider for testing. When omitted, the
45
35
  * provider is created from the imageDescription config as usual.
@@ -1145,6 +1145,7 @@ async function runIndexPassInner(options, logger) {
1145
1145
  });
1146
1146
  if (rec.restoredToStore > 0 || rec.addedToKeywordIndex > 0 || rec.removedOrphans > 0) {
1147
1147
  logger.info(`Quirk reconcile: restored ${rec.restoredToStore} to store, ` +
1148
+ `deduplicated ${rec.deduplicated}, ` +
1148
1149
  `added ${rec.addedToKeywordIndex} to keyword index, removed ${rec.removedOrphans} orphans`);
1149
1150
  }
1150
1151
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @fileoverview Handler implementations for all MCP tools: semantic search, file skeleton, symbol usage lookup, and image description.
3
3
  */
4
- import { type EmbeddingProvider, type VectorStore, type KeywordIndex, type SearchResult } from "../core/interfaces.js";
4
+ import { type EmbeddingProvider, type VectorStore, type KeywordIndex, type SearchResult, type DecisionProvider, type DecisionQuestion, type DecisionAnswer } from "../core/interfaces.js";
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";
@@ -100,5 +100,21 @@ export interface DescribeImageResult {
100
100
  }
101
101
  /** Describe an image file using the configured vision provider (Ollama, OpenAI, Anthropic, Gemini, or OpenCode Zen). Honors `imageDescription.onDemand` overrides. */
102
102
  export declare function handleDescribeImage(params: DescribeImageParams, cfg: RagConfig, worktree: string, visionProvider?: ImageVisionProvider): Promise<DescribeImageResult>;
103
+ /** Parameters for the make_decision MCP tool. */
104
+ export interface MakeDecisionParams {
105
+ /** Text to judge (keep it short — the decision context is ~2k tokens). */
106
+ state: string;
107
+ /** 1-64 named questions of type `choice`, `noul`, or `score`. */
108
+ questions: DecisionQuestion[];
109
+ }
110
+ /** Result of a decision-model operation. */
111
+ export interface MakeDecisionResult {
112
+ /** Typed answers, one per question, in request order. */
113
+ answers: DecisionAnswer[];
114
+ /** Human-readable formatted output. */
115
+ formatted: string;
116
+ }
117
+ /** Answer classification/decision questions with the configured decision model (tev1 via Ollama's /v1/systemone). */
118
+ export declare function handleMakeDecision(params: MakeDecisionParams, cfg: RagConfig, provider?: DecisionProvider): Promise<MakeDecisionResult>;
103
119
  /** Find usages and references of a symbol across the indexed codebase using hybrid (keyword + vector) search. */
104
120
  export declare function handleFindUsages(params: FindUsagesParams, embedder: EmbeddingProvider, store: VectorStore, cfg: RagConfig, keywordIndex?: KeywordIndex, retrieveFn?: typeof retrieve): Promise<FindUsagesResult>;
@@ -8,6 +8,9 @@ import { retrieve } from "../retriever/retriever.js";
8
8
  import { getRerankerFor } from "../reranker/factory.js";
9
9
  import { optimizeContext, DEFAULT_CONTEXT_OPTIMIZATION } from "../retriever/context-optimizer.js";
10
10
  import { extractSkeleton, getExtension } from "../chunker/skeleton.js";
11
+ import { createDecisionProvider } from "../decision/factory.js";
12
+ import { validateDecisionRequest } from "../decision/validate.js";
13
+ import { formatDecisionAnswers } from "../decision/format.js";
11
14
  import { readFileSync } from "node:fs";
12
15
  import { resolve, isAbsolute, relative, sep as pathSep } from "node:path";
13
16
  /**
@@ -168,6 +171,33 @@ export async function handleDescribeImage(params, cfg, worktree, visionProvider)
168
171
  ].join("\n");
169
172
  return { description, formatted };
170
173
  }
174
+ /** Answer classification/decision questions with the configured decision model (tev1 via Ollama's /v1/systemone). */
175
+ export async function handleMakeDecision(params, cfg, provider) {
176
+ const decisionConfig = cfg.decision;
177
+ if (!decisionConfig?.enabled) {
178
+ throw new Error("Decision model is not enabled in config (decision.enabled)");
179
+ }
180
+ const validationError = validateDecisionRequest({
181
+ state: params.state,
182
+ questions: params.questions,
183
+ maxStateChars: decisionConfig.maxStateChars,
184
+ });
185
+ if (validationError) {
186
+ throw new Error(`Invalid decision request: ${validationError}`);
187
+ }
188
+ const decisionProvider = provider ?? createDecisionProvider(decisionConfig);
189
+ const result = await decisionProvider.decide({
190
+ state: params.state,
191
+ questions: params.questions,
192
+ });
193
+ return {
194
+ answers: result.answers,
195
+ formatted: formatDecisionAnswers(result.answers, {
196
+ provider: decisionProvider.name,
197
+ model: decisionConfig.model,
198
+ }),
199
+ };
200
+ }
171
201
  /** Find usages and references of a symbol across the indexed codebase using hybrid (keyword + vector) search. */
172
202
  export async function handleFindUsages(params, embedder, store, cfg, keywordIndex, retrieveFn) {
173
203
  const retrieveFn_ = retrieveFn ?? retrieve;
@@ -1,8 +1,9 @@
1
1
  /**
2
- * @fileoverview MCP server setup exposing search_semantic, get_file_skeleton, find_usages, and describe_image tools.
2
+ * @fileoverview MCP server setup exposing search_semantic, get_file_skeleton, find_usages, and describe_image tools (plus make_decision when `decision.enabled`).
3
3
  */
4
4
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
5
5
  import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
6
+ import { type RagContext } from "../core/bootstrap.js";
6
7
  /** Options for creating the MCP server. */
7
8
  export interface McpServerOptions {
8
9
  /** Path to the RAG config file. */
@@ -11,6 +12,13 @@ export interface McpServerOptions {
11
12
  cwd?: string;
12
13
  /** Custom transport (defaults to stdio). */
13
14
  transport?: Transport;
15
+ /**
16
+ * Pre-resolved pipeline context. When provided, the caller owns its lifecycle —
17
+ * the server will not close the store or HTTP pools. Used by tests (and any
18
+ * embedder that boots the server in-process) so `tools/list` can be verified
19
+ * against the real registrations without resolving config or probing providers.
20
+ */
21
+ context?: RagContext;
14
22
  }
15
23
  /** A running MCP server instance with a close method. */
16
24
  export interface RagMcpInstance {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @fileoverview MCP server setup exposing search_semantic, get_file_skeleton, find_usages, and describe_image tools.
2
+ * @fileoverview MCP server setup exposing search_semantic, get_file_skeleton, find_usages, and describe_image tools (plus make_decision when `decision.enabled`).
3
3
  */
4
4
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
5
5
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
@@ -7,11 +7,13 @@ import { z } from "zod/v4";
7
7
  import { resolveRagContext } from "../core/bootstrap.js";
8
8
  import { retrieve } from "../retriever/retriever.js";
9
9
  import process from "node:process";
10
- import { handleSearchSemantic, handleFileSkeleton, handleFindUsages, handleDescribeImage, } from "./handlers.js";
10
+ import { handleSearchSemantic, handleFileSkeleton, handleFindUsages, handleDescribeImage, handleMakeDecision, } from "./handlers.js";
11
+ import { SYSTEMONE_MAX_QUESTIONS } from "../decision/systemone.js";
11
12
  /** Create and start an MCP server exposing search_semantic, get_file_skeleton, find_usages, and describe_image tools. */
12
13
  export async function createMcpServer(options) {
13
14
  const cwd = options?.cwd ?? process.cwd();
14
- const ctx = await resolveRagContext({
15
+ const ownedContext = options?.context === undefined;
16
+ const ctx = options?.context ?? await resolveRagContext({
15
17
  cwd,
16
18
  configPath: options?.configPath,
17
19
  });
@@ -91,12 +93,54 @@ export async function createMcpServer(options) {
91
93
  };
92
94
  }
93
95
  });
96
+ // Decision tool — only when explicitly enabled (tev1 requires Ollama >= 0.35
97
+ // and a pulled model). The MCP server resolves config at startup.
98
+ if (ctx.config.decision?.enabled) {
99
+ server.tool("make_decision", "Classify, route, or score a short text with a local decision model (tev1 via Ollama /v1/systemone). " +
100
+ "Provide `state` and 1-64 named questions of type `choice`, `noul`, or `score` and get the chosen option, " +
101
+ "true/false verdict, or rubric level with probabilities.", {
102
+ state: z.string().min(1, "A state text is required."),
103
+ questions: z
104
+ .array(z.object({
105
+ id: z.string().min(1, "A question id is required."),
106
+ type: z.enum(["choice", "noul", "score"]),
107
+ instructions: z.string().min(1, "Question instructions are required."),
108
+ criteria: z
109
+ .union([
110
+ z.record(z.string(), z.string()),
111
+ z.array(z.string()),
112
+ z.object({
113
+ true: z.string().optional(),
114
+ false: z.string().optional(),
115
+ }),
116
+ ])
117
+ .optional(),
118
+ }))
119
+ .min(1)
120
+ .max(SYSTEMONE_MAX_QUESTIONS),
121
+ }, async (args) => {
122
+ try {
123
+ const result = await handleMakeDecision(args, ctx.config);
124
+ return {
125
+ content: [{ type: "text", text: result.formatted }],
126
+ };
127
+ }
128
+ catch (err) {
129
+ return {
130
+ content: [{ type: "text", text: `Decision failed: ${err instanceof Error ? err.message : String(err)}` }],
131
+ isError: true,
132
+ };
133
+ }
134
+ });
135
+ }
94
136
  const transport = options?.transport ?? new StdioServerTransport();
95
137
  await server.connect(transport);
96
138
  return {
97
139
  server,
98
140
  close: async () => {
99
141
  await server.close();
142
+ if (!ownedContext)
143
+ return;
100
144
  await ctx.store.close();
101
145
  ctx.keywordIndex.close();
102
146
  // Release keep-alive sockets held by the embedder/description providers
@@ -18,12 +18,21 @@ export declare const MANDATORY_GUIDANCE_LINES: readonly string[];
18
18
  * `memory.promptEnforcement` is true.
19
19
  */
20
20
  export declare const QUIRK_ENFORCEMENT_LINES: readonly string[];
21
+ /**
22
+ * The conditional decision-model guidance lines. Only included when
23
+ * `decision.enabled` is true (the tool is not registered otherwise).
24
+ */
25
+ export declare const DECISION_GUIDANCE_LINES: readonly string[];
26
+ /** Extra guidance line, only when `decision.routeBeforeAsking` is enabled. */
27
+ export declare const DECISION_ROUTING_GUIDANCE_LINE = "- Before asking the user to choose between options, route the decision through `make_decision` first \u2014 ask the user only when the model is undecided (low confidence) or the choice is a matter of personal preference.";
21
28
  /**
22
29
  * Build the flat guidance lines array for the runtime system-prompt injector.
23
30
  * Returns a new array each call.
24
31
  */
25
32
  export declare function buildSystemGuidanceLines(opts: {
26
33
  promptEnforcement: boolean;
34
+ decisionEnabled?: boolean;
35
+ routeBeforeAsking?: boolean;
27
36
  }): string[];
28
37
  /**
29
38
  * Build the markdown-formatted AGENTS.md directive section, wrapped in sentinel
@@ -31,4 +40,6 @@ export declare function buildSystemGuidanceLines(opts: {
31
40
  */
32
41
  export declare function buildAgentsMdDirective(opts: {
33
42
  promptEnforcement: boolean;
43
+ decisionEnabled?: boolean;
44
+ routeBeforeAsking?: boolean;
34
45
  }): string;
@@ -68,17 +68,55 @@ export const QUIRK_ENFORCEMENT_LINES = [
68
68
  "",
69
69
  "Anti-pattern — NEVER finish a coding session without adding quirks for resolved errors.",
70
70
  ];
71
+ /**
72
+ * The conditional decision-model guidance lines. Only included when
73
+ * `decision.enabled` is true (the tool is not registered otherwise).
74
+ */
75
+ export const DECISION_GUIDANCE_LINES = [
76
+ "- `make_decision(state, questions)`: classify, route, or score short text with the local tev1 decision model. Questions are `choice`, `noul`, or `score`; keep `state` short and don't rely on it alone for high-stakes decisions.",
77
+ ];
78
+ /** Extra guidance line, only when `decision.routeBeforeAsking` is enabled. */
79
+ export const DECISION_ROUTING_GUIDANCE_LINE = "- Before asking the user to choose between options, route the decision through `make_decision` first — ask the user only when the model is undecided (low confidence) or the choice is a matter of personal preference.";
71
80
  /**
72
81
  * Build the flat guidance lines array for the runtime system-prompt injector.
73
82
  * Returns a new array each call.
74
83
  */
75
84
  export function buildSystemGuidanceLines(opts) {
76
85
  const lines = [...MANDATORY_GUIDANCE_LINES];
86
+ if (opts.decisionEnabled) {
87
+ const decisionLines = [...DECISION_GUIDANCE_LINES];
88
+ if (opts.routeBeforeAsking) {
89
+ decisionLines.push(DECISION_ROUTING_GUIDANCE_LINE);
90
+ }
91
+ // Keep the tool list grouped: insert right after the describe_image line.
92
+ const idx = lines.findIndex((line) => line.startsWith("- `describe_image"));
93
+ if (idx >= 0) {
94
+ lines.splice(idx + 1, 0, ...decisionLines);
95
+ }
96
+ else {
97
+ lines.push(...decisionLines);
98
+ }
99
+ }
77
100
  if (opts.promptEnforcement) {
78
101
  lines.push(...QUIRK_ENFORCEMENT_LINES);
79
102
  }
80
103
  return lines;
81
104
  }
105
+ /**
106
+ * The conditional decision-model section for the AGENTS.md directive. Only
107
+ * included when `decision.enabled` is true.
108
+ */
109
+ const DECISION_AGENTS_SECTION_LINES = [
110
+ "### Decision model (`make_decision`)",
111
+ "",
112
+ "- Classify, route, or score short text with the local tev1 model (`state` + 1-64 questions).",
113
+ "- Question types: `choice` (options map to descriptions), `noul` (true/false probability), `score` (ordered rubric levels, lowest first).",
114
+ "- Keep `state` short (~2k-token context); add a `none` option when no listed option may fit.",
115
+ "- Never use it as the only check for a high-stakes decision.",
116
+ "",
117
+ ];
118
+ /** Extra directive bullet, only when `decision.routeBeforeAsking` is enabled. */
119
+ const DECISION_ROUTING_AGENTS_LINE = "- With `decision.routeBeforeAsking` enabled: route option choices through `make_decision` before asking the user; ask the user only when the model is undecided (low confidence) or the choice is preference-based.";
82
120
  /**
83
121
  * Build the markdown-formatted AGENTS.md directive section, wrapped in sentinel
84
122
  * markers so `mergeAgentsMdContent` can replace it in place on re-runs.
@@ -126,6 +164,21 @@ export function buildAgentsMdDirective(opts) {
126
164
  "- Treating image files as text — use `describe_image` instead of reading raw bytes",
127
165
  "- Using `npx opencode-rag quirk` shell commands instead of the built-in quirk tools (`add_quirk` / `recall_quirks` / `update_quirk` / `delete_quirk`) (the tools are faster, already loaded in-process, and go through the trust monitor)",
128
166
  ];
167
+ if (opts.decisionEnabled) {
168
+ const decisionSection = [...DECISION_AGENTS_SECTION_LINES];
169
+ if (opts.routeBeforeAsking) {
170
+ // Insert before the trailing blank line of the section.
171
+ decisionSection.splice(decisionSection.length - 1, 0, DECISION_ROUTING_AGENTS_LINE);
172
+ }
173
+ // Place the decision section before the code-navigation decision tree.
174
+ const idx = lines.indexOf("### Decision tree — ALWAYS follow this order");
175
+ if (idx >= 0) {
176
+ lines.splice(idx, 0, ...decisionSection);
177
+ }
178
+ else {
179
+ lines.push(...decisionSection);
180
+ }
181
+ }
129
182
  if (opts.promptEnforcement) {
130
183
  lines.push("", "### MANDATORY quirk capture rules — you MUST call `add_quirk` when", "- A build, test, or type-check command fails and you resolve it", "- You discover an undocumented library constraint, peer dep, or workaround", "- You learn an environment-specific requirement (OS, tool version, etc.)", "- You make a design decision that future sessions should remember", "- You resolve a gotcha that cost more than one attempt", "", "### MANDATORY quirk hygiene — you MUST call `update_quirk` or `delete_quirk` when", "- A stored quirk is outdated, wrong, or has been fixed — update it or delete it instead of adding a contradicting duplicate", "- NEVER finish a coding session without adding quirks for resolved errors.");
131
184
  }
@@ -2,7 +2,7 @@
2
2
  * @fileoverview Factory functions for OpenCode autonomous-agent tools (file skeleton, find usages, describe image).
3
3
  */
4
4
  import type { ToolDefinition } from "@opencode-ai/plugin";
5
- import { type EmbeddingProvider, type VectorStore, type KeywordIndex } from "../core/interfaces.js";
5
+ import { type EmbeddingProvider, type VectorStore, type KeywordIndex, type DecisionProvider } from "../core/interfaces.js";
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";
@@ -149,3 +149,26 @@ export interface DeleteQuirkToolOptions {
149
149
  * @returns A tool definition suitable for OpenCode plugin registration.
150
150
  */
151
151
  export declare function createDeleteQuirkTool(options: DeleteQuirkToolOptions): ToolDefinition;
152
+ /** Options for creating the `make_decision` tool. */
153
+ export interface MakeDecisionToolOptions {
154
+ /** Resolved RAG config; `config.decision` gates and parameterizes the call. */
155
+ config: RagConfig;
156
+ /** Injectable provider (tests); defaults to `createDecisionProvider(config.decision)`. */
157
+ provider?: DecisionProvider;
158
+ }
159
+ /**
160
+ * Create the `make_decision` tool.
161
+ *
162
+ * Answers classification/decision questions against a short text using the
163
+ * configured decision model (tev1 via Ollama's `/v1/systemone`). The model is
164
+ * NOT a chat model — `state` is scored against named questions of type
165
+ * `choice`, `noul`, or `score`, and answers carry probabilities.
166
+ *
167
+ * Registered by the plugin only when `decision.enabled` is true; the tool
168
+ * re-checks the flag at execution time so runtime config changes degrade to a
169
+ * clear error instead of a stale provider call.
170
+ *
171
+ * @param options - Resolved config and an optional injected provider.
172
+ * @returns A tool definition suitable for OpenCode plugin registration.
173
+ */
174
+ export declare function createMakeDecisionTool(options: MakeDecisionToolOptions): ToolDefinition;