Entropic 2.9.4
Local-first agentic inference engine
Loading...
Searching...
No Matches
delegation.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
19
20#include <nlohmann/json.hpp>
21
22#include <algorithm>
23
24static auto logger = entropic::log::get("core.delegation");
25
26namespace entropic {
27
28// ── Construction ─────────────────────────────────────────
29
41 RunChildLoopFn run_child,
42 void* run_child_data,
43 const TierResolutionInterface& tier_resolution,
44 const std::filesystem::path& repo_dir,
45 SandboxManager* sandbox_mgr)
46 : run_child_fn_(run_child),
47 run_child_data_(run_child_data),
48 tier_res_(tier_resolution),
49 sandbox_mgr_(sandbox_mgr),
50 repo_dir_(repo_dir) {
51}
52
60 todo_callbacks_ = callbacks;
61}
62
71 ScopedSandbox::SwapDirFn swap_fn, void* user_data) {
72 swap_dir_fn_ = swap_fn;
73 swap_dir_data_ = user_data;
74}
75
83 storage_ = storage;
84}
85
95 ent_decision_t (*on_start)(const ent_delegation_request_t*, void*),
96 ent_decision_t (*on_complete)(const ent_delegation_result_t*, void*),
97 void* user_data) {
98 delegation_start_cb_ = on_start;
99 delegation_complete_cb_ = on_complete;
100 delegation_cb_data_ = user_data;
101}
102
119ent_decision_t DelegationManager::fire_start_cb(
120 const std::string& delegation_id,
121 const std::string& target_tier,
122 const std::string& task,
123 int depth,
124 bool is_pipeline) {
125 if (delegation_start_cb_ == nullptr) {
126 return ENT_DECISION_ACCEPT;
127 }
129 req.delegation_id = delegation_id.c_str();
130 req.target_tier = target_tier.c_str();
131 req.task = task.c_str();
132 req.depth = depth;
133 req.is_pipeline = is_pipeline ? 1 : 0;
134 // Exception shield: per docs/architecture-cpp.md design rule #6,
135 // exceptions do not cross .so boundaries. A buggy consumer
136 // throwing a C++ exception out of the callback would otherwise
137 // unwind through the engine's stack with undefined cleanup. Fail
138 // safe by treating a throw as REJECT (gh#29 hardening).
139 try {
140 return delegation_start_cb_(&req, delegation_cb_data_);
141 } catch (...) {
142 logger->warn("delegation_start_cb threw for {}; treating as "
143 "REJECT (gh#29 exception shield)",
144 delegation_id);
145 return ENT_DECISION_REJECT;
146 }
147}
148
177 const SandboxInfo& sb_info, const SandboxResult& sandbox_result,
178 const DelegationResult& result,
179 const std::vector<const char*>& files_c, size_t files_len) {
181 res.delegation_id = sb_info.delegation_id.c_str();
182 res.target_tier = result.target_tier.c_str();
183 res.success = result.success ? 1 : 0;
184 res.summary = result.summary.c_str();
185 res.patch = sandbox_result.patch.c_str();
186 res.patch_len = sandbox_result.patch.size();
187 res.files_touched = files_c.data();
188 res.files_touched_len = files_len;
189 return res;
190}
191
197void DelegationManager::deliver_sandbox_result(
198 const SandboxInfo& sb_info,
199 const SandboxResult& sandbox_result,
200 const DelegationResult& result) {
201
202 if (delegation_complete_cb_ == nullptr) {
203 persist_pending_patch(sb_info, sandbox_result,
204 "no complete callback registered");
205 return;
206 }
207
208 std::vector<std::string> files_owned;
209 files_owned.reserve(sandbox_result.files_touched.size());
210 for (const auto& p : sandbox_result.files_touched) {
211 files_owned.push_back(p.string());
212 }
213 std::vector<const char*> files_c;
214 files_c.reserve(files_owned.size() + 1);
215 for (const auto& s : files_owned) { files_c.push_back(s.c_str()); }
216 files_c.push_back(nullptr);
217
219 sb_info, sandbox_result, result, files_c, files_owned.size());
220
221 ent_decision_t decision =
222 invoke_complete_cb(res, sb_info.delegation_id);
223 if (decision == ENT_DECISION_REJECT) {
224 persist_pending_patch(sb_info, sandbox_result,
225 "consumer REJECTED");
226 } else {
227 logger->info("Delegation {}: consumer ACCEPTED ({} files, "
228 "{} bytes)",
229 sb_info.delegation_id,
230 sandbox_result.files_touched.size(),
231 sandbox_result.patch.size());
232 }
233}
234
243ent_decision_t DelegationManager::invoke_complete_cb(
244 const ent_delegation_result_t& res, const std::string& delegation_id) {
245 // Exception shield: a buggy consumer must never unwind through the
246 // engine. Treat throw as REJECT so the patch is preserved on disk
247 // for inspection (gh#29 hardening — same policy as fire_start_cb).
248 try {
249 return delegation_complete_cb_(&res, delegation_cb_data_);
250 } catch (...) {
251 logger->warn("delegation_complete_cb threw for {}; treating as "
252 "REJECT (patch preserved to pending/)", delegation_id);
253 return ENT_DECISION_REJECT;
254 }
255}
256
262void DelegationManager::persist_pending_patch(
263 const SandboxInfo& sb_info,
264 const SandboxResult& sandbox_result,
265 const char* reason) {
266 auto path = sandbox_mgr_->write_pending_patch(
267 sb_info.delegation_id, sandbox_result.patch);
268 if (path) {
269 logger->warn("Delegation {}: {}; patch saved to {} "
270 "({} files, {} bytes)",
271 sb_info.delegation_id, reason, path->string(),
272 sandbox_result.files_touched.size(),
273 sandbox_result.patch.size());
274 }
275}
276
277// ── Single delegation ────────────────────────────────────
278
302std::optional<DelegationResult>
303DelegationManager::check_delegation_preconditions(
304 const ChildContextInfo& info,
305 const std::string& target_tier,
306 const std::string& task,
307 const std::string& del_id,
308 int depth,
309 std::optional<SandboxInfo>& sb_info) {
310 std::optional<DelegationResult> early;
311 if (!info.valid) {
312 logger->error("Tier '{}' not found", target_tier);
313 early = DelegationResult{
314 "Unknown tier: " + target_tier, false, target_tier, task};
315 } else if (fire_start_cb(del_id, target_tier, task, depth, false)
316 == ENT_DECISION_REJECT) {
317 logger->info("Delegation {} ({}) rejected by start callback",
318 del_id, target_tier);
319 early = DelegationResult{
320 "Delegation rejected by consumer", false, target_tier, task};
321 } else if (sandbox_mgr_ != nullptr) {
322 sb_info = sandbox_mgr_->create_sandbox(del_id);
323 if (!sb_info.has_value()) {
324 // gh#33 bug 2 (v2.1.6): pre-2.1.6 a failed create_sandbox
325 // silently fell through to running the child against the
326 // parent's cwd and surfaced an opaque
327 // "(No response from delegate)" on later failure. The
328 // sandbox-unavailable mode is now consumer-visible.
329 logger->error(
330 "Delegation {} ({}): session sandbox unavailable",
331 del_id, target_tier);
332 early = DelegationResult{
333 "(DELEGATION FAILED: session sandbox unavailable)",
334 false, target_tier, task};
335 }
336 }
337 return early;
338}
339
351 LoopContext& parent_ctx,
352 const std::string& target_tier,
353 const std::string& task,
354 std::optional<int> max_turns) {
355
356 logger->info("Delegation: target_tier='{}', task='{}', depth={}",
357 target_tier, task,
358 parent_ctx.delegation_depth + 1);
359 auto info = tier_res_.resolve_tier
360 ? tier_res_.resolve_tier(target_tier, tier_res_.user_data)
362
363 std::string del_id =
364 "d" + std::to_string(parent_ctx.delegation_depth + 1);
365 std::optional<SandboxInfo> sb_info;
366 if (auto early = check_delegation_preconditions(
367 info, target_tier, task, del_id,
368 parent_ctx.delegation_depth + 1, sb_info)) {
369 return *early;
370 }
371
372 auto child_ctx = build_child_context(parent_ctx, info, task);
373 child_ctx.locked_tier = target_tier;
374
375 DelegationResult result;
376
377 if (sb_info && swap_dir_fn_ != nullptr) {
378 ScopedSandbox scope(swap_dir_fn_, swap_dir_data_,
379 sb_info->path, repo_dir_);
380 result = run_child(child_ctx, target_tier, task, max_turns);
381 } else {
382 result = run_child(child_ctx, target_tier, task, max_turns);
383 }
384
385 finalize_sandbox_for(sb_info, result);
386 return result;
387}
388
412LoopContext DelegationManager::build_resumed_child_context(
413 const LoopContext& parent_ctx,
414 const ChildContextInfo& info,
415 const std::string& target_tier,
416 const std::string& task,
417 std::vector<Message> seed_history) {
418 LoopContext child_ctx;
419 child_ctx.delegation_depth = parent_ctx.delegation_depth + 1;
420 child_ctx.delegation_ancestor_tiers =
421 parent_ctx.delegation_ancestor_tiers;
422 child_ctx.delegation_ancestor_tiers.push_back(target_tier);
423 child_ctx.parent_conversation_id = parent_ctx.conversation_id;
424 child_ctx.all_tools = info.tools;
425 child_ctx.active_phase = "default";
426 child_ctx.locked_tier = target_tier;
427 child_ctx.messages = std::move(seed_history);
428 bool has_system = !child_ctx.messages.empty()
429 && child_ctx.messages.front().role == "system";
430 if (!has_system && !info.system_prompt.empty()) {
431 Message sys;
432 sys.role = "system";
433 sys.content = info.system_prompt;
434 child_ctx.messages.insert(child_ctx.messages.begin(),
435 std::move(sys));
436 }
437 Message user;
438 user.role = "user";
439 user.content = task;
440 if (!info.completion_instructions.empty()) {
441 user.content += "\n\n" + info.completion_instructions;
442 }
443 child_ctx.messages.push_back(std::move(user));
444 return child_ctx;
445}
446
453 LoopContext& parent_ctx,
454 const std::string& target_tier,
455 const std::string& task,
456 std::vector<Message> seed_history,
457 std::optional<int> max_turns) {
458
459 logger->info("Resume delegation: target_tier='{}' task='{}' "
460 "history_messages={}",
461 target_tier, task, seed_history.size());
462 auto info = tier_res_.resolve_tier
463 ? tier_res_.resolve_tier(target_tier, tier_res_.user_data)
465
466 std::string del_id =
467 "d" + std::to_string(parent_ctx.delegation_depth + 1) + "r";
468 std::optional<SandboxInfo> sb_info;
469 if (auto early = check_delegation_preconditions(
470 info, target_tier, task, del_id,
471 parent_ctx.delegation_depth + 1, sb_info)) {
472 return *early;
473 }
474
475 auto child_ctx = build_resumed_child_context(
476 parent_ctx, info, target_tier, task, std::move(seed_history));
477
478 DelegationResult result;
479 if (sb_info && swap_dir_fn_ != nullptr) {
480 ScopedSandbox scope(swap_dir_fn_, swap_dir_data_,
481 sb_info->path, repo_dir_);
482 result = run_child(child_ctx, target_tier, task, max_turns);
483 } else {
484 result = run_child(child_ctx, target_tier, task, max_turns);
485 }
486 finalize_sandbox_for(sb_info, result);
487 return result;
488}
489
490// ── Pipeline ─────────────────────────────────────────────
491
513static std::string pipeline_context(
514 size_t stage_idx, size_t total,
515 const std::vector<std::string>& stages,
516 const std::string& prior_output) {
517 std::string ctx = "[PIPELINE CONTEXT]\n";
518 ctx += "Stage " + std::to_string(stage_idx + 1) +
519 " of " + std::to_string(total) + "\n";
520 ctx += "Role: " + stages[stage_idx] + "\n";
521 ctx += "Stay within your role. Do not perform work "
522 "outside your stage's responsibility.\n";
523 if (!prior_output.empty()) {
524 ctx += "\n[PRIOR STAGE OUTPUT]\n";
525 ctx += prior_output;
526 ctx += "\n";
527 }
528 ctx += "\n";
529 return ctx;
530}
531
548 LoopContext& parent_ctx,
549 const std::vector<std::string>& stages,
550 const std::string& task) {
551
552 logger->info("Pipeline: {} stages, task='{}'", stages.size(), task);
553
554 // gh#29 (v2.1.5): single pre-flight gate for the whole pipeline.
555 auto first = stages.empty() ? std::string{} : stages.front();
556 if (fire_start_cb("pipeline", first, task,
557 parent_ctx.delegation_depth + 1, true)
558 == ENT_DECISION_REJECT) {
559 logger->info("Pipeline rejected by start callback");
560 return {"Pipeline rejected by consumer", false, first, task};
561 }
562
563 // Shared sandbox for the entire pipeline (gh#29). Each stage runs
564 // inside the same directory so later stages observe earlier stages'
565 // file edits — preserving the v2.1.4 forward-carry behavior.
566 std::optional<SandboxInfo> shared_sb;
567 if (sandbox_mgr_ != nullptr) {
568 shared_sb = sandbox_mgr_->create_sandbox("pipeline");
569 if (!shared_sb.has_value()) {
570 // gh#33 bug 2 (v2.1.6): see execute_delegation comment.
571 logger->error(
572 "Pipeline ({} stages): session sandbox unavailable",
573 stages.size());
574 return {"(DELEGATION FAILED: session sandbox unavailable)",
575 false, first, task};
576 }
577 }
578
579 DelegationResult last_result;
580 last_result.task = task;
581
582 for (size_t i = 0; i < stages.size(); ++i) {
583 if (!run_pipeline_stage(parent_ctx, stages, i, task,
584 shared_sb, last_result)) {
585 break;
586 }
587 }
588
589 finalize_sandbox_for(shared_sb, last_result);
590 return last_result;
591}
592
611bool DelegationManager::run_pipeline_stage(
612 LoopContext& parent_ctx,
613 const std::vector<std::string>& stages,
614 size_t stage_idx,
615 const std::string& task,
616 const std::optional<SandboxInfo>& shared_sb,
617 DelegationResult& last_result) {
618
619 const auto& tier_name = stages[stage_idx];
620 std::string prior = (stage_idx == 0) ? std::string{} : last_result.summary;
621 std::string stage_task =
622 pipeline_context(stage_idx, stages.size(), stages, prior) + task;
623
624 auto info = tier_res_.resolve_tier
625 ? tier_res_.resolve_tier(tier_name, tier_res_.user_data)
626 : ChildContextInfo{};
627
628 if (!info.valid) {
629 logger->error("Pipeline stage {}: tier '{}' not found",
630 stage_idx, tier_name);
631 last_result.success = false;
632 last_result.summary = "Unknown tier: " + tier_name;
633 return false;
634 }
635
636 auto child_ctx = build_child_context(parent_ctx, info, stage_task);
637 child_ctx.locked_tier = tier_name;
638
639 if (shared_sb && swap_dir_fn_ != nullptr) {
640 ScopedSandbox scope(swap_dir_fn_, swap_dir_data_,
641 shared_sb->path, repo_dir_);
642 last_result = run_child(child_ctx, tier_name, stage_task,
643 std::nullopt);
644 } else {
645 last_result = run_child(child_ctx, tier_name, stage_task,
646 std::nullopt);
647 }
648
649 logger->info("Pipeline stage {} ({}): {}", stage_idx, tier_name,
650 last_result.success ? "complete" : "failed");
651 return last_result.success;
652}
653
654// ── Child context ────────────────────────────────────────
655
665LoopContext DelegationManager::build_child_context(
666 const LoopContext& parent_ctx,
667 const ChildContextInfo& info,
668 const std::string& task) {
669
670 LoopContext child;
671 child.delegation_depth = parent_ctx.delegation_depth + 1;
672 // gh#48 (regression fix, v2.7.4): propagate the parent conversation id so
673 // the storage create_delegation INSERT satisfies the delegations→
674 // conversations FK. Without this the row is rejected, the delegations
675 // table stays empty, and entropic.followup returns the no-matches
676 // sentinel. (Present in build_resumed_child_context; lost here when the
677 // child-context builders were extracted.)
678 child.parent_conversation_id = parent_ctx.conversation_id;
679 // P1-9: propagate ancestor chain + append parent tier so the
680 // child can reject cycles (A→B→A) before executing.
681 child.delegation_ancestor_tiers = parent_ctx.delegation_ancestor_tiers;
682 if (!parent_ctx.locked_tier.empty()) {
683 child.delegation_ancestor_tiers.push_back(parent_ctx.locked_tier);
684 }
685 child.locked_tier = info.system_prompt.empty()
686 ? parent_ctx.locked_tier : "";
687 child.all_tools = info.tools;
688 child.active_phase = "default";
689
690 // System prompt as first message
691 Message sys;
692 sys.role = "system";
693 sys.content = info.system_prompt;
694 child.messages.push_back(std::move(sys));
695
696 // Task as user message (with completion instructions)
697 std::string user_content = task;
698 if (!info.completion_instructions.empty()) {
699 user_content += "\n\n" + info.completion_instructions;
700 }
701 Message user;
702 user.role = "user";
703 user.content = std::move(user_content);
704 child.messages.push_back(std::move(user));
705
706 return child;
707}
708
716std::string DelegationManager::extract_summary(
717 const LoopContext& child_ctx) const {
718 // Prefer explicit completion summary
719 auto it = child_ctx.metadata.find("explicit_completion_summary");
720 if (it != child_ctx.metadata.end() && !it->second.empty()) {
721 return it->second;
722 }
723
724 // Fall back to last assistant message
725 for (auto rit = child_ctx.messages.rbegin();
726 rit != child_ctx.messages.rend(); ++rit) {
727 if (rit->role == "assistant" && !rit->content.empty()) {
728 return rit->content;
729 }
730 }
731
732 return "(No response from delegate)";
733}
734
755std::string DelegationManager::create_storage_record(
756 LoopContext& child_ctx, const std::string& target_tier,
757 const std::string& task, std::optional<int> max_turns) {
758 if (!storage_ || !storage_->create_delegation) {
759 return "";
760 }
761 std::string del_id, child_conv_id;
762 auto src_tier = child_ctx.locked_tier.empty()
763 ? "root" : child_ctx.locked_tier.c_str();
764 storage_->create_delegation(
765 child_ctx.parent_conversation_id.c_str(),
766 src_tier, target_tier.c_str(), task.c_str(),
767 max_turns.value_or(0), del_id, child_conv_id,
768 storage_->user_data);
769 child_ctx.conversation_id = child_conv_id;
770 return del_id;
771}
772
780void DelegationManager::complete_storage_record(
781 const std::string& delegation_id,
782 const DelegationResult& result) {
783 if (!storage_ || !storage_->complete_delegation
784 || delegation_id.empty()) {
785 return;
786 }
787 const char* status = result.success ? "completed" : "failed";
788 storage_->complete_delegation(
789 delegation_id.c_str(), status,
790 result.summary.c_str(), storage_->user_data);
791}
792
803DelegationResult DelegationManager::run_child(
804 LoopContext& child_ctx,
805 const std::string& target_tier,
806 const std::string& task,
807 std::optional<int> max_turns) {
808
809 // Save parent todo list
810 std::string saved_todo;
811 if (todo_callbacks_.save != nullptr) {
812 saved_todo = todo_callbacks_.save(todo_callbacks_.user_data);
813 }
814 if (todo_callbacks_.install_fresh != nullptr) {
815 todo_callbacks_.install_fresh(todo_callbacks_.user_data);
816 }
817
818 auto delegation_id = create_storage_record(
819 child_ctx, target_tier, task, max_turns);
820
821 logger->info("Running child loop: tier={} depth={} msgs={} "
822 "system_hash={:016x}",
823 target_tier, child_ctx.delegation_depth,
824 child_ctx.messages.size(),
825 std::hash<std::string>{}(
826 child_ctx.messages.empty()
827 ? std::string{}
828 : child_ctx.messages[0].content));
829
830 if (run_child_fn_ != nullptr) {
831 run_child_fn_(child_ctx, run_child_data_);
832 }
833
834 // Restore parent todo list
835 if (todo_callbacks_.restore != nullptr && !saved_todo.empty()) {
836 todo_callbacks_.restore(saved_todo, todo_callbacks_.user_data);
837 }
838
839 auto result = build_child_result(
840 target_tier, task, child_ctx);
841 complete_storage_record(delegation_id, result);
842 log_child_result(result);
843 return result;
844}
845
866DelegationResult DelegationManager::build_child_result(
867 const std::string& target_tier,
868 const std::string& task,
869 LoopContext& child_ctx) {
870 DelegationResult result;
871 result.target_tier = target_tier;
872 result.task = task;
873 auto tr = child_ctx.metadata.find("terminal_reason");
874 if (tr != child_ctx.metadata.end()) {
875 result.terminal_reason = tr->second;
876 }
877 result.success = (child_ctx.state == AgentState::COMPLETE
878 && result.terminal_reason.empty());
879 result.turns_used = child_ctx.metrics.iterations;
880 result.summary = extract_summary(child_ctx);
881 // Issue #10 (v2.1.4): hoist coverage_gap signal from child
882 // metadata onto DelegationResult so the parent's
883 // finalize_delegation_result can branch on it without re-parsing
884 // ctx.metadata. dir_complete writes these keys when the child
885 // calls entropic.complete with coverage_gap=true.
886 auto cg = child_ctx.metadata.find("coverage_gap");
887 if (cg != child_ctx.metadata.end() && cg->second == "true") {
888 result.coverage_gap = true;
889 auto gd = child_ctx.metadata.find("gap_description");
890 if (gd != child_ctx.metadata.end()) {
891 result.gap_description = gd->second;
892 }
893 auto sf = child_ctx.metadata.find("suggested_files_json");
894 if (sf != child_ctx.metadata.end()) {
895 auto parsed = nlohmann::json::parse(
896 sf->second, nullptr, false);
897 if (parsed.is_array()) {
898 result.suggested_files =
899 parsed.get<std::vector<std::string>>();
900 }
901 }
902 }
903 result.child_messages = std::move(child_ctx.messages);
904 return result;
905}
906
913void DelegationManager::log_child_result(
914 const DelegationResult& result) {
915 if (result.terminal_reason.empty()) {
916 logger->info("Child loop done: tier={} success={} turns={}",
917 result.target_tier, result.success,
918 result.turns_used);
919 } else {
920 logger->warn("Child loop done: tier={} success=false "
921 "turns={} reason={}",
922 result.target_tier, result.turns_used,
923 result.terminal_reason);
924 }
925}
926
942void DelegationManager::finalize_sandbox_for(
943 const std::optional<SandboxInfo>& sb_info,
944 const DelegationResult& result) {
945 if (!sb_info || !sandbox_mgr_) {
946 return;
947 }
948
949 if (result.success) {
950 auto patch_result = sandbox_mgr_->finalize_sandbox(*sb_info);
951 if (patch_result) {
952 deliver_sandbox_result(*sb_info, *patch_result, result);
953 } else {
954 logger->error("Delegation {}: finalize_sandbox failed; "
955 "no patch delivered", sb_info->delegation_id);
956 }
957 } else {
958 logger->info("Delegation {} failed: discarding sandbox without "
959 "generating patch", sb_info->delegation_id);
960 }
961 sandbox_mgr_->discard_sandbox(*sb_info);
962}
963
964} // namespace entropic
DelegationResult execute_delegation(LoopContext &parent_ctx, const std::string &target_tier, const std::string &task, std::optional< int > max_turns=std::nullopt)
Run a child inference loop for the target tier.
void set_todo_callbacks(const TodoCallbacks &callbacks)
Set todo list save/restore callbacks.
DelegationResult execute_resume_delegation(LoopContext &parent_ctx, const std::string &target_tier, const std::string &task, std::vector< Message > seed_history, std::optional< int > max_turns=std::nullopt)
Resume a prior delegation with pre-loaded conversation history.
void set_dir_swap(ScopedSandbox::SwapDirFn swap_fn, void *user_data)
Set directory swap callback for ScopedSandbox.
DelegationResult execute_pipeline(LoopContext &parent_ctx, const std::vector< std::string > &stages, const std::string &task)
Run a multi-stage delegation pipeline sequentially.
DelegationManager(RunChildLoopFn run_child, void *run_child_data, const TierResolutionInterface &tier_resolution, const std::filesystem::path &repo_dir={}, SandboxManager *sandbox_mgr=nullptr)
Construct with engine loop callback and tier resolution.
void set_storage(const struct StorageInterface *storage)
Set storage interface for delegation record persistence.
void set_delegation_callbacks(ent_decision_t(*on_start)(const ent_delegation_request_t *, void *), ent_decision_t(*on_complete)(const ent_delegation_result_t *, void *), void *user_data)
Set delegation start/complete callbacks (gh#29, v2.1.5).
Create, finalize, and discard per-delegation filesystem sandboxes.
Definition sandbox.h:99
void discard_sandbox(const SandboxInfo &info)
Remove a sandbox directory.
Definition sandbox.cpp:533
std::optional< SandboxResult > finalize_sandbox(const SandboxInfo &info)
Produce the final patch artifact for a sandbox.
Definition sandbox.cpp:506
std::optional< SandboxInfo > create_sandbox(const std::string &delegation_id, std::optional< SandboxInfo > chain_from=std::nullopt)
Create a new delegation sandbox.
Definition sandbox.cpp:390
std::optional< std::filesystem::path > write_pending_patch(const std::string &delegation_id, const std::string &patch)
Write a patch to the session's pending/ directory.
Definition sandbox.cpp:552
RAII directory swapper for sandbox-scoped tool execution.
Definition sandbox.h:285
void(*)(const std::filesystem::path &path, void *user_data) SwapDirFn
Callback type for directory swapping.
Definition sandbox.h:298
DelegationManager — child loop creation and execution.
Types for the agentic loop engine.
ent_decision_t
Consumer decision returned from delegation callbacks.
Definition entropic.h:997
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Activate model on GPU (WARM → ACTIVE).
static std::string pipeline_context(size_t stage_idx, size_t total, const std::vector< std::string > &stages, const std::string &prior_output)
Build pipeline context prefix for a stage.
static ent_delegation_result_t build_delegation_result_struct(const SandboxInfo &sb_info, const SandboxResult &sandbox_result, const DelegationResult &result, const std::vector< const char * > &files_c, size_t files_len)
Deliver a finalized patch to consumer or pending/.
void(*)(LoopContext &ctx, void *user_data) RunChildLoopFn
Callback type for running a child engine loop.
Definition delegation.h:70
Request describing a delegation that is about to run.
Definition entropic.h:1011
const char * delegation_id
Short id ("d1", "d2", "pipeline")
Definition entropic.h:1012
Result of a finalized delegation, delivered to the consumer.
Definition entropic.h:1034
const char * delegation_id
Short id (matches request)
Definition entropic.h:1035
Resolved tier information for building child delegation contexts.
Result returned from a child delegation loop.
Definition delegation.h:33
bool success
Whether child reached COMPLETE via real entropic.complete.
Definition delegation.h:35
std::string summary
Final summary from child.
Definition delegation.h:34
std::string task
Original task text.
Definition delegation.h:37
std::string target_tier
Tier that executed.
Definition delegation.h:36
Mutable state carried through the agentic loop.
std::vector< std::string > delegation_ancestor_tiers
Tier stack from root to this loop (P1-9, 2.0.6-rc16)
std::string active_phase
Active inference phase.
std::string conversation_id
Conversation ID for storage (v1.8.8)
int delegation_depth
0 = root, 1+ = child
std::vector< Message > messages
Conversation history.
std::string locked_tier
Tier locked for this loop ("" = none)
std::string parent_conversation_id
Parent conv ID (delegation)
std::vector< std::string > all_tools
Full tool list as raw JSON strings.
A message in a conversation.
Definition message.h:35
std::string content
Message text content (always populated)
Definition message.h:37
std::string role
Message role.
Definition message.h:36
Identifies one delegation's sandbox directory.
Definition sandbox.h:49
std::string delegation_id
Short delegation id (e.g. "d1", "pipeline")
Definition sandbox.h:51
Final artifact emitted by a finalized sandbox.
Definition sandbox.h:66
std::string patch
Unified diff text.
Definition sandbox.h:67
Storage interface for conversation persistence.
bool(* create_delegation)(const char *parent_id, const char *delegating_tier, const char *target_tier, const char *task, int max_turns, std::string &delegation_id, std::string &child_conversation_id, void *user_data)
Create a delegation record with child conversation.
bool(* complete_delegation)(const char *delegation_id, const char *status, const char *summary, void *user_data)
Complete a delegation record.
void * user_data
Opaque pointer (storage backend)
Tier resolution callbacks for delegation and auto-chain.
void * user_data
Opaque pointer (facade context)
ChildContextInfo(* resolve_tier)(const std::string &tier_name, void *user_data)
Build context info for a child delegation to the given tier.
Callback type for saving/restoring todo list state.
Definition delegation.h:79
std::string(* save)(void *user_data)
Save current todo list state. Returns opaque state string.
Definition delegation.h:81
void(* restore)(const std::string &saved, void *user_data)
Restore a previously saved todo list state.
Definition delegation.h:85
void(* install_fresh)(void *user_data)
Install a fresh empty todo list for child.
Definition delegation.h:83
void * user_data
Opaque pointer (facade context)
Definition delegation.h:86