Skip to content

feat(approval): make Formal review context-aware - #337

Open
zhixiongzh wants to merge 5 commits into
openJiuwen-ai:mainfrom
jychen-waterloo:feat/formal-context-aware
Open

zhixiongzh wants to merge 5 commits into
openJiuwen-ai:mainfrom
jychen-waterloo:feat/formal-context-aware

Conversation

@zhixiongzh

@zhixiongzh zhixiongzh commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Paired: GitHub #337 ↔ GitCode !234

What type of PR is this?

/kind feature

What does this PR do / why do we need it:

Follow-up to #83. Earlier turns may contain a user restriction that still applies or explicit consent for the pending action. Reviewing only the current turn can miss either. Formal now uses the conversation context actually delivered to the model as evidence and evaluates the pending action separately.

Spec

  • Boolean results. All 14 predicates return true or false. Formal no longer falls back to Direct. Invalid responses are retried within the existing review budget; exhausted retries, failures, and timeouts require human review.
  • Explicit user boundaries. Replace scope_escalation with user_boundary. Formal checks specific restrictions or preconditions set by the user. Inferred task scope alone does not trigger this rule.
  • Three hard rules. Sensitive data exfiltration, file and folder deletion, and sensitive file access require human review whenever they match, regardless of conversational consent. data_exfiltration focuses on sensitive transfers. Deletion includes removing files or folders and emptying an existing file; ordinary writes and partial edits do not by themselves count as deletion.
  • Ten soft rules. A match can be cleared when the user explicitly authorizes the pending action and meets that rule’s consent requirements.

iCode applies the rules in order: hard rules → user boundary → uncleared soft rules → automatic approval. The approval dialog shows rules requiring human review, and the audit records predicate matches and applicable consent results.

  • Conversation context. Formal uses the history actually delivered to the model, including user messages, assistant text, prior tool calls, and available compaction summaries. Messages are kept in the order seen by the model; queued messages not yet delivered are not evidence. If a user message is delivered while an action is awaiting approval, the stale review is invalidated and the action is reviewed against the updated context. Parent-agent and workflow-generated inputs are distinguished from genuine user instructions. Summaries provide background and cannot establish consent or lift a boundary.
  • Verified ask_user evidence. Each verified answer is paired with its displayed question and options and scoped to the matching tool call. Hook changes are represented separately.
  • Pending action. The action is sent separately from context, with workspace roots and current local and UTC time.

Formal remains opt-in; Direct remains the default.

Scope of this PR

This PR leaves the tool allowlist and denylist logic unchanged. In a Git workspace, an existing allowlist can therefore auto-approve a write_file call that empties a file at a non-sensitive path before the action reaches Formal. A follow-up PR should address this alongside the syntax-tree parsing work in #225.

Which issue(s) this PR fixes:

No linked issue. Follow-up to #83.

What scenarios were tested, and what were the verification results:

After rebasing onto upstream/main at 3930a4f8, Smart Test passed: 19,647 tests passed, 69 skipped, and 3 warnings; 1,028 architecture tests passed. The Smart Test selected 20,744 of 42,700 collected non-integration tests.

Self-checklist:

  • Design — Maintainer review is pending.
  • Test — Smart Test and regression coverage passed locally.
  • Verification — See the Checks tab for CI on the updated revision.
  • Interface — The public ApprovalJudge.evaluate() interface and existing settings are unchanged.
  • Document — The English and Chinese approval guides and ACP interface reference describe the updated Formal behavior.

@openjiuwen-collaboration-bot

openjiuwen-collaboration-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

head_sha: a5d20d487a7d8c7c8fda0f3d7c566f6dd4afb736

变更摘要

本 PR 将 Formal 审批从「仅看当前轮次风险谓词、失败回落到 Direct」升级为「上下文感知 + 布尔判定」策略:所有谓词统一返回 true/false,并根据「硬规则 → 用户边界 → 未被豁免的软规则 → 自动批准」的顺序合成最终裁决,不再进入 Direct 复核路径;同时 Formal 会单独接收会话历史(用户消息、助手文本、历史工具调用、压缩摘要,以及经验证的 ask_user 回答)作为待审批动作的证据,只有显式配置为 formal 时生效,Direct 仍为默认。

