basis 0.8.1

The basis SDK: workspace discovery, run lifecycle, one event stream, and the two seams. No protocol, no transport, no TTY.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! What opening a workspace settles, and what it must not.
//!
//! Split out of `builder.rs` only for its size. The tests for the knobs
//! ADR-0018 moved to the runtime — history, credential redaction, interceptor
//! ordering, the compatible provider — moved with them, to
//! `runtime/builder/tests.rs`.

use mentra::{
    ContentBlock,
    test::{MockRuntime, MockToolCall},
};

use crate::context::{ContextDocument, ContextScope};
use crate::memory::{Memory, MemoryKind};

use super::*;

/// The agent config a workspace that said nothing produces, with the snapshot
/// directory pinned so no test can name a real one.
fn defaults(workspace: &Path, context: &WorkspaceContext) -> mentra::agent::AgentConfig {
    configured(workspace, context, None)
}

/// `defaults`, with the host's say over the prompt.
fn configured(
    workspace: &Path,
    context: &WorkspaceContext,
    system_prompt: Option<&SystemPrompt>,
) -> mentra::agent::AgentConfig {
    remembering(workspace, context, system_prompt, &[])
}

/// `configured`, with the index block `memories` render.
fn remembering(
    workspace: &Path,
    context: &WorkspaceContext,
    system_prompt: Option<&SystemPrompt>,
    memories: &[Memory],
) -> mentra::agent::AgentConfig {
    with_roster(
        workspace,
        context,
        system_prompt,
        memories,
        ToolRoster::default(),
    )
}

/// `remembering`, with the roster stated explicitly instead of defaulted —
/// what item (d) of decision D3 needs to pin: the rendered prompt does not
/// consult the roster at all.
fn with_roster(
    workspace: &Path,
    context: &WorkspaceContext,
    system_prompt: Option<&SystemPrompt>,
    memories: &[Memory],
    roster: ToolRoster,
) -> mentra::agent::AgentConfig {
    agent_config(
        workspace,
        context,
        system_prompt,
        crate::memory::index_block(memories).as_deref(),
        roster,
        Compaction::default(),
        PathBuf::from("/transcripts"),
    )
}

/// One memory, for the prompt tests below.
fn deploy_memories() -> Vec<Memory> {
    vec![Memory {
        name: "deploy-notes".to_string(),
        description: "how deploys go out".to_string(),
        kind: MemoryKind::Project,
        path: PathBuf::from("/mem/deploy-notes.md"),
        scope: ContextScope::Global,
    }]
}

/// One workspace document, for the prompt tests below.
fn house_rules() -> WorkspaceContext {
    WorkspaceContext::from_documents(vec![ContextDocument {
        path: PathBuf::from("/repo/AGENTS.md"),
        scope: ContextScope::Workspace,
        content: "house rules".to_string(),
    }])
}

#[test]
fn context_becomes_the_system_prompt_and_the_workspace_is_scoped() {
    let agent = defaults(Path::new("/repo"), &house_rules());

    assert!(
        agent
            .system
            .expect("a system prompt")
            .contains("house rules")
    );
    assert_eq!(agent.workspace.base_dir, PathBuf::from("/repo"));
}

#[test]
fn an_empty_workspace_context_leaves_the_system_prompt_unset() {
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    assert_eq!(agent.system, None);
}

#[test]
fn a_fresh_builder_says_nothing_about_the_system_prompt() {
    // The seam is not a default: basis ships no system prompt of its own, and
    // an unset knob has to leave the discovery-only prompt byte-identical.
    assert_eq!(WorkspaceBuilder::new("/repo").system_prompt, None);
    assert_eq!(
        configured(Path::new("/repo"), &house_rules(), None).system,
        house_rules().render()
    );
}

#[test]
fn an_appended_prompt_comes_after_the_workspace_because_it_is_more_specific() {
    // The rendered block tells the model later blocks take precedence, and the
    // host's text is the statement of the program running this agent — which no
    // repository can know about, and none should be able to overrule.
    let agent = configured(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Append("answer in Chinese".to_string())),
    );

    let system = agent.system.expect("a system prompt");
    let context = system.find("house rules").expect("the workspace's say");
    let host = system.find("answer in Chinese").expect("the host's say");
    assert!(context < host, "{system}");
}

#[test]
fn an_appended_prompt_stands_alone_when_the_workspace_says_nothing() {
    let agent = configured(
        Path::new("/repo"),
        &WorkspaceContext::default(),
        Some(&SystemPrompt::Append("answer in Chinese".to_string())),
    );

    assert_eq!(agent.system.as_deref(), Some("answer in Chinese"));
}

#[test]
fn a_replaced_prompt_drops_the_context_block_entirely() {
    let agent = configured(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Replace(
            "you are Acme's reviewer".to_string(),
        )),
    );

    assert_eq!(agent.system.as_deref(), Some("you are Acme's reviewer"));
}

#[test]
fn appending_nothing_leaves_the_workspaces_prompt_as_it_was() {
    // One obvious meaning, and it is the one the workspace already had.
    let agent = configured(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Append("   \n".to_string())),
    );

    assert_eq!(agent.system, house_rules().render());
}

#[test]
fn replacing_with_nothing_is_how_a_host_asks_for_no_system_prompt() {
    // Not an error: "the prompt is nothing" is a thing a host can mean, and it
    // renders as `None` for the same reason an empty workspace does.
    let agent = configured(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Replace(String::new())),
    );

    assert_eq!(agent.system, None);
}

#[test]
fn the_last_system_prompt_said_is_the_one_that_holds() {
    // One field, so replace-then-append is append rather than both — and the
    // enum is what makes "both" unspellable instead of undefined.
    let builder = WorkspaceBuilder::new("/repo")
        .with_system_prompt(SystemPrompt::Replace("first".to_string()))
        .with_system_prompt(SystemPrompt::Append("second".to_string()));

    assert_eq!(
        builder.system_prompt,
        Some(SystemPrompt::Append("second".to_string()))
    );
}

#[test]
fn the_memory_index_lands_after_the_context_documents() {
    // The index is workspace data, so it goes where the rendered block's
    // preamble puts the more specific statement — after the documents.
    let agent = remembering(Path::new("/repo"), &house_rules(), None, &deploy_memories());

    let system = agent.system.expect("a system prompt");
    let context = system.find("house rules").expect("the workspace's say");
    let index = system.find("<memories>").expect("the index");
    assert!(context < index, "{system}");
    assert!(system.contains("deploy-notes — how deploys go out"));
}

#[test]
fn a_replaced_prompt_drops_the_memory_index_too() {
    // `Replace` is the whole prompt; the index rides the same render path as
    // the documents, so it goes with them rather than surviving them.
    let agent = remembering(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Replace(
            "you are Acme's reviewer".to_string(),
        )),
        &deploy_memories(),
    );

    assert_eq!(agent.system.as_deref(), Some("you are Acme's reviewer"));
}

#[test]
fn a_hosts_append_still_lands_after_the_memory_index() {
    // The host's text stays the most specific statement — nothing on disk,
    // memories included, may outrank the program actually running this agent.
    let agent = remembering(
        Path::new("/repo"),
        &house_rules(),
        Some(&SystemPrompt::Append("answer in Chinese".to_string())),
        &deploy_memories(),
    );

    let system = agent.system.expect("a system prompt");
    let index = system.find("<memories>").expect("the index");
    let host = system.find("answer in Chinese").expect("the host's say");
    assert!(index < host, "{system}");
}

#[test]
fn no_memories_leave_the_prompt_byte_identical() {
    // Zero memories, zero cost: a workspace that never heard of the
    // convention renders exactly the prompt it always did.
    let agent = remembering(Path::new("/repo"), &house_rules(), None, &[]);

    assert_eq!(agent.system, house_rules().render());
}

#[test]
fn memories_alone_still_make_a_prompt() {
    // An empty workspace with memories is not an empty prompt: the index is
    // workspace data even when no document is.
    let agent = remembering(
        Path::new("/repo"),
        &WorkspaceContext::default(),
        None,
        &deploy_memories(),
    );

    let system = agent.system.expect("a system prompt");
    assert!(system.starts_with("<memories>"), "{system}");
}

#[test]
fn the_memory_index_renders_whatever_the_roster_says() {
    // Item (d) of decision D3: the roster decides which tools the model may
    // call, not what the system prompt says. A roster narrowed to `spawn`
    // alone still ships the same memory index a default roster would — the
    // prompt is `WorkspaceContext` and `memory::index_block`, assembled with
    // no view of `ToolRoster` at all.
    let agent = with_roster(
        Path::new("/repo"),
        &house_rules(),
        None,
        &deploy_memories(),
        ToolRoster::only([crate::tools::SPAWN]),
    );

    let system = agent.system.expect("a system prompt");
    assert!(system.contains("<memories>"), "{system}");
    assert!(system.contains("deploy-notes — how deploys go out"));
    assert!(
        !agent.tool_profile.allows("read"),
        "the roster still narrows what the model may actually call"
    );
}

#[tokio::test]
async fn the_memory_index_reaches_the_provider_request() {
    // The block is prompt, not schema: nothing on the event stream names it,
    // so the place to assert it is the request the provider is actually sent.
    let mock = MockRuntime::builder()
        .model("mock-model", "openai")
        .text("noted")
        .build()
        .expect("the mock runtime builds");
    let config = remembering(Path::new("/repo"), &house_rules(), None, &deploy_memories());
    let mut session = mock
        .runtime()
        .create_session_with_config("memories", mock.model(), config)
        .expect("session");

    session
        .append_turn(vec![ContentBlock::Text {
            text: "hello".to_string(),
        }])
        .await
        .expect("the scripted turn runs");

    let requests = mock.recorded_requests().await;
    let system = requests
        .last()
        .and_then(|request| request.system.as_deref())
        .expect("a system prompt reached the provider");
    assert!(system.contains("<memories>"), "{system}");
    assert!(system.contains("deploy-notes — how deploys go out"));
    assert!(
        system.find("house rules") < system.find("<memories>"),
        "the index comes after the documents: {system}"
    );
}

#[test]
fn the_two_doors_spawn_replaces_leave_the_roster() {
    // ADR-0016. Left alongside `spawn` they would restore what it removed:
    // three names arriving at one approval gate, and three rule namespaces,
    // for a question an operator asks once.
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    for replaced in ["shell", "background_run", "task"] {
        assert!(
            !agent.tool_profile.allows(replaced),
            "{replaced} is still offered to the model"
        );
    }
    assert!(
        agent.tool_profile.allows(crate::tools::SPAWN),
        "the door that replaces them has to be open"
    );
}

#[test]
fn hiding_is_a_roster_fact_and_not_a_capability_one() {
    // What lets `spawn` still reach the command executor underneath: nothing
    // here is an allow-list, so the tools stay registered on the runtime and
    // only stop being *offered*. A profile that named an allowed set instead
    // would take the capability away with the listing.
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    assert_eq!(
        agent.tool_profile.allowed_tools, None,
        "an allow-list here would silently drop every tool nobody thought to name"
    );
    assert!(agent.tool_profile.allows("read"));
}

#[tokio::test]
async fn the_default_roster_is_exactly_this() {
    // The whole visible set, from the runtime's own registry rather than from
    // a list written here, so a tool mentra adds upstream arrives as a failing
    // test instead of as a silent new door. Adding a name to this list is a
    // decision to offer it; removing one is a decision to stop.
    //
    // Sorted, because `mentra::Runtime::tools` sorts by name.
    let runtime = crate::runtime::Runtime::builder()
        .with_base_url("http://127.0.0.1:1/v1")
        .with_api_key("test-key")
        .with_ephemeral_history()
        .build()
        .expect("builds offline");
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    let offered = runtime
        .mentra_runtime()
        .tools()
        .into_iter()
        .map(|tool| tool.provider.name)
        .filter(|name| agent.tool_profile.allows(name))
        .collect::<Vec<_>>();

    assert_eq!(
        offered,
        [
            // mentra's compaction intrinsic.
            "compact",
            // mentra's split file tools (`RuntimeBuilder::with_file_tools`).
            "edit",
            "glob",
            "grep",
            "ls",
            "read",
            // basis's own, and ADR-0016's one door.
            crate::tools::SPAWN,
            "write",
        ],
        "the model's whole API, in one place"
    );

    // Registered later rather than at build — mentra registers `load_skill`
    // when a skill loader is installed — so it cannot appear above, and its
    // visibility is asserted here instead. Skills are basis's own convention;
    // this tool is how one is loaded.
    assert!(agent.tool_profile.allows("load_skill"));
}

