_glm47_failclosed_test.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Model-free checks of the fail-closed validator and name resolution."""
  2. from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionToolsParam
  3. from vllm.tool_parsers.abstract_tool_parser import ToolParserManager as M
  4. M.import_tool_parser("/tmp/plugin.py")
  5. P = M.get_tool_parser("glm47_failclosed")._parser_engine_cls
  6. def tool(name, props):
  7. return ChatCompletionToolsParam.model_validate({
  8. "type": "function",
  9. "function": {"name": name, "parameters": {"type": "object", "properties": props}},
  10. })
  11. parser = P.__new__(P) # only _tools is needed
  12. parser._tools = [
  13. tool("bash", {"command": {}, "timeout": {}, "workdir": {}}),
  14. tool("run_code", {"code": {}, "description": {}}),
  15. ]
  16. print("== name resolution")
  17. for emitted, want in [("bash", "bash"), ("bash</arg_key>x", "bash"),
  18. ("bash1635", None), ("run_code", "run_code"),
  19. ("nope", None), ("run_code</arg_value>", "run_code")]:
  20. got = parser._resolve_name(emitted)
  21. print(f" {emitted:<24} -> {str(got):<10} {'ok' if got == want else 'WANTED ' + str(want)}")
  22. print("== validation")
  23. cases = [
  24. ("good call", "bash", '{"command": "ls", "timeout": 5}', True),
  25. ("no args", "bash", "", True),
  26. ("unknown tool", "list_files", '{"path": "/"}', False),
  27. ("corrupted name", "bash</arg_value><arg_key>x", '{"command": "ls"}', False),
  28. ("key not in schema", "bash", '{"prefix": "x"}', False),
  29. ("corrupt key", "run_code",
  30. '{"print_code_snapshot</arg_value><arg_key>description": "x"}', False),
  31. ("not json", "bash", '{"command": ', False),
  32. ("args not an object", "bash", '["command"]', False),
  33. ]
  34. for label, name, args, want_ok in cases:
  35. reason = parser._reject_reason(name, args)
  36. ok = reason is None
  37. mark = "ok" if ok == want_ok else "UNEXPECTED"
  38. print(f" {label:<20} {'accept' if ok else 'refuse':<7} {mark:<11} {(reason or '')[:60]}")