主要改动

  • 策略资产与谓词模型重构:principles.json 的 14 条谓词改为 instructions + criterion.true/false + 可选 consent_bar 结构,移除 scope_escalation、external_action、destructive_action,新增 data_exfiltration、file_folder_deletion、remote_repoint、git_destructive、leak_path、user_boundary、exfil_scouting、agent_state_tamper、unverified_delete、credential_leakage;predicate.py 相应删除 TruthValue/PrincipleExample/PredicateDecision.REASONING,把 _DECISION_VERSION 升到 2,新增 evaluate_structured_response、structured_definitions、structured_schema,并在 PredicateEvaluation.blockers 中记录触发阶段。

  • Formal 提示词与判定流程改造:formal.py 新增 build_formal_user_prompt,把历史渲染为 <context>、把待审动作单独放入 <pending_action>,过滤系统提示、system-reminder、未验证工具结果,只保留摘要、用户/助手文本、历史工具调用与 digest 匹配的 ask_user 结果,并支持 <、> 转义;evaluate 移除 review_direct 回落,改由谓词结构化响应直接给出 ALLOW/NEEDS_REVIEW,FormalAuditState.verdict 只在未批准时以 blockers 作为 triggered。

  • 历史传递链路打通:新增 TOOL_HISTORY_PREFIX_KWARG 与 SessionContext.load_tool_history_prefix,history.py 的 CompressibleHistoryProvider.before_run 在服务端持有历史时提供该回调;middleware.py 增加 active_tool_batch_messages 上下文变量与 FunctionMiddleware.captures_tool_history 属性,loop.py 的 _LoopRun 改为 await self._start() 并按需捕获 _approval_history、_continued_history,在工具批次执行时绑定历史;approval.py 通过 captures_tool_history 与 pending_formal_call_id 把当前 provider call id 注入审批判定。

  • ask_user 回答可信度绑定:ask_user.py 模块新增 ASK_USER_RESPONSE_DIGEST_KEY 与 ask_user_response_digest(),AskUser 中间件在生成结果时把结果摘要写入工具结果元数据,供 Formal 校验历史中的 ask_user 回答确为用户真实答案。

  • 审计、展示与日志:review_reason.py 的 _RISKS 映射更新为新谓词 ID,并在存在未知 ID 时保留原判定理由;trajectory/approvals.py 的 _bounded_formal_audit 在 predicate_results 中补充布尔 consent_bar_met;judge.py 抽出 uses_formal_review 属性,并对 predicate 阶段日志改用 secure_open_owner_only_append 以仅属主可读方式追加;chrys.pot、zh-Hans 及 i18n 校验清单同步新增/废弃对应文案键。

@openjiuwen-collaboration-bot

openjiuwen-collaboration-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

head_sha: a5d20d487a7d8c7c8fda0f3d7c566f6dd4afb736

代码审查

审查结论

  • [建议] src/chrys/kernel/agent.py:567 fix: 接线 load_tool_history_prefix 或移除这段不可达的钩子
类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: a5d20d487a7d8c7c8fda0f3d7c566f6dd4afb736

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

Comment thread src/chrys/kernel/agent.py
if active_session is not None:
effective_client_kwargs["session"] = active_session
if session_context.load_tool_history_prefix is not None:
effective_client_kwargs[TOOL_HISTORY_PREFIX_KWARG] = session_context.load_tool_history_prefix

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head_sha: a5d20d487a7d8c7c8fda0f3d7c566f6dd4afb736

🟡 Medium Priority

