wechatbridge-cli 1.4.5__tar.gz → 1.4.6__tar.gz

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 (23) hide show
  1. {wechatbridge_cli-1.4.5/wechatbridge_cli.egg-info → wechatbridge_cli-1.4.6}/PKG-INFO +1 -1
  2. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/tests/test_hardening.py +447 -0
  3. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/__init__.py +1 -1
  4. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/runner_common.py +180 -66
  5. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6/wechatbridge_cli.egg-info}/PKG-INFO +1 -1
  6. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/LICENSE +0 -0
  7. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/README.md +0 -0
  8. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/pyproject.toml +0 -0
  9. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/setup.cfg +0 -0
  10. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/tests/test_codex.py +0 -0
  11. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/__main__.py +0 -0
  12. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/agy.py +0 -0
  13. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/codex.py +0 -0
  14. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/config.py +0 -0
  15. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/grok.py +0 -0
  16. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/ilink.py +0 -0
  17. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/main.py +0 -0
  18. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge/update_check.py +0 -0
  19. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge_cli.egg-info/SOURCES.txt +0 -0
  20. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge_cli.egg-info/dependency_links.txt +0 -0
  21. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge_cli.egg-info/entry_points.txt +0 -0
  22. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge_cli.egg-info/requires.txt +0 -0
  23. {wechatbridge_cli-1.4.5 → wechatbridge_cli-1.4.6}/wechatbridge_cli.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wechatbridge-cli
3
- Version: 1.4.5
3
+ Version: 1.4.6
4
4
  Summary: Bridge WeChat messages to agy, Grok Build, or Codex CLIs — text/image/file/voice in, CLI replies and generated files back.
5
5
  Author: WeChatBridge contributors
6
6
  License: MIT
@@ -1890,3 +1890,450 @@ class TestPreflightBeforeSlot(unittest.IsolatedAsyncioTestCase):
1890
1890
 
1891
1891
  if __name__ == "__main__":
1892
1892
  unittest.main()
1893
+
1894
+
1895
+
1896
+ class TestClassifyCliError(unittest.TestCase):
1897
+ """_classify_cli_error returns correct category for each error pattern."""
1898
+
1899
+ def _cat(self, raw: str, backend: str = "agy") -> str:
1900
+ from wechatbridge.runner_common import _classify_cli_error
1901
+ return _classify_cli_error(raw, backend=backend)
1902
+
1903
+ def _fmt(self, raw: str, backend: str = "agy") -> str:
1904
+ from wechatbridge.runner_common import format_cli_error
1905
+ return format_cli_error(raw, backend=backend)
1906
+
1907
+ # --- payload_too_large --------------------------------------------------------
1908
+
1909
+ def test_request_payload_size_exceeds(self):
1910
+ self.assertEqual(self._cat("request payload size exceeds the limit"), "payload_too_large")
1911
+
1912
+ def test_payload_too_large(self):
1913
+ self.assertEqual(self._cat("payload too large"), "payload_too_large")
1914
+
1915
+ def test_request_entity_too_large(self):
1916
+ self.assertEqual(self._cat("Request Entity Too Large"), "payload_too_large")
1917
+
1918
+ def test_content_length_exceeds(self):
1919
+ self.assertEqual(self._cat("Content-Length exceeds the limit of 8MB"), "payload_too_large")
1920
+
1921
+ def test_content_length_is_too_large(self):
1922
+ self.assertEqual(self._cat("content length is too large"), "payload_too_large")
1923
+
1924
+ def test_http_status_413_explicit(self):
1925
+ self.assertEqual(self._cat("HTTP status 413"), "payload_too_large")
1926
+ self.assertEqual(self._cat("status code 413"), "payload_too_large")
1927
+ self.assertEqual(self._cat("http code 413"), "payload_too_large")
1928
+ self.assertEqual(self._cat("code=413"), "payload_too_large")
1929
+
1930
+ def test_json_code_413(self):
1931
+ self.assertEqual(self._cat('{"code": 413, "message": "too big"}'), "payload_too_large")
1932
+
1933
+ def test_413_in_date_not_matched(self):
1934
+ """A bare 413 in a date string must NOT be classified as payload_too_large."""
1935
+ self.assertNotEqual(self._cat("Session expired on 2024-04-13 at 10:00"), "payload_too_large")
1936
+
1937
+ def test_413_in_path_not_matched(self):
1938
+ """A bare 413 in a file path or directory name must NOT match."""
1939
+ self.assertNotEqual(self._cat("file not found: /tmp/record-413.txt"), "payload_too_large")
1940
+
1941
+ def test_413_in_version_not_matched(self):
1942
+ """Version numbers containing 413 must NOT match."""
1943
+ self.assertNotEqual(self._cat("library version 1.413.0"), "payload_too_large")
1944
+
1945
+ # --- context_too_large --------------------------------------------------------
1946
+
1947
+ def test_input_token_count_exceeds(self):
1948
+ self.assertEqual(self._cat("input token count exceeds the maximum"), "context_too_large")
1949
+
1950
+ def test_context_length_exceeded(self):
1951
+ self.assertEqual(self._cat("context length exceeded"), "context_too_large")
1952
+
1953
+ def test_maximum_context_length(self):
1954
+ self.assertEqual(self._cat("maximum context length is 128k tokens"), "context_too_large")
1955
+
1956
+ def test_context_window_exceeded(self):
1957
+ self.assertEqual(self._cat("context window exceeded"), "context_too_large")
1958
+
1959
+ def test_too_many_tokens(self):
1960
+ self.assertEqual(self._cat("too many tokens in this conversation"), "context_too_large")
1961
+
1962
+ def test_your_input_context_is_too_long(self):
1963
+ self.assertEqual(self._cat("your input context is too long for this model"), "context_too_large")
1964
+
1965
+ def test_invalid_argument_with_context(self):
1966
+ self.assertEqual(self._cat("INVALID_ARGUMENT: context is too long"), "context_too_large")
1967
+
1968
+ def test_resource_exhausted_with_token(self):
1969
+ self.assertEqual(self._cat("RESOURCE_EXHAUSTED: token limit exceeded"), "context_too_large")
1970
+
1971
+ def test_resource_exhausted_with_maximum(self):
1972
+ self.assertEqual(self._cat("resource_exhausted: maximum context size reached"), "context_too_large")
1973
+
1974
+ # --- invalid_argument (generic) -----------------------------------------------
1975
+
1976
+ def test_generic_invalid_argument(self):
1977
+ self.assertEqual(self._cat("INVALID_ARGUMENT: unknown field xyz"), "invalid_argument")
1978
+
1979
+ def test_invalid_argument_no_context_token(self):
1980
+ self.assertEqual(self._cat("invalid_argument: bad request format"), "invalid_argument")
1981
+
1982
+ # --- auth ---------------------------------------------------------------------
1983
+
1984
+ def test_auth_generic(self):
1985
+ self.assertEqual(self._cat("Not signed in. Please run login --device"), "auth")
1986
+
1987
+ def test_auth_unauthorized(self):
1988
+ self.assertEqual(self._cat("unauthorized access"), "auth")
1989
+
1990
+ def test_auth_401_with_token(self):
1991
+ self.assertEqual(self._cat("401 error: token expired"), "auth")
1992
+
1993
+ def test_auth_codex_specific(self):
1994
+ self.assertEqual(self._cat("codex login required", backend="codex"), "auth")
1995
+
1996
+ def test_auth_codex_api_key(self):
1997
+ self.assertEqual(self._cat("codex_api_key missing", backend="codex"), "auth")
1998
+
1999
+ # --- rate_limit ---------------------------------------------------------------
2000
+
2001
+ def test_eligibility_resource_exhausted(self):
2002
+ self.assertEqual(self._cat("Eligibility check failed: RESOURCE_EXHAUSTED"), "resource_exhausted")
2003
+
2004
+ def test_resource_exhausted_generic(self):
2005
+ self.assertEqual(self._cat("RESOURCE_EXHAUSTED: resource has been exhausted"), "resource_exhausted")
2006
+
2007
+ def test_rate_limit_exceeded(self):
2008
+ self.assertEqual(self._cat("rate limit exceeded, please slow down"), "rate_limit")
2009
+
2010
+ def test_too_many_requests(self):
2011
+ self.assertEqual(self._cat("HTTP 429 Too Many Requests"), "rate_limit")
2012
+
2013
+ def test_bare_429(self):
2014
+ self.assertEqual(self._cat("upstream returned status 429"), "bare_429")
2015
+
2016
+ # --- quota --------------------------------------------------------------------
2017
+
2018
+ def test_quota_exceeded(self):
2019
+ self.assertEqual(self._cat("You exceeded your current quota"), "quota")
2020
+
2021
+ def test_daily_quota(self):
2022
+ self.assertEqual(self._cat("daily quota exceeded"), "quota")
2023
+
2024
+ def test_usage_limit(self):
2025
+ self.assertEqual(self._cat("usage limit reached"), "quota")
2026
+
2027
+ # --- network ------------------------------------------------------------------
2028
+
2029
+ def test_connection_refused(self):
2030
+ self.assertEqual(self._cat("connection refused"), "network")
2031
+
2032
+ def test_connection_reset(self):
2033
+ self.assertEqual(self._cat("connection reset by peer"), "network")
2034
+
2035
+ def test_network_unreachable(self):
2036
+ self.assertEqual(self._cat("network is unreachable"), "network")
2037
+
2038
+ # --- cascade_timeout ----------------------------------------------------------
2039
+
2040
+ def test_cascade_timeout(self):
2041
+ self.assertEqual(self._cat("timeout waiting for cascade"), "cascade_timeout")
2042
+
2043
+ def test_cascade_response_timeout(self):
2044
+ self.assertEqual(self._cat("timeout waiting for response"), "cascade_timeout")
2045
+
2046
+ # --- timeout ------------------------------------------------------------------
2047
+
2048
+ def test_generic_timeout(self):
2049
+ self.assertEqual(self._cat("timeout occurred"), "timeout")
2050
+
2051
+ def test_timed_out(self):
2052
+ self.assertEqual(self._cat("operation timed out"), "timeout")
2053
+
2054
+ def test_deadline_exceeded(self):
2055
+ self.assertEqual(self._cat("deadline exceeded"), "timeout")
2056
+
2057
+ # --- permission ---------------------------------------------------------------
2058
+
2059
+ def test_permission_denied(self):
2060
+ self.assertEqual(self._cat("permission denied"), "permission")
2061
+
2062
+ # --- session_not_found --------------------------------------------------------
2063
+
2064
+ def test_no_session_found(self):
2065
+ self.assertEqual(self._cat("no session found"), "session_not_found")
2066
+
2067
+ # --- model_invalid ------------------------------------------------------------
2068
+
2069
+ def test_model_not_found(self):
2070
+ self.assertEqual(self._cat("model not found"), "model_invalid")
2071
+
2072
+ def test_model_unknown(self):
2073
+ self.assertEqual(self._cat("unknown model xyz"), "model_invalid")
2074
+
2075
+ # --- command_not_found --------------------------------------------------------
2076
+
2077
+ def test_command_not_found(self):
2078
+ self.assertEqual(self._cat("command not found: xyz"), "command_not_found")
2079
+
2080
+ # --- not_found ----------------------------------------------------------------
2081
+
2082
+ def test_not_found_generic(self):
2083
+ self.assertEqual(self._cat("not found"), "not_found")
2084
+
2085
+ def test_enoent(self):
2086
+ self.assertEqual(self._cat("enoent: no such file"), "not_found")
2087
+
2088
+ # --- unknown ------------------------------------------------------------------
2089
+
2090
+ def test_empty_is_unknown(self):
2091
+ self.assertEqual(self._cat(""), "unknown")
2092
+
2093
+ def test_garbage_is_unknown(self):
2094
+ self.assertEqual(self._cat("abc123 random noise"), "unknown")
2095
+
2096
+
2097
+ class TestFormatCliErrorCategories(unittest.TestCase):
2098
+ """format_cli_error produces correct user-facing copy for each category."""
2099
+
2100
+ def _fmt(self, raw: str, backend: str = "agy") -> str:
2101
+ from wechatbridge.runner_common import format_cli_error
2102
+ return format_cli_error(raw, backend=backend)
2103
+
2104
+ def test_payload_too_large_title(self):
2105
+ out = self._fmt("request payload size exceeds the limit")
2106
+ self.assertIn("请求内容过大", out)
2107
+ self.assertIn("❌", out)
2108
+ self.assertIn("/new", out)
2109
+
2110
+ def test_context_too_large_title(self):
2111
+ out = self._fmt("context length exceeded")
2112
+ self.assertIn("会话内容过长", out)
2113
+ self.assertIn("❌", out)
2114
+ self.assertIn("/new", out)
2115
+
2116
+ def test_invalid_argument_title(self):
2117
+ out = self._fmt("INVALID_ARGUMENT: bad field")
2118
+ self.assertIn("请求参数无效", out)
2119
+ self.assertIn("❌", out)
2120
+
2121
+ def test_rate_limit_still_notice(self):
2122
+ out = self._fmt("rate limit exceeded")
2123
+ self.assertIn("🔔", out)
2124
+ self.assertIn("请求较多", out)
2125
+
2126
+ def test_quota_still_notice(self):
2127
+ out = self._fmt("quota exceeded")
2128
+ self.assertIn("🔔", out)
2129
+ self.assertIn("额度相关", out)
2130
+
2131
+ def test_auth_still_error(self):
2132
+ out = self._fmt("not signed in")
2133
+ self.assertIn("❌", out)
2134
+ self.assertIn("未登录", out)
2135
+
2136
+ def test_network_still_error(self):
2137
+ out = self._fmt("connection refused")
2138
+ self.assertIn("网络错误", out)
2139
+
2140
+ def test_timeout_still_error(self):
2141
+ out = self._fmt("timeout")
2142
+ self.assertIn("超时", out)
2143
+
2144
+ def test_unknown_still_generic(self):
2145
+ out = self._fmt("something weird happened")
2146
+ self.assertIn("执行失败", out)
2147
+
2148
+
2149
+ class TestFormatCliErrorNoRawInReply(unittest.TestCase):
2150
+ """format_cli_error must never echo raw English text to WeChat users."""
2151
+
2152
+ def _fmt(self, raw: str) -> str:
2153
+ from wechatbridge.runner_common import format_cli_error
2154
+ return format_cli_error(raw, backend="agy")
2155
+
2156
+ def test_no_raw_payload_error(self):
2157
+ out = self._fmt("request payload size exceeds the limit")
2158
+ self.assertNotIn("payload", out.lower())
2159
+ self.assertNotIn("request", out.lower())
2160
+
2161
+ def test_no_raw_context_error(self):
2162
+ out = self._fmt("context length exceeded")
2163
+ self.assertNotIn("context", out.lower())
2164
+ self.assertNotIn("exceeded", out.lower())
2165
+
2166
+ def test_no_raw_invalid_argument(self):
2167
+ out = self._fmt("INVALID_ARGUMENT: bad field")
2168
+ self.assertNotIn("INVALID_ARGUMENT", out)
2169
+
2170
+ def test_no_raw_rate_limit(self):
2171
+ out = self._fmt("rate limit exceeded")
2172
+ self.assertNotIn("rate", out.lower())
2173
+ self.assertNotIn("limit", out.lower())
2174
+
2175
+
2176
+ class TestFormatCliErrorLogging(unittest.TestCase):
2177
+ """format_cli_error logs category, not raw text."""
2178
+
2179
+ def test_log_contains_category_not_raw(self):
2180
+ from wechatbridge.runner_common import format_cli_error, _classify_cli_error
2181
+ import logging
2182
+ import io
2183
+
2184
+ # First, verify the category is correct
2185
+ category = _classify_cli_error("request payload size exceeds the limit")
2186
+ self.assertEqual(category, "payload_too_large")
2187
+
2188
+ # Capture log output — set logger level to INFO so our info() call propagates
2189
+ log_capture = io.StringIO()
2190
+ handler = logging.StreamHandler(log_capture)
2191
+ handler.setLevel(logging.DEBUG)
2192
+ logger = logging.getLogger("wechatbridge.runner")
2193
+ old_level = logger.level
2194
+ logger.setLevel(logging.DEBUG)
2195
+ logger.addHandler(handler)
2196
+ try:
2197
+ format_cli_error("request payload size exceeds the limit", backend="agy")
2198
+ finally:
2199
+ logger.removeHandler(handler)
2200
+ logger.setLevel(old_level)
2201
+
2202
+ log_output = log_capture.getvalue()
2203
+ self.assertIn("category=payload_too_large", log_output)
2204
+ # Verify raw text is NOT in the log
2205
+ self.assertNotIn("request payload", log_output)
2206
+
2207
+ def test_category_independent_of_raw(self):
2208
+ """_classify_cli_error is a pure function; verify it directly."""
2209
+ from wechatbridge.runner_common import _classify_cli_error
2210
+
2211
+ # payload_too_large
2212
+ self.assertEqual(_classify_cli_error("request payload size exceeds the limit"), "payload_too_large")
2213
+ self.assertEqual(_classify_cli_error("payload too large"), "payload_too_large")
2214
+ # context_too_large
2215
+ self.assertEqual(_classify_cli_error("context length exceeded"), "context_too_large")
2216
+ self.assertEqual(_classify_cli_error("too many tokens"), "context_too_large")
2217
+ # invalid_argument
2218
+ self.assertEqual(_classify_cli_error("INVALID_ARGUMENT: bad field"), "invalid_argument")
2219
+ # rate_limit (resource_exhausted without context/token keyword)
2220
+ self.assertEqual(_classify_cli_error("RESOURCE_EXHAUSTED: quota"), "resource_exhausted")
2221
+ # rate_limit (explicit)
2222
+ self.assertEqual(_classify_cli_error("rate limit exceeded"), "rate_limit")
2223
+ # bare 429
2224
+ self.assertEqual(_classify_cli_error("status 429"), "bare_429")
2225
+ # quota
2226
+ self.assertEqual(_classify_cli_error("quota exceeded"), "quota")
2227
+
2228
+
2229
+ class TestFormatCliError413Boundary(unittest.TestCase):
2230
+ """413 must not be misclassified when appearing in dates, paths, or versions."""
2231
+
2232
+ def _cat(self, raw: str) -> str:
2233
+ from wechatbridge.runner_common import _classify_cli_error
2234
+ return _classify_cli_error(raw)
2235
+
2236
+ def test_date_with_413(self):
2237
+ self.assertNotEqual(self._cat("2024-04-13T10:00:00"), "payload_too_large")
2238
+
2239
+ def test_path_with_413(self):
2240
+ self.assertNotEqual(self._cat("/tmp/file-413.txt"), "payload_too_large")
2241
+
2242
+ def test_version_with_413(self):
2243
+ self.assertNotEqual(self._cat("version 1.413.2"), "payload_too_large")
2244
+
2245
+ def test_error_code_413_without_prefix(self):
2246
+ """A bare '413' without status/code/http prefix should NOT match."""
2247
+ self.assertNotEqual(self._cat("error 413 occurred"), "payload_too_large")
2248
+
2249
+ def test_explicit_status_413(self):
2250
+ self.assertEqual(self._cat("status 413"), "payload_too_large")
2251
+
2252
+ def test_explicit_code_413(self):
2253
+ self.assertEqual(self._cat("code: 413"), "payload_too_large")
2254
+
2255
+
2256
+ class TestFormatCliErrorContextResourceExhausted(unittest.TestCase):
2257
+ """RESOURCE_EXHAUSTED with context/token keywords must classify as context_too_large."""
2258
+
2259
+ def _cat(self, raw: str) -> str:
2260
+ from wechatbridge.runner_common import _classify_cli_error
2261
+ return _classify_cli_error(raw)
2262
+
2263
+ def test_resource_exhausted_with_context(self):
2264
+ self.assertEqual(self._cat("RESOURCE_EXHAUSTED: context length exceeded"), "context_too_large")
2265
+
2266
+ def test_resource_exhausted_with_token(self):
2267
+ self.assertEqual(self._cat("RESOURCE_EXHAUSTED: token limit reached"), "context_too_large")
2268
+
2269
+ def test_resource_exhausted_with_maximum(self):
2270
+ self.assertEqual(self._cat("RESOURCE_EXHAUSTED: maximum context size"), "context_too_large")
2271
+
2272
+ def test_invalid_argument_with_context(self):
2273
+ self.assertEqual(self._cat("INVALID_ARGUMENT: context too long"), "context_too_large")
2274
+
2275
+
2276
+ class TestFormatCliErrorPreservesExistingThrottle(unittest.TestCase):
2277
+ """Existing throttle/quota behavior must not be regressed."""
2278
+
2279
+ def _fmt(self, raw: str) -> str:
2280
+ from wechatbridge.runner_common import format_cli_error
2281
+ return format_cli_error(raw, backend="agy")
2282
+
2283
+ def test_eligibility_still_busy(self):
2284
+ out = self._fmt("Eligibility check failed: RESOURCE_EXHAUSTED (code 429)")
2285
+ self.assertIn("助手通道繁忙", out)
2286
+ self.assertIn("🔔", out)
2287
+
2288
+ def test_quota_exceeded_still_quota(self):
2289
+ out = self._fmt("You exceeded your current quota")
2290
+ self.assertIn("额度相关", out)
2291
+ self.assertIn("🔔", out)
2292
+
2293
+ def test_rate_limit_still_busy(self):
2294
+ out = self._fmt("rate limit exceeded")
2295
+ self.assertIn("请求较多", out)
2296
+ self.assertIn("🔔", out)
2297
+
2298
+ def test_bare_429_still_busy(self):
2299
+ out = self._fmt("upstream returned status 429")
2300
+ self.assertIn("助手通道繁忙", out)
2301
+ self.assertIn("🔔", out)
2302
+
2303
+
2304
+ class TestClearInitializedPreservesHistory(unittest.TestCase):
2305
+ """clear_initialized must only remove .initialized flags, not conversation history."""
2306
+
2307
+ def test_clear_initialized_only_removes_flags(self):
2308
+ from wechatbridge.runner_common import clear_initialized, ensure_session_dir
2309
+
2310
+ with tempfile.TemporaryDirectory() as td:
2311
+ sd = os.path.join(td, "user_test")
2312
+ os.makedirs(sd, exist_ok=True)
2313
+ # Create .initialized flag
2314
+ flag = os.path.join(sd, ".initialized.agy")
2315
+ with open(flag, "w") as f:
2316
+ f.write("1")
2317
+ # Create some history files
2318
+ history_dir = os.path.join(sd, ".gemini", "antigravity-cli", "conversations")
2319
+ os.makedirs(history_dir, exist_ok=True)
2320
+ history_file = os.path.join(history_dir, "conv.db")
2321
+ with open(history_file, "w") as f:
2322
+ f.write("history data")
2323
+ # Create prefs
2324
+ prefs_file = os.path.join(sd, "prefs.json")
2325
+ with open(prefs_file, "w") as f:
2326
+ f.write("{}")
2327
+
2328
+ clear_initialized(sd, backend="agy")
2329
+
2330
+ # Flag is removed
2331
+ self.assertFalse(os.path.exists(flag))
2332
+ # History is preserved
2333
+ self.assertTrue(os.path.exists(history_file))
2334
+ # Prefs are preserved
2335
+ self.assertTrue(os.path.exists(prefs_file))
2336
+
2337
+
2338
+ if __name__ == "__main__":
2339
+ unittest.main()
@@ -1,2 +1,2 @@
1
1
  """WeChatBridge — bridge WeChat messages to agy, Grok Build, or Codex CLIs."""
