Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Status

Proposed for the local session recovery fix in #1027.
Implemented for local session recovery (#1027) and hookless task titles (#1029).

## Context

Expand All @@ -12,9 +12,11 @@ OMP 18.22.2 marks each rendered user message as a terminal prompt zone. The end

On Windows x64 a new OMP conversation wrote its user message and assistant reply to its native transcript. Pebrel accepted the session-start hook, then cleared its foreground identity in the command-completion handler while PowerShell and OMP were both still running. Autosave lost the session. The same sequence occurred while displaying a restored message. [Windows trace](https://github.com/Kuddev/pebrel/actions/runs/44487591689)

InstaBox reproduced #1029 on 2.6.0 and 2.6.1 with hooks disabled. The native OMP title still contained the task after a reply, while the workspace tab showed only the directory. The OMP message renderer appends consecutive OSC 133 B, C and D;0 markers.

## Decision

While a local OMP process owns the pane through its hook, leave its session intact on the successful completion marker. The existing session-end hook or background process-tree reconciliation handles exit. A nonzero shell exit still records failure immediately.
The terminal records whether a successful completion immediately followed the empty B/C/D sequence used by OMP. In a local OMP pane, that sequence preserves the active command and its title with or without hooks. A separate shell completion retains its exit code.

## Rejected alternatives

Expand Down
2 changes: 1 addition & 1 deletion pebrel_app/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ impl input::Processor<EventProxy, ActionContext<'_, Notifier, EventProxy>> {
run.phase = crate::runtime_api::RuntimeRunPhase::Started;
}
},
TerminalEvent::CommandDone { exit_code } => {
TerminalEvent::CommandDone { exit_code, .. } => {
// 新 PTY 初始化提示符也可能先发一个 CommandDone。Runtime
// 提交 barrier 尚未冲刷时,它不能结束当前请求。
if self.ctx.nebula_state.runtime_submit_barrier.is_some() {
Expand Down
4 changes: 2 additions & 2 deletions pebrel_app/src/gpui_shell/terminal/event_mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ mod tests {
sender.send(Event::AiHookEnvelope(format!("working:{id}").into_bytes()));
sender.send(Event::AiHookEnvelope(format!("attention:{id}").into_bytes()));
sender.send(Event::AiHookEnvelope(format!("done:{id}").into_bytes()));
sender.send(Event::CommandDone { exit_code: Some(0) });
sender.send(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false });
}
for (id, (sender, receiver)) in sessions.iter_mut().enumerate() {
assert!(matches!(receiver.try_recv().unwrap(), Event::CommandStart));
Expand All @@ -271,7 +271,7 @@ mod tests {
}
assert!(matches!(
receiver.try_recv().unwrap(),
Event::CommandDone { exit_code: Some(0) }
Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }
));
assert!(receiver.try_recv().is_err());
sender.send(Event::Wakeup);
Expand Down
12 changes: 3 additions & 9 deletions pebrel_app/src/gpui_shell/terminal/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ impl TerminalView {
// 退出码接到「上一条命令失败」的未读徽章上(⚠ 三角);`Some(0)` 与
// `None`(没带退出码的 133;D)都算过关。记在下面那道 barrier 之后:
// 属于上一轮/初始化的那个边沿不能改写徽章。
TermEvent::CommandDone { exit_code } => {
TermEvent::CommandDone { exit_code, empty_prompt_zone } => {
// 新 PTY 初始化提示符也可能先发一个 CommandDone。Runtime
// 文本还在等待回显 barrier 时,这个边沿属于上一轮/初始化,
// 不能清掉尚未发送的 Enter 或把新请求提前投影成 idle。
Expand All @@ -829,14 +829,8 @@ impl TerminalView {
{
return;
}
// OMP redraws each user message with OSC 133;B, C, D;0. Its
// native session-end hook and local process probe own CLI exit.
if exit_code == Some(0)
&& self.running_program.as_deref() == Some("omp")
&& self.agent_activity.hook_seen()
&& self.suggest.suggest_env.is_this_machine()
&& self.session.as_ref().is_some_and(|session| session.shell_pid != 0)
{
// OMP emits a successful command boundary when it paints a user message.
if empty_prompt_zone && self.local_omp_foreground() {
return;
}
self.finish_foreground_command(exit_code, cx);
Expand Down
49 changes: 45 additions & 4 deletions pebrel_app/src/gpui_shell/terminal/view/activity_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ fn omp_message_redraw_keeps_its_session_until_the_process_exits(cx: &mut TestApp
event.agent_pid = Some(100);
assert!(view.handle_ai_hook(&event, cx));
view.on_command_start(cx);
view.process_event(TermEvent::CommandDone { exit_code: Some(0) }, cx);
view.process_event(
TermEvent::CommandDone { exit_code: Some(0), empty_prompt_zone: true },
cx,
);
assert_eq!(view.ai_session.as_ref().unwrap().session_id, "omp-message-redraw");
assert!(view.agent_activity.hook_seen());

Expand All @@ -272,12 +275,44 @@ fn omp_message_redraw_keeps_its_session_until_the_process_exits(cx: &mut TestApp
{"kind":"session-start","session_id":"omp-real-error"}"#;
let next = crate::ai_hook::parse_remote_envelope(wire, Some(42)).unwrap();
assert!(view.handle_ai_hook(&next, cx));
view.process_event(TermEvent::CommandDone { exit_code: Some(1) }, cx);
view.process_event(
TermEvent::CommandDone { exit_code: Some(1), empty_prompt_zone: false },
cx,
);
assert!(view.ai_session.is_none());
assert!(view.last_command_failed);
});
}

#[gpui::test]
fn hookless_omp_keeps_its_task_title_until_shell_completion(cx: &mut TestAppContext) {
let (view, window, _) = open(cx);
view.update(window, |view, cx| {
view.session.as_mut().unwrap().shell_pid = 4242;
view.suggest.suggest_env = crate::display::SuggestEnv::Local;
view.process_event(TermEvent::CwdReport("/tmp/ddlclock".into()), cx);
view.suggest.last_committed = "omp".into();
view.on_command_start(cx);
view.process_event(TermEvent::Title("π > title fixture".into()), cx);
let title = view.tab_label(cx);
assert!(title.contains("title fixture"));
view.suggest.last_committed = "a message typed into OMP".into();
view.process_event(TermEvent::CommandStart, cx);
view.process_event(
TermEvent::CommandDone { exit_code: Some(0), empty_prompt_zone: true },
cx,
);
assert_eq!(view.tab_label(cx), title);
assert_eq!(view.running_program.as_deref(), Some("omp"));
view.process_event(
TermEvent::CommandDone { exit_code: Some(0), empty_prompt_zone: false },
cx,
);
assert!(view.running_program.is_none());
assert_eq!(view.tab_label(cx), "ddlclock");
});
}

#[gpui::test]
fn command_end_clears_progress_and_stale_agent_state(cx: &mut TestAppContext) {
let (view, window, _) = open(cx);
Expand All @@ -287,7 +322,10 @@ fn command_end_clears_progress_and_stale_agent_state(cx: &mut TestAppContext) {
view.on_command_start(cx);
view.process_event(TermEvent::Progress { state: 3, value: None }, cx);
assert_eq!(view.sidebar_activity(), SidebarActivity::Running);
view.process_event(TermEvent::CommandDone { exit_code: Some(code) }, cx);
view.process_event(
TermEvent::CommandDone { exit_code: Some(code), empty_prompt_zone: false },
cx,
);
screen(view, "◦ Working (1s • esc to interrupt)\n› Ask Codex to do anything");
view.refresh_agent_screen_state(cx);
assert_eq!(view.progress, crate::taskbar::TaskProgress::None);
Expand Down Expand Up @@ -507,7 +545,10 @@ fn ssh_agent_second_launch_reestablishes_identity(cx: &mut TestAppContext) {
assert_eq!(view.sidebar_activity(), SidebarActivity::Running);

// ---- 退出:SessionEnd 丢失(bridge 忙/超时),只剩 133;D 收尾 ----
view.process_event(TermEvent::CommandDone { exit_code: Some(0) }, cx);
view.process_event(
TermEvent::CommandDone { exit_code: Some(0), empty_prompt_zone: false },
cx,
);
assert_eq!(view.running_program, None, "command boundary clears identity");

// ---- 会话 2:新 CLI 进程(process 换新、序号从 1 重来)----
Expand Down
10 changes: 9 additions & 1 deletion pebrel_app/src/gpui_shell/terminal/view/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ impl TerminalView {
)
}

pub(super) fn local_omp_foreground(&self) -> bool {
self.running_program.as_deref() == Some("omp")
&& self.suggest.suggest_env.is_this_machine()
&& self.session.as_ref().is_some_and(|session| session.shell_pid != 0)
}

pub(super) fn on_command_start(&mut self, cx: &mut Context<Self>) {
if !self.agent_activity.hook_seen() {
// A C marker in OMP's message redraw belongs to the existing foreground command.
if !(self.agent_activity.hook_seen() || self.command_running && self.local_omp_foreground())
{
self.answers.begin_command();
// Shell capture covers wrappers/WSL until a hook owns the session.
// A late OSC 133;C must not replace an already identified Agent.
Expand Down
40 changes: 26 additions & 14 deletions pebrel_app/src/gpui_shell/terminal/view/startup_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,10 @@ fn issue_358_history_popup_matches_a_command_prefix_without_a_trailing_space(
feed(view, format!("\x1b[2J\x1b[H❯ {command}").as_bytes());
view.commit_line(cx);
view.process_event(Event::CommandStart, cx);
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(
Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false },
cx,
);
}
feed(view, "\x1b[2J\x1b[H❯ cls".as_bytes());
view.ghost_enabled = true;
Expand Down Expand Up @@ -661,7 +664,10 @@ fn hybrid_tab_opens_history_behind_native_prediction_without_writing(cx: &mut Te
feed(view, format!("\x1b[2J\x1b[H❯ {command}").as_bytes());
view.commit_line(cx);
view.process_event(Event::CommandStart, cx);
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(
Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false },
cx,
);
}
view.ghost_enabled = true;
view.completion_style = crate::display::CompletionStyle::Hybrid;
Expand Down Expand Up @@ -855,7 +861,7 @@ fn ended_command_does_not_leave_osc_progress_running(cx: &mut TestAppContext) {
view.process_event(Event::CommandStart, cx);
view.process_event(Event::Progress { state: 3, value: None }, cx);
assert_eq!(view.sidebar_activity(), SidebarActivity::Running);
view.process_event(Event::CommandDone { exit_code }, cx);
view.process_event(Event::CommandDone { exit_code, empty_prompt_zone: false }, cx);
let expected = if exit_code.is_some_and(|code| code != 0) {
SidebarActivity::CommandFailed
} else {
Expand Down Expand Up @@ -933,7 +939,10 @@ fn ai_tab_name_shows_reported_task_and_restores_directory_on_exit(cx: &mut TestA
let hover = view.tab_tooltip(&view.tab_label(cx));
assert!(hover.contains("/home/test/很长的项目目录"));
assert!(hover.contains("修复 SSH 标签名称"));
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(
Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false },
cx,
);
assert_eq!(view.tab_label(cx), "很长的项目目录");
assert!(!view.tab_tooltip(&view.tab_label(cx)).contains("修复 SSH 标签名称"));
}
Expand All @@ -960,7 +969,7 @@ fn static_tab_titles_keep_the_directory_while_hover_names_the_ai_task(cx: &mut T
assert_eq!(view.tab_label(cx), "VALE");

cx.global_mut::<Settings>().static_tab_titles = true;
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }, cx);
assert_eq!(view.tab_label(cx), "很长的项目目录");
});
}
Expand All @@ -984,7 +993,7 @@ fn ai_task_name_preserves_remote_identity_in_hover_and_shell_fallback(cx: &mut T
view.tab_tooltip(&view.tab_label(cx)),
"修复登录失败\nSG-1 新加坡\nroot@192.0.2.10:2222\n/srv/project\nclaude · 修复登录失败"
);
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }, cx);
assert_eq!(view.tab_label(cx), "SG-1 新加坡");
view.running_program = Some("htop".into());
view.process_event(Event::Title("ordinary application title".into()), cx);
Expand All @@ -1002,7 +1011,7 @@ fn review_regression_cold_resume_survives_initial_prompt_and_clears_on_exit(
view.seed_ai_session("codex".into(), "saved-42".into(), cx);
assert!(receiver.try_recv().is_err(), "do not submit into shell initialization");
assert_eq!(view.session_agent().unwrap().session_id.as_deref(), Some("saved-42"));
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }, cx);
assert!(view.pending_shell_command.is_some());
feed(view, b"\x1b]133;A\x07hello@host:/home/hello$ ");
view.process_event(Event::Wakeup, cx);
Expand All @@ -1011,7 +1020,7 @@ fn review_regression_cold_resume_survives_initial_prompt_and_clears_on_exit(
assert!(view.ai_session.is_none(), "submission is not confirmation");
assert!(matches!(receiver.try_recv().unwrap(), Msg::Input(bytes) if bytes.as_ref() == b"codex resume saved-42"));
// The initial shell edge cannot consume the pending Enter or identity.
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }, cx);
assert!(view.recovery.awaiting_confirmation);
feed(view, b"codex resume saved-42");
view.flush_pending_runtime_submit(cx);
Expand All @@ -1024,7 +1033,7 @@ fn review_regression_cold_resume_survives_initial_prompt_and_clears_on_exit(
event.pane = Some(view.pane_id);
assert!(view.handle_ai_hook(&event, cx));
assert_eq!(view.ai_session.as_ref().unwrap().session_id, "saved-42");
view.process_event(Event::CommandDone { exit_code: Some(0) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(0), empty_prompt_zone: false }, cx);
assert!(view.running_program.is_none());
assert!(view.ai_session.is_none(), "both foreground fields must clear together");
assert!(view.runtime_agent().is_none());
Expand Down Expand Up @@ -1113,7 +1122,10 @@ fn failed_cold_resume_keeps_target_for_retry(cx: &mut TestAppContext) {
view.flush_pending_runtime_submit(cx);
view.process_event(Event::CommandStart, cx);
feed(view, b"\r\nNo session found matching 'saved-42'\r\n");
view.process_event(Event::CommandDone { exit_code: Some(1) }, cx);
view.process_event(
Event::CommandDone { exit_code: Some(1), empty_prompt_zone: false },
cx,
);
assert!(view.ai_session.is_none());
assert_eq!(view.session_agent(), Some(saved));
assert!(view.recovery.awaiting_confirmation);
Expand Down Expand Up @@ -1473,7 +1485,7 @@ fn missing_codex_target_opens_the_native_chooser_and_confirms_the_users_choice(
view.update(window, |view, cx| {
let saved = submit_codex_restore(view, &receiver, cx);
feed(view, b"\r\nERROR: No saved session found with ID saved-missing. Run codex resume without an ID.\r\n");
view.process_event(Event::CommandDone { exit_code: Some(1) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(1), empty_prompt_zone: false }, cx);
assert_eq!(view.session_agent(), Some(saved));
assert!(view.pending_shell_command.is_some());
assert!(receiver.try_iter().all(|message| !matches!(message, Msg::Input(_))));
Expand Down Expand Up @@ -1523,7 +1535,7 @@ fn unrelated_codex_failures_do_not_replace_the_saved_target(cx: &mut TestAppCont
view.update(window, |view, cx| {
let saved = submit_codex_restore(view, &receiver, cx);
feed(view, format!("\r\n{message}\r\n").as_bytes());
view.process_event(Event::CommandDone { exit_code }, cx);
view.process_event(Event::CommandDone { exit_code, empty_prompt_zone: false }, cx);
assert!(view.pending_shell_command.is_none(), "{message}: {exit_code:?}");
assert_eq!(view.session_agent(), Some(saved));
});
Expand Down Expand Up @@ -1606,15 +1618,15 @@ fn a_failed_codex_chooser_does_not_start_an_automatic_retry_loop(cx: &mut TestAp
let saved = submit_codex_restore(view, &receiver, cx);
let error = b"\r\nERROR: No saved session found with ID saved-missing.\r\n";
feed(view, error);
view.process_event(Event::CommandDone { exit_code: Some(1) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(1), empty_prompt_zone: false }, cx);
feed(view, b"\x1b]133;A\x07user@host:~$ ");
view.flush_pending_shell_command(cx);
feed(view, b"codex resume");
view.flush_pending_runtime_submit(cx);
view.process_event(Event::CommandStart, cx);
receiver.try_iter().for_each(drop);
feed(view, error);
view.process_event(Event::CommandDone { exit_code: Some(1) }, cx);
view.process_event(Event::CommandDone { exit_code: Some(1), empty_prompt_zone: false }, cx);
assert!(view.pending_shell_command.is_none());
assert_eq!(view.session_agent(), Some(saved));
assert!(view.can_retry_recovery());
Expand Down
8 changes: 6 additions & 2 deletions pebrel_terminal/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ pub enum Event {

/// OSC 133;D — the command finished. `exit_code` comes from Nebula's own
/// shell integration (`133;D;<code>`); bare third-party `133;D` is `None`.
CommandDone { exit_code: Option<i32> },
CommandDone {
exit_code: Option<i32>,
/// OSC 133 B, C and D;0 arrived consecutively with no command text or output.
empty_prompt_zone: bool,
},

/// OSC 1337 `SetUserVar` — a shell-integration variable (assistant
/// queries and future channels).
Expand Down Expand Up @@ -144,7 +148,7 @@ impl Debug for Event {
placement.id
),
Event::CommandStart => write!(f, "CommandStart"),
Event::CommandDone { exit_code } => write!(f, "CommandDone({exit_code:?})"),
Event::CommandDone { exit_code, .. } => write!(f, "CommandDone({exit_code:?})"),
Event::UserVar { name, value } => {
write!(f, "UserVar({name}, {} chars)", value.chars().count())
},
Expand Down
Loading
Loading