新增链路里 SessionContext.load_tool_history_prefix(sessions.py:105)默认为 None,本 diff 中只有两处读取方:agent.py:567-568(非 None 时才塞进 _chrys_tool_history_prefix)与 loop.py:1731-1732(非 None 时才 await)。在本 PR 的 14 个改动文件中没有任何地方给它赋值,也没有任何外部调用点能在本次改动前就赋值一个此时才新增的属性,因此在 src/chrys/kernel 范围内该分支恒为假,_tool_history_prefix 永远为空元组。

后果(文档承诺与实现不一致):docs/en、docs/zh-Hans 的 approval.md 新增段落声称「当 Responses 会话通过服务端续接时,逐项风险检查也会使用 iCode 本地保存的可见历史」,PR 描述也声称「earlier turns may contain a user restriction that still applies」。由于 provider 续接时本轮的 self._messages 只包含本轮新增消息,跨轮的可见历史只能来自这个前缀钩子;钩子无人赋值 ⇒ 跨轮历史缺失 ⇒ user_boundary/软规则拿不到上一轮用户限制作为证据,Formal 可能直接返回 ALLOW 自动放行用户此前明确禁止的操作(该路径属于 fail-open 方向,与本次改动的安全目标相反)。同时这段新增 plumbing 无法被任何测试覆盖。

建议:为 load_tool_history_prefix 提供真正的赋值方(由持有本地历史的 history/context provider 写入 SessionContext),或删除 agent.py/loop.py 中的该 kwarg 传递;若暂不实现,应在文档中说明 provider 续接场景不包含跨轮历史。

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: a65c9f8a7a382d67d1349f9c88839318ec6667ee

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

@0x7c13

0x7c13 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

看一下这 5 项建议是否合理:

  1. [P1] 保留真实用户指令,区分子代理委派文本的来源。 内核子代理把父代理撰写的委派文本作为 user 消息,Formal 因此跳过另行传入的真实用户指令。主会话也存在遗漏:历史里已有用户消息时,刚提交、已进入审批上下文但尚未送达主模型的补充限制不会发给裁判。两条路径均用模拟测试复现。建议标明委派文本的来源,并把历史里缺失的真实用户指令补入裁判请求。formal.py

  2. [P2] 将并行 ask_user 的回答与实际展示的问题绑定。 两个并行单选问题只回答其中一个「是」、取消另一个时,交换被回答的问题会产生完全相同的裁判请求。裁判可能把授权归到另一项操作。建议把实际展示的问题和已验证的回答成对传递。formal.py

  3. [P2] 结果 hook 附加说明后,仍应保留真实 ask_user 回答。 当前摘要在 hook 执行前计算,Formal 用 hook 处理后的整段结果重新校验。我用模拟引擎确认,只追加一段说明,就会让真实回答「否」从裁判请求中消失。建议单独保存经验证的结构化问答,让 Formal 从该记录读取。ask_user.py · hook_dispatch.py

  4. [P2] 整文件清空需要进入人工确认。 新规则把清空已有文件列为硬规则,但 Git 工作区的写入预批准只判断目标路径和敏感性,在调用 Formal 前就返回。本地用替身确认,即使工具策略要求审批,整文件清空也会被标记为 auto_approved。建议在预批准分支识别整文件清空,并转交人工确认。principles.json · approval.py

  5. [P2] 恢复 Formal 裁判所需的运行时字段。 新请求省略了工作区、工具类型和当前时间。同一相对路径在不同工作区会生成相同的请求,裁判也缺少判断用户指定时间窗口所需的当前时间。建议把这些字段随待审动作一并发送。formal.py

@0x7c13

0x7c13 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@zhixiongzh

