using System;using System.Diagnostics;using System.IO;using System.Linq;using System.Text.Json;using Agent.Core.ToolCalling;using Lilith.Agent.SelfImprovement;namespace Lilith.Agent;public partial class Lilith{ public void RegisterSelfImprovementTools(ToolRegistry registry) { registry.Register(new AgentTool( "self_improve_get_source_layout", "Returns live/sandbox/backup paths and the self-improvement workflow. Call before editing yourself.", _ => InvokeGetLayout(), null, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_generate_tool", "Generates a new tool scaffold in the sandbox (category: core | addon | self). Requires an active sandbox.", InvokeGenerateTool, new { type = "object", properties = new { tool_name = new { type = "string", description = "Snake_case tool name, e.g. summarize_file." }, description = new { type = "string", description = "What the tool does (shown to the model)." }, category = new { type = "string", description = "core, addon, or self." }, invoke_body = new { type = "string", description = "Optional C# return expression/body, default returns OK." }, }, required = new[] { "tool_name", "description", "category" }, }, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_backup_live", "Copies the live source tree to backups/{timestamp}. Run before risky edits.", _ => InvokeBackupLive(), null, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_create_sandbox", "Clones the live source tree into sandbox for isolated edits and builds.", _ => InvokeCreateSandbox(), null, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_build_sandbox", "dotnet build Release for sandbox LilithConsole.", _ => InvokeBuildSandbox(), null, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_verify_sandbox_tool", "Builds sandbox and runs tool-calling design tests; checks tool_name exists in source.", InvokeVerifySandbox, new { type = "object", properties = new { tool_name = new { type = "string", description = "Tool to verify." }, }, required = new[] { "tool_name" }, }, ToolCategory.Self)); registry.Register(new AgentTool( "self_improve_promote_sandbox", "Promotes sandbox over live, then restarts this Lilith process with the upgraded build. Only after verification passes.", _ => InvokePromoteSandbox(), null, ToolCategory.Self)); } private SelfImprovementPaths RequirePaths() { if (Workspace is null) throw new InvalidOperationException("Workspace is required for self-improvement."); var paths = new SelfImprovementPaths(Workspace); SourceTreeService.SeedLiveIfEmpty(paths); return paths; } private string InvokeGetLayout() { var paths = RequirePaths(); return SourceTreeService.DescribeLayout(paths); } private string InvokeGenerateTool(string argumentsJson) { var paths = RequirePaths(); if (!Directory.Exists(paths.Sandbox)) return "Error: sandbox does not exist. Call self_improve_create_sandbox first."; ToolScaffoldGenerator.ParseGenerateArgs( argumentsJson, out string toolName, out string description, out string category, out string? invokeBody); string path = ToolScaffoldGenerator.Generate(paths.Sandbox, toolName, description, category, invokeBody); return $"Generated tool '{toolName}' ({category}) at {path}. Next: self_improve_build_sandbox, then self_improve_verify_sandbox_tool."; } private string InvokeBackupLive() { var paths = RequirePaths(); string backup = SourceTreeService.CreateBackup(paths); return $"Live source backed up to {backup}"; } private string InvokeCreateSandbox() { var paths = RequirePaths(); SourceTreeService.CopyTree(paths.Live, paths.Sandbox, overwriteDestination: true); return $"Sandbox created at {paths.Sandbox}"; } private string InvokeBuildSandbox() { var paths = RequirePaths(); if (!Directory.Exists(paths.Sandbox)) return "Error: sandbox does not exist. Call self_improve_create_sandbox first."; return SandboxBuildService.BuildSandboxConsole(paths); } private string InvokeVerifySandbox(string argumentsJson) { var paths = RequirePaths(); if (!Directory.Exists(paths.Sandbox)) return "Error: sandbox does not exist."; using var doc = JsonDocument.Parse(string.IsNullOrWhiteSpace(argumentsJson) ? "{}" : argumentsJson); string toolName = doc.RootElement.GetProperty("tool_name").GetString() ?? ""; return SandboxVerificationService.VerifyTool(paths, toolName); } private string InvokePromoteSandbox() { var paths = RequirePaths(); if (!Directory.Exists(paths.Sandbox)) return "Error: sandbox does not exist."; SourceTreeService.CreateBackup(paths); SourceTreeService.CopyTree(paths.Sandbox, paths.Live, overwriteDestination: true); string liveProject = Path.Combine(paths.Live, "Interfaces", "ConsoleApp", "LilithConsole", "LilithConsole.csproj"); string buildLog = File.Exists(liveProject) ? SandboxBuildService.BuildProject(liveProject, paths.Live) : "Skipped live build (console project missing)."; string promoteNote = Path.Combine(paths.Root, "last-promote.txt"); File.WriteAllText(promoteNote, $"Promoted at {DateTime.UtcNow:O}\n{buildLog}"); string builtExe = Path.Combine( paths.Live, "Interfaces", "ConsoleApp", "LilithConsole", "bin", "Release", "net8.0", "LilithConsole.exe"); string? exe = File.Exists(builtExe) ? builtExe : Process.GetCurrentProcess().MainModule?.FileName; if (string.IsNullOrWhiteSpace(exe)) return $"Promoted sandbox to live at {paths.Live}. Restart Lilith manually to run the upgraded build."; string args = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(a => a.Contains(' ') ? $"\"{a}\"" : a)); Process.Start(new ProcessStartInfo(exe, args) { UseShellExecute = true }); Environment.Exit(0); return "Restarting with upgraded build."; }}
Documentation
Lilith self-improvement (Version 7+)
Lilith can extend herself only by creating tools in three categories:
| Category | Location | Examples | |----------|----------|----------| | Core | Agent-Core | Memory, time (rare; system-level) | | Addon | Agent-Addons | Weather, C# projects, apps | | Self | Lilith | Workspace files, self-improvement |
Layout
ShippedSource/— copy ofsrc/Version7next to the built app (MSBuildCopyShippedSource){workspace}/output/self-improvement/live— current editable source{workspace}/output/self-improvement/sandbox— clone for edits and builds{workspace}/output/self-improvement/backups/{timestamp}— backups before promote
Tools
1. self_improve_get_source_layout 2. self_improve_generate_tool 3. self_improve_backup_live 4. self_improve_create_sandbox 5. self_improve_build_sandbox 6. self_improve_verify_sandbox_tool 7. self_improve_promote_sandbox — copies sandbox → live, builds, restarts
Set GENESIS_REPO_ROOT to the repo root when developing from source instead of ShippedSource.