Skip to content

[v2-P1] 表达式系统增强 — randint/clamp/upper/lower/contains #84

Description

@HeDaas-Code

Parent

ROADMAP:docs/ROADMAP.md §3.6(表达式系统增强 · P1)
关联偏差:D5(simpleeval 版本锁定,不在本 issue 范围——另一分支 feature/v2-p0-gui-first 已处理,避免冲突)

背景

当前 src/core/engine/expr/builtin_funcs.pyBUILTIN_FUNCS 仅含 8 个基础函数(int/str/float/bool/len/min/max/abs/round),缺少随机、范围裁剪、字符串变换、包含判断。DSL 创作者无法写:

node if randint(1, 6) == 6 [lucky, unlucky]

ROADMAP §3.6 已明确验收此场景。

What to build

BUILTIN_FUNCS 增加 5 个安全包装函数(沙箱内可调,不暴露 random 模块本体):

函数 签名 说明
randint (a: int, b: int) -> int 受控随机,闭区间 [a, b];参数校验 int
clamp (val, lo, hi) -> int/float 范围裁剪;val < lo → lo> hi → hi
upper (s: str) -> str 字符串大写
lower (s: str) -> str 字符串小写
contains (container, item) -> bool 包含判断(list/str/dict)

步骤

  1. src/core/engine/expr/builtin_funcs.py:实现 5 个包装函数 + 注册进 BUILTIN_FUNCS(替换现有注释占位)
  2. tests/core/test_expr_builtin_funcs.py:新建,覆盖每个函数的正/边界/错误路径
  3. 现有 tests/core/test_expr_dispatcher.py::TestBuiltinFuncs 不破(白名单核心函数断言仍通过)
  4. 全套测试保持绿(基线 211 → 预期 211+N)

不在本 issue 范围(避免与另一 agentic 冲突)

  • ❌ simpleeval 版本锁定(D5)——已在 feature/v2-p0-gui-first 处理
  • CustomExecutor.register_function 剧情自定义函数
  • @LLM-jud 装饰器(P2,ROADMAP §3.7)

协作声明

我是与 feature/v2-p0-gui-first 协作的第二个 agentic。本 issue 选择该任务正是因为:

  • 不在 v2-p0 的 9 个 open issue 中(另一 agentic 不会做)
  • 仅改 expr/builtin_funcs.py + 新增测试文件,与 executor/runtime/gui 无交集
  • ROADMAP 明确背书的 P1

Acceptance criteria

  • randint(1,6)[1,6] 范围内
  • clamp(15, 0, 10) == 10clamp(-3, 0, 10) == 0clamp(5, 0, 10) == 5
  • upper("abc") == "ABC"lower("ABC") == "abc"
  • contains([1,2,3], 2) is Truecontains("hello", "ell") is Truecontains("hello", "xyz") is False
  • node if randint(1, 6) == 6 [lucky, unlucky] 在章节 fixture 中可求值(集成测试可选)
  • 全套测试绿
  • 不破坏 §8 MVP 表 / §11 不变量守护

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions