Skip to content

Add rule-based extra CLI arguments for agent session resume and fork - #965

Open
Cec1c wants to merge 6 commits into
Kuddev:mainfrom
Cec1c:feat/agent-resume-fork-rules
Open

Cec1c wants to merge 6 commits into
Kuddev:mainfrom
Cec1c:feat/agent-resume-fork-rules

Conversation

@Cec1c

@Cec1c Cec1c commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Result / 用户结果

  • Add per-agent extra CLI argument rules (agent_resume_args_<slug> and agent_resume_enabled_<slug> in nebula_settings.txt), allowing users to configure and enable/disable custom CLI suffixes per coding agent (claude, codex, antigravity, gemini, opencode, cursor, kimi, pi, omp, copilot, grok, amp) — such as --yolo for Codex/OpenCode or --dangerously-skip-permissions for Claude Code/Antigravity. (The graphical editor pane under Settings → Agents is split out into an independent follow-up PR so each module remains within size/L.)

  • Automatically append active per-agent rules (quoted safely for the target pane's shell) when Pebrel resumes or forks an AI session across cold session recovery, pi session file resolution, codex resume fallback, tab context-menu Fork, and Command Palette Resume/Fork.

  • Surface desensitized shell-compatibility / syntax feedback and explicit one-shot bypass when a rule cannot be passed safely (for example, " / % / ! in cmd.exe or malformed syntax): failed session Resume/Fork names only the agent, target shell, and failure reason (never echoing the raw rule text) with an explicit Continue without rule / 本次不应用规则 action in the pane recovery banner and tab context menu (without altering saved settings), and automatically supersedes stale blocked recovery when an explicit command or manual session starts in the pane.

  • Ordinary terminal startup commands, disabled rules, and agents without configured rules remain untouched; invalid or unquoted shell-operator inputs fail closed during session recovery.

  • 在 nebula_settings.txt 中新增按智能体独立配置的规则式附加命令行参数合同(agent_resume_args_<slug> 与 agent_resume_enabled_<slug>),支持为各智能体(Claude Code、Codex、Antigravity、Gemini、OpenCode、Cursor、Kimi、Pi、Oh My Pi、Copilot、Grok、Amp)单独配置并启停恢复/分叉后缀规则(图形化设置编辑面板按职责边界拆分至独立后续 PR,使本 PR 保持在 size/L 范围内)。

  • 在冷启动会话恢复、pi 会话文件解析、codex resume 选择回退、标签页右键分叉(Fork)以及命令面板(Command Palette)恢复/分叉时自动按当前终端 Shell(POSIX、powershell.exe、pwsh.exe、cmd.exe)安全转义并附加已启用的规则参数。

  • 补齐规则与 cmd.exe 不相容或语法无效时的脱敏提示、显式单次重试与旧状态让位:在恢复或分叉被拦截时,终端底部恢复横幅与后台通知仅报告具体智能体名称、目标 Shell 与原因(不回显可能含凭据或私有路径的规则原文),并在底部横幅与标签页右键菜单提供显式的 「本次不应用规则」 按钮(仅本次跳过规则重试,不修改已保存配置);当窗格运行新命令或手动打开其他会话时自动取代旧阻断恢复状态。

  • 普通终端启动命令、已停用规则及未配置规则的智能体保持原有行为;包含未加引号 Shell 运算符或语法错误的输入按 fail-closed 拒绝执行。

Design / 设计边界

  • Responsibility and affected modules:

    • nebula_settings/src/agent_resume.rs and nebula_settings/src/reset.rs: zero-dependency persistence contract (agent_resume_args_<slug> + agent_resume_enabled_<slug>), canonical rule ordering, case-insensitive lookup, and full cleanup on settings reset.
    • nebula_app/src/agent_resume.rs and nebula_app/src/platform/agent_resume.rs: application-layer validation/tokenization (AppendError::{InvalidSyntax, IncompatibleShell}), and native per-shell argv quoting (Posix, powershell.exe legacy Windows quoting, pwsh.exe, and cmd.exe trailing-backslash escaping).
    • nebula_app/src/gpui_shell/terminal/view/startup_command.rs, session_failure.rs, agent_activity.rs, runtime.rs, view.rs, workspace/tab_menu.rs, workspace/session_recovery.rs, workspace/agents.rs, and workspace/palette.rs: apply submit_with_resume_rule / run_agent_session_command exclusively at confirmed AI session Resume/Fork entrypoints (after tab insertion so background notifications resolve pane tab ownership), surface desensitized BlockedResumeRule banner/toast diagnostics, implement one-shot retry_without_rule, and supersede inactive/blocked attempts when a new command or manual session starts.
  • Why this belongs here; interfaces that remain unchanged:

    • nebula_settings retains its zero-production-dependency contract by storing raw rule strings and booleans without shell or UI types; shell quoting stays in nebula_app/src/platform/. Existing session identity persistence (AgentSession) and agent hook protocols are unchanged.
  • Dependency, data-format, threading, or lifetime changes (ADR if applicable):

    • Zero new crate dependencies or background threads. Adds optional flat keys agent_resume_args_<slug> and agent_resume_enabled_<slug> (defaulting to enabled when args is present and enabled is omitted) in nebula_settings.txt, cleared on reset. Decision recorded in architecture/notes/nebula_settings/agent_resume/2026-11-10-agent-resume-fork-rules.md and linked from nebula_settings/AGENTS.md.
  • Compatibility and migration/fallback behavior:

    • Missing keys default to empty rules (identical to previous behavior). Active rules take effect immediately on the next Resume or Fork action without restarting Pebrel.
  • 职责划分与涉及模块:

    • nebula_settings/src/agent_resume.rs、reset.rs:维持零生产依赖合同,负责 agent_resume_args_<slug> 与 agent_resume_enabled_<slug> 的读写、稳定排序与重置清理。
    • nebula_app/src/agent_resume.rs、nebula_app/src/platform/agent_resume.rs:负责自然命令行后缀与 JSON 字符串数组双格式解析(区分 InvalidSyntax 与 IncompatibleShell),以及针对 POSIX、powershell.exe、pwsh.exe、cmd.exe 的原生字面量参数转义。
    • startup_command.rs、session_failure.rs、agent_activity.rs、runtime.rs、tab_menu.rs、session_recovery.rs、workspace/agents.rs、workspace/palette.rs:仅在确认的 AI 会话恢复与分叉入口应用规则(并在标签插入工作区后触发以保证通知路由可达),在规则被拦截时展示脱敏提示及显式「本次不应用规则」重试入口,并在新命令或手动新会话启动时自动作废旧恢复尝试。

Evidence / 验证依据

  • Commands and actual results (include unrun checks):
    • cargo test -p nebula-settings --locked -> passed (110 passed)
    • cargo test --manifest-path tools/i18n-contract/Cargo.toml --locked -> 24 passed, 0 failed, 1 ignored
    • cargo test -p nebula --bin pebrel --features gpui-shell,gpui-test-support --locked agent_resume -> passed (6 passed: unit + native powershell.exe/pwsh.exe/cmd.exe argv probe + terminal restore/fork/cmd.exe desensitized notice/banner + one-shot retry_without_rule + manual session / running command superseding)
    • cargo test -p nebula --test file_line_budget --locked -> passed
    • py -3.12 -m unittest scripts.tests.test_ci_native_tests scripts.tests.test_architecture_budgets scripts.tests.test_architecture_dependencies scripts.tests.test_architecture_governance -> passed
    • py -3.12 scripts/check_architecture.py --base 8f69d3327000976bcc56442ffaedfcd8a1e24159 -> passed
    • py -3.12 scripts/check_prohibited_names.py range --base 8f69d3327000976bcc56442ffaedfcd8a1e24159 --head HEAD -> passed
    • py -3.12 scripts/check_platform_cfg.py -> passed (459 / budget 459)
    • Unrun locally: macOS and Linux desktop targets (verified on Windows x64; cross-platform builds/tests run on GitHub Actions CI).
  • Regression tests: what fails before the fix?
    • agent_resume_rules_default_to_empty_and_round_trip, disabled_or_removed_rules_preserve_canonical_state, reset_clears_every_agent_resume_rule, natural_suffix_and_json_array_append_quoted_arguments_across_shells, shell_injection_and_malformed_inputs_are_rejected, windows_powershell_and_pwsh_deliver_the_exact_native_argument_array, agent_resume_and_fork_rules_append_quoted_arguments_and_respect_enabled_toggle, and agent_resume_blocked_recovery_yields_to_manual_sessions_and_running_commands.
  • UI changes: screenshots, long translations, keyboard access, DPI checks:
    • Typed i18n IDs added in en-US.json and zh-CN.json with English fallback for partial catalogs (i18n-contract verified). Blocked terminal panes render blocked-resume-rule with session-retry-without-rule and session-rule-dismiss controls plus tab context-menu SessionRetryWithoutRule.
  • Hot-path changes: allocation/work/load measurements, where applicable:
    • Cold-path only (invoked once when submitting a session Resume/Fork command; zero terminal render/input hot-path changes, and static translation lookup zero-allocation test passes).

Required Review / 必须确认

  • I followed CONTRIBUTING.md, docs/architecture.md, and docs/project-constraints.md.
  • I split responsibilities, not arbitrary line ranges; no duplicate behavior authority was added.
  • python3 scripts/check_architecture.py --base 8f69d3327000976bcc56442ffaedfcd8a1e24159 passes; budgets were not inflated to fit the change.
  • Tests cover success and failure; platform/feature coverage limitations are stated.
  • New messages use typed i18n IDs and matching placeholders; untranslated content has an explicit fallback.
  • Governance changes include a counterexample, corrected contract, tests, and a maintainer-reviewed decision. Not applicable: no governance changes.

@Cec1c
Cec1c requested a review from Kuddev as a code owner November 12, 2026 13:05

@Kuddev Kuddev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

我逐行检查了 9c3c0d0,在隔离的 Linux 工作树跑了相关测试:设置层 3 项、应用层 6 项全部通过。合入前请补齐规则与目标 Shell 不相容时的反馈。

规则编辑器只检查通用语法,符合语法就显示「已保存」。但 Cmd 路径会拒绝包含双引号、百分号或感叹号的参数。例如现有 PowerShell 回归中的 model_provider="custom" 可以保存,在 Cmd 会导致整条会话恢复失败。当前恢复只显示通用错误,用户不知道应停用哪个规则。

建议指出具体规则及目标 Shell,并允许用户显式选择「本次不应用规则」后重试。同时请补一个真正通过 cmd.exe 启动探针的 argv 回归,覆盖可传递的带空格参数及不可传递参数的反馈。现有 Windows 原生 argv 用例只运行两个 PowerShell。

@Cec1c

Cec1c commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

感谢逐行审阅!已按建议全部补齐并推送新提交:

  1. 区分语法错误与目标 Shell 不相容(AppendError)及双端明确提示:
    • nebula_app::agent_resume::append 现返回 Result<String, AppendError>,显式区分 InvalidSyntax 与 IncompatibleShell(PathQuote),并提供 incompatible_with_cmd 与 shell_label。
    • 设置页提前预警:当输入或保存语法合法但包含 "、% 或 !(例如 ["--config","model_provider=\"custom\""])的规则时,允许保存供 PowerShell / POSIX 终端使用,同时以警告色提示该规则包含 cmd.exe 不支持安全传递的字符(agent-resume-cmd-warning)。
    • 恢复 / 分叉拦截明确指出具体规则与目标 Shell:在 cmd.exe 中恢复或分叉命中不兼容规则时按 fail-closed 拦截,通知文案与当前终端底部横幅(blocked-resume-rule)明确列出智能体名称、具体规则内容({rule})与目标 Shell(cmd.exe);同时将 session_recovery.rs 与 workspace/agents.rs 的恢复/分叉调用移至 insert_tab_at 之后,确保后台标签页通知也能正确解析所属标签页。
  2. 显式「本次不应用规则」单次重试:
    • 在受阻终端底部横幅(session-retry-without-rule)以及标签页右键菜单中新增显式的 「本次不应用规则」(Continue without rule) 操作(TerminalView::retry_without_rule),点击后仅对本次恢复/分叉执行不附带规则的原始命令,保留已保存的规则配置不被改写。
  3. 补齐真实 cmd.exe 原生 argv 探针与 GPUI 回归:
    • platform::agent_resume::native_tests::windows_powershell_and_pwsh_deliver_the_exact_native_argument_array 新增通过真实 cmd.exe(/d /s /c)启动编译出的 argv probe.exe 的原生探针断言,验证空串、带空格参数("two words")、尾部反斜杠("a b\\")及字面量运算符("literal & echo nope | rem")精确送达 std::env::args(),并验证 model_provider="custom"、%COMSPEC%、!VAR! 稳定返回 Err(AppendError::IncompatibleShell(PathQuote::CommandPrompt))。
    • settings_pane::agent_resume_tests 与 terminal::view::startup_tests 已补充 cmd.exe 警告展示、恢复拦截提示、「本次不应用规则」点击重试且不改动持久化设置的回归测试。