#[test]
fn the_doors_basis_does_not_surface_stay_shut() {
    // Named individually rather than read off `UNSURFACED_TOOLS`, because a
    // test that reads the constant it is checking asserts nothing. Each of
    // these fails a different way and none of the failures is visible to the
    // person running the agent — see the constant for which is which.
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    for shut in [
        // A second delegation door beside `spawn`, which is what ADR-0016
        // removed `task` for; plus the yield back to a teammate loop basis
        // never starts, which on a basis run just ends the turn.
        "team_spawn",
        "team_send",
        "team_read_inbox",
        "team_broadcast",
        "team_request",
        "team_respond",
        "team_list_requests",
        "idle",
        // A board nothing in basis reads: every call succeeds and nothing
        // observable happens.
        "task_create",
        "task_claim",
        "task_update",
        "task_list",
        "task_get",
        // Reports on `background_run`, which ADR-0016 hid.
        "check_background",
        // A store basis decided against (D2): recall is off, and a tool
        // writing where nothing reads would report success into a void.
        "memory_pin",
        "memory_forget",
        "memory_search",
    ] {
        assert!(
            !agent.tool_profile.allows(shut),
            "{shut} is still offered to the model"
        );
    }
}

#[test]
fn commands_are_available_unless_the_caller_says_otherwise() {
    // ADR-0013: the first `basis "run the tests"` has to work.
    assert!(WorkspaceBuilder::new("/repo").shell.is_granted());
}

#[test]
fn discovery_is_enabled_until_the_caller_turns_it_off() {
    assert!(WorkspaceBuilder::new("/repo").discovery_enabled);
    assert!(
        !WorkspaceBuilder::new("/repo")
            .without_discovery()
            .discovery_enabled
    );
}

#[test]
fn discovery_stays_off_after_later_discovery_setters() {
    let builder = WorkspaceBuilder::new("/repo")
        .without_discovery()
        .with_context(ContextConfig::default())
        .with_config(Config::default())
        .with_skills(SkillsConfig::default())
        .with_memory(MemoryConfig::default())
        .with_templates(TemplatesConfig::default())
        .with_hooks(HooksConfig::default())
        .with_tools(ToolsConfig::default());
    #[cfg(feature = "mcp")]
    let builder = builder.with_mcp(McpConfig::default());

    assert!(
        !builder.discovery_enabled,
        "a later source-specific setter must not reactivate discovery"
    );
}

#[test]
fn a_fresh_builder_carries_a_private_default_runtime_and_an_inherited_model() {
    // The sugar's shape: `Workspace::open(path)` must behave as it always
    // has, so the default source is a default private recipe — and the model
    // is *unsaid*, deferring to that runtime's policy.
    let builder = WorkspaceBuilder::new("/repo");

    assert!(matches!(builder.runtime, RuntimeSource::Private(_)));
    assert!(matches!(builder.model, WorkspaceModel::Inherited));
}

#[test]
fn builders_return_new_values() {
    let base = WorkspaceBuilder::new("/repo");
    let derived = base.with_model(ModelSelector::Id("pinned".to_string()));

    assert!(matches!(
        derived.model,
        WorkspaceModel::Selector(ModelSelector::Id(ref id)) if id == "pinned"
    ));
    assert!(matches!(
        WorkspaceBuilder::new("/repo").model,
        WorkspaceModel::Inherited
    ));
}

