| 1 | //! Behavioural guards: positive statements of promises a later commit must not |
| 2 | //! quietly remove. |
| 3 | //! |
| 4 | //! Each guard cites the issue that created it. The contrast is the failure mode |
| 5 | //! in #7433: an inline test was renamed and its assertion inverted in place |
| 6 | //! (`2282c33f0`, `system_prompt_merges_workspace_and_configured_skills_dir` → |
| 7 | //! `system_prompt_carries_no_skills_index`), so the suite stayed green while the |
| 8 | //! documented behaviour disappeared. A positive statement in this file fails |
| 9 | //! when the behaviour goes away; it can only be deleted or rewritten, and either |
| 10 | //! is a one-line diff a reviewer sees. |
| 11 | //! |
| 12 | //! Keep one promise per guard. This is not a general test suite: put ordinary |
| 13 | //! behaviour tests next to the code they cover. |
| 14 | |
| 15 | use std::path::{Path, PathBuf}; |
| 16 | |
| 17 | fn repo_root() -> PathBuf { |
| 18 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 19 | .join("../..") |
| 20 | .canonicalize() |
| 21 | .expect("resolve workspace root from CARGO_MANIFEST_DIR") |
| 22 | } |
| 23 | |
| 24 | fn read(rel: &str) -> String { |
| 25 | std::fs::read_to_string(repo_root().join(rel)).unwrap_or_else(|err| panic!("read {rel}: {err}")) |
| 26 | } |
| 27 | |
| 28 | /// #7433 — an issue is not startable until a maintainer grants `agent-ready`. |
| 29 | /// |
| 30 | /// The Agent task form may stay for maintainers, but it must not pre-authorise |
| 31 | /// an agent by applying the label at creation. The metadata contract test |
| 32 | /// (`.github/scripts/agent-task-metadata.test.sh`) and this guard fail together |
| 33 | /// if the line comes back. |
| 34 | #[test] |
| 35 | fn agent_task_form_does_not_self_apply_agent_ready() { |
| 36 | let template = read(".github/ISSUE_TEMPLATE/agent-task.yml"); |
| 37 | assert!( |
| 38 | template.contains("name: Agent task"), |
| 39 | "the Agent task form moved or was deleted; update this guard deliberately (#7433)" |
| 40 | ); |
| 41 | if let Some(line) = template |
| 42 | .lines() |
| 43 | .find(|line| line.trim_start().starts_with("labels:") && line.contains("agent-ready")) |
| 44 | { |
| 45 | panic!("the Agent task form self-applies agent-ready again (#7433): {line}"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// #7423 — synthesized tool-call ids never collide with literal ids. |
| 50 | /// |
| 51 | /// `client/chat/tests/stream_decoder.rs` proves the behaviour for specific |
| 52 | /// streams. This guard keeps the allocation rule itself: literal ids seed the |
| 53 | /// assigned set, a fallback id is allocated against it, and a collision retries |
| 54 | /// with a suffix. A refactor that drops any half fails here even if the unit |
| 55 | /// tests move with it. |
| 56 | #[test] |
| 57 | fn chat_fallback_ids_are_allocated_against_literal_ids() { |
| 58 | let source = read("crates/engine/src/client/chat.rs"); |
| 59 | assert!( |
| 60 | source.contains("filter_map(|(_, id)| id.clone())"), |
| 61 | "literal tool-call ids are no longer seeded into the assigned set (#7423)" |
| 62 | ); |
| 63 | assert!( |
| 64 | source.contains("synthesize_chat_call_id(ordinal, &mut assigned)"), |
| 65 | "fallback ids are no longer allocated against the assigned set (#7423)" |
| 66 | ); |
| 67 | assert!( |
| 68 | source.contains("while !assigned.insert(id.clone())"), |
| 69 | "fallback ids no longer retry on collision (#7423)" |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn metrics_stats_filters_before_retention_and_surfaces_partial_warnings() { |
| 75 | let stats = read("crates/cli/src/metrics/stats.rs"); |
| 76 | assert!( |
| 77 | stats.contains("!self.in_project(Some(&metadata.workspace))") |
| 78 | && stats.contains("!self.in_window(metadata.updated_at)"), |
| 79 | "Collector::session must short-circuit unmatched projects/windows before accounting (#7578)" |
| 80 | ); |
| 81 | assert!( |
| 82 | stats.contains("let turn_in_window = self.in_window(turn.created_at)"), |
| 83 | "Collector::turn must pre-filter out-of-window turns before retention (#7578)" |
| 84 | ); |
| 85 | assert!( |
| 86 | stats.contains("fn format_human_header(") && stats.contains("\"Since: {}\""), |
| 87 | "native metrics human output must print the resolved UTC Since header (#7578)" |
| 88 | ); |
| 89 | let metrics = read("crates/cli/src/metrics.rs"); |
| 90 | assert!( |
| 91 | metrics.contains("fn legacy_partial_report_warning("), |
| 92 | "legacy metrics text output must surface receipt read gaps and 16 MiB cap exhaustion (#7578)" |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn subagent_lifecycle_audit_persists_across_session_switch_and_exec() { |
| 98 | let event_loop = read("crates/tui/src/tui/ui/event_loop.rs"); |
| 99 | assert!( |
| 100 | event_loop |
| 101 | .contains("let active_agent_lifecycle_event = observe_terminal_agent_lifecycle_event("), |
| 102 | "TUI event loop must persist terminal sub-agent audit events before active-session UI filtering (#6315)" |
| 103 | ); |
| 104 | let exec_agent = read("crates/app-server/src/exec_agent.rs"); |
| 105 | assert!( |
| 106 | exec_agent |
| 107 | .contains("codewhale_engine::tools::subagent::log_subagent_lifecycle_audit(&event);"), |
| 108 | "headless exec must persist sub-agent lifecycle audit events through the shared logger (#6315)" |
| 109 | ); |
| 110 | let subagent = read("crates/engine/src/tools/subagent/mod.rs"); |
| 111 | assert!( |
| 112 | subagent.contains("pub fn log_subagent_lifecycle_audit(") |
| 113 | && subagent.contains("pub usage: Option<AgentRunUsage>,"), |
| 114 | "subagent module must expose shared audit logging and persist measured usage (#6315)" |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn compaction_summary_preserves_provider_tool_choice_and_caps_full_history_images() { |
| 120 | let compaction = read("crates/engine/src/compaction.rs"); |
| 121 | assert!( |
| 122 | compaction.contains("crate::core::engine::tool_catalog::default_tool_choice(false)"), |
| 123 | "compaction summary requests must preserve default_tool_choice(false) across providers (#7564)" |
| 124 | ); |
| 125 | assert!( |
| 126 | compaction.contains("if forbid_tools_on_retry && request.tool_choice.is_some()"), |
| 127 | "compaction summary must switch to tool_choice='none' only on tool-call retry (#7564)" |
| 128 | ); |
| 129 | assert!( |
| 130 | compaction.contains("cap_request_images(&mut capped_messages, cap)"), |
| 131 | "compaction must apply cap_request_images over full history before slicing the tail (#7564)" |
| 132 | ); |
| 133 | let engine = read("crates/engine/src/core/engine.rs"); |
| 134 | assert!( |
| 135 | engine |
| 136 | .matches("self.session.last_request_tools = None;") |
| 137 | .count() |
| 138 | >= 2, |
| 139 | "session restore and switch must clear last_request_tools (#7564)" |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn file_read_paths_verify_opened_handles_and_feed_pdf_ocr_from_bytes() { |
| 145 | let file_rs = read("crates/engine/src/tools/file.rs"); |
| 146 | assert!( |
| 147 | file_rs.contains("fn open_verified_read_handle(") |
| 148 | && file_rs.contains("verify_opened_read_file(&file, &plan.boundary)?"), |
| 149 | "read and read_file must open and verify a single handle before reading (#7576)" |
| 150 | ); |
| 151 | assert!( |
| 152 | file_rs.contains("extract_pdf_bytes_result(&bytes, pages, pdf_command)") |
| 153 | && file_rs.contains("read_image_bytes_via_ocr(bytes,"), |
| 154 | "PDF and OCR reads must consume verified bytes instead of reopening paths by name (#7576)" |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn unreachable_permission_rules_warn_on_load_and_in_permissions_command() { |
| 160 | let execpolicy = read("crates/execpolicy/src/lib.rs"); |
| 161 | assert!( |
| 162 | execpolicy |
| 163 | .contains("pub fn unreachable_rule_warning(rule: &ToolAskRule) -> Option<String>"), |
| 164 | "execpolicy must keep unreachable_rule_warning for dead permission rules (#7575, #7576)" |
| 165 | ); |
| 166 | let perms_cfg = read("crates/config/src/permissions.rs"); |
| 167 | assert!( |
| 168 | perms_cfg.contains("collect_unreachable_rule_warnings("), |
| 169 | "permissions config loader must emit warnings for unreachable rules (#7575, #7576)" |
| 170 | ); |
| 171 | let perms_cmd = read("crates/tui/src/commands/groups/config/permissions.rs"); |
| 172 | assert!( |
| 173 | perms_cmd.contains("rule.warning.as_deref()"), |
| 174 | "/permissions output must surface per-rule unreachable warnings (#7575, #7576)" |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn skills_config_and_loader_preserve_listing_budget_settings() { |
| 180 | let skills = read("crates/skills/src/lib.rs"); |
| 181 | assert!( |
| 182 | skills.contains("listing_max_description_chars") |
| 183 | && skills.contains("listing_budget_fraction"), |
| 184 | "skills loader must honour listing_max_description_chars and listing_budget_fraction (#7432)" |
| 185 | ); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn dsh_tools_guard_denies_reason_objects_without_requiring_kind_deny() { |
| 190 | let root_ts = read("crates/engine/extension-host/src/root.ts"); |
| 191 | assert!( |
| 192 | root_ts.contains("(kind === undefined || kind === 'deny')") |
| 193 | && root_ts.contains("return { kind: 'deny', reason: reason.trim() }"), |
| 194 | "ToolsShim.guard must not fail open on ToolGuardDenial objects without kind='deny'" |
| 195 | ); |
| 196 | } |
| 197 |