fix: distinguish empty documentation searches from tool failures
This commit is contained in:
parent
3a30aac297
commit
13f1e99aee
@ -8,7 +8,15 @@ HZ="$ROOT/../.horizon"
|
||||
if command -v oe-mcp-cli >/dev/null 2>&1; then
|
||||
exec oe-mcp-cli search "$Q"
|
||||
fi
|
||||
grep -rIl --include='*.md' -i -- "$Q" "$HZ" 2>/dev/null | head -8 | while read -r f; do
|
||||
matches=$(grep -rIl --include='*.md' -i -- "$Q" "$HZ")
|
||||
status=$?
|
||||
if [ "$status" -eq 1 ]; then
|
||||
echo "未找到匹配文档。查询按完整关键词或准确片段匹配;请缩短为单一关键词后重试。"
|
||||
exit 0
|
||||
fi
|
||||
[ "$status" -eq 0 ] || exit "$status"
|
||||
# Consume upstream output so bounded display does not generate SIGPIPE under pipefail.
|
||||
printf '%s\n' "$matches" | sed -n '1,8p' | while read -r f; do
|
||||
echo "=== $f"
|
||||
grep -n -i -B2 -A8 -- "$Q" "$f" | head -60
|
||||
grep -n -i -B2 -A8 -- "$Q" "$f" | sed -n '1,60p' || exit "$?"
|
||||
done
|
||||
|
||||
125
scripts/test_oe_doc.py
Normal file
125
scripts/test_oe_doc.py
Normal file
@ -0,0 +1,125 @@
|
||||
"""Local Markdown fixtures only; no document service, provider or board calls."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / 'agent/oe-doc.sh'
|
||||
NODE = os.environ.get('BENCH_TEST_NODE') or shutil.which('node')
|
||||
|
||||
|
||||
class OeDocTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.root = Path(self.tmp.name)
|
||||
(self.root / 'bench').mkdir()
|
||||
self.docs = self.root / '.horizon/docs'
|
||||
self.docs.mkdir(parents=True)
|
||||
self.bin = self.root / 'bin'
|
||||
self.bin.mkdir()
|
||||
for name in ('bash', 'grep', 'head', 'sed', 'sleep'):
|
||||
(self.bin / name).symlink_to(shutil.which(name))
|
||||
self.env = {**os.environ, 'PATH': str(self.bin), 'BENCH_ROOT': str(self.root / 'bench')}
|
||||
|
||||
def run_doc(self, query='quant_config'):
|
||||
return subprocess.run([str(SCRIPT), query], env=self.env, capture_output=True,
|
||||
text=True, timeout=10)
|
||||
|
||||
def fake_grep(self, body):
|
||||
p = self.bin / 'grep'
|
||||
p.unlink()
|
||||
p.write_text('#!/usr/bin/env bash\n' + body)
|
||||
p.chmod(0o700)
|
||||
|
||||
def test_no_match_is_normal_and_explains_keyword_contract(self):
|
||||
(self.docs / 'doc.md').write_text('quant_config node_config\n')
|
||||
result = self.run_doc('quant_config node_config qtype int16')
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn('未找到', result.stdout)
|
||||
self.assertIn('单一关键词', result.stdout)
|
||||
|
||||
def test_success_preserves_matching_context(self):
|
||||
(self.docs / 'doc.md').write_text('before\nquant_config: int16\nafter\n')
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn('2:quant_config: int16', result.stdout)
|
||||
self.assertIn('1-before', result.stdout)
|
||||
|
||||
def test_missing_document_root_preserves_real_error(self):
|
||||
shutil.rmtree(self.root / '.horizon')
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertTrue(result.stderr)
|
||||
self.assertNotIn('未找到', result.stdout)
|
||||
|
||||
@unittest.skipIf(os.geteuid() == 0, 'root can bypass fixture file permissions')
|
||||
def test_permission_error_is_not_a_no_match(self):
|
||||
doc = self.docs / 'doc.md'
|
||||
doc.write_text('quant_config\n')
|
||||
doc.chmod(0)
|
||||
self.addCleanup(doc.chmod, 0o600)
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertTrue(result.stderr)
|
||||
self.assertNotIn('未找到', result.stdout)
|
||||
|
||||
def test_file_limit_does_not_turn_truncation_into_sigpipe(self):
|
||||
for i in range(500):
|
||||
(self.docs / (f'{i:04d}-' + 'x' * 160 + '.md')).write_text('quant_config\n')
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(result.stdout.count('=== '), 8)
|
||||
|
||||
def test_line_limit_does_not_turn_truncation_into_sigpipe(self):
|
||||
(self.docs / 'doc.md').write_text('quant_config: int16\n' * 20000)
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(len(result.stdout.splitlines()), 61)
|
||||
|
||||
def test_read_error_after_search_cannot_be_hidden_by_later_success(self):
|
||||
self.fake_grep('if [[ "$1" == -rIl ]]; then printf "first.md\\nsecond.md\\n"; '
|
||||
'elif [[ "${!#}" == first.md ]]; then echo "fixture I/O error" >&2; exit 2; '
|
||||
'else echo "1:quant_config"; fi\n')
|
||||
result = self.run_doc()
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertIn('fixture I/O error', result.stderr)
|
||||
|
||||
@unittest.skipUnless(NODE, 'Node required for the existing execFileSync boundary')
|
||||
def test_node_wrapper_distinguishes_success_empty_and_read_error(self):
|
||||
doc = self.docs / 'doc.md'
|
||||
doc.write_text('quant_config: int16\n')
|
||||
for query, expected_status in [('quant_config', 0), ('absent-keyword', 0)]:
|
||||
with self.subTest(query=query):
|
||||
self.assertEqual(self.node_doc(query)['status'], expected_status)
|
||||
shutil.rmtree(self.root / '.horizon')
|
||||
result = self.node_doc('quant_config')
|
||||
self.assertEqual(result['status'], 2)
|
||||
self.assertGreater(result['stderr_bytes'], 0)
|
||||
|
||||
@unittest.skipUnless(NODE, 'Node required for the existing execFileSync boundary')
|
||||
def test_node_timeout_remains_an_error(self):
|
||||
self.fake_grep('sleep 2\n')
|
||||
result = self.node_doc('quant_config', timeout_ms=50)
|
||||
self.assertEqual(result['code'], 'ETIMEDOUT')
|
||||
self.assertEqual(result['signal'], 'SIGTERM')
|
||||
|
||||
def node_doc(self, query, timeout_ms=60000):
|
||||
code = '''const {execFileSync}=require('node:child_process');
|
||||
try {const out=execFileSync(process.argv[1],[process.argv[2]],{
|
||||
encoding:'utf8',timeout:Number(process.argv[3]),maxBuffer:4<<20});
|
||||
console.log(JSON.stringify({status:0,stdout_bytes:Buffer.byteLength(out)}));
|
||||
} catch(e) {console.log(JSON.stringify({status:e.status,code:e.code,signal:e.signal,
|
||||
stdout_bytes:Buffer.byteLength(e.stdout??''),stderr_bytes:Buffer.byteLength(e.stderr??'')}));}'''
|
||||
result = subprocess.run([NODE, '-e', code, str(SCRIPT), query, str(timeout_ms)],
|
||||
env=self.env, capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
Loading…
x
Reference in New Issue
Block a user