(注:原提交 9c3c0d04 计数行数为 1,393 行,补齐上述 UI 提示、单次跳过重试入口及原生 cmd.exe / GPUI 回归后源码计数增至约 1,760 行,所有单文件均远低于 800 行软上限;若保持在同一 PR 合入,烦请视审阅情况协助添加 size-exempt 标签,或告知是否需要拆分。)

@Kuddev Kuddev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

复审了 b97f34ab。设置页预警和原生 cmd.exe 参数测试已补齐,五平台原生套件通过。我也在 Linux ARM64 编译运行了本提交的参数解析、恢复状态和通知格式化代码:16 项通过、1 项跳过,其中 3 项复现了下面的问题。请修正后再合并。

  1. 阻断通知会带出整条参数。 恢复失败分支 把规则原文放进通用通知。用带假标记的规则验证,后台窗格会把标记同时交给应用内横幅的日志正文和系统通知预览;关闭应用内 AI 通知也仍走系统通道。参数可能包含凭据或私有路径。请只在通知中保留智能体名、Shell 和失败原因,原文留在设置里编辑;补一个假标记不出现在通知正文的回归。
  2. 用户另开会话后,旧恢复仍占用当前窗格。 旧会话被规则阻断后,在同一窗格手动打开另一个会话,身份校验 仍拒绝新会话 ID;命令结束后,「本次不应用规则」依然会提交旧命令。重试条件 也允许在已标记有命令运行、尚未识别出程序名时排队。请让显式的新命令或会话替换旧恢复尝试,并确认重试只适用于仍属于当前窗格的那次阻断。建议覆盖「阻断旧会话 → 手动打开另一会话 → 新会话正常登记,旧重试消失」。

pr-size 统计了 1,759 行源码改动,超过 1,500 行默认门槛。这两项修正前先不加豁免标签;如果要拆,建议把规则设置与实际接入恢复流程按可独立验证的职责划分。

@Cec1c

Cec1c commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

已按审阅意见在 9758de9e 完成修复并补齐回归测试:

  1. 不在通知或横幅中回显原始恢复规则文本:BlockedResumeRule 不再保存 rule,session.restore_rule_incompatible 与 session.restore_rule_invalid 仅提示 Agent 名称及当前不兼容的 Shell 名称,引导到「设置 → AI 集成」修改规则。回归测试覆盖英/中双语提示与实际发出的 Notification::Text,断言伪造标记不会出现在任何可见提示中。
  2. 显式新命令与手动启动的新会话取代被拦截的旧恢复请求,且未识别前台命令期间禁止重试:
    • SessionRecovery 引入单调递增 attempt 及 supersede / supersede_if_inactive;显式命令执行(mark_command_running、run_command、run_agent_session_command)与手动会话在线事件(SessionRecovery::accepts 在 failed 态放行新会话、confirm 推进 attempt 并忽略旧会话 SessionEnd)都会清空旧阻塞恢复状态,后续快照改为保存新的前台会话身份。
    • can_retry_recovery 与 can_retry_without_rule 统一接入 shell_idle_for_recovery_retry()(覆盖 command_running && !command_running_disproved,即使尚未识别出 running_program 也不再允许重试)并校验 blocked.attempt == recovery.attempt。
    • startup_tests::open_at 初始化夹具时清空 agent_resume_args,避免本机真实设置影响默认冷恢复基线。

关于 pr-size:两项阻塞点均已修正且保留了完整的跨层回归覆盖;若审阅认可当前整体边界,烦请协助标记 size-exempt,若仍倾向按「规则持久化/设置 UI」与「终端恢复接入」拆分为两个独立 PR 也可随时告知。

…ession recovery

Split out the graphical Settings editor pane into an independent follow-up
module per review guidance so this PR stays within a single cohesive,
independently verifiable boundary (1,278 changed source lines, size/L):

- Keep [agent_resume_args] persistence and reset contracts in nebula_settings.
- Keep CLI suffix / JSON array validation, POSIX/PowerShell/cmd.exe literal
  argv quoting, and native Windows shell argv probes.
- Keep TerminalView session Resume/Fork rule application, desensitized
  blocked-rule diagnostics, one-shot "Continue without rule" bypass, and
  attempt superseding when a new command or manual session starts.
@Cec1c

Cec1c commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

补充更新(c23e372c):已按上次审阅关于职责划分的建议解决 pr-size 超限问题(当前净改动 1,278 行,size/L <= 1,500):

  • 本 PR 保持完整的核心与恢复闭环:保留 nebula_settings 持久化与重置合同、多 Shell 字面量参数解析/转义与 Windows 原生 powershell.exe / pwsh.exe / cmd.exe argv 探针,以及 TerminalView 会话恢复/分叉接入(含 9758de9e 的两项审阅修正:阻断提示脱敏不回显规则原文、新命令/手动新会话取代旧恢复尝试)及其全部回归测试。
  • 图形化设置编辑面板独立拆分:settings_pane/agent_resume.rs 及其交互测试拆出至后续独立 PR,待本 PR 合入后单独提交审阅;同时同步调整阻断提示文案为通用「设置」,避免在本 PR 阶段指向尚未合入的子面板。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants