bamboo-sdk 2026.7.21

Ergonomic top-level Agent SDK facade for the Bamboo agent framework
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
//! Ergonomic top-level Agent SDK.
//!
//! A concise facade over the engine runtime: the caller supplies their own
//! instruction (a system-prompt fragment), a model, and an optional tool
//! policy; the engine assembles the complete system prompt around it at run
//! time. Library consumers can write:
//!
//! ```rust,ignore
//! use bamboo_agent::agent::Agent;
//!
//! let agent = Agent::builder()
//!     .model("claude-sonnet-4-6")
//!     .instruction("You help users research topics thoroughly.")
//!     .with_defaults_for_data_dir(data_dir).await?
//!     .build()?;
//!
//! let mut session = Session::new("s1", "claude-sonnet-4-6");
//! agent.run(&mut session, "investigate X").await?;
//! ```
//!
//! ## Surface
//!
//! - [`Agent`] — stable entry point wrapping the engine runtime. `run` /
//!   `run_stream` execute the agent loop with the configured instruction +
//!   tool policy + model applied to the session.
//! - [`AgentBuilder`] — concise builder (`.model()`, `.instruction()`,
//!   `.tools()`) that assembles default deps via
//!   [`AgentBuilder::with_defaults_for_data_dir`].
//! - [`ExecuteRequestBuilder`] — ergonomic builder over the multi-field
//!   [`bamboo_engine::ExecuteRequest`].
//! - [`ToolSpec`] + [`builtin_tool_names`] — tool
//!   descriptors derived from the canonical `BUILTIN_TOOL_NAMES`.
//!
//! ## Anti-fork invariant
//!
//! The SDK never reimplements the agent loop. `run` / `run_stream` funnel into
//! `bamboo_engine::Agent::execute` (the single canonical execution path).

mod builder;
mod error;
mod execute_request;
mod tools;

use std::sync::Arc;

use async_trait::async_trait;
pub use builder::AgentBuilder;
pub use execute_request::ExecuteRequestBuilder;
use tokio::sync::mpsc;

use bamboo_engine::session_app::errors::{SessionLoadError, SessionSaveError};
use bamboo_engine::session_app::repository::SessionAccess;
use bamboo_engine::session_app::respond::{
    submit_pending_response, PERMISSION_REEXECUTE_METADATA_KEY,
};
use bamboo_engine::session_app::types::RespondInput;

// Re-exported so callers can name the token returned by the `*_cancellable` /
// `*_with_cancel` run helpers without depending on `tokio-util` directly.
pub use tokio_util::sync::CancellationToken;
pub use tools::{
    builtin_tool_names, builtin_tool_specs, BuiltinTool, ToolSpec, CANONICAL_TOOL_NAMES,
};

pub use error::SdkError;

// Convenience re-exports of commonly used types (single source of truth — these
// supersede the old duplicate re-export chain, resolving TD-2).
pub use bamboo_agent_core::{
    AgentError, AgentEvent, Message, MessageContent, PendingQuestion, Role, Session,
    TokenBudgetUsage, TokenUsage,
};
pub use bamboo_domain::{TaskItem, TaskItemStatus, TaskList};
pub use bamboo_engine::session_app::respond::PlanModeTransition;
pub use bamboo_engine::ExecuteRequest;
pub use bamboo_llm::LLMProvider;
pub use bamboo_mcp::manager::McpServerManager;
pub use bamboo_mcp::McpServerConfig;
pub use bamboo_storage::SessionIndexEntry;
pub use bamboo_tools::permission::{PermissionChecker, PermissionType};
pub use bamboo_tools::{BuiltinToolExecutor, BuiltinToolExecutorBuilder, ToolOutputManager};

/// Default event-channel buffer used by [`Agent::run`].
const EVENT_CHANNEL_CAPACITY: usize = 256;

/// Stable, ergonomic entry point for agent execution.
///
/// Wraps a [`bamboo_engine::Agent`] (which owns the shared runtime) plus the
/// instruction / tool policy / model configured at build time. Clone is cheap.
#[derive(Clone)]
pub struct Agent {
    inner: bamboo_engine::Agent,
    /// Instruction (system-prompt fragment) injected into the session at `run`
    /// time; the engine assembles the full prompt around it.
    system_prompt: Option<String>,
    /// Model override applied to the session at `run` time.
    model: Option<String>,
    /// Concrete session-index handle, present only when assembled via
    /// [`AgentBuilder::with_defaults_for_data_dir`]. Backs
    /// [`list_sessions`](Self::list_sessions) — the type-erased
    /// `Arc<dyn Storage>` the engine builder takes can't list.
    session_store: Option<Arc<bamboo_storage::SessionStoreV2>>,
    /// Permission checker configured via
    /// [`AgentBuilder::permission_checker`], if any. Used by
    /// [`answer`](Self::answer) to apply permission grants implied by an
    /// approved permission prompt, mirroring what the HTTP `/respond` handler
    /// does for `state.permission_checker`.
    permission_checker: Option<Arc<dyn bamboo_tools::permission::PermissionChecker>>,
}

impl Agent {
    /// Return a new ergonomic builder.
    pub fn builder() -> AgentBuilder {
        AgentBuilder::new()
    }

    /// Wrap an existing engine [`Agent`](bamboo_engine::Agent) with no extra
    /// role configuration.
    pub fn from_runtime(inner: bamboo_engine::Agent) -> Self {
        Self {
            inner,
            system_prompt: None,
            model: None,
            session_store: None,
            permission_checker: None,
        }
    }

    /// Wrap an engine [`Agent`](bamboo_engine::Agent) plus the instruction /
    /// model configuration assembled by [`AgentBuilder`].
    pub(crate) fn from_runtime_with_config(
        inner: bamboo_engine::Agent,
        system_prompt: Option<String>,
        model: Option<String>,
        session_store: Option<Arc<bamboo_storage::SessionStoreV2>>,
        permission_checker: Option<Arc<dyn bamboo_tools::permission::PermissionChecker>>,
    ) -> Self {
        Self {
            inner,
            system_prompt,
            model,
            session_store,
            permission_checker,
        }
    }

    /// Run the agent loop on `session` with the given input, draining events
    /// internally until completion.
    ///
    /// The configured instruction + model are applied to the session before
    /// execution; the tool set was fixed on the agent's executor at build time.
    ///
    /// NOTE: this variant **discards every [`AgentEvent`]** (tool calls, tokens,
    /// intermediate errors) — you only get the final `Result`. To observe the
    /// run, use [`run_stream`](Self::run_stream) instead. To cancel a blocking
    /// run from another task, use [`run_with_cancel`](Self::run_with_cancel).
    pub async fn run(
        &self,
        session: &mut Session,
        input: impl Into<String>,
    ) -> Result<(), AgentError> {
        session.add_message(Message::user(input.into()));
        self.run_session(session).await
    }

    /// Like [`run`](Self::run) but driven by a caller-owned
    /// [`CancellationToken`]: cancelling the token from another task stops the
    /// loop at the next check point. Events are still discarded (see `run`).
    pub async fn run_with_cancel(
        &self,
        session: &mut Session,
        input: impl Into<String>,
        cancel_token: CancellationToken,
    ) -> Result<(), AgentError> {
        session.add_message(Message::user(input.into()));
        self.run_session_with_cancel(session, cancel_token).await
    }

    /// Run the agent loop on `session` exactly as it stands — i.e. on a
    /// caller-provided message list — without appending a new turn. The last
    /// `User` message already in the session drives execution.
    ///
    /// This is how you pass a full conversation / message list: build the
    /// session from your messages, then run it.
    ///
    /// ```rust,ignore
    /// let mut session = Session::new("s1", "claude-sonnet-4-6");
    /// session.add_message(Message::user("hi"));
    /// session.add_message(Message::assistant("hello!", None));
    /// session.add_message(Message::user("now summarize our chat"));
    /// agent.run_session(&mut session).await?; // no extra input appended
    /// ```
    pub async fn run_session(&self, session: &mut Session) -> Result<(), AgentError> {
        self.run_session_with_cancel(session, CancellationToken::new())
            .await
    }

    /// Like [`run_session`](Self::run_session) but driven by a caller-owned
    /// [`CancellationToken`], so a blocking run can be cancelled from another
    /// task. Events are still discarded (see [`run`](Self::run)).
    pub async fn run_session_with_cancel(
        &self,
        session: &mut Session,
        cancel_token: CancellationToken,
    ) -> Result<(), AgentError> {
        let (event_tx, mut event_rx) = mpsc::channel::<AgentEvent>(EVENT_CHANNEL_CAPACITY);

        // Drain events so the bounded channel never blocks the loop.
        let drain = tokio::spawn(async move { while event_rx.recv().await.is_some() {} });

        let result = self.execute_internal(session, event_tx, cancel_token).await;

        // Stop draining once execution returns. Detached engine tasks (e.g.
        // background evaluations) may still hold a cloned sender, so awaiting
        // natural channel closure could hang; abort instead.
        drain.abort();
        result
    }

    /// Append `input` as a new user turn, then stream the run's
    /// [`AgentEvent`]s. The execution runs on a background task; the caller
    /// drives it by reading from the returned receiver until it closes.
    pub fn run_stream(
        &self,
        mut session: Session,
        input: impl Into<String>,
    ) -> mpsc::Receiver<AgentEvent> {
        session.add_message(Message::user(input.into()));
        self.run_stream_session(session)
    }

    /// Like [`run_stream`](Self::run_stream), but also returns a
    /// [`CancellationToken`] for the run: call `token.cancel()` to stop the loop
    /// at the next check point. Dropping the receiver does NOT cancel the run, so
    /// this is the way to interrupt a streaming agent.
    pub fn run_stream_cancellable(
        &self,
        mut session: Session,
        input: impl Into<String>,
    ) -> (mpsc::Receiver<AgentEvent>, CancellationToken) {
        session.add_message(Message::user(input.into()));
        self.run_stream_session_cancellable(session)
    }

    /// Stream the run's [`AgentEvent`]s for a caller-provided message list,
    /// without appending a new turn (the last `User` message drives execution).
    pub fn run_stream_session(&self, session: Session) -> mpsc::Receiver<AgentEvent> {
        self.run_stream_session_with_cancel(session, CancellationToken::new())
    }

    /// Like [`run_stream_session`](Self::run_stream_session) but also returns the
    /// run's [`CancellationToken`] so the caller can interrupt it.
    pub fn run_stream_session_cancellable(
        &self,
        session: Session,
    ) -> (mpsc::Receiver<AgentEvent>, CancellationToken) {
        let cancel_token = CancellationToken::new();
        let rx = self.run_stream_session_with_cancel(session, cancel_token.clone());
        (rx, cancel_token)
    }

    /// Stream a caller-provided message list under a caller-owned
    /// [`CancellationToken`]. The shared entry point the other `run_stream*`
    /// helpers funnel into.
    pub fn run_stream_session_with_cancel(
        &self,
        mut session: Session,
        cancel_token: CancellationToken,
    ) -> mpsc::Receiver<AgentEvent> {
        let (event_tx, event_rx) = mpsc::channel::<AgentEvent>(EVENT_CHANNEL_CAPACITY);
        let agent = self.clone();

        tokio::spawn(async move {
            if let Err(error) = agent
                .execute_internal(&mut session, event_tx, cancel_token)
                .await
            {
                tracing::warn!("Agent::run_stream execution failed: {error}");
            }
        });

        event_rx
    }

    /// Escape hatch for full per-request control: run a fully-specified
    /// [`ExecuteRequest`] (split fast/background/summarization models, provider
    /// handle, skill selection, custom event channel, cancellation token, …) on
    /// `session` via the single canonical engine execution path — the same path
    /// [`run`](Self::run) / [`run_stream`](Self::run_stream) funnel into.
    ///
    /// Unlike `run`/`run_stream`, this does NOT apply the builder's configured
    /// instruction or model: the caller owns the request entirely. Build it with
    /// [`ExecuteRequestBuilder`].
    ///
    /// ```rust,ignore
    /// let (tx, _rx) = tokio::sync::mpsc::channel(256);
    /// let req = ExecuteRequestBuilder::new("investigate X", tx, Default::default())
    ///     .model("claude-sonnet-4-6")
    ///     .build();
    /// agent.execute(&mut session, req).await?;
    /// ```
    pub async fn execute(
        &self,
        session: &mut Session,
        request: ExecuteRequest,
    ) -> Result<(), AgentError> {
        self.inner.execute(session, request).await
    }

    /// Shared execution path: prepare the session (system prompt + model), build
    /// the [`ExecuteRequest`], and delegate to the canonical engine execution
    /// path. Tool restriction is applied via the agent's executor (built time).
    async fn execute_internal(
        &self,
        session: &mut Session,
        event_tx: mpsc::Sender<AgentEvent>,
        cancel_token: CancellationToken,
    ) -> Result<(), AgentError> {
        // If `answer()` just approved a gated tool call, `session.metadata` carries
        // the re-execution marker `submit_pending_response` set — the gated tool
        // never actually ran (the permission gate intercepted it before
        // execution), so re-run it now for real and write the genuine output back
        // before the loop resumes. No-op when the marker is absent (the common,
        // non-permission path), so this is safe to run unconditionally on every
        // entry into the loop, not just `resume`. See
        // `reexecute_approved_tool_if_pending` for the full rationale.
        self.reexecute_approved_tool_if_pending(session, &event_tx)
            .await;

        // Apply the instruction as the session's leading System message and set
        // the configured model via the single authoritative pre-execution
        // mutation point. The builder's prompt is AUTHORITATIVE: it replaces a
        // leading System message, otherwise inserts one at index 0, so a
        // caller-supplied session can't silently shadow the configured
        // instruction.
        bamboo_engine::session_app::execution_prep::prepare_session_for_execution(
            session,
            self.system_prompt.as_deref(),
            self.model.as_deref(),
        );

        // The last user message in the session drives execution (the engine
        // skips echoing `initial_message`, so we surface it for logging only).
        let initial_message = session
            .messages
            .iter()
            .rev()
            .find(|m| matches!(m.role, Role::User))
            .map(|m| m.content.clone())
            .unwrap_or_default();

        // Tool restriction is handled at build time: the agent's executor is
        // built from exactly the configured tool set, so no per-run
        // `disabled_tools` filter is needed here.
        let mut builder = ExecuteRequestBuilder::new(initial_message, event_tx, cancel_token);
        if let Some(model) = self.model.clone() {
            builder = builder.model(model);
        }

        self.inner.execute(session, builder.build()).await
    }

    /// Port of `bamboo-server`'s `resume_adapter.rs` re-execution logic: after
    /// [`answer`](Self::answer) approves a permission prompt,
    /// `submit_pending_response` stamps `session.metadata` with
    /// [`PERMISSION_REEXECUTE_METADATA_KEY`] (the approved tool call's id) — the
    /// gated tool was intercepted BEFORE it ran, so its recorded result is only
    /// the synthetic "Selected response: Approve" placeholder. This re-runs the
    /// original tool call for real, against the SAME executor the loop itself
    /// uses ([`bamboo_engine::Agent::default_tools`]), and overwrites the
    /// placeholder tool-result message with the genuine output — so the resumed
    /// loop sees what the operation actually did instead of inferring it.
    ///
    /// Emits the same `ToolStart`/`ToolComplete` (or `ToolError`) lifecycle
    /// events onto `event_tx` that a normal dispatch would, so a streaming
    /// consumer sees the re-run tool card update exactly like the HTTP surface
    /// does. Best-effort persists the updated session via
    /// [`persistence`](Self::persistence) so the real output survives even if the
    /// process stops before the loop's own next save — logged, not propagated,
    /// since the loop's subsequent save will also capture it.
    ///
    /// No-op (returns immediately) when the marker is absent, so it is safe to
    /// call unconditionally at the top of every execution, not just resumes.
    async fn reexecute_approved_tool_if_pending(
        &self,
        session: &mut Session,
        event_tx: &mpsc::Sender<AgentEvent>,
    ) {
        let Some(tool_call_id) = session.metadata.remove(PERMISSION_REEXECUTE_METADATA_KEY) else {
            return;
        };

        let Some(tool_call) = find_pending_tool_call(session, &tool_call_id) else {
            tracing::warn!(
                session_id = %session.id,
                tool_call_id = %tool_call_id,
                "Permission re-exec marker set but tool call not found in history"
            );
            return;
        };

        let executor = self.inner.default_tools();
        let tool_name = tool_call.function.name.clone();
        let is_mutating = bamboo_tools::orchestrator::classify_tool(&tool_name)
            == bamboo_tools::orchestrator::ToolMutability::Mutating;

        // Frame the re-run with the same lifecycle events the normal loop emits
        // (via ToolEmitter) so a streaming consumer's tool card updates
        // (running -> finished) and ToolComplete carries the REAL output — raw
        // `execute_with_context` only streams tool tokens, not lifecycle.
        let mut emitter = bamboo_tools::ToolEmitter::new(&tool_call.id, &tool_name, is_mutating);
        emitter.set_auto_approved(true);
        let _ = event_tx
            .send(emitter.begin().clone().into_agent_event())
            .await;

        let exec_result = {
            let ctx = bamboo_agent_core::tools::ToolExecutionContext {
                session_id: Some(session.id.as_str()),
                tool_call_id: tool_call_id.as_str(),
                event_tx: Some(event_tx),
                available_tool_schemas: None,
                bypass_permissions: false,
                can_async_resume: false,
                bash_completion_sink: None,
                pre_parsed_args: None,
            };
            executor.execute_with_context(&tool_call, ctx).await
        };

        let (content, success) = match exec_result {
            Ok(tool_result) => {
                let _ = event_tx
                    .send(
                        emitter
                            .finish(Some("Re-executed after approval".to_string()))
                            .clone()
                            .into_agent_event(),
                    )
                    .await;
                let _ = event_tx
                    .send(AgentEvent::ToolComplete {
                        tool_call_id: tool_call.id.clone(),
                        result: tool_result.clone(),
                    })
                    .await;
                (tool_result.result, tool_result.success)
            }
            Err(error) => {
                let message = format!("Tool re-execution after approval failed: {error}");
                let _ = event_tx
                    .send(emitter.error(message.clone()).clone().into_agent_event())
                    .await;
                (message, false)
            }
        };

        tracing::info!(
            session_id = %session.id,
            tool_name = %tool_name,
            tool_call_id = %tool_call_id,
            success,
            "Re-executed approved tool after permission grant"
        );
        apply_tool_result(session, &tool_call_id, content, success);

        if let Err(error) = self.persistence().save_runtime_session(session).await {
            tracing::warn!(
                session_id = %session.id,
                %error,
                "Failed to persist session after tool re-execution (loop's own save will retry)"
            );
        }
    }

    /// Access the shared storage backend.
    pub fn storage(&self) -> &Arc<dyn bamboo_agent_core::storage::Storage> {
        self.inner.storage()
    }

    /// Access the runtime persistence adapter.
    pub fn persistence(&self) -> &Arc<dyn bamboo_domain::RuntimeSessionPersistence> {
        self.inner.persistence()
    }

    // ------------------------------------------------------------------
    // Permission / approval + resume
    // ------------------------------------------------------------------

    /// Answer a suspended session's pending question — a
    /// `conclusion_with_options` clarification OR a permission-approval
    /// prompt (`NeedClarification` / `ToolApprovalRequested` events; both
    /// suspend via the same `session.pending_question` mechanism, per
    /// `bamboo_engine::session_app::respond`). This is the in-process
    /// equivalent of the HTTP `POST /api/v1/sessions/{id}/respond` endpoint —
    /// same use case function (`submit_pending_response`), so behavior
    /// (validation, plan-mode transitions, permission-grant extraction)
    /// matches exactly.
    ///
    /// `response` must be one of the pending question's `options` unless it
    /// `allow_custom`s a free-form answer (returns
    /// [`SdkError::InvalidResponse`] otherwise). Loads the session by ID from
    /// [`storage`](Self::storage), so the run must have already suspended
    /// (and thus persisted) before calling this — the session is NOT taken
    /// from an in-memory handle.
    ///
    /// If this `Agent` was built with
    /// [`AgentBuilder::permission_checker`](AgentBuilder::permission_checker),
    /// any permission grants implied by an approved permission prompt are
    /// applied to it automatically (mirroring what the HTTP handler does for
    /// `state.permission_checker`), so the resumed re-attempt of the gated
    /// operation passes the checker without prompting again.
    ///
    /// After answering, resume execution with [`resume`](Self::resume) /
    /// [`resume_stream`](Self::resume_stream) on the returned
    /// [`AnswerOutcome::session`] — or use
    /// [`answer_and_resume_stream`](Self::answer_and_resume_stream) to do both
    /// in one call.
    ///
    /// Like the HTTP server's `/respond` handler, approving a gated tool call
    /// here also re-executes it for real once the run resumes: `answer` (via
    /// `submit_pending_response`) stamps the returned session's metadata with
    /// `PERMISSION_REEXECUTE_METADATA_KEY`, and the next call into
    /// [`resume`](Self::resume)/[`resume_stream`](Self::resume_stream)/`run*`
    /// re-runs the originally-gated tool call against the agent's tool executor
    /// and overwrites the synthetic "Selected response: Approve" placeholder
    /// with the operation's genuine output before the loop continues — see
    /// `reexecute_approved_tool_if_pending`.
    ///
    /// NOTE: `ChildApprovalRequested` (an out-of-process sub-agent worker's
    /// gated tool, proxied over the actor protocol) is a SEPARATE mechanism
    /// from `pending_question`/`respond` and is not covered by this method —
    /// use [`answer_child_approval`](Self::answer_child_approval) instead.
    pub async fn answer(
        &self,
        session_id: impl Into<String>,
        response: impl Into<String>,
    ) -> Result<AnswerOutcome, SdkError> {
        let input = RespondInput {
            session_id: session_id.into(),
            user_response: response.into(),
            model: None,
            model_ref: None,
            provider: None,
            reasoning_effort: None,
        };
        let (session, response, plan_mode_transition, permission_grants) =
            submit_pending_response(self, input).await?;

        if let Some(checker) = &self.permission_checker {
            for (perm_type, resource) in &permission_grants {
                checker.grant_session_permission(*perm_type, resource.clone());
            }
        }

        Ok(AnswerOutcome {
            session,
            response,
            plan_mode_transition,
            permission_grants,
        })
    }

    /// Resume execution on `session` — i.e. continue the agent loop from its
    /// current state (e.g. the tool result [`answer`](Self::answer) just
    /// appended) WITHOUT appending a new user turn, draining events
    /// internally until completion. An alias for
    /// [`run_session`](Self::run_session) that documents intent at the call
    /// site: the engine's execution entry point always resumes from whatever
    /// is already in `session.messages` (`run`/`run_stream` are the ones that
    /// append a fresh turn first).
    pub async fn resume(&self, session: &mut Session) -> Result<(), AgentError> {
        self.run_session(session).await
    }

    /// Like [`resume`](Self::resume), but driven by a caller-owned
    /// [`CancellationToken`].
    pub async fn resume_with_cancel(
        &self,
        session: &mut Session,
        cancel_token: CancellationToken,
    ) -> Result<(), AgentError> {
        self.run_session_with_cancel(session, cancel_token).await
    }

    /// Like [`resume`](Self::resume), but streams [`AgentEvent`]s instead of
    /// draining them. An alias for
    /// [`run_stream_session`](Self::run_stream_session).
    pub fn resume_stream(&self, session: Session) -> mpsc::Receiver<AgentEvent> {
        self.run_stream_session(session)
    }

    /// Like [`resume_stream`](Self::resume_stream), but also returns a
    /// [`CancellationToken`] for the resumed run.
    pub fn resume_stream_cancellable(
        &self,
        session: Session,
    ) -> (mpsc::Receiver<AgentEvent>, CancellationToken) {
        self.run_stream_session_cancellable(session)
    }

    /// Convenience: [`answer`](Self::answer) a pending question, then
    /// immediately [`resume_stream`](Self::resume_stream) on the resulting
    /// session — the common "ask → answer → resume" flow in one call.
    pub async fn answer_and_resume_stream(
        &self,
        session_id: impl Into<String>,
        response: impl Into<String>,
    ) -> Result<mpsc::Receiver<AgentEvent>, SdkError> {
        let outcome = self.answer(session_id, response).await?;
        Ok(self.resume_stream(outcome.session))
    }