复核了 cc5f1d44,还有 4 项需要处理。

  1. [P1] 工作流生成的节点输入仍会被当作用户本人发言。 内核工作流把上游节点生成的 prompt 作为普通 user 消息送给 Formal。模拟运行确认,裁判会同时收到真实运行请求和标成 user 的上游输出,因此可能把生成文本视为用户授权。请给节点输入标明工作流来源,并以真实运行请求作为用户证据。agent_node.py

  2. [P2] 排队用户消息会被放到后来生成的助手文字之后。 我在模型返回前提交了一条简短肯定回复,随后才让模拟模型生成待审调用前的提议。裁判收到的顺序却是提议在先、回复在后,可能把该回复绑定到用户发送时尚未看到的提议。请保留消息顺序,或为排队输入标明来源和时序,让裁判能判断它回应的是哪段对话。formal.py

  3. [P2] 子代理系统提醒被误标为父代理委派文本。 委派消息先改成 parent_agent,而系统提醒过滤仍只匹配 user。模拟子代理运行中,运行环境和技能提醒因此进入了裁判请求。请在改写角色前按原始消息过滤系统提醒。formal.py

  4. [P2] 整文件清空仍走 Git 工作区写入预批准。 写入分支在调用 Formal 前直接返回,清空已有文件无法触发本 PR 新增的硬规则。请让整文件清空进入人工确认。approval.py

当前分支与 main 在 judge.py 有合并冲突。解决时请保留主分支的请求角色和作用域透传,以及本 PR 的审批日志保护。

另外main有些改动有conflicts,建议rebase一下

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: 5691ee2a56942efa21f6445f8e9b21fd17f225eb

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

@0x7c13

0x7c13 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

[P2,新发现] 审批进行中撤回的用户指令仍可能被用于自动批准。 撤回会从当前上下文移除排队消息,但已经发给裁判的请求保留了旧快照。我用模拟引擎确认,裁判在撤回后仍能基于该消息发布批准,仲裁路径也没有重新校验证据。建议在自动放行前核对本次审查使用的排队消息 ID,发现撤回时重新审查或转人工确认。arbitration.py
[P2,上一轮遗留] 整文件清空仍会走 Git 工作区写入预批准。 该分支在 Formal 审查前直接放行,因此清空已有文件不会触发本 PR 的硬规则。建议将整文件清空转交人工确认。approval.py

@zhixiongzh

zhixiongzh commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@0x7c13 感谢复核!第一点想先和你确认一下设计。用户在 Agent 运行时提交的补充消息,排队期间仍可取消,所以建议 Formal 只把已送达的用户消息作为审批依据。如果用户希望补充指令立即影响当前审批,可以点击「立即发送」。提交成功后,如果动作仍在等待审批,Formal 就用更新后的上下文重新审查。这样,裁判不会提前引用一条用户仍可取消的消息。你觉得按这个时点处理合适吗?

整文件清空按之前沟通的范围处理。本 PR 聚焦 Formal,沿用现有黑白名单逻辑。Git 工作区内非敏感路径的 write_file 仍可能在进入 Formal 前被白名单放行。这个场景会另提 PR,结合 #225 的语法树解析处理,范围说明已补到 PR 描述中。

@0x7c13

0x7c13 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

@0x7c13 感谢复核!第一点想先和你确认一下设计。用户在 Agent 运行时提交的补充消息,排队期间仍可取消,所以建议 Formal 只把已送达的用户消息作为审批依据。如果用户希望补充指令立即影响当前审批,可以点击「立即发送」。提交成功后,如果动作仍在等待审批,Formal 就用更新后的上下文重新审查。这样,裁判不会提前引用一条用户仍可取消的消息。你觉得按这个时点处理合适吗?

整文件清空按之前沟通的范围处理。本 PR 聚焦 Formal,沿用现有黑白名单逻辑。Git 工作区内非敏感路径的 write_file 仍可能在进入 Formal 前被白名单放行。这个场景会另提 PR,结合 #225 的语法树解析处理,范围说明已补到 PR 描述中。

同意,没问题,Formal 只使用已送达的用户消息。建议以实际进入模型请求的消息为准,普通排队和「立即发送」按钮被接受都早于这个时点。

刚核对过,PR head 仍是 5691ee2。,但当前代码仍会把可撤回的排队消息交给 Formal,旧裁决因此仍可能在消息撤回后自动放行。当前代码

建议等「只使用已送达消息」的改动落地,并验证「立即发送」后旧审批失效,再合入。整文件清空已明确留到后续 PR,可以从 #337 的合入阻塞项移除。