2
- __version__ = "1.4.5"
2
+ __version__ = "1.4.6"
@@ -283,25 +283,65 @@ def format_artifact_send_failure_notice(art_name: str, reason: str) -> str:
283
283
  )
284
284
 
285
285
 
286
- def format_cli_error(raw_message: str, *, backend: str = "") -> str:
287
- """Map backend stderr/JSON error text into a short Chinese user reply.
288
-
289
- Never put English raw blobs or internal path/env names into user text.
290
- Details stay in the server log only.
286
+ def _classify_cli_error(raw_message: str, *, backend: str = "") -> str:
287
+ """Classify a backend error string into a category label.
288
+
289
+ Returns one of:
290
+ payload_too_large, context_too_large, invalid_argument,
291
+ auth, resource_exhausted, rate_limit, bare_429, quota,
292
+ network, timeout, cascade_timeout, permission,
293
+ session_not_found, model_invalid, command_not_found, not_found,
294
+ unknown
295
+
296
+ This is a pure function (no I/O, no logging). ``format_cli_error``
297
+ drives user-facing copy from the category; callers that need the
298
+ category directly (e.g. structured logging) can use this too.
291
299
  """
292
- raw = clean_output(raw_message or "") or "未知错误"
300
+ raw = clean_output(raw_message or "") or ""
293
301
  lower = raw.lower()
294
302
  backend = (backend or "").strip().lower()
295
- logger.info(
296
- "format_cli_error backend=%s raw=%.300s",
297
- backend or "?",
298
- raw,
299
- )
300
303
 
301
- # codex-specific auth/login recognition (backend-scoped, precise).
302
- # Only explicit login semantics match, so ordinary API errors, rate
303
- # limits and model errors are never misclassified as a login problem.
304
- # agy/grok keep using the generic block below (results unchanged).
304
+ # --- 1. Payload / request body too large (413) --------------------------------
305
+ # Match explicit payload/body-size phrases first to avoid
306
+ # catching a bare 413 that appears in dates or path segments.
307
+ _PAYLOAD_SIZE_RE = re.compile(
308
+ r"request\s+payload\s+size\s+exceeds\s+the\s+limit"
309
+ r"|payload\s+too\s+large"
310
+ r"|request\s+entity\s+too\s+large"
311
+ r"|content[-\s]?length\s+(?:exceeds|exceeded|is\s+too\s+large)",
312
+ re.IGNORECASE,
313
+ )
314
+ if _PAYLOAD_SIZE_RE.search(lower):
315
+ return "payload_too_large"
316
+ # HTTP 413 with surrounding context (status/code/http prefix or JSON \"code\":413)
317
+ if re.search(
318
+ r"(?:status|code|http)\s*[:=]?\s*413\b", lower
319
+ ) or re.search(r'\"code\"\s*:\s*413', lower):
320
+ return "payload_too_large"
321
+
322
+ # --- 2. Context / token limit -------------------------------------------------
323
+ _CONTEXT_TOKEN_RE = re.compile(
324
+ r"input\s+token\s+count\s+exceeds\s+the\s+maximum"
325
+ r"|context\s+length\s+exceeded"
326
+ r"|maximum\s+context\s+length"
327
+ r"|context\s+window\s+exceeded"
328
+ r"|too\s+many\s+tokens"
329
+ r"|your\s+input\s+context\s+is\s+too\s+long",
330
+ re.IGNORECASE,
331
+ )
332
+ if _CONTEXT_TOKEN_RE.search(lower):
333
+ return "context_too_large"
334
+ # INVALID_ARGUMENT or RESOURCE_EXHAUSTED combined with context/token max signals
335
+ _ISAE = re.compile(r"invalid_argument|resource_exhausted", re.IGNORECASE)
336
+ _CTX_KW = re.compile(r"context|token|maximum", re.IGNORECASE)
337
+ if _ISAE.search(lower) and _CTX_KW.search(lower):
338
+ return "context_too_large"
339
+
340
+ # --- 3. Generic INVALID_ARGUMENT (not payload/context) ------------------------
341
+ if "invalid_argument" in lower:
342
+ return "invalid_argument"
343
+
344
+ # --- 4. Auth / login ----------------------------------------------------------
305
345
  if backend == "codex":
306
346
  if (
307
347
  "codex login" in lower
@@ -326,12 +366,8 @@ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
326
366
  or "unauthorized" in lower
327
367
  or "401" in lower and ("auth" in lower or "token" in lower or "login" in lower)
328
368
  ):
329
- return format_error(
330
- "未登录",
331
- "助手尚未登录或凭证失效,请联系管理员处理。",
332
- )
369
+ return "auth"
333
370
 
334
- # Auth / login — ops details stay in logs; users contact admin
335
371
  if (
336
372
  "not signed in" in lower
337
373
  or "authenticate" in lower
@@ -345,28 +381,18 @@ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
345
381
  or ("xai_api_key" in lower and ("sign" in lower or "login" in lower or "auth" in lower))
346
382
  or "api_key" in lower and ("missing" in lower or "invalid" in lower or "required" in lower)
347
383
  ):
348
- return format_error(
349
- "未登录",
350
- "助手尚未登录或凭证失效,请联系管理员处理。",
351
- )
384
+ return "auth"
352
385
 
353
- # Upstream eligibility / control-plane RESOURCE_EXHAUSTED (short-window throttle).
354
- # Typical agy stderr: "Eligibility check failed: RESOURCE_EXHAUSTED (code 429):
355
- # Resource has been exhausted (e.g. check quota)." — not daily-quota zero,
356
- # not bridge concurrency, not user spam. More specific patterns first.
386
+ # --- 5. Rate limit / quota (RESOURCE_EXHAUSTED without context/token) ---------
387
+ # Must come AFTER context_too_large so "context/token + RESOURCE_EXHAUSTED"
388
+ # does not land here.
357
389
  if (
358
390
  "eligibility" in lower
359
391
  or "resource_exhausted" in lower
360
392
  or "resource exhausted" in lower
361
393
  ):
362
- return format_notice(
363
- "助手通道繁忙",
364
- "上游助手通道暂时限流或繁忙,请稍等片刻再试。",
365
- )
394
+ return "resource_exhausted"
366
395
 
367
- # Explicit account / daily quota (after resource-exhausted so "check quota"
368
- # in that message does not steal this branch).
369
- # Require exceed/limit/daily/… semantics — bare "quota usage report" must not match.
370
396
  if (
371
397
  "quota exceeded" in lower
372
398
  or "quota_exceeded" in lower
@@ -384,33 +410,22 @@ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
384
410
  )
385
411
  )
386
412
  ):
387
- return format_notice(
388
- "额度相关",
389
- "当前额度或配额可能受限,请稍后再试或联系管理员。",
390
- )
413
+ return "quota"
391
414
 
392
- # Explicit rate limit / too many requests (no resource-exhausted wording).
393
415
  if (
394
416
  "rate limit" in lower
395
417
  or "rate_limit" in lower
396
418
  or "too many requests" in lower
397
419
  ):
398
- return format_notice(
399
- "请求较多",
400
- "当前请求较多,请稍后再试。",
401
- )
420
+ return "rate_limit"
402
421
 
403
422
  # Bare 429 fallback — word-boundary so "1429" / "x4290" do not match.
404
- # Also accept explicit "status 429" / "code 429" forms.
405
423
  if re.search(r"\b429\b", lower) or re.search(
406
424
  r"(?:status|code|http)\s*[:=]?\s*429\b", lower
407
425
  ):
408
- return format_notice(
409
- "助手通道繁忙",
410
- "上游暂时限流或繁忙,请稍等片刻再试。",
411
- )
426
+ return "bare_429"
412
427
 
413
- # Network
428
+ # --- 6. Network ---------------------------------------------------------------
414
429
  if (
415
430
  "connection refused" in lower
416
431
  or "connection reset" in lower
@@ -423,26 +438,21 @@ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
423
438
  or "fetch failed" in lower
424
439
  or "socket hang up" in lower
425
440
  ):
426
- return format_error("网络错误", "连不上服务,请检查网络后重试。")
441
+ return "network"
427
442
 
428
- # Cascade / API hang (agy) — plain language for users
443
+ # --- 7. Timeout / cascade -----------------------------------------------------
429
444
  if "timeout waiting for cascade" in lower or "timeout waiting for response" in lower:
430
- return format_error(
431
- "模型响应超时",
432
- "模型响应超时,请稍后重试或简化指令。",
433
- )
434
-
435
- if "permission" in lower and ("denied" in lower or "refuse" in lower or "rejected" in lower):
436
- return format_error("权限不足", "没有执行该操作的权限。")
445
+ return "cascade_timeout"
437
446
 
438
447
  if "timeout" in lower or "timed out" in lower or "deadline exceeded" in lower:
439
- return format_error("超时", "等待响应超时,请稍后重试。")
448
+ return "timeout"
449
+
450
+ # --- 8. Permission / misc specific --------------------------------------------
451
+ if "permission" in lower and ("denied" in lower or "refuse" in lower or "rejected" in lower):
452
+ return "permission"
440
453
 
441
454
  if "no session found" in lower:
442
- return format_error(
443
- "会话不存在",
444
- "上一轮对话记录已过期,请重新发一次消息即可。",
445
- )
455
+ return "session_not_found"
446
456
 
447
457
  if "model" in lower and (
448
458
  "not found" in lower
@@ -453,15 +463,119 @@ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
453
463
  or "not supported" in lower
454
464
  or "no such" in lower
455
465
  ):
456
- return format_error("模型无效", "指定的模型不可用,请用 `/models` 查看后重选。")
466
+ return "model_invalid"
457
467
 
458
468
  if "command not found" in lower or "not a command" in lower:
469
+ return "command_not_found"
470
+
471
+ if "not found" in lower or "no such file" in lower or "enoent" in lower:
472
+ return "not_found"
473
+
474
+ return "unknown"
475
+
476
+
477
+ def format_cli_error(raw_message: str, *, backend: str = "") -> str:
478
+ """Map backend stderr/JSON error text into a short Chinese user reply.
479
+
480
+ Never put English raw blobs or internal path/env names into user text.
481
+ Details stay in the server log only.
482
+ """
483
+ raw = clean_output(raw_message or "") or "未知错误"
484
+ category = _classify_cli_error(raw, backend=backend)
485
+ logger.info(
486
+ "format_cli_error backend=%s category=%s",
487
+ backend or "?",
488
+ category,
489
+ )
490
+
491
+ # --- category → user-facing copy -----------------------------------------------
492
+ if category == "auth":
493
+ return format_error(
494
+ "未登录",
495
+ "助手尚未登录或凭证失效,请联系管理员处理。",
496
+ )
497
+
498
+ if category == "payload_too_large":
499
+ return format_error(
500
+ "请求内容过大",
501
+ (
502
+ "本次发送的内容超出服务端限制。\n"
503
+ "如果已续聊很久,请发 /new 开始新会话;\n"
504
+ "若新会话仍失败,请减少本次文字、图片或文件。"
505
+ ),
506
+ )
507
+
508
+ if category == "context_too_large":
509
+ return format_error(
510
+ "会话内容过长",
511
+ (
512
+ "会话累积内容超出模型上下文限制。\n"
513
+ "请发 /new 开始新会话后重试。"
514
+ ),
515
+ )
516
+
517
+ if category == "invalid_argument":
518
+ return format_error(
519
+ "请求参数无效",
520
+ "本次输入参数有误,请检查内容后重试。",
521
+ )
522
+
523
+ # --- throttle / quota (🔔 notice style — existing behaviour preserved) ---------
524
+ if category == "resource_exhausted":
525
+ return format_notice(
526
+ "助手通道繁忙",
527
+ "上游助手通道暂时限流或繁忙,请稍等片刻再试。",
528
+ )
529
+
530
+ if category == "rate_limit":
531
+ return format_notice(
532
+ "请求较多",
533
+ "当前请求较多,请稍后再试。",
534
+ )
535
+
536
+ if category == "bare_429":
537
+ return format_notice(
538
+ "助手通道繁忙",
539
+ "上游暂时限流或繁忙,请稍等片刻再试。",
540
+ )
541
+
542
+ if category == "quota":
543
+ return format_notice(
544
+ "额度相关",
545
+ "当前额度或配额可能受限,请稍后再试或联系管理员。",
546
+ )
547
+
548
+ if category == "network":
549
+ return format_error("网络错误", "连不上服务,请检查网络后重试。")
550
+
551
+ if category == "cascade_timeout":
552
+ return format_error(
553
+ "模型响应超时",
554
+ "模型响应超时,请稍后重试或简化指令。",
555
+ )
556
+
557
+ if category == "permission":
558
+ return format_error("权限不足", "没有执行该操作的权限。")
559
+
560
+ if category == "timeout":
561
+ return format_error("超时", "等待响应超时,请稍后重试。")
562
+
563
+ if category == "session_not_found":
564
+ return format_error(
565
+ "会话不存在",
566
+ "上一轮对话记录已过期,请重新发一次消息即可。",
567
+ )
568
+
569
+ if category == "model_invalid":
570
+ return format_error("模型无效", "指定的模型不可用,请用 `/models` 查看后重选。")
571
+
572
+ if category == "command_not_found":
459
573
  return format_error(
460
574
  "助手不可用",
461
575
  "助手程序未正确安装或配置,请联系管理员。",
462
576
  )
463
577
 
464
- if "not found" in lower or "no such file" in lower or "enoent" in lower:
578
+ if category == "not_found":
465
579
  return format_error("未找到", "请求的资源或文件不存在。")
466
580
 
467
581
  # Unknown: fixed Chinese only — never echo English raw to WeChat users
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wechatbridge-cli
3
- Version: 1.4.5
3
+ Version: 1.4.6
4
4
  Summary: Bridge WeChat messages to agy, Grok Build, or Codex CLIs — text/image/file/voice in, CLI replies and generated files back.
5
5
  Author: WeChatBridge contributors
6
6
  License: MIT