| 1 | //! Per-scope instruction budget, selection, and notice behaviour (#6526). |
| 2 | //! |
| 3 | //! One test per branch the design specifies: the `AGENTS.md`/`CLAUDE.md` |
| 4 | //! truth table, skip-as-absent, symlink dedupe, per-scope trims, rules |
| 5 | //! charging, configured entries, and the `refuse` policy. |
| 6 | |
| 7 | use super::*; |
| 8 | use tempfile::tempdir; |
| 9 | |
| 10 | fn write(path: &Path, body: &str) { |
| 11 | if let Some(parent) = path.parent() { |
| 12 | fs::create_dir_all(parent).expect("mkdir"); |
| 13 | } |
| 14 | fs::write(path, body).expect("write"); |
| 15 | } |
| 16 | |
| 17 | fn with_instruction_settings<T>( |
| 18 | budget: InstructionBudgetConfig, |
| 19 | run: impl FnOnce(&InstructionSettings) -> T, |
| 20 | ) -> T { |
| 21 | run(&InstructionSettings { |
| 22 | budget, |
| 23 | ..Default::default() |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | fn budgets(global: usize, project: usize) -> InstructionBudgets { |
| 28 | InstructionBudgets { |
| 29 | global_tokens: global, |
| 30 | project_tokens: project, |
| 31 | static_guard_tokens: Some(900_000), |
| 32 | file_max_bytes: 8 * 1024 * 1024, |
| 33 | policy: InstructionBudgetPolicy::Trim, |
| 34 | claude_fallback: false, |
| 35 | usable_window_tokens: Some(1_000_000), |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn load_isolated(workspace: &Path, home: &Path) -> ProjectContext { |
| 40 | load_project_context_with_parents_and_home(workspace, Some(home)) |
| 41 | } |
| 42 | |
| 43 | fn assemble( |
| 44 | workspace: &Path, |
| 45 | home: &Path, |
| 46 | configured: &[crate::prompts::InstructionSource], |
| 47 | budgets: &InstructionBudgets, |
| 48 | ) -> (ProjectContext, InstructionAssembly) { |
| 49 | let mut ctx = load_isolated(workspace, home); |
| 50 | let assembly = assemble_instruction_block(&mut ctx, configured, budgets); |
| 51 | (ctx, assembly) |
| 52 | } |
| 53 | |
| 54 | fn claude_imports() -> ForeignInstructionImports { |
| 55 | ForeignInstructionImports::from_config(&["claude".to_string()]).0 |
| 56 | } |
| 57 | |
| 58 | // ── Selection ────────────────────────────────────────────────────────────── |
| 59 | |
| 60 | #[test] |
| 61 | fn agents_md_wins_over_claude_md() { |
| 62 | let ws = tempdir().unwrap(); |
| 63 | let home = tempdir().unwrap(); |
| 64 | write(&ws.path().join("AGENTS.md"), "AGENTS_WINS"); |
| 65 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_LOSES"); |
| 66 | |
| 67 | let ctx = load_isolated(ws.path(), home.path()); |
| 68 | let text = ctx.instructions.unwrap_or_default(); |
| 69 | assert!(text.contains("AGENTS_WINS"), "{text}"); |
| 70 | assert!( |
| 71 | !text.contains("CLAUDE_LOSES"), |
| 72 | "AGENTS.md wins; CLAUDE.md is not co-read without an import:\n{text}" |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn claude_md_requires_opt_in() { |
| 78 | let ws = tempdir().unwrap(); |
| 79 | let home = tempdir().unwrap(); |
| 80 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_FALLBACK_TEXT"); |
| 81 | |
| 82 | let ctx = load_isolated(ws.path(), home.path()); |
| 83 | assert!( |
| 84 | !ctx.instructions |
| 85 | .unwrap_or_default() |
| 86 | .contains("CLAUDE_FALLBACK_TEXT") |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn claude_fallback_can_be_disabled() { |
| 92 | let config = InstructionBudgetConfig { |
| 93 | claude_fallback: Some(false), |
| 94 | ..Default::default() |
| 95 | }; |
| 96 | with_instruction_settings(config, |settings| { |
| 97 | let ws = tempdir().unwrap(); |
| 98 | let home = tempdir().unwrap(); |
| 99 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_OPTED_OUT"); |
| 100 | |
| 101 | let ctx = load_project_context_with_settings( |
| 102 | ws.path(), |
| 103 | &InstructionSettings { |
| 104 | home: Some(home.path().to_path_buf()), |
| 105 | ..settings.clone() |
| 106 | }, |
| 107 | ); |
| 108 | assert!( |
| 109 | !ctx.instructions |
| 110 | .unwrap_or_default() |
| 111 | .contains("CLAUDE_OPTED_OUT"), |
| 112 | "claude_fallback = false is an explicit opt-out" |
| 113 | ); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn claude_import_reads_when_agents_absent_and_fallback_disabled() { |
| 119 | let config = InstructionBudgetConfig { |
| 120 | claude_fallback: Some(false), |
| 121 | ..Default::default() |
| 122 | }; |
| 123 | with_instruction_settings(config, |settings| { |
| 124 | let ws = tempdir().unwrap(); |
| 125 | let home = tempdir().unwrap(); |
| 126 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_EXPLICIT_IMPORT"); |
| 127 | |
| 128 | // The import set is explicit here: the process-wide default is |
| 129 | // "import nothing", and the fallback flag is off, so only the explicit |
| 130 | // `claude` import can read the file. |
| 131 | let mut ctx = load_project_context_with_settings( |
| 132 | ws.path(), |
| 133 | &InstructionSettings { |
| 134 | home: Some(home.path().to_path_buf()), |
| 135 | imports: claude_imports(), |
| 136 | ..settings.clone() |
| 137 | }, |
| 138 | ); |
| 139 | let assembly = assemble_instruction_block(&mut ctx, &[], &budgets(1_000, 1_000)); |
| 140 | assert!( |
| 141 | assembly |
| 142 | .block |
| 143 | .unwrap_or_default() |
| 144 | .contains("CLAUDE_EXPLICIT_IMPORT") |
| 145 | ); |
| 146 | }); |
| 147 | |
| 148 | // The explicit import outranks the fallback flag: load through the import |
| 149 | // set the loader reads. |
| 150 | let ws = tempdir().unwrap(); |
| 151 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_IMPORTED"); |
| 152 | let ctx = load_project_context_with_imports(ws.path(), &claude_imports()); |
| 153 | assert!( |
| 154 | ctx.instructions |
| 155 | .unwrap_or_default() |
| 156 | .contains("CLAUDE_IMPORTED") |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn explicit_claude_import_reads_both() { |
| 162 | let ws = tempdir().unwrap(); |
| 163 | write(&ws.path().join("AGENTS.md"), "AGENTS_PRIMARY"); |
| 164 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_CO_READ"); |
| 165 | |
| 166 | let ctx = load_project_context_with_imports(ws.path(), &claude_imports()); |
| 167 | let text = ctx.instructions.unwrap_or_default(); |
| 168 | assert!(text.contains("AGENTS_PRIMARY"), "{text}"); |
| 169 | assert!( |
| 170 | text.contains("CLAUDE_CO_READ"), |
| 171 | "an explicit claude import co-reads CLAUDE.md next to AGENTS.md:\n{text}" |
| 172 | ); |
| 173 | let agents_at = text.find("AGENTS_PRIMARY").unwrap(); |
| 174 | let claude_at = text.find("CLAUDE_CO_READ").unwrap(); |
| 175 | assert!(agents_at < claude_at, "AGENTS.md reads first"); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn agents_md_skipped_falls_back_to_claude() { |
| 180 | let config = InstructionBudgetConfig { |
| 181 | file_max_bytes: Some(16), |
| 182 | claude_fallback: Some(true), |
| 183 | ..Default::default() |
| 184 | }; |
| 185 | with_instruction_settings(config, |settings| { |
| 186 | let ws = tempdir().unwrap(); |
| 187 | let home = tempdir().unwrap(); |
| 188 | write(&ws.path().join("AGENTS.md"), "A".repeat(200).as_str()); |
| 189 | // Comfortably under the 16-byte ceiling above, or the fallback would |
| 190 | // be skipped for the same reason as `AGENTS.md`. |
| 191 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_OK"); |
| 192 | |
| 193 | let ctx = load_project_context_with_settings( |
| 194 | ws.path(), |
| 195 | &InstructionSettings { |
| 196 | home: Some(home.path().to_path_buf()), |
| 197 | ..settings.clone() |
| 198 | }, |
| 199 | ); |
| 200 | let text = ctx.instructions.unwrap_or_default(); |
| 201 | assert!( |
| 202 | text.contains("CLAUDE_OK"), |
| 203 | "a skipped AGENTS.md counts as absent:\n{text}" |
| 204 | ); |
| 205 | let skipped = ctx |
| 206 | .notices |
| 207 | .iter() |
| 208 | .find(|notice| notice.limit == InstructionLimit::FileCeiling) |
| 209 | .expect("the skip is reported"); |
| 210 | assert_eq!(skipped.action, InstructionAction::Skipped); |
| 211 | assert!( |
| 212 | skipped |
| 213 | .fallback_selected |
| 214 | .as_deref() |
| 215 | .is_some_and(|label| label.contains("CLAUDE.md")), |
| 216 | "the notice names the file that replaced it: {skipped:?}" |
| 217 | ); |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn global_claude_is_last_resort_after_instructions_md() { |
| 223 | let ws = tempdir().unwrap(); |
| 224 | let home = tempdir().unwrap(); |
| 225 | write( |
| 226 | &home.path().join(".codewhale/instructions.md"), |
| 227 | "GLOBAL_INSTRUCTIONS_WIN", |
| 228 | ); |
| 229 | write(&home.path().join(".claude/CLAUDE.md"), "GLOBAL_CLAUDE_LAST"); |
| 230 | |
| 231 | let ctx = load_isolated(ws.path(), home.path()); |
| 232 | let text = ctx.instructions.unwrap_or_default(); |
| 233 | assert!(text.contains("GLOBAL_INSTRUCTIONS_WIN"), "{text}"); |
| 234 | assert!( |
| 235 | !text.contains("GLOBAL_CLAUDE_LAST"), |
| 236 | "a foreign file never outranks codewhale's own:\n{text}" |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn global_claude_is_read_when_nothing_else_exists() { |
| 242 | let config = InstructionBudgetConfig { |
| 243 | claude_fallback: Some(true), |
| 244 | ..Default::default() |
| 245 | }; |
| 246 | with_instruction_settings(config, |settings| { |
| 247 | let ws = tempdir().unwrap(); |
| 248 | let home = tempdir().unwrap(); |
| 249 | write(&home.path().join(".claude/CLAUDE.md"), "GLOBAL_CLAUDE_ONLY"); |
| 250 | |
| 251 | let ctx = load_project_context_with_settings( |
| 252 | ws.path(), |
| 253 | &InstructionSettings { |
| 254 | home: Some(home.path().to_path_buf()), |
| 255 | ..settings.clone() |
| 256 | }, |
| 257 | ); |
| 258 | assert!( |
| 259 | ctx.instructions |
| 260 | .unwrap_or_default() |
| 261 | .contains("GLOBAL_CLAUDE_ONLY") |
| 262 | ); |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | #[cfg(unix)] |
| 267 | #[test] |
| 268 | fn symlinked_duplicate_is_loaded_once() { |
| 269 | let ws = tempdir().unwrap(); |
| 270 | let home = tempdir().unwrap(); |
| 271 | let global = home.path().join(".codewhale/AGENTS.md"); |
| 272 | write(&global, "SHARED_SYMLINK_BODY"); |
| 273 | std::os::unix::fs::symlink(&global, ws.path().join("AGENTS.md")).expect("symlink"); |
| 274 | |
| 275 | let ctx = load_isolated(ws.path(), home.path()); |
| 276 | let text = ctx.instructions.unwrap_or_default(); |
| 277 | assert_eq!( |
| 278 | text.matches("SHARED_SYMLINK_BODY").count(), |
| 279 | 1, |
| 280 | "one physical file loads once:\n{text}" |
| 281 | ); |
| 282 | assert!( |
| 283 | ctx.notices.iter().any(|notice| { |
| 284 | notice.path.as_deref() |
| 285 | == Some( |
| 286 | ws.path() |
| 287 | .canonicalize() |
| 288 | .unwrap() |
| 289 | .join("AGENTS.md") |
| 290 | .as_path(), |
| 291 | ) |
| 292 | && notice.action == InstructionAction::Skipped |
| 293 | }), |
| 294 | "the duplicate is reported, not silently dropped: {:?}", |
| 295 | ctx.notices |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | // ── Per-scope budgets ────────────────────────────────────────────────────── |
| 300 | |
| 301 | #[test] |
| 302 | fn no_trim_within_scope_budget() { |
| 303 | let ws = tempdir().unwrap(); |
| 304 | let home = tempdir().unwrap(); |
| 305 | write(&ws.path().join("AGENTS.md"), "SMALL PROJECT FILE"); |
| 306 | |
| 307 | let (ctx, assembly) = assemble(ws.path(), home.path(), &[], &budgets(10_000, 10_000)); |
| 308 | assert!( |
| 309 | !assembly.notices.iter().any(|notice| notice.is_loss()), |
| 310 | "nothing is lost while a source is within its budget: {:?}", |
| 311 | assembly.notices |
| 312 | ); |
| 313 | assert_eq!( |
| 314 | ctx.instructions.as_deref(), |
| 315 | Some("SMALL PROJECT FILE"), |
| 316 | "content is unmodified" |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn per_scope_budgets_are_independent() { |
| 322 | let ws = tempdir().unwrap(); |
| 323 | let home = tempdir().unwrap(); |
| 324 | write( |
| 325 | &home.path().join(".codewhale/AGENTS.md"), |
| 326 | format!("GLOBAL {}", "g".repeat(4_000)).as_str(), |
| 327 | ); |
| 328 | write(&ws.path().join("AGENTS.md"), "PROJECT INTACT"); |
| 329 | |
| 330 | let (_ctx, assembly) = assemble(ws.path(), home.path(), &[], &budgets(50, 10_000)); |
| 331 | let global_loss = assembly |
| 332 | .notices |
| 333 | .iter() |
| 334 | .find(|notice| notice.is_loss()) |
| 335 | .expect("the global scope is over its own budget"); |
| 336 | assert_eq!(global_loss.scope, InstructionScope::Global); |
| 337 | assert!( |
| 338 | !assembly |
| 339 | .notices |
| 340 | .iter() |
| 341 | .any(|notice| notice.is_loss() && notice.scope == InstructionScope::Project), |
| 342 | "a huge global file does not shrink the project scope: {:?}", |
| 343 | assembly.notices |
| 344 | ); |
| 345 | assert!( |
| 346 | assembly |
| 347 | .block |
| 348 | .unwrap_or_default() |
| 349 | .contains("PROJECT INTACT") |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn trim_keeps_the_nearest_scope() { |
| 355 | let ws = tempdir().unwrap(); |
| 356 | let home = tempdir().unwrap(); |
| 357 | fs::create_dir_all(ws.path().join(".git")).unwrap(); |
| 358 | write(&ws.path().join(".git/HEAD"), "ref: refs/heads/main\n"); |
| 359 | write( |
| 360 | &ws.path().join("AGENTS.md"), |
| 361 | format!("ROOT-MARKER {}", "R".repeat(20_000)).as_str(), |
| 362 | ); |
| 363 | let nested = ws.path().join("crates").join("tui"); |
| 364 | write(&nested.join("AGENTS.md"), "NEAREST-MARKER stays"); |
| 365 | |
| 366 | let (_ctx, assembly) = assemble(&nested, home.path(), &[], &budgets(10_000, 200)); |
| 367 | let block = assembly.block.unwrap_or_default(); |
| 368 | assert!(block.contains("NEAREST-MARKER stays"), "{block}"); |
| 369 | assert!( |
| 370 | !block.contains("ROOT-MARKER"), |
| 371 | "the broadest segment is dropped first:\n{block}" |
| 372 | ); |
| 373 | } |
| 374 | |
| 375 | #[test] |
| 376 | fn single_oversized_file_keeps_the_head() { |
| 377 | let ws = tempdir().unwrap(); |
| 378 | let home = tempdir().unwrap(); |
| 379 | let body = format!("HEAD-MARKER {}", "z".repeat(20_000)); |
| 380 | write(&ws.path().join("AGENTS.md"), &body); |
| 381 | |
| 382 | let (_ctx, assembly) = assemble(ws.path(), home.path(), &[], &budgets(10_000, 300)); |
| 383 | let block = assembly.block.unwrap_or_default(); |
| 384 | assert!(block.contains("HEAD-MARKER"), "{block}"); |
| 385 | let head_at = block.find("HEAD-MARKER").expect("head marker"); |
| 386 | assert!( |
| 387 | head_at < block.find("instructions trimmed").unwrap_or(usize::MAX), |
| 388 | "the head survives and the marker records the cut:\n{block}" |
| 389 | ); |
| 390 | assert!(block.contains("instructions trimmed"), "{block}"); |
| 391 | assert!( |
| 392 | block.contains("estimated tokens"), |
| 393 | "the marker carries numbers" |
| 394 | ); |
| 395 | assert!(assembly.global_tokens <= 300); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn zero_bytes_left_is_reported_as_skipped() { |
| 400 | let ws = tempdir().unwrap(); |
| 401 | let home = tempdir().unwrap(); |
| 402 | write(&ws.path().join("AGENTS.md"), &"q".repeat(4_000)); |
| 403 | |
| 404 | let (_ctx, assembly) = assemble(ws.path(), home.path(), &[], &budgets(10_000, 1)); |
| 405 | let notice = assembly |
| 406 | .notices |
| 407 | .iter() |
| 408 | .find(|notice| notice.scope == InstructionScope::Project) |
| 409 | .expect("a notice"); |
| 410 | assert_eq!( |
| 411 | notice.action, |
| 412 | InstructionAction::Skipped, |
| 413 | "no source bytes left means skipped, not trimmed: {notice:?}" |
| 414 | ); |
| 415 | assert_eq!(notice.limit, InstructionLimit::ScopeBudget); |
| 416 | } |
| 417 | |
| 418 | #[test] |
| 419 | fn rules_share_the_project_scope_budget() { |
| 420 | let ws = tempdir().unwrap(); |
| 421 | let home = tempdir().unwrap(); |
| 422 | write(&ws.path().join("AGENTS.md"), &"I".repeat(1_000)); |
| 423 | write(&ws.path().join(".codewhale/rules/a.md"), &"R".repeat(4_000)); |
| 424 | |
| 425 | let tokens_for_instructions = estimate_instruction_tokens(&"I".repeat(1_000)); |
| 426 | let (_ctx, assembly) = assemble( |
| 427 | ws.path(), |
| 428 | home.path(), |
| 429 | &[], |
| 430 | &budgets(10_000, tokens_for_instructions + 10), |
| 431 | ); |
| 432 | let rules = assembly |
| 433 | .notices |
| 434 | .iter() |
| 435 | .find(|notice| notice.origin == InstructionOrigin::Rules) |
| 436 | .expect("rules are charged after the chain"); |
| 437 | assert_eq!(rules.limit, InstructionLimit::RulesBudget); |
| 438 | } |
| 439 | |
| 440 | #[test] |
| 441 | fn rules_block_is_head_trimmed_with_numbers() { |
| 442 | let ws = tempdir().unwrap(); |
| 443 | let home = tempdir().unwrap(); |
| 444 | write(&ws.path().join("AGENTS.md"), "SHORT"); |
| 445 | write(&ws.path().join(".codewhale/rules/a.md"), &"R".repeat(8_000)); |
| 446 | |
| 447 | let (_ctx, assembly) = assemble(ws.path(), home.path(), &[], &budgets(10_000, 400)); |
| 448 | let block = assembly.block.unwrap_or_default(); |
| 449 | assert!(block.contains("rules block trimmed"), "{block}"); |
| 450 | assert!( |
| 451 | block.contains("estimated tokens"), |
| 452 | "numbers, not just a marker" |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | // ── Per-file ceiling, empties, cap, duplicates ───────────────────────────── |
| 457 | |
| 458 | #[test] |
| 459 | fn file_over_ceiling_is_skipped_not_trimmed() { |
| 460 | let config = InstructionBudgetConfig { |
| 461 | file_max_bytes: Some(8), |
| 462 | ..Default::default() |
| 463 | }; |
| 464 | with_instruction_settings(config, |settings| { |
| 465 | let ws = tempdir().unwrap(); |
| 466 | let home = tempdir().unwrap(); |
| 467 | write( |
| 468 | &ws.path().join("AGENTS.md"), |
| 469 | "THIS IS LONGER THAN EIGHT BYTES", |
| 470 | ); |
| 471 | |
| 472 | let ctx = load_project_context_with_settings( |
| 473 | ws.path(), |
| 474 | &InstructionSettings { |
| 475 | home: Some(home.path().to_path_buf()), |
| 476 | ..settings.clone() |
| 477 | }, |
| 478 | ); |
| 479 | assert!( |
| 480 | !ctx.instructions |
| 481 | .unwrap_or_default() |
| 482 | .contains("LONGER THAN EIGHT") |
| 483 | ); |
| 484 | let notice = ctx |
| 485 | .notices |
| 486 | .iter() |
| 487 | .find(|notice| notice.limit == InstructionLimit::FileCeiling) |
| 488 | .expect("the ceiling is reported"); |
| 489 | assert_eq!(notice.action, InstructionAction::Skipped); |
| 490 | }); |
| 491 | } |
| 492 | |
| 493 | #[test] |
| 494 | fn empty_file_is_skipped() { |
| 495 | let ws = tempdir().unwrap(); |
| 496 | let home = tempdir().unwrap(); |
| 497 | write(&ws.path().join("AGENTS.md"), " \n\t\n"); |
| 498 | write(&ws.path().join("CLAUDE.md"), "CLAUDE_AFTER_EMPTY"); |
| 499 | |
| 500 | let ctx = load_project_context_with_settings( |
| 501 | ws.path(), |
| 502 | &InstructionSettings { |
| 503 | home: Some(home.path().to_path_buf()), |
| 504 | budget: InstructionBudgetConfig { |
| 505 | claude_fallback: Some(true), |
| 506 | ..Default::default() |
| 507 | }, |
| 508 | ..Default::default() |
| 509 | }, |
| 510 | ); |
| 511 | assert!( |
| 512 | ctx.notices |
| 513 | .iter() |
| 514 | .any(|notice| notice.limit == InstructionLimit::EmptyFile) |
| 515 | ); |
| 516 | assert!( |
| 517 | ctx.instructions |
| 518 | .unwrap_or_default() |
| 519 | .contains("CLAUDE_AFTER_EMPTY") |
| 520 | ); |
| 521 | } |
| 522 | |
| 523 | #[test] |
| 524 | fn rules_cap_is_reported() { |
| 525 | let ws = tempdir().unwrap(); |
| 526 | let home = tempdir().unwrap(); |
| 527 | for index in 0..MAX_RULES_FILES + 5 { |
| 528 | write( |
| 529 | &ws.path() |
| 530 | .join(format!(".codewhale/rules/rule_{index:03}.md")), |
| 531 | "rule body", |
| 532 | ); |
| 533 | } |
| 534 | |
| 535 | let ctx = load_isolated(ws.path(), home.path()); |
| 536 | let capped = ctx |
| 537 | .notices |
| 538 | .iter() |
| 539 | .filter(|notice| notice.limit == InstructionLimit::RulesCap) |
| 540 | .count(); |
| 541 | assert_eq!(capped, 5, "every file beyond the cap is reported"); |
| 542 | } |
| 543 | |
| 544 | // ── Configured entries and the cache ─────────────────────────────────────── |
| 545 | |
| 546 | #[test] |
| 547 | fn configured_instructions_are_read_fresh() { |
| 548 | let ws = tempdir().unwrap(); |
| 549 | let home = tempdir().unwrap(); |
| 550 | let extra = ws.path().join("extra.md"); |
| 551 | write(&extra, "VERSION_ONE"); |
| 552 | let configured = vec![crate::prompts::InstructionSource::File(extra.clone())]; |
| 553 | |
| 554 | let (_ctx, first) = assemble( |
| 555 | ws.path(), |
| 556 | home.path(), |
| 557 | &configured, |
| 558 | &budgets(10_000, 10_000), |
| 559 | ); |
| 560 | assert!(first.block.unwrap_or_default().contains("VERSION_ONE")); |
| 561 | |
| 562 | write(&extra, "VERSION_TWO"); |
| 563 | let (_ctx, second) = assemble( |
| 564 | ws.path(), |
| 565 | home.path(), |
| 566 | &configured, |
| 567 | &budgets(10_000, 10_000), |
| 568 | ); |
| 569 | let block = second.block.unwrap_or_default(); |
| 570 | assert!( |
| 571 | block.contains("VERSION_TWO") && !block.contains("VERSION_ONE"), |
| 572 | "configured entries are read after the cache lookup:\n{block}" |
| 573 | ); |
| 574 | } |
| 575 | |
| 576 | #[test] |
| 577 | fn inline_instructions_stay_outside_cache() { |
| 578 | let ws = tempdir().unwrap(); |
| 579 | let home = tempdir().unwrap(); |
| 580 | write(&ws.path().join("AGENTS.md"), "PROJECT"); |
| 581 | let configured = vec![crate::prompts::InstructionSource::Inline { |
| 582 | name: "embedded:test".to_string(), |
| 583 | content: "INLINE_ONLY".to_string(), |
| 584 | }]; |
| 585 | |
| 586 | let (_ctx, assembly) = assemble( |
| 587 | ws.path(), |
| 588 | home.path(), |
| 589 | &configured, |
| 590 | &budgets(10_000, 10_000), |
| 591 | ); |
| 592 | assert!(assembly.block.unwrap_or_default().contains("INLINE_ONLY")); |
| 593 | |
| 594 | let cached = load_isolated(ws.path(), home.path()); |
| 595 | assert!( |
| 596 | !cached |
| 597 | .instructions |
| 598 | .unwrap_or_default() |
| 599 | .contains("INLINE_ONLY"), |
| 600 | "inline content never enters the file-keyed cache" |
| 601 | ); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn instructions_array_counts_toward_global() { |
| 606 | let ws = tempdir().unwrap(); |
| 607 | let home = tempdir().unwrap(); |
| 608 | write(&ws.path().join("AGENTS.md"), "PROJECT_KEPT"); |
| 609 | let global_file = ws.path().join("global-extra.md"); |
| 610 | write(&global_file, &"G".repeat(4_000)); |
| 611 | let configured = vec![crate::prompts::InstructionSource::File(global_file)]; |
| 612 | |
| 613 | let (_ctx, assembly) = assemble(ws.path(), home.path(), &configured, &budgets(60, 10_000)); |
| 614 | assert!( |
| 615 | assembly |
| 616 | .notices |
| 617 | .iter() |
| 618 | .any(|notice| notice.is_loss() && notice.scope == InstructionScope::Global), |
| 619 | "configured entries are global authority: {:?}", |
| 620 | assembly.notices |
| 621 | ); |
| 622 | assert!(assembly.block.unwrap_or_default().contains("PROJECT_KEPT")); |
| 623 | assert!(assembly.project_tokens > 0); |
| 624 | } |
| 625 | |
| 626 | #[test] |
| 627 | fn cache_returns_untrimmed_segments_across_budgets() { |
| 628 | let ws = tempdir().unwrap(); |
| 629 | let home = tempdir().unwrap(); |
| 630 | let body = format!("FULL_BODY {}", "c".repeat(10_000)); |
| 631 | write(&ws.path().join("AGENTS.md"), &body); |
| 632 | |
| 633 | let (_small, _assembly) = assemble(ws.path(), home.path(), &[], &budgets(10_000, 50)); |
| 634 | let cached = load_isolated(ws.path(), home.path()); |
| 635 | assert_eq!( |
| 636 | cached.instructions.as_deref().map(str::len), |
| 637 | Some(body.len()), |
| 638 | "a trim must not be written back into the cache" |
| 639 | ); |
| 640 | assert_eq!(cached.instruction_segments.len(), 1); |
| 641 | } |
| 642 | |
| 643 | // ── Policy ──────────────────────────────────────────────────────────────── |
| 644 | |
| 645 | #[test] |
| 646 | fn refuse_policy_stops_on_trim_but_not_on_skip() { |
| 647 | let trim_config = InstructionBudgetConfig { |
| 648 | project_budget_tokens: Some(10), |
| 649 | policy: Some(InstructionBudgetPolicy::Refuse), |
| 650 | ..Default::default() |
| 651 | }; |
| 652 | with_instruction_settings(trim_config, |settings| { |
| 653 | let ws = tempdir().unwrap(); |
| 654 | write(&ws.path().join("AGENTS.md"), &"T".repeat(4_000)); |
| 655 | let outcome = |
| 656 | startup_instruction_notice(ws.path(), &[], Some(128_000), Some(2_048), settings); |
| 657 | let error = outcome.expect_err("refuse stops on a trim"); |
| 658 | assert!(error.contains("refuse"), "{error}"); |
| 659 | }); |
| 660 | |
| 661 | let skip_config = InstructionBudgetConfig { |
| 662 | file_max_bytes: Some(8), |
| 663 | policy: Some(InstructionBudgetPolicy::Refuse), |
| 664 | ..Default::default() |
| 665 | }; |
| 666 | with_instruction_settings(skip_config, |settings| { |
| 667 | let ws = tempdir().unwrap(); |
| 668 | write(&ws.path().join("AGENTS.md"), &"S".repeat(1_000)); |
| 669 | let notice = |
| 670 | startup_instruction_notice(ws.path(), &[], Some(128_000), Some(2_048), settings) |
| 671 | .expect("a skip is not a refusal") |
| 672 | .expect("a skip is reported"); |
| 673 | assert!(notice.contains("skipped"), "{notice}"); |
| 674 | }); |
| 675 | } |
| 676 | |
| 677 | #[test] |
| 678 | fn startup_notice_names_the_loss_and_numbers() { |
| 679 | with_instruction_settings( |
| 680 | InstructionBudgetConfig { |
| 681 | project_budget_tokens: Some(50), |
| 682 | ..Default::default() |
| 683 | }, |
| 684 | |settings| { |
| 685 | let ws = tempdir().unwrap(); |
| 686 | write(&ws.path().join("AGENTS.md"), &"N".repeat(4_000)); |
| 687 | let notice = |
| 688 | startup_instruction_notice(ws.path(), &[], Some(128_000), Some(2_048), settings) |
| 689 | .expect("valid config") |
| 690 | .expect("a trim is reported"); |
| 691 | assert!(notice.contains("trimmed"), "{notice}"); |
| 692 | assert!(notice.contains("estimated tokens"), "{notice}"); |
| 693 | assert!(notice.contains("bytes"), "{notice}"); |
| 694 | }, |
| 695 | ); |
| 696 | } |
| 697 | |
| 698 | #[test] |
| 699 | fn file_max_bytes_zero_is_rejected() { |
| 700 | let config = InstructionBudgetConfig { |
| 701 | file_max_bytes: Some(0), |
| 702 | ..Default::default() |
| 703 | }; |
| 704 | assert!(config.validate().is_err()); |
| 705 | } |
| 706 |