From 539ea0f50dfdbc18d5616e554e2cb1413468c38e Mon Sep 17 00:00:00 2001 From: Gergo Magyar Date: Wed, 28 Oct 2026 08:40:38 +0000 Subject: [PATCH] fix(opencode): skip ambiguous search options --- docs/editor-setup.md | 2 ++ gitnexus/README.md | 7 ++-- gitnexus/hooks/opencode/gitnexus-plugin.js | 41 ++++++++++++++++++++++ gitnexus/test/unit/opencode-plugin.test.ts | 6 ++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/docs/editor-setup.md b/docs/editor-setup.md index b9d16502f..98a42e701 100644 --- a/docs/editor-setup.md +++ b/docs/editor-setup.md @@ -22,6 +22,8 @@ OpenCode's plugin uses the v1 `tool.execute.after` API: it enriches search results and adds a stale-index hint after git mutations. The integration is verified with OpenCode 1.19.5; OpenCode v2 uses a different plugin API. +For Bash tools, the hooks recognize literal commands and known options; ambiguous shell constructs are skipped. Stale-index hints concern the current OpenCode project. + > ¹ **Antigravity hooks** follow the [Gemini CLI hooks reference](https://geminicli.com/docs/hooks/reference/) (Antigravity 2.0 is the documented successor to Gemini CLI). Augmentation runs in `AfterTool` because `BeforeTool` has no context-injection channel in the Gemini contract — the agent sees graph context appended to the tool result via `hookSpecificOutput.additionalContext`. Stale-index hints land in the same channel after a successful `git commit/merge/rebase/cherry-pick/pull`. The schema may evolve if Antigravity-specific hook docs diverge from Gemini CLI's; the implementation will track those changes. diff --git a/gitnexus/README.md b/gitnexus/README.md index 650eada5d..3149034d2 100644 --- a/gitnexus/README.md +++ b/gitnexus/README.md @@ -139,8 +139,11 @@ Add to `~/.cursor/mcp.json` (global — works for all projects): plugin (`~/.config/opencode/plugins/gitnexus.js`, auto-loaded by OpenCode — search augmentation via the OpenCode v1 `tool.execute.after` API and a stale-index hint after git mutations). Restart OpenCode after setup. Verified -with OpenCode 1.19.5; the OpenCode v2 plugin API is not covered. Manual MCP -configuration: +with OpenCode 1.19.5; the OpenCode v2 plugin API is not covered. Bash hooks +recognize literal commands and known options, skip ambiguous shell constructs, +and check the current OpenCode project's index for staleness. + +Manual MCP configuration: Add to `~/.config/opencode/config.json`: diff --git a/gitnexus/hooks/opencode/gitnexus-plugin.js b/gitnexus/hooks/opencode/gitnexus-plugin.js index 5dad98cfe..8a02babac 100644 --- a/gitnexus/hooks/opencode/gitnexus-plugin.js +++ b/gitnexus/hooks/opencode/gitnexus-plugin.js @@ -106,6 +106,40 @@ const FLAGS_WITH_VALUES = new Set([ '--exclude', ]); +/** Known switches without values; unfamiliar options make the query ambiguous. */ +const SEARCH_SWITCHES = new Set([ + '-n', + '-i', + '-s', + '-S', + '-F', + '-w', + '-x', + '-v', + '-c', + '-l', + '-q', + '-o', + '-H', + '-u', + '-U', + '-a', + '-I', + '-P', + '--hidden', + '--no-ignore', + '--line-number', + '--column', + '--smart-case', + '--ignore-case', + '--fixed-strings', + '--word-regexp', + '--invert-match', + '--with-filename', + '--no-heading', + '--pcre2', +]); + /** * Split an argv string into tokens, respecting single/double quoting so a * multi-word search phrase stays one token: `rg "foo bar"` → [rg, foo bar]. @@ -164,6 +198,7 @@ function extractBashSearchPattern(command) { if (typeof command !== 'string' || !/\brg\b|\bgrep\b/.test(command)) return null; const tokens = tokenizeArgv(command); if (!/(?:^|[/\\])(?:rg|grep)(?:\.exe)?$/.test(tokens[0] || '')) return null; + const isGrep = /(?:^|[/\\])grep(?:\.exe)?$/.test(tokens[0]); let pattern = null; let explicitPattern = null; let endOptions = false; @@ -192,6 +227,12 @@ function extractBashSearchPattern(command) { explicitPattern ??= token.slice(token.indexOf('=') + 1); else if (token.startsWith('-e')) explicitPattern ??= token.slice(2); else if (FLAGS_WITH_VALUES.has(token)) pending = 'skip'; + else if (!isGrep && token === '-r') pending = 'skip'; + else if (isGrep && ['-r', '-R', '-h', '--recursive'].includes(token)) continue; + else if (SEARCH_SWITCHES.has(token)) continue; + else if (token.includes('=') && FLAGS_WITH_VALUES.has(token.slice(0, token.indexOf('=')))) + continue; + else return null; continue; } pattern ??= token; diff --git a/gitnexus/test/unit/opencode-plugin.test.ts b/gitnexus/test/unit/opencode-plugin.test.ts index a2e0cd80c..41f280f40 100644 --- a/gitnexus/test/unit/opencode-plugin.test.ts +++ b/gitnexus/test/unit/opencode-plugin.test.ts @@ -67,6 +67,8 @@ describe('search augmentation through the public hook', () => { ['bash', { command: "rg '$literal' src" }, '$literal'], ['bash', { command: 'rg UserService # comment' }, 'UserService'], ['bash', { command: 'rg \\\nUserService src' }, 'UserService'], + ['bash', { command: 'rg -r replacement UserService src' }, 'UserService'], + ['bash', { command: 'rg --hidden -i UserService src' }, 'UserService'], ])('augments %s %j with %s', async (tool, args, pattern) => { expect(await invoke(tool, args)).toBe('hits\n\n[GitNexus] callers: A'); expect(execFileMock).toHaveBeenCalledTimes(1); @@ -104,6 +106,10 @@ describe('search augmentation through the public hook', () => { ['bash', { command: 'rg `query` src' }], ['bash', { command: 'rg UserService\\' }], ['bash', { command: 'rg -n # UserService' }], + ['bash', { command: 'rg --unknown-option value UserService src' }], + ['bash', { command: 'rg --sort path UserService src' }], + ['bash', { command: 'rg --iglob "*.ts" UserService src' }], + ['bash', { command: 'rg -rreplacement UserService src' }], ['bash', { command: null }], ['bash', {}], ['read', { filePath: 'a.ts' }],