Signed-off-by: zhixiongzh <zhixiong.zhuang96@gmail.com>
Signed-off-by: zhixiongzh <zhixiong.zhuang96@gmail.com>
…n Formal

Include queued user instructions that are already available to approval
but have yet to reach the main agent. Preserve parent conversation
context for sub-agent reviews and identify delegation text as agent-authored.

Pair each ask_user answer with the question and options shown to the user.
Scope the pairing to its tool-call batch so reused call IDs cannot mix
answers across turns. Keep the original answer and label hook additions
or replacements separately in the review prompt.

Include the tool kind, workspace roots, and current time in the pending
action. Update the integration assertions and document how ACP hands
failed or timed-out Formal reviews to the user.
Distinguish workflow input and sub-agent delegation from real user instructions. Filter system reminders before rewriting roles.

Keep queued inputs in submission order across tool batches, child approvals, and retries. Deduplicate consumed messages by ID and drop withdrawn inputs from delegation snapshots.
Keep Formal evidence in the order the model actually received it and exclude queued input that has not been delivered. Invalidate an in-flight review when a user message steers the turn, and preserve the public judge interface.
@zhixiongzh

Copy link
Copy Markdown
Contributor Author

@0x7c13 关于你确认的消息时点,已按此处理:Formal 只使用实际进入本次模型请求的消息,并按模型收到的顺序构造上下文;仅排队、尚未送达的消息不会作为审批证据。用户补充消息送达后,如果动作仍在等待审批,旧审查会失效,并基于更新后的上下文重新审查。因此,排队消息的提交时间不用于重排裁判看到的历史。

整文件清空仍走现有 Git 工作区白名单的情况不在本 PR 的改动范围内;PR 描述继续注明后续结合 #225 另行处理。

背景评论:关于 Formal 只使用已送达消息的确认

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: 26f12a2a734911061dca6157db204c331cf27396

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

@0x7c13

0x7c13 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

复核了 a65c9f8a,有 5 项建议在合并前处理。

  1. [P1] 保留真实用户指令,区分子代理委派文本的来源。 内核子代理把父代理撰写的委派文本作为 user 消息,Formal 因此跳过另行传入的真实用户指令。主会话也存在遗漏:历史里已有用户消息时,刚提交、已进入审批上下文但尚未送达主模型的补充限制不会发给裁判。两条路径均用模拟测试复现。建议标明委派文本的来源,并把历史里缺失的真实用户指令补入裁判请求。formal.py

  2. [P2] 将并行 ask_user 的回答与实际展示的问题绑定。 两个并行单选问题只回答其中一个「是」、取消另一个时,交换被回答的问题会产生完全相同的裁判请求。裁判可能把授权归到另一项操作。建议把实际展示的问题和已验证的回答成对传递。formal.py

  3. [P2] 结果 hook 附加说明后,仍应保留真实 ask_user 回答。 当前摘要在 hook 执行前计算,Formal 用 hook 处理后的整段结果重新校验。我用模拟引擎确认,只追加一段说明,就会让真实回答「否」从裁判请求中消失。建议单独保存经验证的结构化问答,让 Formal 从该记录读取。ask_user.py · hook_dispatch.py

  4. [P2] 整文件清空需要进入人工确认。 新规则把清空已有文件列为硬规则,但 Git 工作区的写入预批准只判断目标路径和敏感性,在调用 Formal 前就返回。本地用替身确认,即使工具策略要求审批,整文件清空也会被标记为 auto_approved。建议在预批准分支识别整文件清空,并转交人工确认。principles.json · approval.py

  5. [P2] 恢复 Formal 裁判所需的运行时字段。 新请求省略了工作区、工具类型和当前时间。同一相对路径在不同工作区会生成相同的请求,裁判也缺少判断用户指定时间窗口所需的当前时间。建议把这些字段随待审动作一并发送。formal.py

看下这些问题是否合理

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants