Files
Kigi-CLI/crates/codegen/kigi-pager-pty-harness/src/scenarios/plan_approval_resume.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

186 lines
6.9 KiB
Rust

//! Plan-approval chrome restored by the shell after quit + resume.
//!
//! When `exit_plan_mode` is parked and the user quits, the shell persists
//! `awaiting_plan_approval = true` in `plan_mode.json`. On `--continue` the
//! shell re-issues the `kigi/exit_plan_mode` reverse-request — a real live ACP
//! waiter — so the pager re-shows approval chrome through its normal path with
//! no pager-side disk logic. Approving then leaves plan mode and starts the
//! implement turn.
//!
//! Without the shell re-park no reverse-request reaches the resumed pager, so no
//! approval chrome appears and this scenario fails.
use std::path::Path;
use std::time::Duration;
use anyhow::{Context, Result, bail};
use super::wait_for_welcome;
use crate::{ContentController, PtyHarness, pager_binary};
const DEFAULT_ROWS: u16 = 50;
const DEFAULT_COLS: u16 = 120;
const WELCOME_TIMEOUT: Duration = Duration::from_secs(20);
/// Turn 1 seeds the session before quit; turn 2 is the implement turn the shell
/// injects once the resumed approval is approved.
const SETUP_SENTINEL: &str = "GBT3703SETUP";
const IMPLEMENT_SENTINEL: &str = "GBT3703IMPLEMENTED";
const PLAN_BODY: &str = "\
# Plan GBT3703Repro
## Steps
1. Seed plan file on disk
2. Quit pager with the approval parked
3. Resume and expect restored approval chrome
";
pub async fn assert_plan_approval_restored_after_resume() -> Result<()> {
let content = ContentController::start()
.await
.context("start ContentController")?;
// One response per agent turn, FIFO; only 2+-tool requests draw from the
// queue, so aux requests never steal a turn.
content.set_turns([
format!("{SETUP_SENTINEL}: drafted a plan for the user to review."),
format!("{IMPLEMENT_SENTINEL}: implementing the approved plan."),
]);
let project = tempfile::tempdir().context("project dir")?;
std::fs::create_dir_all(project.path().join(".git")).context("create .git")?;
let binary = pager_binary().context("resolve pager binary")?;
let mut first = PtyHarness::spawn_with_content_in_dir(
&binary,
DEFAULT_ROWS,
DEFAULT_COLS,
&content,
&[],
Some(project.path()),
)
.context("spawn first pager")?;
wait_for_welcome(&mut first).await?;
first.inject_keys(b"go\r").context("submit setup turn")?;
first
.wait_for_text(SETUP_SENTINEL, Duration::from_secs(30))
.context("setup turn rendered")?;
// Quit and reap BEFORE seeding so the still-live shell cannot re-persist
// and clobber the seeded state.
first.inject_keys(b"\x11").context("ctrl-q once")?;
first.update(Duration::from_millis(200));
first.inject_keys(b"\x11").context("ctrl-q confirm")?;
first.quit().context("reap first pager")?;
let seeded = seed_parked_approval(content.home()).context("seed parked approval")?;
assert!(seeded > 0, "no session dir seeded");
let mut resumed = PtyHarness::spawn_with_content_in_dir(
&binary,
DEFAULT_ROWS,
DEFAULT_COLS,
&content,
&["--continue"],
Some(project.path()),
)
.context("spawn resumed pager")?;
// Approval chrome can open immediately and cover chat history, so key on the
// chrome markers rather than SETUP_SENTINEL, which the plan viewer may hide.
resumed
.wait_for_text("request changes", WELCOME_TIMEOUT)
.context("restored approval 'request changes' after --continue")?;
resumed
.wait_for_text("quit plan", Duration::from_secs(5))
.context("restored approval 'quit plan' after resume")?;
let screen = resumed.screen_contents();
if !screen.contains("approve") {
bail!("expected approval primary action after resume\n{screen}");
}
// Any one of these proves the session was restored: with chrome covering the
// transcript the plan body from disk is often the only visible evidence.
if !screen.contains("GBT3703Repro")
&& !screen.contains(SETUP_SENTINEL)
&& !screen.contains("Seed plan file on disk")
{
bail!("expected resumed session content (plan body or setup sentinel)\n{screen}");
}
if resumed.contains_text("panicked") {
bail!("pager panicked\n{screen}");
}
resumed.inject_keys(b"a").context("press 'a' to approve")?;
resumed
.wait_for_text(IMPLEMENT_SENTINEL, Duration::from_secs(30))
.context("approve must leave plan mode and start the implement turn")?;
resumed.quit().context("quit resumed pager")?;
Ok(())
}
/// Parks a plan approval in every session dir under the sandbox home, since the
/// id of the session the first pager created is not known here.
fn seed_parked_approval(home: &Path) -> Result<usize> {
let sessions_root = home.join(".kigi").join("sessions");
if !sessions_root.is_dir() {
bail!(
"expected sessions under {} after first turn",
sessions_root.display()
);
}
let mut seeded = 0usize;
for cwd_ent in std::fs::read_dir(&sessions_root).context("read sessions root")? {
let cwd_ent = cwd_ent.context("cwd entry")?;
if !cwd_ent.file_type().context("ft")?.is_dir() {
continue;
}
for sess_ent in std::fs::read_dir(cwd_ent.path()).context("read cwd sessions")? {
let sess_ent = sess_ent.context("session entry")?;
if !sess_ent.file_type().context("ft")?.is_dir() {
continue;
}
let dir = sess_ent.path();
std::fs::write(dir.join("plan.md"), PLAN_BODY).context("write plan.md")?;
write_awaiting_plan_mode(&dir.join("plan_mode.json"))?;
seeded += 1;
}
}
if seeded == 0 {
bail!(
"expected at least one session dir under {}",
sessions_root.display()
);
}
Ok(seeded)
}
/// The shape mirrors `kigi_shell::session::plan_mode::PlanModeSnapshot`, but this
/// test-only harness edits it as raw JSON rather than pull in the heavy shell
/// crate; preserving unknown fields keeps that decoupling safe as the schema grows.
fn write_awaiting_plan_mode(path: &Path) -> Result<()> {
let mut value: serde_json::Value = std::fs::read_to_string(path)
.ok()
.and_then(|raw| serde_json::from_str(&raw).ok())
.unwrap_or_else(|| {
serde_json::json!({
"state": "Active",
"was_previously_active": true,
"reminder_count": 0,
"pending_exit_reminder": false,
})
});
let obj = value
.as_object_mut()
.context("plan_mode.json must be a JSON object")?;
// Must be Active for the re-park; awaiting flag is the trigger.
obj.insert("state".into(), serde_json::Value::String("Active".into()));
obj.insert(
"awaiting_plan_approval".into(),
serde_json::Value::Bool(true),
);
std::fs::write(path, serde_json::to_vec_pretty(&value)?).context("write plan_mode.json")?;
Ok(())
}