    /// Answer an out-of-process child sub-agent's gated-tool approval request
    /// (an [`AgentEvent::ChildApprovalRequested`] surfaced on a run's event
    /// stream — e.g. from [`run_stream`](Self::run_stream)/
    /// [`resume_stream`](Self::resume_stream)) — the in-process equivalent of
    /// the HTTP `POST /api/v1/child-approval/{child_session_id}` endpoint (see
    /// `bamboo_server::handlers::agent::child_approval`).
    ///
    /// This is a SEPARATE mechanism from [`answer`](Self::answer)/
    /// [`pending_question`](Session::pending_question): a child sub-agent
    /// worker running out-of-process (over the actor protocol, e.g. a broker
    /// worker) that hits a gated tool escalates the approval request UP to this
    /// process rather than suspending its own `pending_question`, and the
    /// engine tracks it in a process-global pending-approval registry
    /// (`bamboo_engine::external_agents::live`) keyed by `(child_session_id,
    /// request_id)` — both taken verbatim from the surfaced event. There is no
    /// session to load/save here: this only delivers the decision over the
    /// child's live connection (or fails it closed if the child already
    /// disconnected or the request already resolved/timed out).
    ///
    /// Returns `true` if the decision was delivered to a genuinely-pending
    /// request, `false` if `request_id` is unknown, was already answered, timed
    /// out, or the child is no longer live — mirroring the HTTP handler's
    /// 200-vs-404 distinction. A `false` result does not need cleanup on the
    /// caller's part: the request is either already resolved or has moved on.
    ///
    /// # Boundary
    ///
    /// This method only covers the TOP-orchestrator, human-in-the-loop leg of
    /// child approval (the leg that surfaces `ChildApprovalRequested` at all).
    /// It requires the agent to actually be driving an out-of-process child —
    /// i.e. the caller has wired the engine's `external_agents` actor transport
    /// (broker/worker) — which `AgentBuilder::with_defaults_for_data_dir` does
    /// NOT assemble; that machinery is a separate, opt-in subsystem. Calling
    /// this without a live child matching `child_session_id`/`request_id`
    /// simply returns `false` (no panic, no error) — the same as an unmatched
    /// HTTP POST.
    pub fn answer_child_approval(
        &self,
        child_session_id: impl AsRef<str>,
        request_id: impl AsRef<str>,
        approved: bool,
    ) -> bool {
        bamboo_engine::external_agents::live::deliver_approval_checked(
            child_session_id.as_ref(),
            request_id.as_ref(),
            approved,
        )
    }

    // ------------------------------------------------------------------
    // Session ergonomics
    // ------------------------------------------------------------------

    /// List every session in the data directory, most-recently-updated first.
    ///
    /// Only available when this `Agent` was built via
    /// [`AgentBuilder::with_defaults_for_data_dir`] (which assembles the
    /// concrete session-index handle this needs) — returns
    /// [`SdkError::Unsupported`] otherwise.
    pub async fn list_sessions(&self) -> Result<Vec<bamboo_storage::SessionIndexEntry>, SdkError> {
        let store = self.session_store.as_ref().ok_or_else(|| {
            SdkError::Unsupported(
                "list_sessions requires an Agent built via with_defaults_for_data_dir".to_string(),
            )
        })?;
        Ok(store.list_index_entries().await)
    }

    /// Load a session by ID (its full message history + runtime state), or
    /// `Ok(None)` if it doesn't exist.
    pub async fn get_session(&self, session_id: &str) -> Result<Option<Session>, SdkError> {
        self.storage()
            .load_session(session_id)
            .await
            .map_err(SdkError::Io)
    }

    /// The message history for a session, or [`SdkError::SessionNotFound`] if
    /// it doesn't exist.
    pub async fn session_history(&self, session_id: &str) -> Result<Vec<Message>, SdkError> {
        self.get_session(session_id)
            .await?
            .map(|session| session.messages)
            .ok_or_else(|| SdkError::SessionNotFound(session_id.to_string()))
    }

    /// Delete a session. Returns `true` if a session was actually deleted.
    pub async fn delete_session(&self, session_id: &str) -> Result<bool, SdkError> {
        self.storage()
            .delete_session(session_id)
            .await
            .map_err(SdkError::Io)
    }
}

/// Outcome of [`Agent::answer`] — the updated session, the recorded response,
/// and any side effects `submit_pending_response` computed (plan-mode
/// transitions, permission grants implied by an approval).
#[derive(Debug)]
pub struct AnswerOutcome {
    /// The session after the pending question was answered (tool result
    /// recorded, `pending_question` cleared, resume markers set).
    pub session: Session,
    /// The response text that was recorded.
    pub response: String,
    /// Plan-mode entered/exited transition, if the answered question was
    /// `EnterPlanMode`/`ExitPlanMode`.
    pub plan_mode_transition: Option<PlanModeTransition>,
    /// `(PermissionType, resource)` grants implied by approving a permission
    /// prompt (empty unless the pending question was a permission approval).
    /// Already applied to this `Agent`'s configured
    /// [`permission_checker`](AgentBuilder::permission_checker), if any.
    pub permission_grants: Vec<(bamboo_tools::permission::PermissionType, String)>,
}

/// [`SessionAccess`] for [`Agent`], backed by [`Agent::storage`] (reads) and
/// [`Agent::persistence`] (writes) — no separate cache tier, unlike the
/// server's cache+storage+persistence-backed `SessionRepository`, since the
/// SDK has no cross-request cache to keep coherent. This is what lets
/// [`Agent::answer`] call the same `bamboo_engine::session_app::respond`
/// use-case function the HTTP `/respond` handler calls on `AppState`.
#[async_trait]
impl SessionAccess for Agent {
    async fn load_session(&self, id: &str) -> Result<Option<Session>, SessionLoadError> {
        self.storage()
            .load_session(id)
            .await
            .map_err(|e| SessionLoadError::StorageError(e.to_string()))
    }

    async fn load_or_create(&self, id: &str, model: &str) -> Result<Session, SessionLoadError> {
        match SessionAccess::load_session(self, id).await? {
            Some(session) => Ok(session),
            None => Ok(Session::new(id.to_string(), model.to_string())),
        }
    }

    async fn load_merged(&self, id: &str) -> Result<Option<Session>, SessionLoadError> {
        // No separate cache tier — storage is the single source of truth.
        SessionAccess::load_session(self, id).await
    }

    async fn save_session(&self, session: &mut Session) -> Result<(), SessionSaveError> {
        self.persistence()
            .save_runtime_session(session)
            .await
            .map_err(|e| SessionSaveError::StorageError(e.to_string()))
    }

    async fn save_and_cache(&self, session: &mut Session) -> Result<(), SessionSaveError> {
        SessionAccess::save_session(self, session).await
    }
}

/// Find the original tool call (with its arguments) by id in the session
/// history. Mirrors `bamboo-server`'s `resume_adapter::find_pending_tool_call`.
fn find_pending_tool_call(
    session: &Session,
    tool_call_id: &str,
) -> Option<bamboo_agent_core::tools::ToolCall> {
    session.messages.iter().find_map(|message| {
        message
            .tool_calls
            .as_ref()
            .and_then(|calls| calls.iter().find(|call| call.id == tool_call_id).cloned())
    })
}

/// Overwrite the tool-result message for `tool_call_id` with the real tool
/// output. Mirrors `bamboo-server`'s `resume_adapter::apply_tool_result`.
fn apply_tool_result(session: &mut Session, tool_call_id: &str, content: String, success: bool) {
    for message in &mut session.messages {
        if message.tool_call_id.as_deref() == Some(tool_call_id) {
            message.content = content;
            message.tool_success = Some(success);
            return;
        }
    }
}

#[cfg(test)]
mod approval_and_session_tests {
    use super::*;
    use bamboo_tools::permission::{
        PermissionChecker, PermissionContext, PermissionError, PermissionMode, PermissionType,
    };
    use std::sync::Mutex as StdMutex;

    /// A minimal data dir with a keyless-but-constructible provider config, so
    /// `with_defaults_for_data_dir` succeeds without any network I/O — mirrors
    /// `tests/agent_sdk.rs`'s `s_t4_3` setup.
    async fn build_test_agent(data_dir: std::path::PathBuf) -> Agent {
        let config_json = r#"{
            "provider": "anthropic",
            "providers": {
                "anthropic": { "api_key": "test-key", "model": "claude-test" }
            }
        }"#;
        std::fs::write(data_dir.join("config.json"), config_json).expect("write config");

        AgentBuilder::new()
            .model("claude-test")
            .instruction("test agent")
            .with_defaults_for_data_dir(data_dir)
            .await
            .expect("defaults should assemble")
            .build()
            .expect("agent should build")
    }

    fn seed_session_with_pending_question(
        session_id: &str,
        options: Vec<String>,
        allow_custom: bool,
    ) -> Session {
        let mut session = Session::new(session_id.to_string(), "claude-test".to_string());
        session.set_pending_question(
            "call-1".to_string(),
            "ConclusionWithOptions".to_string(),
            "Pick one".to_string(),
            options,
            allow_custom,
        );
        session
    }

    #[tokio::test]
    async fn answer_resolves_pending_question_and_persists() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;

        let session = seed_session_with_pending_question(
            "sess-answer-ok",
            vec!["A".to_string(), "B".to_string()],
            false,
        );
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let outcome = agent
            .answer("sess-answer-ok", "A")
            .await
            .expect("answer should succeed");
        assert_eq!(outcome.response, "A");
        assert!(outcome.session.pending_question.is_none());
        assert!(outcome.permission_grants.is_empty());

        // Persisted: reloading independently shows the same state.
        let reloaded = agent
            .storage()
            .load_session("sess-answer-ok")
            .await
            .expect("load")
            .expect("present");
        assert!(reloaded.pending_question.is_none());
        assert!(reloaded
            .messages
            .iter()
            .any(|m| m.tool_call_id.as_deref() == Some("call-1")
                && m.content.contains("Selected response: A")));
    }

    #[tokio::test]
    async fn answer_rejects_response_outside_fixed_options() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;

        let session = seed_session_with_pending_question(
            "sess-answer-invalid",
            vec!["A".to_string(), "B".to_string()],
            false,
        );
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let error = agent
            .answer("sess-answer-invalid", "not-an-option")
            .await
            .expect_err("response outside options should be rejected");
        assert!(matches!(error, SdkError::InvalidResponse(_)));
    }

    #[tokio::test]
    async fn answer_errors_when_no_pending_question() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;

        let session = Session::new("sess-no-pending".to_string(), "claude-test".to_string());
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let error = agent
            .answer("sess-no-pending", "anything")
            .await
            .expect_err("no pending question should error");
        assert!(matches!(error, SdkError::NoPendingQuestion));
    }

    #[tokio::test]
    async fn answer_errors_when_session_missing() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;

        let error = agent
            .answer("does-not-exist", "anything")
            .await
            .expect_err("missing session should error");
        assert!(matches!(error, SdkError::SessionNotFound(id) if id == "does-not-exist"));
    }

    #[tokio::test]
    async fn session_ergonomics_list_get_history_delete_round_trip() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;

        let mut session_a = Session::new("sess-a".to_string(), "claude-test".to_string());
        session_a.add_message(Message::user("hello"));
        agent
            .storage()
            .save_session(&session_a)
            .await
            .expect("save a");

        let session_b = Session::new("sess-b".to_string(), "claude-test".to_string());
        agent
            .storage()
            .save_session(&session_b)
            .await
            .expect("save b");

        let listed = agent.list_sessions().await.expect("list_sessions");
        let ids: Vec<&str> = listed.iter().map(|entry| entry.id.as_str()).collect();
        assert!(ids.contains(&"sess-a"));
        assert!(ids.contains(&"sess-b"));

        let history = agent
            .session_history("sess-a")
            .await
            .expect("session_history");
        assert_eq!(history.len(), 1);
        assert_eq!(history[0].content, "hello");

        let missing_history = agent.session_history("does-not-exist").await;
        assert!(matches!(
            missing_history,
            Err(SdkError::SessionNotFound(id)) if id == "does-not-exist"
        ));

        let deleted = agent.delete_session("sess-a").await.expect("delete");
        assert!(deleted);
        assert!(agent
            .get_session("sess-a")
            .await
            .expect("get_session")
            .is_none());
    }

    /// A stub `PermissionChecker` that only records `grant_session_permission`
    /// calls, so the test can assert `Agent::answer` applies the permission
    /// grants `submit_pending_response` extracts from an approved permission
    /// prompt — mirroring what the HTTP `/respond` handler does explicitly for
    /// `state.permission_checker`.
    #[derive(Default)]
    struct RecordingPermissionChecker {
        grants: StdMutex<Vec<(PermissionType, String)>>,
    }

    #[async_trait]
    impl PermissionChecker for RecordingPermissionChecker {
        async fn needs_confirmation(&self, _perm_type: PermissionType, _resource: &str) -> bool {
            false
        }

        async fn request_confirmation(
            &self,
            _ctx: PermissionContext,
        ) -> Result<bool, PermissionError> {
            Ok(true)
        }

        fn grant_session_permission(&self, perm_type: PermissionType, resource: String) {
            self.grants.lock().unwrap().push((perm_type, resource));
        }

        fn set_permission_mode(&self, _mode: PermissionMode) {}
    }

    #[tokio::test]
    async fn answer_applies_permission_grants_to_configured_checker() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let config_json = r#"{
            "provider": "anthropic",
            "providers": {
                "anthropic": { "api_key": "test-key", "model": "claude-test" }
            }
        }"#;
        std::fs::write(tmp.path().join("config.json"), config_json).expect("write config");

        let checker = Arc::new(RecordingPermissionChecker::default());
        let agent = AgentBuilder::new()
            .model("claude-test")
            .permission_checker(checker.clone())
            .with_defaults_for_data_dir(tmp.path().to_path_buf())
            .await
            .expect("defaults should assemble")
            .build()
            .expect("agent should build");

        // Seed a session suspended on an approved permission prompt: the
        // synthesized `awaiting_permission_approval` tool-result payload
        // `check_permissions_for` writes before pausing (see
        // `bamboo_tools::executor` / `session_app::respond`).
        let mut session = Session::new("sess-permission".to_string(), "claude-test".to_string());
        session.set_pending_question(
            "call-perm-1".to_string(),
            "Write".to_string(),
            "Permission required".to_string(),
            vec!["Approve".to_string(), "Deny".to_string()],
            false,
        );
        session.add_message(Message::tool_result(
            "call-perm-1",
            serde_json::json!({
                "status": "awaiting_permission_approval",
                "question": "Permission required",
                "permission_type": "write_file",
                "resource": "/tmp/example.txt",
                "options": ["Approve", "Deny"],
                "allow_custom": false,
            })
            .to_string(),
        ));
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let outcome = agent
            .answer("sess-permission", "Approve")
            .await
            .expect("answer should succeed");
        assert_eq!(
            outcome.permission_grants,
            vec![(PermissionType::WriteFile, "/tmp/example.txt".to_string())]
        );

        let recorded = checker.grants.lock().unwrap();
        assert_eq!(
            *recorded,
            vec![(PermissionType::WriteFile, "/tmp/example.txt".to_string())]
        );
    }

    #[tokio::test]
    async fn list_sessions_unsupported_without_defaults_for_data_dir() {
        // An Agent wrapped directly via `from_runtime` (no session_store
        // handle) should report `Unsupported`, not panic.
        // Building one still needs a real engine Agent, so reuse the defaults
        // path and drop the handle to simulate a manually-injected Agent.
        let tmp = tempfile::tempdir().expect("tempdir");
        let agent = build_test_agent(tmp.path().to_path_buf()).await;
        let bare = Agent::from_runtime_with_config(
            // Reuse the inner engine agent — only the SDK-level session_store
            // handle is what `list_sessions` checks.
            agent.inner.clone(),
            None,
            None,
            None,
            None,
        );
        let result = bare.list_sessions().await;
        assert!(matches!(result, Err(SdkError::Unsupported(_))));
    }
}

#[cfg(test)]
mod reexecute_and_child_approval_tests {
    use super::*;
    use bamboo_agent_core::tools::{FunctionCall, Tool, ToolCall, ToolCtx, ToolError, ToolOutcome};
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// A tool whose real output is trivially distinguishable from the
    /// synthetic "Selected response: Approve" placeholder `submit_pending_response`
    /// writes, and which counts invocations — so tests can assert it actually ran
    /// (not merely that the metadata marker was consumed).
    struct RealOutputTool {
        calls: AtomicUsize,
    }

    impl RealOutputTool {
        fn new() -> Self {
            Self {
                calls: AtomicUsize::new(0),
            }
        }
    }

    #[async_trait]
    impl Tool for RealOutputTool {
        fn name(&self) -> &str {
            "real_output_tool"
        }

        fn description(&self) -> &str {
            "test-only tool that returns a distinctive real result"
        }

        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({ "type": "object", "properties": {} })
        }

        async fn invoke(
            &self,
            _args: serde_json::Value,
            _ctx: ToolCtx,
        ) -> Result<ToolOutcome, ToolError> {
            let n = self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(ToolOutcome::Completed(
                bamboo_agent_core::tools::ToolResult::text(true, format!("REAL TOOL OUTPUT #{n}")),
            ))
        }
    }

    async fn build_test_agent_with_tool(
        data_dir: std::path::PathBuf,
        tool: Arc<RealOutputTool>,
    ) -> Agent {
        let config_json = r#"{
            "provider": "anthropic",
            "providers": {
                "anthropic": { "api_key": "test-key", "model": "claude-test" }
            }
        }"#;
        std::fs::write(data_dir.join("config.json"), config_json).expect("write config");

        AgentBuilder::new()
            .model("claude-test")
            .instruction("test agent")
            .tool_shared(tool)
            .with_defaults_for_data_dir(data_dir)
            .await
            .expect("defaults should assemble")
            .build()
            .expect("agent should build")
    }

    /// Seed a session suspended on an approved permission prompt for
    /// `real_output_tool`, matching the shape `check_permissions_for` writes
    /// before pausing: an assistant message carrying the gated tool call, plus
    /// the synthesized `awaiting_permission_approval` tool-result payload.
    fn seed_gated_tool_session(session_id: &str, tool_call_id: &str) -> Session {
        let mut session = Session::new(session_id.to_string(), "claude-test".to_string());
        session.add_message(Message::assistant(
            "",
            Some(vec![ToolCall {
                id: tool_call_id.to_string(),
                tool_type: "function".to_string(),
                function: FunctionCall {
                    name: "real_output_tool".to_string(),
                    arguments: "{}".to_string(),
                },
            }]),
        ));
        session.set_pending_question(
            tool_call_id.to_string(),
            "real_output_tool".to_string(),
            "Permission required".to_string(),
            vec!["Approve".to_string(), "Deny".to_string()],
            false,
        );
        session.add_message(Message::tool_result(
            tool_call_id,
            serde_json::json!({
                "status": "awaiting_permission_approval",
                "question": "Permission required",
                "permission_type": "write_file",
                "resource": "/tmp/example.txt",
                "options": ["Approve", "Deny"],
                "allow_custom": false,
            })
            .to_string(),
        ));
        session
    }

    #[tokio::test]
    async fn approve_marks_session_for_reexecution() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool).await;

        let session = seed_gated_tool_session("sess-mark", "call-mark-1");
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let outcome = agent
            .answer("sess-mark", "Approve")
            .await
            .expect("answer should succeed");

        assert_eq!(
            outcome
                .session
                .metadata
                .get(PERMISSION_REEXECUTE_METADATA_KEY)
                .map(String::as_str),
            Some("call-mark-1"),
            "approving a permission prompt must stamp the re-exec marker"
        );
    }

    #[tokio::test]
    async fn deny_does_not_mark_session_for_reexecution() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool).await;

        let session = seed_gated_tool_session("sess-deny", "call-deny-1");
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let outcome = agent
            .answer("sess-deny", "Deny")
            .await
            .expect("answer should succeed");

        assert!(outcome.permission_grants.is_empty());
        assert!(!outcome
            .session
            .metadata
            .contains_key(PERMISSION_REEXECUTE_METADATA_KEY));
        // The tool result stays the synthetic "Selected response: Deny" — the
        // gated tool must NOT have run.
        let tool_message = outcome
            .session
            .messages
            .iter()
            .find(|m| m.tool_call_id.as_deref() == Some("call-deny-1"))
            .expect("tool result message present");
        assert_eq!(tool_message.content, "Selected response: Deny");
    }

    #[tokio::test]
    async fn approve_then_reexecute_runs_real_tool_and_overwrites_placeholder() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool.clone()).await;

        let session = seed_gated_tool_session("sess-reexec", "call-reexec-1");
        agent
            .storage()
            .save_session(&session)
            .await
            .expect("seed session");

        let outcome = agent
            .answer("sess-reexec", "Approve")
            .await
            .expect("answer should succeed");
        let mut session = outcome.session;

        // Sanity: before re-execution the tool result is still the synthetic
        // placeholder, and the real tool has not run yet.
        let placeholder = session
            .messages
            .iter()
            .find(|m| m.tool_call_id.as_deref() == Some("call-reexec-1"))
            .expect("tool result message present");
        assert_eq!(placeholder.content, "Selected response: Approve");
        assert_eq!(tool.calls.load(Ordering::SeqCst), 0);

        // Drive the same re-execution step `resume`/`run*` apply internally
        // (via `execute_internal`) at the top of the next execution.
        let (event_tx, mut event_rx) = mpsc::channel::<AgentEvent>(16);
        agent
            .reexecute_approved_tool_if_pending(&mut session, &event_tx)
            .await;
        drop(event_tx);

        // The gated tool ran exactly once, and the placeholder is replaced with
        // its real output.
        assert_eq!(tool.calls.load(Ordering::SeqCst), 1);
        let real_result = session
            .messages
            .iter()
            .find(|m| m.tool_call_id.as_deref() == Some("call-reexec-1"))
            .expect("tool result message present");
        assert_eq!(real_result.content, "REAL TOOL OUTPUT #0");
        assert_eq!(real_result.tool_success, Some(true));
        assert!(
            !session
                .metadata
                .contains_key(PERMISSION_REEXECUTE_METADATA_KEY),
            "the marker must be consumed (removed) after re-execution"
        );

        // Lifecycle events (ToolStart-equivalent begin + ToolComplete) were
        // emitted, matching what a normal dispatch would stream.
        let mut saw_tool_complete = false;
        while let Ok(event) = event_rx.try_recv() {
            if let AgentEvent::ToolComplete { tool_call_id, .. } = event {
                assert_eq!(tool_call_id, "call-reexec-1");
                saw_tool_complete = true;
            }
        }
        assert!(saw_tool_complete, "expected a ToolComplete event");

        // Persisted too (best-effort save inside the helper).
        let reloaded = agent
            .storage()
            .load_session("sess-reexec")
            .await
            .expect("load")
            .expect("present");
        let reloaded_result = reloaded
            .messages
            .iter()
            .find(|m| m.tool_call_id.as_deref() == Some("call-reexec-1"))
            .expect("tool result message present");
        assert_eq!(reloaded_result.content, "REAL TOOL OUTPUT #0");
    }

    #[tokio::test]
    async fn reexecute_is_noop_without_pending_marker() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool.clone()).await;

        let mut session = Session::new("sess-noop".to_string(), "claude-test".to_string());
        session.add_message(Message::user("hi"));

        let (event_tx, _event_rx) = mpsc::channel::<AgentEvent>(16);
        agent
            .reexecute_approved_tool_if_pending(&mut session, &event_tx)
            .await;

        assert_eq!(tool.calls.load(Ordering::SeqCst), 0);
        assert_eq!(session.messages.len(), 1);
    }

    #[tokio::test]
    async fn reexecute_warns_and_clears_marker_when_tool_call_missing() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool.clone()).await;

        let mut session = Session::new("sess-missing".to_string(), "claude-test".to_string());
        // Marker set, but no matching tool_calls entry exists in history.
        session.metadata.insert(
            PERMISSION_REEXECUTE_METADATA_KEY.to_string(),
            "ghost-call".to_string(),
        );

        let (event_tx, mut event_rx) = mpsc::channel::<AgentEvent>(16);
        agent
            .reexecute_approved_tool_if_pending(&mut session, &event_tx)
            .await;
        drop(event_tx);

        assert_eq!(tool.calls.load(Ordering::SeqCst), 0);
        assert!(event_rx.try_recv().is_err(), "no events should be emitted");
        assert!(
            !session
                .metadata
                .contains_key(PERMISSION_REEXECUTE_METADATA_KEY),
            "the marker is removed even when the tool call can't be found, so a \
             missing/pruned call can't wedge every future execution"
        );
    }

    #[tokio::test]
    async fn answer_child_approval_delivers_only_genuinely_pending_requests() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let tool = Arc::new(RealOutputTool::new());
        let agent = build_test_agent_with_tool(tmp.path().to_path_buf(), tool).await;

        // Unregistered pair: rejected, mirroring an unmatched HTTP POST.
        assert!(!agent.answer_child_approval("child-x", "req-unknown", true));

        // A live child connection (as the actor adapter registers for the
        // duration of a running child) plus the pending-approval marker it
        // records just before surfacing `ChildApprovalRequested` — both are
        // process-global engine state, set up here exactly as
        // `external_agents::actor_adapter::drive` would.
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        let _live_guard = bamboo_engine::external_agents::live::register("child-x", tx);
        bamboo_engine::external_agents::live::register_pending_approval("child-x", "req-1");

        assert!(agent.answer_child_approval("child-x", "req-1", true));
        match rx.try_recv() {
            Ok(bamboo_subagent::proto::ParentFrame::ApprovalReply { id, approved }) => {
                assert_eq!(id, "req-1");
                assert!(approved);
            }
            other => panic!("expected an ApprovalReply frame, got {other:?}"),
        }

        // One-shot: a replay of the same request_id is rejected.
        assert!(!agent.answer_child_approval("child-x", "req-1", true));
    }
}