fix(opencode): skip ambiguous search options

This commit is contained in:
Gergo Magyar 2026-10-28 08:40:38 +00:00
parent 6044db96a9
commit 539ea0f50d
4 changed files with 54 additions and 2 deletions

View file

@ -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.
<a id="fn-antigravity-hooks"></a>
> ¹ **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.

View file

@ -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`:

View file

@ -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;

View file

@ -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' }],