#[test]
fn model_inputs_are_mutually_exclusive_and_last_call_wins() {
    let mut resolved = ModelInfo::new("resolved", "openai").with_context_window(200_000);
    resolved.display_name = Some("Resolved model".to_string());

    let selector_last = WorkspaceBuilder::new("/repo")
        .with_resolved_model(resolved.clone())
        .with_model(ModelSelector::Id("selected".to_string()));
    assert!(matches!(
        selector_last.model,
        WorkspaceModel::Selector(ModelSelector::Id(ref id)) if id == "selected"
    ));

    let resolved_last = WorkspaceBuilder::new("/repo")
        .with_model(ModelSelector::Id("selected".to_string()))
        .with_resolved_model(resolved.clone());
    assert!(matches!(
        resolved_last.model,
        WorkspaceModel::Resolved(ref held) if held == &resolved
    ));
}

#[tokio::test]
async fn a_shared_runtime_is_the_one_the_workspace_borrows() {
    let runtime = Arc::new(
        crate::runtime::Runtime::builder()
            .with_base_url("http://127.0.0.1:1/v1")
            .with_api_key("test-key")
            .with_ephemeral_history()
            .build()
            .expect("builds offline"),
    );

    let builder = WorkspaceBuilder::new("/repo").with_runtime(Arc::clone(&runtime));

    match &builder.runtime {
        RuntimeSource::Shared(held) => assert!(
            Arc::ptr_eq(held, &runtime),
            "borrowing must not clone the substrate"
        ),
        RuntimeSource::Private(_) | RuntimeSource::Reusable(_) => {
            panic!("with_runtime must switch the source")
        }
    }
}

#[tokio::test]
async fn reusable_rebuild_refuses_an_unexpected_basis_runtime_owner() {
    let root = tempfile::tempdir().expect("workspace");
    let mut definition = mentra::provider_core::responses::openai_definition();
    definition.descriptor.id = mentra::ProviderId::new("reusable-owner-test");
    definition.base_url = Some("http://127.0.0.1:1/".to_string());
    let seed = mentra::provider_core::responses::ResponsesProvider::new(
        definition,
        mentra::provider_core::StaticCredentialSource::new("test-key"),
    );
    let recipe = RuntimeBuilder::default()
        .with_reusable_registered_provider(
            "reusable-owner-test",
            move || Ok::<_, std::io::Error>(seed.fresh_session_scope()),
            |_provider| async { Ok::<_, std::io::Error>(()) },
        )
        .with_ephemeral_history()
        .into_reusable_recipe()
        .expect("recipe");
    let workspace = Workspace::builder(root.path())
        .with_runtime_recipe(recipe)
        .without_discovery()
        .fresh_only()
        .with_resolved_model(ModelInfo::new("test-model", "reusable-owner-test"))
        .with_tool_roster(ToolRoster::only(std::iter::empty::<String>()))
        .open()
        .await
        .expect("workspace opens")
        .bind_host_tools(Vec::new())
        .expect("tool-free checkout binds");

    let unexpected_owner = Arc::clone(&workspace.runtime);
    let error = workspace
        .rebuild_for_reuse()
        .await
        .expect_err("the extra Basis runtime owner must consume the pool entry");
    assert!(matches!(error, RunError::ReusableRuntimeNotUnique));
    drop(unexpected_owner);
}

#[test]
fn a_builder_holding_a_credentialed_recipe_does_not_print_it() {
    // WorkspaceBuilder's Debug delegates to the recipe's, which redacts; this
    // pins that the delegation actually happens.
    let printed = format!(
        "{:?}",
        WorkspaceBuilder::new("/repo").with_runtime_builder(
            crate::runtime::RuntimeBuilder::default().with_api_key("sk-secret-value")
        )
    );

    assert!(!printed.contains("sk-secret-value"), "{printed}");
    assert!(printed.contains("redacted"), "{printed}");
}

#[test]
fn an_unconfigured_workspace_keeps_every_tool_result() {
    let agent = defaults(Path::new("/repo"), &WorkspaceContext::default());

    assert_eq!(
        agent.compaction.keep_recent_tool_results,
        usize::MAX,
        "mentra's off switch for micro-compaction is what basis defaults to"
    );
    assert_eq!(agent.compaction.auto_compact_threshold_tokens, Some(50_000));
    assert_eq!(agent.compaction.projected_tool_result_budget, None);
}

#[test]
fn what_a_host_says_about_compaction_is_what_the_agent_carries() {
    let compaction = Compaction::default()
        .with_keep_recent_tool_results(Some(2))
        .with_auto_threshold_tokens(Some(400_000))
        .with_preserve_recent_user_tokens(1_000);

    let agent = agent_config(
        Path::new("/repo"),
        &WorkspaceContext::default(),
        None,
        None,
        ToolRoster::default(),
        compaction,
        PathBuf::from("/transcripts"),
    );

    assert_eq!(agent.compaction.keep_recent_tool_results, 2);
    assert_eq!(agent.compaction.projected_tool_result_budget, None);
    assert_eq!(
        agent.compaction.auto_compact_threshold_tokens,
        Some(400_000)
    );
    assert_eq!(agent.compaction.preserve_recent_user_tokens, 1_000);
}

#[test]
fn with_compaction_returns_a_new_builder() {
    let base = WorkspaceBuilder::new("/repo");
    let derived =
        base.with_compaction(Compaction::default().with_keep_recent_tool_results(Some(1)));

    assert_eq!(
        derived.compaction,
        Compaction::default().with_keep_recent_tool_results(Some(1))
    );
    assert_eq!(
        WorkspaceBuilder::new("/repo").compaction,
        Compaction::default(),
        "a fresh builder keeps everything"
    );
}

/// A workspace holding five files a model would read before it edits anything,
/// each one long enough to be worth eliding — mentra blanks a tool result over
/// 100 bytes — and each carrying a marker no other file has.
fn workspace_of_five_files() -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("tempdir");

    for index in 1..=5 {
        std::fs::write(
            dir.path().join(format!("file{index}.txt")),
            format!("marker-{index} {}", "x".repeat(200)),
        )
        .expect("write file");
    }

    dir
}

/// One tool-calling turn per file, then the sentence the model ends on.
fn reading_all_five() -> MockRuntime {
    let mut builder = MockRuntime::builder().model("mock-model", "openai");

    for index in 1..=5 {
        builder = builder.tool_calls(vec![
            MockToolCall::new(
                "files",
                serde_json::json!({
                    "operations": [{
                        "op": "read",
                        "path": format!("file{index}.txt"),
                    }],
                }),
            )
            .with_id(format!("read-{index}")),
        ]);
    }

    builder
        .text("read them all")
        .build()
        .expect("the mock runtime builds")
}

#[tokio::test]
async fn every_tool_result_the_model_read_is_still_in_front_of_it() {
    // The invariant this pins. A finite `keep_recent_tool_results` replaces
    // older bodies with `[Previous: used files]` on the way to the provider,
    // at any context size and on any model. Mentra now reports that projection,
    // but the model still loses the body. Basis keeps every result unless a
    // host asks for elision by number.
    let workspace = workspace_of_five_files();
    let mock = reading_all_five();
    let mut session = mock
        .runtime()
        .create_session_with_config(
            "compaction",
            mock.model(),
            defaults(workspace.path(), &WorkspaceContext::default()),
        )
        .expect("session");

    session
        .append_turn(vec![ContentBlock::Text {
            text: "read every file".to_string(),
        }])
        .await
        .expect("the scripted turn runs");

    let requests = mock.recorded_requests().await;
    let last = requests.last().expect("the model was asked something");
    let results: Vec<String> = last
        .messages
        .iter()
        .flat_map(|message| &message.content)
        .filter_map(|block| match block {
            ContentBlock::ToolResult { content, .. } => Some(content.to_display_string()),
            _ => None,
        })
        .collect();

    assert_eq!(results.len(), 5, "five reads, five results");
    for (index, result) in results.iter().enumerate() {
        assert!(
            result.contains(&format!("marker-{}", index + 1)),
            "the model can no longer see what it read: {result}"
        );
    }
}