feat: evolve textfx into a mini shell #62
Reference in New Issue
Block a user
No description provided.
Delete Branch "pi-agent/konabot:feat/textfx-minishell"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
User description
概述
将 textfx 从简单的文本处理器链重构为最小 shell 风格 DSL,支持管道、条件、循环等控制流。
主要变更
架构重构
新增 shell-like 语法
|;&&/||>/>>if ... then ... else ... fiwhile ... do ... done新增内建命令
test/[:条件测试!:逻辑取反true/false:真假命令安全限制
测试
tests/test_textfx_shell.py:19 个 shell 语法测试tests/test_textfx_runtime_limits.py:3 个运行限制测试文档
konabot/docs/user/textfx.txt,补充所有新语法说明和示例PR Type
Enhancement
Description
将 textfx 重构为迷你 shell DSL
新增管道、条件、循环等控制流语法
新增
true/false/test内建命令添加超时与并发限制及相关测试
Diagram Walkthrough
File Walkthrough
__init__.py
注册新增内建命令处理器konabot/plugins/handle_text/init.py
THTrue、THFalse、THTest处理器base.py
重构执行引擎支持 AST 语句分发konabot/plugins/handle_text/base.py
run_pipeline从遍历command_groups重构为遍历statementsIfNode和WhileNodeAST 节点的分发执行unix_handlers.py
新增 true/false/test 内建命令konabot/plugins/handle_text/handlers/unix_handlers.py
THTrue命令,始终返回成功THFalse命令,始终返回失败THTest命令,支持字符串非空、-n/-z、字符串比较、整数比较及方括号别名test_textfx_runtime_limits.py
新增运行时限制相关测试tests/test_textfx_runtime_limits.py
_get_textfx_user_key的三种场景单元测试(群聊、私聊、session 回退)TEXTFX_MAX_RUNTIME_SECONDS验证测试_textfx_running_users集合行为测试test_textfx_shell.py
新增 shell 语法全面测试用例tests/test_textfx_shell.py
&&/||短路、!取反、引号与 trimif/then/else/fi条件语句及嵌套while/do/done循环及迭代次数限制textfx.txt
补充 shell 语法用户文档konabot/docs/user/textfx.txt
true/false/test/[命令文档及示例if/then/else/fi条件语句文档及示例while/do/done循环语句文档及示例PR Reviewer Guide 🔍
(Review updated until commit
8f40572a38)Here are some key observations to aid the review process:
测试不充分
test_textfx_timeout_limit实际上并没有测试超时机制是否生效,只是验证了常量值和脚本能解析。test_textfx_concurrent_limit也只是手动操作 set 来验证 add/discard,没有真正测试并发场景下第二个请求是否会被拒绝。这些测试给人一种有覆盖的错觉,但实际上没有验证核心的运行时限制逻辑。异常吞没
捕获了所有
Exception并返回通用错误消息,这会掩盖具体的错误类型(如asyncio.TimeoutError、RecursionError等)。建议至少在错误消息中区分超时和其他异常,或者让特定异常(如KeyboardInterrupt、SystemExit)向上传播,避免调试困难。输入注入风险
THTest的handle方法直接使用用户提供的 args 进行int()转换和字符串比较,虽然当前实现看起来安全,但如果未来扩展支持更多操作符(如文件测试-f、-d),需要注意不要暴露文件系统访问。当前的错误消息会将用户输入原样返回(f"test 不支持的表达式: {' '.join(args)}"),在某些上下文中可能被利用做消息注入。PR Code Suggestions ✨
Latest suggestions up to
8f40572Explore these optional code suggestions:
内部错误时缺少提前返回逻辑
旧代码中,管道组内某条命令失败(
code != 0)时会立即中止整条流水线并返回。新代码的for statement循环里捕获了异常,但对于_execute_group/_execute_if/_execute_while返回非零code的情况没有做任何中止处理,所有 statement都会无条件继续执行。这可能导致本应中止的脚本继续运行后续语句,改变了原有的错误传播语义。建议在
code < 0(内部错误)时提前返回,或根据设计意图保留短路逻辑。konabot/plugins/handle_text/base.py [565-573]
Suggestion importance[1-10]: 4
__
Why: The suggestion raises a valid point about error propagation semantics changing from the old code. However, the old code's early-return behavior was specifically for pipeline within a command group (pipe-separated commands), not across semicolon-separated statements. In shell semantics, semicolon-separated statements are independent and should all execute regardless of prior failures. The new code delegates to
_execute_group,_execute_if, and_execute_whilewhich likely handle internal pipe-level error propagation themselves. The suggestion to short-circuit oncode < 0is a reasonable defensive measure but may not align with the intended shell-like semantics of the refactor. The improvement is minor and speculative.Previous suggestions
Suggestions up to commit
8f40572缺少异常捕获导致错误未处理
旧代码在执行管道命令时有
try/except包裹,能捕获意外的 Python异常并返回友好错误信息。新代码移除了这层保护,
_execute_if、_execute_while、_execute_group中任何未预期的异常都会直接向上抛出,可能导致用户看到原始堆栈或 bot 崩溃。建议在循环体外加上异常捕获。
konabot/plugins/handle_text/base.py [565-571]
Suggestion importance[1-10]: 6
__
Why: The suggestion correctly observes that the old code had
try/exceptwrapping pipeline execution, and the new code removes it. Unhandled exceptions in_execute_if,_execute_while, or_execute_groupcould propagate and crash the bot or expose raw tracebacks. However, it's possible the exception handling was moved into those helper methods (which aren't shown in the diff), so this is a reasonable but not certain concern. Theimproved_codeaccurately reflects the suggested change.方括号别名检测逻辑永远不生效
当
THTest通过keywords = ["["]以[别名被调用时,这里的self.name始终是"test",不会等于"[",因此永远不会进入该分支。应该通过检查参数(例如args的第一个元素或调用名)来判断是否以[形式调用,并在此时验证末尾是否有匹配的],否则报错提示语法不正确。konabot/plugins/handle_text/handlers/unix_handlers.py [121-122]
Suggestion importance[1-10]: 5
__
Why: The suggestion correctly identifies that
self.nameis always"test"sincekeywordsonly adds aliases for lookup, not for changingself.name. However, theimproved_codeis just comments rather than actual working code, and the current code still handles]removal on line 124-125 regardless of invocation method, so the practical impact is limited — the bracket syntax still works, just without strict validation of the closing].0cb236b561to805e60a9ffPersistent review updated to latest commit
8f40572a38