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
//! Asking before the agent does something consequential.
//!
//! Two pieces, and only two. [`ApprovalGate`] is the tool authorizer basis
//! installs on every runtime; it answers one question — *is this call worth
//! asking about* — and puts every call where the answer is yes to whoever is
//! answering. [`Approver`] is whoever that is, and it is the only thing that
//! decides.
//!
//! There was a third piece until ADR-0010: an `ApprovalPolicy` enum the core
//! interpreted, whose three values were three trait impls in disguise. Two of
//! them ship here — [`AllowAll`] and [`DenyAll`] — and the third, asking a
//! person, lives where the terminal is: `basis-acp` supplies an approver that
//! asks the client, and the binary one that asks at a TTY (ADR-0011). What the
//! enum could never express, the trait can: allow edits but deny the network,
//! ask over Slack with a timeout, escalate after the third refusal.
//!
//! [`DenyAllGate`] is [`DenyAll`] restated as an authorizer, and not a fourth
//! piece: a refusal is the one answer an approver cannot make stick on its own,
//! because a remembered rule resolves the gate's `Prompt` before any approver
//! is consulted. A run that must refuse installs both, saying the same thing in
//! the same words at the two layers that get asked.
//!
//! The first of those is the one this module has to make *writable* rather than
//! merely describable, and it is written on [`Approver`]. It reads
//! [`ApprovalRequest::side_effect_level`] and names no tool, which is the whole
//! point: a policy spelled as a list of tool names is a policy that silently
//! stops covering the next MCP server a workspace connects.
//!
//! Nothing installs an approver by default, and that is deliberate: with no
//! approver the run gets [`AllowAll`], which is what a headless run needs.
//! Anything stricter is one argument to
//! [`run_with_approver`](crate::run::run_with_approver).
use Duration;
use async_trait;
use Value;
/// How far outside this process a call reaches: nothing, this machine's state,
/// another process, or the world.
///
/// mentra's, deliberately, and re-exported here under the rule written on
/// [`CancellationToken`](crate::CancellationToken) — every mentra type basis's
/// surface makes a caller *name*, basis re-exports. Both
/// [`is_consequential`] and [`ApprovalRequest::side_effect_level`] ask an
/// approver to name it, and without this line writing the policy those exist
/// for would mean adding mentra to the host's own manifest, pinned to whatever
/// version basis happens to resolve.
pub use ToolSideEffectLevel;
/// Everything writing a [`ToolAuthorizer`] of one's own makes a caller name.
///
/// The same rule as [`ToolSideEffectLevel`] above, applied to the seam
/// [`PreparedRun::with_tool_authorizer`](crate::PreparedRun::with_tool_authorizer)
/// opened: a host that installs an authorizer on a live session has to spell
/// the trait, the request it is handed, the decision it returns and the error
/// it may return instead, and none of those should cost it a mentra dependency
/// of its own — pinned to whatever version basis happens to resolve.
///
/// These stay in this module rather than joining [`ApprovalGate`] at the crate
/// root. `RuntimeError` is mentra's runtime error and cannot sit unqualified
/// beside basis's own [`RunError`](crate::RunError) without the two reading as
/// a pair, and the five are only meaningful together — an authorizer names all
/// of them or none.
pub use ;
/// What the agent wants to do, as put to an [`Approver`].
///
/// Deliberately not `#[non_exhaustive]`, though hosts read it far more often
/// than they build one. The struct has no constructor and no builder, so
/// sealing it would make an `ApprovalRequest` *unconstructable* outside this
/// crate — and every host testing its own approver builds one, as `basis-acp`
/// and `basis-cli` both do. Sealing would trade a compile error that names the
/// new field, on the day a field is added, for a permanent one with no way past
/// it.
/// What an [`Approver`] decided.
///
/// # How long "for the session" lasts
///
/// A `…ForSession` answer is remembered against the conversation and answers
/// every later call of that tool on the **live session**: further calls in the
/// same turn, and further runs on the same [`PreparedRun`](crate::PreparedRun)
/// in the same process. It dies at the next attach — resuming the
/// conversation ([`Workspace::resume`](crate::Workspace)) starts it with none
/// remembered, so a later process that picks the conversation back up asks
/// again rather than replaying an answer nobody in that process gave.
///
/// That boundary is inherent now, not enforced. mentra 0.27's
/// `PermissionRuleScope::Process` (mentra#53) is what basis remembers a
/// `…ForSession` answer into: a rung owned by the live session's own
/// permission handle, never written to the runtime store, so a resumed
/// session — a fresh handle, even for the same conversation — simply starts
/// with an empty one. Nothing basis runs has to clear it. (One resume-time
/// clear does still exist, in `Runtime::resume_minted`, but it is a one-way
/// migration for rows a pre-0.12 basis binary remembered into the durable
/// `Session` scope before this change; it plays no part in the guarantee
/// documented here for an answer given today.)
/// How an [`Approver`] answered: the decision, and — when it refused — why.
///
/// The reason is not decoration. A denial reaches the model as that tool
/// call's result, so the wording is the only thing telling it what to do
/// next: a model told merely that something was denied tries the write
/// again, and one told this run does not allow writes stops and reports.
/// An answer that leaves it unset still denies; the model just reads
/// mentra's standing "denied by session approver" instead.
///
/// Allowing needs no reason, because an allowed call explains itself by
/// happening.
/// Answers approval requests. The seam a host plugs its own judgment into.
///
/// Called from the event-forwarding task while the turn is blocked inside
/// mentra waiting, so an implementation must answer rather than defer to
/// something that only happens after the run.
///
/// Async because answering genuinely takes time and the caller is an async
/// task: an ACP approver awaits a round trip to the client, and a terminal one
/// waits on a person. A synchronous signature would force both to block a
/// runtime worker thread — which tokio rejects outright for the ACP case. The
/// attribute to spell an impl with is re-exported at the crate root —
/// [`async_trait`](crate::async_trait) — so it costs no manifest line of the
/// host's own.
///
/// # Fail closed
///
/// **An approver that cannot answer denies.** No terminal to ask at, an answer
/// that never came, a channel whose other end is gone: none of those is
/// consent, and the only calls that reach an approver are the ones that change
/// something outside this process.
///
/// The worked example is the binary's `TerminalApprover`. Asked when stdin is
/// not a terminal — an unattended `basis spawn --approve prompt`, a cron job — it
/// denies without printing a question nobody would read, so the run fails
/// visibly instead of quietly granting whatever came up. `basis-acp`'s client
/// approver applies the same rule to a failed round trip, a cancelled request,
/// an answer it cannot parse, and its own thirty-minute timeout.
///
/// Each of those denials should say which one it was, on the
/// [`reason`](ApprovalAnswer::reason) of its answer. Failing closed silently
/// leaves the model to guess, and it guesses that retrying will work.
///
/// [`ApprovalDecision`]'s own default is [`Deny`](ApprovalDecision::Deny) for
/// the same reason, and so is mentra's when an authorizer times out: silence is
/// never a yes.
///
/// # Allow edits, deny the network
///
/// The policy this module's own documentation has always named as the reason
/// the seam is a trait, written out. Nothing in it names a tool — every call is
/// judged by how far it reaches — so a workspace that connects a new MCP server
/// tomorrow, or ships a `.basis/tools.json` declaring a program, is covered by
/// the rule that was already there.
///
/// ```
/// use basis::{
/// ApprovalAnswer, ApprovalDecision, ApprovalRequest, Approver, ToolSideEffectLevel,
/// async_trait,
/// };
///
/// struct EditsButNotTheNetwork;
///
/// #[async_trait]
/// impl Approver for EditsButNotTheNetwork {
/// async fn approve(&mut self, request: &ApprovalRequest) -> ApprovalAnswer {
/// match request.side_effect_level {
/// // Changes this machine's state and nothing past it: the file
/// // tools, and a delegation to a subagent.
/// Some(ToolSideEffectLevel::LocalState) => ApprovalDecision::Allow.into(),
///
/// // Everything else. `Process` is a command, which can reach the
/// // network by running `curl`; `External` says so outright; and
/// // `None` is a level basis could not recover, which is judged by
/// // the most it could be rather than the least. `ToolSideEffectLevel::None`
/// // never arrives — a read is not put to an approver at all.
/// _ => ApprovalAnswer::new(ApprovalDecision::Deny)
/// .because("this run may change this checkout and nothing beyond it"),
/// }
/// }
/// }
/// ```
/// Forwards to the approver inside.
///
/// Lets a caller hold an approver it chose at runtime — one of several, or one
/// a feature flag picked — and still pass it to anything taking
/// `impl Approver`. The binary is exactly that caller: `--approve` names one of
/// three, and without this each arm would have to duplicate the whole run.
/// Approves everything. What a confined or headless run wants, and what a run
/// given no approver of its own gets.
;
/// Why a refusing run refused, in the words the model reads.
///
/// One function because two layers refuse for the same reason — [`DenyAllGate`]
/// before the call is surfaced at all, [`DenyAll`] for a call some other
/// authorizer surfaced anyway — and a model given two different explanations of
/// one prohibition would be reading a difference that is not there.
/// Refuses everything, so the agent can inspect a workspace and report on it
/// and cannot touch it. Each refusal reaches the model as a tool error, which
/// is how it learns to stop trying.
///
/// **An approver alone is not a refusal a durable rule cannot outrank**, for
/// the reason written on [`ApprovalGate`]: a remembered allow resolves the
/// gate's `Prompt` before an approver is ever consulted. A run whose refusal
/// is the whole point pairs this with [`DenyAllGate`], which states the same
/// refusal where mentra treats it as final.
;
/// [`DenyAll`]'s refusal, stated one layer down as a tool authorizer, where no
/// remembered rule can answer over it.
///
/// # Why the same refusal twice
///
/// [`ApprovalGate`] answers nothing: every consequential call comes back as a
/// `Prompt`, and mentra resolves a `Prompt` against the conversation's
/// remembered rules *before* the approver is consulted. For a policy choosing
/// between asking and allowing that ordering is only a host saying yes in
/// advance. For [`DenyAll`] it is a standing override — a Global- or
/// Project-scope allow seeded through
/// [`session()`](crate::PreparedRun::session)`.permission_handle()` answers
/// ahead of the refusal, and outlives everything a session-scoped mechanism
/// clears.
///
/// Installed with
/// [`PreparedRun::with_tool_authorizer`](crate::PreparedRun::with_tool_authorizer),
/// this refuses first instead. mentra returns an authorizer's `Deny`
/// unchanged: no rule is read, no `PermissionRequested` is emitted, and the
/// approver is never reached — so the model gets exactly the refusal
/// [`DenyAll`] would have given it, in the same words.
///
/// # What it still allows
///
/// A non-consequential call, outright — [`ApprovalGate`]'s
/// [`is_consequential`] filter, unchanged and deliberately so. A gate that
/// denied reads would stop a refusing run from doing the one thing it is for,
/// which is looking. The corollary [`ApprovalGate`] documents holds here too:
/// a rule remembered against a read-only tool is never consulted.
///
/// # Which runs want it
///
/// One whose refusal is fixed for its whole life — a headless `--approve
/// never` invocation, a task recorded to refuse — where there is no live mode
/// for a stateful gate to read. A host whose posture can *change* mid-session
/// wants one that reads that state per call instead; `basis-host`'s
/// `PolicyGate` is that, and installing this one would freeze the mode a
/// session opened in.
;
/// Whether a call changes anything outside this process.
///
/// Read-only calls are never worth asking about — prompting for them trains
/// people to approve without reading, which is worse than not asking.
/// Puts every consequential call to the [`Approver`], and lets the rest
/// through.
///
/// This is the runtime half of approval, installed as mentra's
/// `ToolAuthorizer`. It carries no policy: since ADR-0010 there is nothing left
/// for one to say, because the approver decides. What it still owns is the
/// filter — [`is_consequential`] — and the choice to *surface* rather than
/// answer, which is what turns a call into a `PermissionRequested` event and
/// blocks the turn until someone resolves it.
///
/// Installed even by a run that approves everything, and that is the point.
/// basis installs it when the runtime is built, and a runtime with no
/// authorizer at all allows every call unconditionally — no permission
/// request can ever be raised, and since mentra 0.26 not even a remembered
/// rule is read first. Surfacing unconditionally is what lets the answer be
/// chosen per turn — or changed mid-session, which is how an ACP client's
/// mode picker works at all.
///
/// # When a session replaces it
///
/// Surfacing everything has one cost, and it is the reason
/// [`PreparedRun::with_tool_authorizer`](crate::PreparedRun::with_tool_authorizer)
/// exists: a `Prompt` is resolved by a remembered rule *before* the approver
/// is consulted, so a refusal written on the approver can be pre-empted by a
/// durable allow someone seeded through the session's permission handle. For
/// a policy that chooses between asking and allowing that is only a host
/// saying yes in advance. For one that refuses outright it is a standing
/// override.
///
/// So a host with a refusal that must outrank a remembered answer installs it
/// as a *session* authorizer instead, where mentra treats a `Deny` as final.
/// Two shapes of that ship, for the two shapes of host:
///
/// - A posture fixed for the run's whole life installs [`DenyAllGate`]. The
/// attended CLI's `--approve never` and a task recorded to refuse both do,
/// beside the [`DenyAll`] they were already passing.
/// - A posture that can change mid-session installs one that reads the live
/// state per call. `basis-acp` does, on every session it opens, with
/// `basis-host`'s `PolicyGate`: read-only refuses, and every other mode is
/// this gate verbatim — `Allow` for a read, `Prompt` for everything else,
/// remembered rules and all.
///
/// Every other policy, and every run that installs nothing, is served by this
/// gate on the runtime exactly as before.
///
/// # The gate answers first, and its answers are final
///
/// mentra 0.26 samples the current authorizer once per call and treats
/// `Allow` and `Deny` as terminal; only a `Prompt` may be answered by a
/// remembered rule or forwarded to the approver. That is a deliberate
/// upstream security fix — a remembered allow can no longer bypass a session
/// switched to a stricter authorizer — and basis adopts it as documented
/// order: **rules and approver decide only what this gate surfaces.**
///
/// The corollary is loud because it is easy to miss: this gate answers
/// `Allow` — not `Prompt` — for a call with no side effects
/// ([`is_consequential`]), so **a rule remembered against a read-only tool is
/// never consulted, a seeded deny included**. The tool runs, with no error
/// and no event saying the rule was passed over. A host seeding rules through
/// the session permission handle must seed them for consequential tools only;
/// nothing basis documents promises deny-to-win on a non-consequential call,
/// and reads are deliberately never put to anyone (see [`is_consequential`]).