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
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
//! Agent runtime: wraps oxi-sdk's Agent for Seed execution.
//!
//! The AgentRuntime uses `OxiosEngine.oxi().agent()` (AgentBuilder pattern)
//! to construct agents with full middleware, observability, and security
//! integration from oxi-sdk 0.24.0.
//!
//! # Architecture
//!
//! All tool access goes through `KernelHandle` — the single syscall-table-like
//! path for agent OS control. The runtime:
//!
//! 1. Resolves the agent's CSpace from persona/role/hint
//! 2. Registers tools via `register_tools_from_cspace()`
//! 3. Optionally queries `ToolRetriever` for semantic capability hints
//! 4. Builds an `Agent` via `AgentBuilder` with middleware pipeline
//! 5. Runs via `Agent::run_streaming()` for real-time event processing
//!
//! # oxi-sdk 0.23.0 Integration
//!
//! Uses `AgentBuilder` for agent construction with:
//! - `.with_rate_limit()` — tool call rate limiting
//! - `.with_token_budget()` — per-execution token caps
//! - `.tracer()` / `.cost_tracker()` — observability hooks
//! ## Routing integration (RFC-011)
//!
//! Model usage events (`AgentEvent::Usage`) are recorded to the shared
//! `RoutingStats` so the Web dashboard can display per-model call counts
//! and estimated costs.
use anyhow::Result;
use oxi_sdk::observability::AuditTrail;
use oxi_sdk::{
Agent, AgentConfig, AgentEvent, CompactionEvent, CompactionStrategy, ProviderResolver,
};
use oxi_sdk::{SearchCache, ToolExecutionMode, ToolRegistry};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::Arc;
// RFC-014 Phase D: `ToolRegistry::register_arc` is used in the AgentBuilder
// path to attach CSpace tools after `builder.build()` returns.
use crate::access_manager::{AccessGate, AgentContext, TracingAuditSink, TrailAuditSink};
use crate::capability::resolve::resolve_cspace;
use crate::engine::OxiosEngine;
use crate::memory::{MemoryEntry, MemoryManager, MemoryType};
use crate::persona::PersonaManager;
use crate::tools::registration::register_tools_from_cspace_gated;
use crate::KernelHandle;
use crate::event_bus::KernelEvent;
use crate::session_context::SessionContext;
use crate::types::AgentId;
use oxios_ouroboros::{ExecutionResult, Seed};
/// Global LLM circuit breaker instance — delegates to oxi-sdk's ProviderCircuitBreaker.
static LLM_CIRCUIT_BREAKER: std::sync::OnceLock<oxi_sdk::ProviderCircuitBreaker> =
std::sync::OnceLock::new();
/// Get the global LLM circuit breaker.
fn get_llm_circuit_breaker() -> &'static oxi_sdk::ProviderCircuitBreaker {
LLM_CIRCUIT_BREAKER.get_or_init(|| {
oxi_sdk::ProviderCircuitBreaker::new(
"global".to_string(),
oxi_sdk::CircuitBreakerConfig::default(),
)
})
}
/// Configuration for creating AgentRuntime instances.
#[derive(Debug, Clone)]
pub struct AgentRuntimeConfig {
/// Model ID in `provider/model` format (e.g. `anthropic/claude-sonnet-4-20250514`).
pub model_id: String,
/// How to execute tool calls within a single turn.
pub tool_execution: ToolExecutionMode,
/// Whether auto-retry is enabled for retryable LLM errors.
pub auto_retry_enabled: bool,
/// Bound project paths. AgentRuntime sets CWD to paths[0].
pub project_paths: Vec<std::path::PathBuf>,
/// Scratch workspace directory for temp files.
pub workspace_dir: Option<std::path::PathBuf>,
/// API key resolved from CredentialStore at build time.
pub api_key: Option<String>,
/// Per-provider options for fine-grained control.
pub provider_options: Option<oxi_sdk::ProviderOptions>,
/// Rate limit for tool calls (requests per minute). 0 = unlimited.
pub rate_limit_per_minute: usize,
/// Token budget per agent execution. 0 = unlimited.
pub token_budget: usize,
/// Enable audit logging for all tool executions.
pub audit_tool_calls: bool,
/// Provider-level RPM for rate-limited provider pool. 0 = no pooling.
/// When set, uses `OxiosEngine::pooled_provider()` instead of `create_provider()`.
pub provider_rpm: u32,
}
impl Default for AgentRuntimeConfig {
fn default() -> Self {
Self {
model_id: String::new(),
tool_execution: ToolExecutionMode::Parallel,
auto_retry_enabled: true,
project_paths: Vec::new(),
workspace_dir: None,
api_key: None,
provider_options: None,
rate_limit_per_minute: 0,
token_budget: 0,
audit_tool_calls: false,
provider_rpm: 0,
}
}
}
/// Mutable state shared between the event callback and the main execute flow.
#[derive(Default)]
struct ExecuteState {
final_content: String,
steps_completed: usize,
success: bool,
/// Collected trajectory steps for SONA learning (RFC-020 Phase 2).
/// Ordered by insertion — parallel tools get their final position
/// resolved when they complete, preserving approximate execution order.
trajectory_steps: Vec<oxios_memory::memory::sona::TrajectoryStep>,
/// Map of tool_call_id → (start instant, index into trajectory_steps).
/// Used to correlate ToolExecutionEnd with the correct step when
/// parallel tool calls complete out of order.
pending_tools: std::collections::HashMap<String, (std::time::Instant, usize)>,
/// Ordered tool_call_ids matching trajectory_steps indices.
/// Pushed in ToolExecutionStart, same order as trajectory_steps.
tool_call_ids: Vec<String>,
/// Per-step tool args (JSON string) captured from ToolExecutionStart.
tool_args_map: std::collections::HashMap<String, String>,
/// Per-step error flag from ToolExecutionEnd.
tool_error_map: std::collections::HashMap<String, bool>,
/// Per-step start timestamp (UTC) from ToolExecutionStart.
tool_timestamps: std::collections::HashMap<String, chrono::DateTime<chrono::Utc>>,
/// Cumulative input tokens from AgentEvent::Usage.
total_input_tokens: u64,
/// Cumulative output tokens from AgentEvent::Usage.
total_output_tokens: u64,
}
/// Runtime that wraps an oxi-sdk `Agent` for executing Seeds.
///
/// Each call to [`AgentRuntime::execute`] creates a fresh `Agent`,
/// builds a ToolRegistry based on the agent's CSpace, and runs it to completion.
///
/// All OS-level access goes through `KernelHandle` — the single syscall table
/// for agent control. Provider/model resolution goes through `EngineHandle`,
/// which returns the latest `OxiosEngine` (hot-swapped on config change).
pub struct AgentRuntime {
engine_handle: Arc<crate::engine::EngineHandle>,
config: AgentRuntimeConfig,
/// Single path to all kernel services.
kernel_handle: Arc<KernelHandle>,
/// Persona manager for system prompt injection.
persona_manager: Option<Arc<PersonaManager>>,
/// Semantic tool retriever for capability discovery.
tool_retriever: Option<Arc<crate::tools::retrieval::ToolRetriever>>,
/// Shared routing stats (shared with EngineApi).
routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
/// Autonomous persistence hook (RFC-016).
persistence_hook: Option<Arc<crate::persistence_hook::PersistenceHook>>,
/// Per-session assistant message index counter (RFC-016).
session_msg_counter: Arc<Mutex<HashMap<String, usize>>>,
}
impl AgentRuntime {
/// Creates a new agent runtime with engine handle and kernel access.
///
/// Provider/model resolution goes through `engine_handle` (hot-swapped on config change).
/// Tool access goes through `kernel_handle`.
pub fn new(
engine_handle: Arc<crate::engine::EngineHandle>,
model_id: impl Into<String>,
kernel_handle: Arc<KernelHandle>,
routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
) -> Self {
Self {
engine_handle,
config: AgentRuntimeConfig {
model_id: model_id.into(),
..Default::default()
},
kernel_handle,
persona_manager: None,
tool_retriever: None,
routing_stats,
persistence_hook: None,
session_msg_counter: Arc::new(Mutex::new(HashMap::new())),
}
}
/// Attach a PersonaManager for persona system prompt injection.
pub fn with_persona_manager(mut self, pm: Arc<PersonaManager>) -> Self {
self.persona_manager = Some(pm);
self
}
/// Set the runtime config (overrides defaults).
pub fn with_config(mut self, config: AgentRuntimeConfig) -> Self {
self.config = config;
self
}
/// Attach a ToolRetriever for semantic capability discovery.
pub fn with_tool_retriever(
mut self,
retriever: Arc<crate::tools::retrieval::ToolRetriever>,
) -> Self {
self.tool_retriever = Some(retriever);
self
}
/// Attach a PersistenceHook for autonomous persistence (RFC-016).
pub fn with_persistence_hook(
mut self,
hook: Arc<crate::persistence_hook::PersistenceHook>,
) -> Self {
self.persistence_hook = Some(hook);
self
}
/// Execute a Seed by running the tool-calling agent to completion.
///
/// 1. Resolves CSpace from persona/role/hint
/// 2. Registers tools via CSpace
/// 3. Recalls memories if available
/// 4. Creates Agent via `Agent::new_with_resolver()`
/// 5. Runs via `Agent::run_streaming()`
pub async fn execute(
&self,
agent_id: AgentId,
seed: &Seed,
session_ctx: &mut SessionContext,
) -> Result<ExecutionResult> {
// RFC-015: session_id is derived from seed.id for chat transparency
// event publishing. Most callers run one Seed per session turn, so
// seed.id is a usable session identifier.
let session_id: Option<String> = Some(seed.id.to_string());
self.execute_with_session(agent_id, seed, session_ctx, session_id)
.await
}
/// Like [`execute`](Self::execute) but with an explicit session_id for
/// RFC-015 chat transparency event publishing.
pub async fn execute_with_session(
&self,
agent_id: AgentId,
seed: &Seed,
session_ctx: &mut SessionContext,
session_id: Option<String>,
) -> Result<ExecutionResult> {
let prompt = build_user_prompt(seed);
// Get active persona system prompt.
let persona_prompt = self
.persona_manager
.as_ref()
.map(|pm| pm.active_system_prompt())
.filter(|s| !s.trim().is_empty());
// Determine persona role for CSpace resolution.
let persona_role = self
.persona_manager
.as_ref()
.and_then(|pm| pm.get_active_persona().map(|p| p.role.clone()));
// Resolve CSpace from persona role, seed hint, or default.
let cspace = resolve_cspace(
seed.cspace_hint.as_deref(),
persona_role.as_deref(),
Some("worker"),
agent_id,
);
// Build system prompt (without SKILL.md injection — capabilities are
// surfaced through the CSpace tool set + semantic retrieval instead).
let mut system_prompt = build_system_prompt(
seed,
persona_prompt.as_deref(),
None,
None,
seed.workspace_context.as_deref(),
);
// Semantic capability retrieval: find tools relevant to this seed's goal.
let capabilities_xml = if let Some(ref retriever) = self.tool_retriever {
match retriever.embedder().embed(&seed.goal).await {
Ok(query_vec) => {
let results = retriever.retrieve(&query_vec, 8);
if results.is_empty() {
None
} else {
let xml = crate::tools::retrieval::format_capability_index(&results);
tracing::info!(count = results.len(), "Retrieved relevant capabilities");
Some(xml)
}
}
Err(e) => {
tracing::warn!(error = %e, "Failed to embed seed goal for retrieval");
None
}
}
} else {
None
};
// Build kernel manifest from CSpace active domains.
let kernel_manifest = {
let domains = cspace.active_domains();
if domains.is_empty() {
None
} else {
Some(crate::tools::retrieval::build_kernel_manifest(&domains))
}
};
// Rebuild system prompt with capabilities and manifest if available.
if capabilities_xml.is_some() || kernel_manifest.is_some() {
system_prompt = build_system_prompt(
seed,
persona_prompt.as_deref(),
capabilities_xml.as_deref(),
kernel_manifest.as_deref(),
seed.workspace_context.as_deref(),
);
}
// Blend relevant memories into system prompt.
let memory_manager = self.kernel_handle.agents.memory_manager();
match memory_manager
.recall_with_proactive(&seed.goal, &mut session_ctx.recall_timing)
.await
{
Ok(memories) if !memories.is_empty() => {
tracing::info!(count = memories.len(), "Recalled memories for seed");
system_prompt = memory_manager.blend_into_prompt(&memories, &system_prompt);
}
Ok(_) => tracing::debug!("No memories recalled"),
Err(e) => tracing::warn!(error = %e, "Failed to recall memories"),
}
// Inject learned strategy from SONA (RFC-020 Phase 2).
if let Some(sona) = memory_manager.sona_engine() {
match sona.adapt(&seed.goal).await {
Ok(Some(pattern)) if pattern.confidence > 0.5 => {
tracing::info!(
domain = %pattern.domain,
confidence = pattern.confidence,
"SONA learned pattern injected"
);
system_prompt.push_str(&format!(
"\n\n## Learned Strategy (confidence: {:.0}%)\n{}\n",
pattern.confidence * 100.0,
pattern.strategy,
));
}
Ok(_) => tracing::debug!("No high-confidence SONA pattern found"),
Err(e) => tracing::debug!(error = %e, "SONA adapt failed (non-fatal)"),
}
}
// Blend relevant knowledge notes into system prompt (KnowledgeLens, RFC-003 Phase 3).
match self
.kernel_handle
.knowledge_lens
.recall_for_context(&seed.goal, 5)
.await
{
Ok(ctx) if !ctx.notes.is_empty() => {
tracing::info!(
notes = ctx.notes.len(),
memories = ctx.memories.len(),
"Recalled knowledge context for seed"
);
let knowledge_blend = ctx
.notes
.iter()
.take(3)
.map(|n| format!("## {}\n\n{}", n.name, n.content))
.collect::<Vec<_>>()
.join("\n\n");
system_prompt.push_str("\n\n## Relevant Knowledge\n\n");
system_prompt.push_str(&knowledge_blend);
}
Ok(_) => tracing::debug!("No knowledge recalled"),
Err(e) => tracing::warn!(error = %e, "Failed to recall knowledge context"),
}
// Resolve model from engine (provider resolution happens inside AgentBuilder).
// Get the latest engine — may have been hot-swapped via Web UI config change.
let engine = self.engine_handle.get();
let _model = engine.resolve_model(&self.config.model_id)?;
let seed_id = seed.id;
// Build the agent.
let config = self.config.clone();
let kernel_handle = Arc::clone(&self.kernel_handle);
// Extract audit trail from kernel for TrailAuditSink wiring.
let audit_trail: Option<Arc<AuditTrail>> =
Some(Arc::clone(&self.kernel_handle.security.audit_trail));
let (
mut final_content,
steps_completed,
success,
trajectory_steps,
agent,
tool_call_ids,
tool_args_map,
tool_error_map,
tool_timestamps,
total_input_tokens,
total_output_tokens,
) = {
run_agent(
&config,
&engine,
kernel_handle,
system_prompt,
prompt,
seed_id,
seed.goal.clone(),
agent_id,
cspace,
audit_trail,
self.routing_stats.clone(),
session_id.clone(),
&seed.mount_paths,
)
.await?
};
// ── Post-execution: safety net for empty final content ──
//
// oxi 0.32.0 removed max_iterations — the loop now exits naturally
// when the LLM produces a text-only response (pi-agent behavior).
// This block is kept as a safety net in case the LLM returns empty
// text despite a natural exit (rare, but possible).
if final_content.is_empty() && !trajectory_steps.is_empty() {
let tool_summary: Vec<String> = trajectory_steps
.iter()
.enumerate()
.map(|(i, step)| {
let truncated = if step.output.len() > 800 {
format!("{}...", &step.output[..800])
} else {
step.output.clone()
};
format!("{}. [{}] {}", i + 1, step.input, truncated)
})
.collect();
let summary_prompt = format!(
"도구 실행 결과:\n\n{}\n\n\
위 결과를 바탕으로 사용자의 요청에 대해 자연스럽게 한국어로 답변해주세요. \
도구의 원시 출력을 그대로 복사하지 말고, 의미 있는 내용만 정리해서 전달하세요.",
tool_summary.join("\n")
);
match agent.run(summary_prompt).await {
Ok((response, _events)) => {
if !response.content.is_empty() {
tracing::info!(seed_id = %seed_id, "Post-execution summary generated");
final_content = response.content;
}
}
Err(e) => {
tracing::warn!(error = %e, "Post-execution summary failed");
}
}
}
// Map trajectory steps to tool call records for the execution result.
// tool_call_ids[i] corresponds to trajectory_steps[i].
let tool_calls: Vec<oxios_ouroboros::ToolCallRecord> = trajectory_steps
.iter()
.enumerate()
.map(|(i, step)| {
let tc_id = tool_call_ids.get(i).cloned().unwrap_or_default();
let args_str = tool_call_ids
.get(i)
.and_then(|id| tool_args_map.get(id))
.cloned()
.unwrap_or_default();
let is_error = tool_call_ids
.get(i)
.and_then(|id| tool_error_map.get(id))
.copied()
.unwrap_or(false);
let timestamp = tool_call_ids
.get(i)
.and_then(|id| tool_timestamps.get(id))
.copied();
let input_str = truncate_json_str(&args_str, 500);
oxios_ouroboros::ToolCallRecord {
tool: step.input.clone(),
input: input_str,
output: step.output.clone(),
duration_ms: step.duration_ms,
is_error,
tool_call_id: tc_id,
timestamp,
}
})
.collect();
tracing::info!(
seed_id = %seed_id,
steps = steps_completed,
success,
tool_calls = tool_calls.len(),
"AgentRuntime finished"
);
let result = ExecutionResult {
output: final_content.clone(),
steps_completed,
success,
tool_calls,
tokens_input: total_input_tokens,
tokens_output: total_output_tokens,
model_id: self.engine_handle.get().default_model_id().to_string(),
};
// RFC-016: Autonomous persistence hook.
// Runs after successful execution, fire-and-forget.
if success && let Some(hook) = &self.persistence_hook {
let already_saved_knowledge = trajectory_steps
.iter()
.any(|s| s.input == "knowledge" && s.output.contains("written successfully"));
let hook = hook.clone();
let seed_clone = seed.clone();
let traj_clone = trajectory_steps.clone();
let output_clone = final_content.clone();
let sid = session_id.clone();
// Compute the assistant message index for this execution.
// Increment per-session counter, then use the pre-increment value.
let msg_index = {
let mut counter = self.session_msg_counter.lock();
let idx = counter.entry(sid.clone().unwrap_or_default()).or_insert(0);
let current = *idx;
*idx += 1;
current
};
tokio::spawn(async move {
match hook
.evaluate(
&seed_clone,
&traj_clone,
&output_clone,
already_saved_knowledge,
)
.await
{
Ok(plan) => {
if !plan.memory.is_empty() || !plan.knowledge.is_empty() {
tracing::info!(
memory = plan.memory.len(),
knowledge = plan.knowledge.len(),
message_index = msg_index,
"PersistenceHook executing plan"
);
let session_id = sid.unwrap_or_default();
hook.execute_plan(plan, &session_id, msg_index).await;
}
}
Err(e) => tracing::warn!(error = %e, "PersistenceHook evaluate failed"),
}
});
}
Ok(result)
}
}
/// Create and run an oxi-sdk `Agent` with CSpace-based tool registration.
///
/// Uses `engine.oxi().agent()` (AgentBuilder) for full middleware,
/// observability, and security integration from oxi-sdk 0.23.0.
#[allow(clippy::too_many_arguments)]
async fn run_agent(
config: &AgentRuntimeConfig,
engine: &OxiosEngine,
kernel_handle: Arc<KernelHandle>,
system_prompt: String,
prompt: String,
seed_id: uuid::Uuid,
seed_goal: String,
agent_id: AgentId,
cspace: crate::capability::CSpace,
audit_trail: Option<Arc<AuditTrail>>,
routing_stats: Option<Arc<crate::kernel_handle::RoutingStats>>,
session_id: Option<String>,
mount_paths: &[std::path::PathBuf],
) -> Result<(
String,
usize,
bool,
Vec<oxios_memory::memory::sona::TrajectoryStep>,
Arc<Agent>,
Vec<String>,
std::collections::HashMap<String, String>,
std::collections::HashMap<String, bool>,
std::collections::HashMap<String, chrono::DateTime<chrono::Utc>>,
u64,
u64,
)> {
// Extract workspace.
// RFC-025: prefer the primary Mount's first path, then fall back to the
// legacy config.project_paths, then workspace_dir, then temp.
let workspace = if !mount_paths.is_empty() {
mount_paths[0].clone()
} else if !config.project_paths.is_empty() {
config.project_paths[0].clone()
} else if let Some(ref ws) = config.workspace_dir {
ws.clone()
} else {
std::env::temp_dir()
.join("oxios-agent-workspace")
.join(agent_id.to_string())
};
// Ensure workspace exists.
let _ = std::fs::create_dir_all(&workspace);
tracing::debug!(workspace = %workspace.display(), "Agent workspace scoped");
// Ensure all paths the agent might access are in allowed_paths.
//
// AgentLifecycleManager::ensure_permissions() adds kernel.workspace (~/.oxios/workspace),
// but the agent operates in different directories depending on context:
//
// 1. Process CWD — oxi-sdk 0.35+ bakes `workspace_dir` into file tools
// via `with_cwd`, so ReadTool/LsTool resolve relatives against the
// workspace, NOT the process CWD. However, oxios's own CSpace tools
// (kernel-bridge tools wrapped in GatedTool) and bash/exec
// subprocesses may still resolve against the process CWD. We grant
// it as a safety net so those tools aren't denied by GatedTool.
// 2. The designated workspace — computed from mount_paths / workspace_dir / temp.
// 3. Kernel workspace — state store path for seeds, sessions, etc.
// 4. /tmp -- general temp file access.
//
// All four must be in allowed_paths before GatedTool wraps any tool.
{
use crate::access_manager::{Role, Subject};
let agent_name = format!("agent-{agent_id}");
let mut am = kernel_handle.exec.access_manager().lock();
let perms = am.get_or_create_permissions(&agent_name);
// 1. CWD -- critical: oxi-sdk resolves relative paths here
if let Ok(cwd) = std::env::current_dir() {
let cwd_pattern = format!("{}/**", cwd.to_string_lossy().trim_end_matches('/'));
if !perms.allowed_paths.iter().any(|p| p == &cwd_pattern) {
perms.allow_path(&cwd_pattern);
tracing::debug!(
agent = %agent_name,
path = %cwd_pattern,
"Added CWD to agent allowed paths"
);
}
}
// 2. Designated workspace
let ws_pattern = format!("{}/**", workspace.to_string_lossy().trim_end_matches('/'));
if !perms.allowed_paths.iter().any(|p| p == &ws_pattern) {
perms.allow_path(&ws_pattern);
}
// 2b. RFC-025: every bound Mount grants path access.
// This fixes the latent gap where only project_paths[0] was
// accessible — now all Mount paths (multi-path work) are allowed.
// Parent patterns already covering a path are skipped.
for mount_path in mount_paths {
let pattern = format!("{}/**", mount_path.to_string_lossy().trim_end_matches('/'));
if !perms.allowed_paths.iter().any(|p| p == &pattern) {
perms.allow_path(&pattern);
tracing::debug!(
agent = %agent_name,
path = %pattern,
"Added Mount path to agent allowed paths (RFC-025)"
);
}
}
// 3. Kernel workspace (state store path)
let kernel_ws = kernel_handle
.state
.workspace_path()
.to_string_lossy()
.to_string();
let kernel_ws_pattern = format!("{}/**", kernel_ws.trim_end_matches('/'));
if kernel_ws_pattern != ws_pattern
&& !perms.allowed_paths.iter().any(|p| p == &kernel_ws_pattern)
{
perms.allow_path(&kernel_ws_pattern);
}
// 4. /tmp -- for general temp file access
if !perms.allowed_paths.iter().any(|p| p == "/tmp/**") {
perms.allow_path("/tmp/**");
}
// Ensure RBAC Superuser role so AccessGate Layer 1 passes.
let rbac_subject = Subject::Agent(agent_id);
am.rbac_manager_mut()
.assign_role(rbac_subject, Role::Superuser);
}
// Start distributed trace span for this agent execution.
let _trace_guard = crate::observability::tracer().start(
format!("seed-{}", &seed_id.to_string()[..8]).as_str(),
oxi_sdk::SpanKind::Agent,
);
// ── Register tools based on CSpace (with access gate) ──
let registry = ToolRegistry::new();
let search_cache = Arc::new(SearchCache::new());
// Build agent context for security
let agent_context = AgentContext {
agent_id,
agent_name: format!("agent-{agent_id}"),
cspace: Arc::new(cspace.clone()),
};
// Build audit sink: TrailAuditSink (Merkle chain + JSONL) when audit_trail
// is available, otherwise fall back to TracingAuditSink.
let audit_sink: Arc<dyn crate::access_manager::AuditSink> = if let Some(trail) = audit_trail {
let audit_path = kernel_handle
.state
.workspace_path()
.join("audit")
.join("access.jsonl");
Arc::new(TrailAuditSink::new(trail, audit_path))
} else {
Arc::new(TracingAuditSink)
};
// Build access gate from kernel's security infrastructure
let access_gate = Arc::new(AccessGate::new(
kernel_handle.exec.access_manager().clone(),
Arc::new(kernel_handle.exec.config_snapshot()),
audit_sink,
));
register_tools_from_cspace_gated(
®istry,
&kernel_handle,
&cspace,
search_cache,
agent_id,
access_gate,
agent_context,
);
tracing::info!(
seed_id = %seed_id,
capabilities = cspace.len(),
"Tools registered from CSpace"
);
// ── Build AgentConfig ──
//
// RFC-014 Phase D: `system_prompt` is also passed to the new
// `AgentBuilder::system_prompt()` (which overrides the value embedded
// in `AgentConfig` at build time). We clone here so the builder path
// can consume the value while the legacy `Agent::new_with_resolver`
// path still sees it in the config.
let agent_config = AgentConfig {
name: format!("agent-{agent_id}"),
description: None,
model_id: config.model_id.clone(),
system_prompt: Some(system_prompt.clone()),
timeout_seconds: 300,
temperature: Some(0.7),
max_tokens: Some(8192),
compaction_strategy: CompactionStrategy::Threshold(0.8),
compaction_instruction: None,
context_window: 128_000,
api_key: config.api_key.clone(),
workspace_dir: Some(workspace.clone()),
output_mode: None,
provider_options: config.provider_options.clone(),
};
// ── Build Agent (RFC-014 Phase D) ──
//
// Two paths:
// 1. `provider_rpm == 0` (common): use oxi-sdk 0.26.2's new
// `AgentBuilder` API. The builder unifies model resolution, provider
// creation, and (optionally) middleware wiring. Engine-level
// `authorizer` / `tracer` / `cost_tracker` are propagated through
// the new builder methods.
// 2. `provider_rpm > 0` (rare): keep the legacy
// `Agent::new_with_resolver` + `set_hooks` path because the
// AgentBuilder does not expose a way to inject a pre-built
// `ProviderPool` for rate-limited access. This is a deliberate
// scope-limit per RFC-014/phase-d-agentbuilder.md §2 "Provider
// 선택 로직은 보존".
let agent = if config.provider_rpm > 0 {
// ── Legacy path: rate-limited provider pool ──
let resolver: Arc<dyn ProviderResolver> = Arc::new(engine.oxi().clone());
let provider_name = engine.resolve_model(&config.model_id)?.provider;
let provider = engine.pooled_provider(&provider_name, config.provider_rpm)?;
// Build middleware pipeline.
let mut pipeline = oxi_sdk::MiddlewarePipeline::new();
if config.rate_limit_per_minute > 0 {
pipeline = pipeline.push(oxi_sdk::middleware::builtins::RateLimitMiddleware::new(
config.rate_limit_per_minute,
));
}
if config.token_budget > 0 {
pipeline = pipeline.push(oxi_sdk::middleware::builtins::TokenBudgetMiddleware::new(
config.token_budget,
));
}
if config.audit_tool_calls {
pipeline = pipeline.push(oxi_sdk::middleware::builtins::LoggingMiddleware::new(
tracing::Level::INFO,
));
}
// Create Agent with CSpace tool registry and provider resolver.
let agent = Arc::new(Agent::new_with_resolver(
provider,
agent_config,
Arc::new(registry),
resolver,
));
// Wire middleware pipeline → AgentHooks.
if !pipeline.is_empty() {
let terminate_flag = Arc::new(std::sync::atomic::AtomicBool::new(false));
let agent_id_for_hooks = agent_id.to_string();
let hooks = oxi_sdk::middleware::build_hooks(
Arc::new(pipeline),
agent_id_for_hooks,
terminate_flag,
);
agent.set_hooks(hooks);
}
agent
} else {
// ── New path: AgentBuilder (RFC-014 Phase D) ──
let mut builder = engine
.oxi()
.agent(agent_config)
.workspace(&workspace)
.system_prompt(system_prompt);
// CSpace-based tool registration is oxios-specific and is preserved.
//
// The builder's `.tool()` method takes `impl AgentTool + 'static`
// (a concrete value), but oxios' CSpace tools are `Arc<dyn AgentTool>`.
// The SDK does not expose a way to inject a pre-built `ToolRegistry`
// into the builder, so we register them on the agent's tool registry
// after `build()` returns. This keeps CSpace semantics intact.
//
// We capture the tool names now and apply them once the agent exists.
let cspace_tool_arcs: Vec<Arc<dyn oxi_sdk::AgentTool>> = registry
.names()
.into_iter()
.filter_map(|name| registry.get(&name))
.collect();
// Engine-level observability/security → AgentBuilder (new API).
if let Some(auth) = engine.authorizer() {
builder = builder.authorizer(auth.clone());
}
if let Some(tracer) = engine.tracer() {
builder = builder.tracer(tracer.clone());
}
if let Some(ct) = engine.cost_tracker() {
builder = builder.cost_tracker(ct.clone());
}
// Middleware: AgentBuilder convenience helpers replace the manual
// `MiddlewarePipeline` + `build_hooks()` + `set_hooks()` triple.
if config.rate_limit_per_minute > 0 {
builder = builder.with_rate_limit(config.rate_limit_per_minute);
}
if config.token_budget > 0 {
builder = builder.with_token_budget(config.token_budget);
}
if config.audit_tool_calls {
builder = builder.with_logging();
}
let built = builder.build()?;
let agent = Arc::new(built);
// Attach CSpace tools to the agent's tool registry.
// `Agent::tools()` returns the same `Arc<ToolRegistry>` that
// `AgentBuilder` populated, so `register_arc` is the canonical
// extension point for `Arc<dyn AgentTool>` values.
let agent_tools = agent.tools();
for tool in cspace_tool_arcs {
agent_tools.register_arc(tool);
}
agent
};
// Shared mutable state for the event callback.
let exec_state = Arc::new(Mutex::new(ExecuteState::default()));
let exec_state_cb = Arc::clone(&exec_state);
let memory_for_callback: Arc<MemoryManager> = (*kernel_handle.agents.memory_manager()).clone();
let session_id_for_callback = seed_id.to_string();
let model_id_for_callback = config.model_id.clone();
let agent_id_for_callback = agent_id.to_string();
let routing_stats_for_cb = routing_stats.clone();
// RFC-015: real-time event publishing for chat transparency.
// Falls back to None when the caller did not opt in.
let transparency_session: Option<String> = session_id.clone();
let kernel_handle_for_cb: Arc<KernelHandle> = Arc::clone(&kernel_handle);
// Run the agent with streaming events.
let result = agent
.run_streaming(prompt, move |event| {
let mut s = exec_state_cb.lock();
match event {
AgentEvent::ToolExecutionStart {
tool_name,
tool_call_id,
args,
context,
..
} => {
// Record start time and push a placeholder step.
let idx = s.trajectory_steps.len();
s.pending_tools
.insert(tool_call_id.clone(), (std::time::Instant::now(), idx));
s.tool_args_map.insert(
tool_call_id.clone(),
serde_json::to_string(&args).unwrap_or_default(),
);
s.tool_timestamps
.insert(tool_call_id.clone(), chrono::Utc::now());
s.tool_call_ids.push(tool_call_id.clone());
s.trajectory_steps
.push(oxios_memory::memory::sona::TrajectoryStep {
input: tool_name.clone(),
output: String::new(),
duration_ms: 0,
confidence: 0.0,
});
// RFC-015: broadcast tool start so Web UI can show progress.
if let Some(ref sid) = transparency_session {
let context_json = context
.as_ref()
.map(serde_json::to_value)
.transpose()
.unwrap_or(None);
let _ =
kernel_handle_for_cb
.infra
.publish(KernelEvent::ToolExecutionStarted {
session_id: sid.clone(),
tool_name: tool_name.clone(),
tool_call_id: tool_call_id.clone(),
tool_args: args.clone(),
context: context_json,
});
}
}
AgentEvent::ToolExecutionUpdate {
tool_call_id,
tool_name,
partial_result,
tab_id,
context,
} => {
// RFC-015: forward real-time progress to the event bus
// so the Web UI can show a spinner and progress text
// while the tool is still executing. Best-effort —
// publish failures (e.g. lagged subscribers) are ignored.
//
// `tab_id` and `context` come from oxi-agent 0.29+
// (ToolCallContext: PageVisit, WebSearch, etc.).
// Older agent versions won't send these — they default
// to None and the UI gracefully ignores them.
if let Some(ref sid) = transparency_session {
let context_json = context
.as_ref()
.map(serde_json::to_value)
.transpose()
.unwrap_or(None);
let _ = kernel_handle_for_cb.infra.publish(
KernelEvent::ToolExecutionProgress {
session_id: sid.clone(),
tool_call_id: tool_call_id.clone(),
tool_name: tool_name.clone(),
progress: partial_result,
tab_id,
context: context_json,
},
);
}
}
AgentEvent::ToolExecutionEnd {
tool_name,
tool_call_id,
is_error,
result,
..
} => {
if !is_error {
s.steps_completed += 1;
}
// Look up the exact step by tool_call_id.
let mut duration_ms: u64 = 0;
let mut summary = String::new();
if let Some((start, idx)) = s.pending_tools.remove(tool_call_id.as_str()) {
duration_ms = start.elapsed().as_millis() as u64;
if let Some(step) = s.trajectory_steps.get_mut(idx) {
summary = summarize_tool_result(&result.content, 200);
step.output = summary.clone();
step.duration_ms = duration_ms;
step.confidence = if is_error { 0.3 } else { 0.8 };
}
}
s.tool_error_map.insert(tool_call_id.clone(), is_error);
// RFC-015: broadcast tool completion.
if let Some(ref sid) = transparency_session {
let _ = kernel_handle_for_cb.infra.publish(
KernelEvent::ToolExecutionFinished {
session_id: sid.clone(),
tool_call_id: tool_call_id.clone(),
tool_name: tool_name.clone(),
duration_ms,
is_error,
output_summary: summary,
},
);
}
}
AgentEvent::AgentEnd {
messages,
stop_reason,
..
} => {
if let Some(oxi_sdk::Message::Assistant(a)) = messages.last() {
s.final_content = a.text_content();
}
// oxi 0.32.0: loop exits naturally when LLM produces text-only
// response (StopReason::Stop). Error/Aborted = failure.
// ToolUse should not occur at AgentEnd in 0.32.0 (the loop
// continues until text-only), but treat it as non-failure
// since tool calls were executed successfully.
s.success = matches!(stop_reason.as_deref(), Some("Stop") | Some("ToolUse"));
}
AgentEvent::Error { message, .. } => {
s.final_content = message.clone();
s.success = false;
}
AgentEvent::Usage {
input_tokens,
output_tokens,
} => {
// Accumulate totals for ExecutionResult.
s.total_input_tokens += input_tokens as u64;
s.total_output_tokens += output_tokens as u64;
// Record token usage to cost tracker (existing).
let agent_label = format!("agent-{agent_id_for_callback}");
crate::observability::cost_tracker().record(
&agent_label,
&oxi_sdk::Model::new(
&model_id_for_callback,
&model_id_for_callback,
oxi_sdk::Api::OpenAiCompletions,
"unknown",
"https://unknown.com",
),
oxi_sdk::TokenUsage {
input: input_tokens as u64,
output: output_tokens as u64,
cache_read: 0,
cache_write: 0,
},
);
// Record to routing stats (RFC-011).
if let Some(stats) = &routing_stats_for_cb {
let cost = crate::kernel_handle::engine_api::estimate_cost(
&model_id_for_callback,
input_tokens as u64,
output_tokens as u64,
);
stats.record_model_usage(&model_id_for_callback, cost);
}
// RFC-015: publish cumulative token usage.
if let Some(ref sid) = transparency_session {
let _ = kernel_handle_for_cb
.infra
.publish(KernelEvent::TokenUsageUpdate {
session_id: sid.clone(),
input_tokens: input_tokens as u64,
output_tokens: output_tokens as u64,
});
}
}
AgentEvent::Compaction {
event: CompactionEvent::Completed { result, .. },
} => {
handle_compaction(
result.summary.clone(),
session_id_for_callback.clone(),
memory_for_callback.clone(),
);
// RFC-015: compaction is a form of reasoning — expose it.
if let Some(ref sid) = transparency_session {
let _ =
kernel_handle_for_cb
.infra
.publish(KernelEvent::ReasoningFragment {
session_id: sid.clone(),
content: result.summary.clone(),
source: "compaction".to_string(),
});
}
}
_ => {}
}
})
.await;
// Record circuit breaker result after agent execution.
let circuit = get_llm_circuit_breaker();
if result.is_err() {
circuit.record_failure();
crate::metrics::get_metrics()
.llm_circuit_breaker_state
.set(1.0);
} else {
circuit.record_success();
crate::metrics::get_metrics()
.llm_circuit_breaker_state
.set(0.0);
}
if let Err(e) = result {
tracing::error!(seed_id = %seed_id, error = %e, "Agent failed");
let s = exec_state.lock();
return Ok((
format!("Agent failed: {e}"),
s.steps_completed,
false,
s.trajectory_steps.clone(),
agent,
s.tool_call_ids.clone(),
s.tool_args_map.clone(),
s.tool_error_map.clone(),
s.tool_timestamps.clone(),
s.total_input_tokens,
s.total_output_tokens,
));
}
let s = exec_state.lock();
tracing::info!(
seed_id = %seed_id,
steps = s.steps_completed,
success = s.success,
"Agent completed"
);
// Record trajectory to SONA learning engine (RFC-020 Phase 2).
// Fire-and-forget: don't block the result on learning.
if !s.trajectory_steps.is_empty()
&& let Some(sona) = kernel_handle.agents.memory_manager().sona_engine()
{
let steps = s.trajectory_steps.clone();
let success = s.success;
let sona = Arc::clone(sona);
let domain = infer_domain(&seed_goal);
tokio::spawn(async move {
let verdict = if success {
oxios_memory::memory::sona::Verdict::Success
} else {
oxios_memory::memory::sona::Verdict::Failure
};
let trajectory = oxios_memory::memory::sona::Trajectory::new(steps, verdict, &domain);
if let Err(e) = sona.record(trajectory).await {
tracing::debug!(error = %e, "SONA trajectory recording failed (non-fatal)");
}
});
}
Ok((
s.final_content.clone(),
s.steps_completed,
s.success,
s.trajectory_steps.clone(),
agent,
s.tool_call_ids.clone(),
s.tool_args_map.clone(),
s.tool_error_map.clone(),
s.tool_timestamps.clone(),
s.total_input_tokens,
s.total_output_tokens,
))
}
/// Summarize a tool result string to fit within `max_len` characters.
///
/// Uses char-aware truncation to avoid panicking on multi-byte UTF-8
/// (e.g., Korean, CJK, emoji).
fn summarize_tool_result(result: &str, max_len: usize) -> String {
let trimmed = result.trim();
if trimmed.chars().count() <= max_len {
return trimmed.to_string();
}
// Take the first line or truncate.
let first_line = trimmed.lines().next().unwrap_or("");
if first_line.chars().count() <= max_len {
first_line.to_string()
} else {
let truncated: String = first_line.chars().take(max_len - 3).collect();
format!("{truncated}...")
}
}
/// Truncate a JSON string representation to `max_len` chars for storage
/// in tool call records. Returns the original string if short enough,
/// otherwise truncates and appends "...".
fn truncate_json_str(json_str: &str, max_len: usize) -> String {
if json_str.len() <= max_len {
return json_str.to_string();
}
let truncated: String = json_str.chars().take(max_len - 3).collect();
format!("{truncated}...")
}
/// Infer a domain category from a seed goal for SONA trajectory grouping.
///
/// Extracts the core verb + object from the goal to create a meaningful
/// domain label. Falls back to "general" for unrecognizable patterns.
fn infer_domain(goal: &str) -> String {
let lower = goal.to_lowercase();
let keywords: Vec<&str> = lower.split_whitespace().take(8).collect();
// Check for known domain indicators.
if keywords.iter().any(|k| {
[
"test",
"tests",
"spec",
"testing",
"assert",
"unit test",
"integration",
]
.contains(k)
}) {
return "testing".to_string();
}
if keywords
.iter()
.any(|k| ["deploy", "release", "publish", "ship"].contains(k))
{
return "deployment".to_string();
}
if keywords
.iter()
.any(|k| ["fix", "bug", "patch", "repair", "debug"].contains(k))
{
return "bugfix".to_string();
}
if keywords
.iter()
.any(|k| ["refactor", "restructure", "reorganize", "rewrite"].contains(k))
{
return "refactoring".to_string();
}
if keywords
.iter()
.any(|k| ["doc", "document", "readme", "guide", "explain"].contains(k))
{
return "documentation".to_string();
}
if keywords
.iter()
.any(|k| ["build", "create", "implement", "add", "make", "new"].contains(k))
{
return "development".to_string();
}
if keywords
.iter()
.any(|k| ["analyze", "review", "audit", "inspect", "check"].contains(k))
{
return "analysis".to_string();
}
if keywords
.iter()
.any(|k| ["config", "setup", "install", "configure", "init"].contains(k))
{
return "configuration".to_string();
}
// Fallback: first 2 meaningful words
let meaningful: Vec<&str> = lower
.split_whitespace()
.filter(|w| w.len() > 2)
.take(2)
.collect();
if meaningful.len() >= 2 {
meaningful.join("_")
} else {
"general".to_string()
}
}
/// Handle compaction completion by storing the summary as a Warm memory.
///
/// Extracts the compaction summary from the event and spawns a background
/// task to persist it via MemoryManager. This replaces the inline 30-line
/// block that was previously in the event callback.
fn handle_compaction(summary: String, session_id: String, memory_manager: Arc<MemoryManager>) {
let entry = MemoryEntry {
id: uuid::Uuid::new_v4().to_string(),
memory_type: MemoryType::Conversation,
tier: crate::memory::MemoryTier::Warm,
content: summary,
content_hash: 0,
source: "compaction".to_string(),
session_id: Some(session_id),
tags: vec![],
importance: 0.5,
pinned: false,
protection: crate::memory::ProtectionLevel::None,
auto_classified: false,
session_appearances: 0,
user_corrected: false,
seen_in_sessions: vec![],
created_at: chrono::Utc::now(),
accessed_at: chrono::Utc::now(),
modified_at: chrono::Utc::now(),
access_count: 0,
decay_score: 1.0,
compaction_level: 0,
compacted_from: vec![],
related_ids: vec![],
contradicts: None,
};
tokio::spawn(async move {
if let Err(e) = memory_manager.remember(entry).await {
tracing::warn!(error = %e, "Failed to save compaction summary");
}
});
}
/// Build a system prompt from the Seed's goal, constraints, persona,
/// and optionally a capability index and kernel manifest.
///
/// Note: SKILL.md content is no longer injected here. Capabilities are
/// surfaced through the CSpace tool set + semantic retrieval instead.
fn build_system_prompt(
seed: &Seed,
persona_prompt: Option<&str>,
capabilities_xml: Option<&str>,
kernel_manifest: Option<&str>,
workspace_context: Option<&str>,
) -> String {
let mut prompt = String::from(
"You are an autonomous agent in the Oxios operating system.\n\
You execute Seeds — immutable specifications with goals, constraints, and\n\
acceptance criteria.\n\n\
## Available Tools\n\
You have the following tools:\n\
- **File tools**: read, write, edit files; grep, find, ls for searching\n\
- **Web tools**: web_search for searching the web, get_search_results for retrieving cached results\n\
- **Exec**: run shell commands\n\
- **Memory tools**: memory_read, memory_write, memory_search — agent's internal recall\n\
- **Knowledge**: knowledge — personal markdown vault for documents and notes\n\
- **Kernel tools**: agent, project, persona, cron, security, budget, resource\n\n\
**Important**: When the task involves fetching information from the internet,\n\
websites, or online services, use `web_search` first — do NOT search local files.\n\
When the task asks to \"get\", \"fetch\", \"find online\", or \"look up\" something\n\
from the web, use `web_search`.\n",
);
prompt.push_str(&format!("\n## Goal\n{}\n", seed.goal));
// Preserve user's original wording so the agent sees exact language,
// filenames, and nuances that may have been abstracted in the goal.
if !seed.original_request.is_empty() && seed.original_request != seed.goal {
prompt.push_str(&format!(
"\n## User's Original Request\n{}\n",
seed.original_request
));
}
if !seed.constraints.is_empty() {
prompt.push_str("\n## Constraints\n");
for (i, c) in seed.constraints.iter().enumerate() {
prompt.push_str(&format!("{}. {}\n", i + 1, c));
}
}
if !seed.acceptance_criteria.is_empty() {
prompt.push_str("\n## Acceptance Criteria\n");
for (i, c) in seed.acceptance_criteria.iter().enumerate() {
prompt.push_str(&format!("{}. {}\n", i + 1, c));
}
}
// ── Workspace Context (RFC-025) ──
// Inject active Mounts + project instructions AFTER the goal/constraints
// and BEFORE the persona, so the agent sees its workspace before it acts.
if let Some(ctx) = workspace_context.filter(|s| !s.trim().is_empty()) {
prompt.push_str("\n## Workspace Context\n");
prompt.push_str(ctx);
prompt.push('\n');
}
if !seed.ontology.is_empty() {
prompt.push_str("\n## Domain Entities\n");
for e in &seed.ontology {
prompt.push_str(&format!(
"- **{}** ({}): {}\n",
e.name, e.entity_type, e.description
));
}
}
// Inject persona system prompt
if let Some(pp) = persona_prompt {
prompt.push_str("\n## Persona\n");
prompt.push_str(pp);
prompt.push('\n');
}
// Inject semantic capability index (from ToolRetriever)
if let Some(xml) = capabilities_xml {
prompt.push_str("\n## Available Capabilities\n");
prompt.push_str("The following capabilities are relevant to your goal. ");
prompt.push_str("Use the `read` tool to load SKILL.md for any program.\n\n");
prompt.push_str(xml);
prompt.push('\n');
}
// Inject kernel manifest (from CSpace)
if let Some(manifest) = kernel_manifest {
prompt.push('\n');
prompt.push_str(manifest);
prompt.push('\n');
}
// Execution environment guidance
prompt.push_str(
"\n## Execution Protocol\n\
1. UNDERSTAND — Read the Seed completely before acting.\n\
2. PLAN — Determine the minimal set of actions needed.\n\
3. EXECUTE — Use tools to accomplish the goal. Prefer the simplest approach.\n\
4. VERIFY — After each action, check the result: created a file? read it back.\n\
5. REPORT — Summarize how each acceptance criterion was met, with evidence.\n\n\
## Hard Boundaries\n\
- NEVER modify files outside the workspace scope\n\
- NEVER execute destructive commands without confirming scope\n\
- NEVER claim completion without evidence — show the output, not your opinion\n\
- NEVER add features or improvements beyond the Seed scope\n\
- If you cannot complete the Seed, say so and explain WHY\n\n\
## Scope Guard\n\
The Seed defines your universe. Do not:\n\
- Refactor code the Seed didn't mention\n\
- Add tests the Seed didn't require\n\
- Change configuration the Seed didn't specify\n\
- \"Improve\" anything beyond what the acceptance criteria demand\n\n\
## Error Handling\n\
- If a tool fails, read the error message carefully before retrying\n\
- If a command fails, do NOT immediately retry with --force or sudo\n\
- If stuck after 3 attempts, report the blocker rather than continuing to fail\n\n\
## Shape Matching\n\
Match your output to the task: simple task → concise response.\n\
Do not write 50 lines when 5 would do.\n\
Use `exec` for all command execution (git, gh, osascript, etc.).",
);
prompt
}
/// Build the user prompt from the seed.
fn build_user_prompt(seed: &Seed) -> String {
format!(
"Execute the following goal:\n\n{}\n\nAcceptance criteria:\n{}",
seed.goal,
seed.acceptance_criteria
.iter()
.enumerate()
.map(|(i, c)| format!("{}. {}", i + 1, c))
.collect::<Vec<_>>()
.join("\n")
)
}
impl std::fmt::Debug for AgentRuntime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AgentRuntime")
.field("model_id", &self.config.model_id)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use async_trait::async_trait;
use oxi_sdk::{AgentTool, ToolContext, ToolError};
use oxios_ouroboros::Entity;
use serde_json::Value;
/// A test tool that does nothing — used to populate the registry.
struct DummyTool {
name: String,
}
#[async_trait]
impl AgentTool for DummyTool {
fn name(&self) -> &str {
&self.name
}
fn label(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
"Test tool"
}
fn parameters_schema(&self) -> Value {
serde_json::json!({"type": "object"})
}
async fn execute(
&self,
_tool_call_id: &str,
_params: Value,
_shutdown: Option<tokio::sync::oneshot::Receiver<()>>,
_ctx: &ToolContext,
) -> Result<oxi_sdk::AgentToolResult, ToolError> {
Ok(oxi_sdk::AgentToolResult::success("ok"))
}
}
/// Test that requires_tools validation passes when all tools are present.
#[test]
fn test_requires_tools_validation_passes() {
let registry = ToolRegistry::new();
registry.register(DummyTool {
name: "read".into(),
});
registry.register(DummyTool {
name: "exec".into(),
});
let missing = registry.missing(&["read", "exec"]);
assert!(
missing.is_empty(),
"Expected no missing tools, got: {:?}",
missing
);
}
/// Test that requires_tools validation fails when a tool is missing.
#[test]
fn test_requires_tools_validation_fails() {
let registry = ToolRegistry::new();
registry.register(DummyTool {
name: "read".into(),
});
let missing = registry.missing(&["read", "exec", "nonexistent"]);
assert_eq!(missing, vec!["exec", "nonexistent"]);
}
#[test]
fn test_build_system_prompt_includes_goal() {
let seed = Seed {
id: uuid::Uuid::new_v4(),
goal: "Build a web server".into(),
constraints: vec!["Must use Rust".into()],
acceptance_criteria: vec!["Server responds to requests".into()],
ontology: vec![Entity {
name: "HttpServer".into(),
entity_type: "struct".into(),
description: "The main server struct".into(),
}],
created_at: chrono::Utc::now(),
generation: 0,
parent_seed_id: None,
cspace_hint: None,
original_request: String::new(),
output_schema: None,
project_id: None,
workspace_context: None,
mount_paths: Vec::new(),
};
let prompt = build_system_prompt(&seed, None, None, None, None);
assert!(prompt.contains("Build a web server"));
assert!(prompt.contains("Must use Rust"));
assert!(prompt.contains("Server responds to requests"));
assert!(prompt.contains("HttpServer"));
assert!(prompt.contains("struct"));
}
#[test]
fn test_build_system_prompt_empty() {
let seed = Seed {
id: uuid::Uuid::new_v4(),
goal: "Test goal".into(),
constraints: vec![],
acceptance_criteria: vec![],
ontology: vec![],
created_at: chrono::Utc::now(),
generation: 0,
parent_seed_id: None,
cspace_hint: None,
original_request: String::new(),
output_schema: None,
project_id: None,
workspace_context: None,
mount_paths: Vec::new(),
};
let prompt = build_system_prompt(&seed, None, None, None, None);
assert!(prompt.contains("Test goal"));
}
#[test]
fn test_infer_domain_testing() {
assert_eq!(infer_domain("run all unit tests for the kernel"), "testing");
}
#[test]
fn test_infer_domain_deployment() {
assert_eq!(
infer_domain("deploy the web service to production"),
"deployment"
);
}
#[test]
fn test_infer_domain_bugfix() {
assert_eq!(infer_domain("fix the null pointer error in main"), "bugfix");
}
#[test]
fn test_infer_domain_development() {
assert_eq!(
infer_domain("create a new REST API endpoint"),
"development"
);
}
#[test]
fn test_infer_domain_analysis() {
assert_eq!(
infer_domain("review the code for security issues"),
"analysis"
);
}
#[test]
fn test_infer_domain_fallback() {
let domain = infer_domain("optimize performance metrics");
// Should fall back to first 2 meaningful words
assert!(!domain.is_empty());
}
}