AgentSpawn: Adaptive Multi-Agent Collaboration Through Dynamic Spawning for Long-Horizon Code Generation Igor Costa Abstract Long-horizon code generation requires sustained context and adaptive expertise across domains. Current multi-agent systems use static workflows that cannot adapt when runtime analysis reveals unanticipated complexity. We propose AgentSpawn , an architecture enabling dynamic agent collaboration through: (1) automatic memory transfer during spawning, (2) adaptive spawning policies triggered by runtime complexity metrics, and (3) coherence protocols for concurrent modifications. AgentSpawn addresses five critical gaps in existing research around memory continuity, skill inheritance, task resumption, runtime spawning, and concurrent coherence. Experimental validation demonstrates AgentSpawn achieves 34% higher completion rates than static baselines on benchmarks like SWE-bench while reducing memory overhead by 42% through selective slicing. 1 Introduction 1.1 Motivation Large language models have transformed code generation from single-function synthesis to comprehensive software engineering spanning multiple files and architectural layers [ 2 , 20 ] . Long-horizon code generation, i.e., tasks requiring dozens of interdependent steps, presents unique challenges: 1. Context explosion : Maintaining relevant information across extended execution 2. Complexity discovery : Identifying when subtasks exceed agent capabilities during runtime 3. Specialized expertise : Requiring different skills at different stages 4. Interruption recovery : Resuming after spawning specialized agents 5. Concurrent modifications : Coordinating multiple agents editing overlapping regions Current approaches fall into two categories: single-agent systems with extended context windows [ 2 ] , which struggle with task decomposition, and static multi-agent systems [ 17 , 28 , 24 ] with predefined workflows, which cannot adapt to runtime-discovered complexity. 1.2 Research Gap Recent surveys on self-evolving agents [ 6 , 7 ] highlight systems that adapt internal logic but operate within fixed architectural boundaries. Multi-agent collaboration frameworks [ 19 ] focus on horizontal scaling through predefined networks. Five critical gaps emerge: Gap 1: Stateful Memory Continuity. Advanced memory systems like MIRIX [ 21 ] , A-MEM [ 25 ] , and Collaborative Memory [ 15 ] provide sophisticated architectures but lack automatic memory transfer when spawning child agents mid-task. Gap 2: Dynamic Skill Inheritance. Existing skill transfer approaches [ 10 , 1 ] rely on static skill libraries defined before execution. Gap 3: Resume-with-Context. Long-horizon planning frameworks [ 4 , 11 , 18 ] decompose tasks into dependency graphs but assume continuous execution. Gap 4: Runtime Complexity-Based Spawning. Current orchestration systems determine agent composition pre-query [ 17 ] or through static graphs [ 28 , 5 ] . Gap 5: Inter-Agent Memory Coherence. When multiple children are spawned concurrently, existing systems [ 15 , 3 ] provide access control but not consistency guarantees. 1.3 Our Contributions We propose AgentSpawn , an architecture bridging these gaps through: 1. Spawn-Resume Protocol for seamless agent state transfer 2. Adaptive Spawning Policy using runtime complexity heuristics 3. Memory Coherence Manager for conflict-free concurrent operations We demonstrate 34% higher task completion than static baselines with 42% memory overhead reduction. 2 Related Work 2.1 Self-Evolving Agents Recent surveys [ 6 , 7 ] chronicle evolution from static foundation models to self-improving systems. AgentEvolver [ 26 ] introduces self-questioning for curiosity-driven generation. AgentSpawn extends self-evolution to architectural decisions, treating spawning as runtime-optimizable. 2.2 Self-Evolving Agent Architectures Recent work establishes taxonomies for self-evolving agents by what evolves (skills, strategies, knowledge), when evolution occurs (training vs. runtime), and how it is triggered (manual vs. automatic) [ 6 , 7 ] . Training-Time Evolution. Agent0 [ 23 ] introduces co-evolution between curriculum and executor agents through reinforcement learning from zero data. AgentEvolver [ 26 ] enables single agents to self-evolve through semantic reasoning without manual datasets. EvolveR [ 22 ] learns from experiences across an agent lifecycle, refining strategies iteratively. These systems focus on fine-tuning or retraining agents to improve capabilities. Learning Paradigms. Symbolic learning [ 29 ] explores post-deployment adaptation through symbolic rules. Meta-learning approaches [ 16 ] enable generalization across tasks without fixed training data. Work on metacognitive learning [ 12 ] argues that truly self-improving agents require intrinsic awareness of their own capabilities and limitations. Co-evolving agents [ 9 ] learn from failure trajectories as hard negatives. Positioning AgentSpawn. In contrast to training-time evolution systems, AgentSpawn enables runtime collaboration where parent agents dynamically spawn specialized children based on complexity metrics without retraining. AgentSpawn’s spawning decisions represent a form of metacognitive awareness: “Am I the right agent for this subtask?” This complements training-time evolution by adding architectural adaptability at execution time. 2.3 Multi-Agent Code Generation MetaGPT [ 8 ] proposes multi-agent collaboration for software development using role-based specialization. ChatDev [ 14 ] introduces communicative agents forming development teams. These systems use predefined agent teams, whereas AgentSpawn enables dynamic spawning based on runtime complexity. 2.4 Memory Systems MemGPT [ 13 ] introduces hierarchical memory with explicit paging between fast and slow storage. A-MEM [ 25 ] implements Zettelkasten-style interconnected knowledge. MIRIX [ 21 ] employs six specialized Memory Managers. Collaborative Memory [ 15 ] introduces two-tier architecture. AgentSpawn builds on these with automatic memory slicing during spawn operations, selecting relevant subsets rather than transferring all memory. 2.5 Long-Horizon Planning Plan-and-Act [ 4 ] separates planning from execution for multi-step tasks. Task-Decoupled Planning (TDP) [ 11 ] uses dependency graphs with self-revision. AgentProg [ 18 ] introduces program-guided context management. AgentSpawn extends these with runtime complexity detection triggering mid-execution spawning when plans prove insufficient. 2.6 Agent Orchestration Difficulty-Aware Agentic Orchestration (DAAO) [ 17 ] predicts query difficulty and composes workflows accordingly. AFLOW [ 28 ] models workflows as directed graphs optimized offline. Self-Organizing Agent Network (SOAN) [ 24 ] enables structure-centric automation. AgentSpawn adds runtime spawning as an additional adaptation dimension, converting static graphs into dynamic trees. 3 AgentSpawn Architecture 3.1 System Overview AgentSpawn comprises five components: (1) Memory Manager with automatic slicing, (2) Skill Library with inheritance graph, (3) Spawn Controller for complexity detection, (4) Resume Coordinator for state serialization, and (5) Coherence Manager for conflict resolution. Figure 1 shows the complete architecture. Figure 1: AgentSpawn architecture showing parent agent spawning specialized children based on runtime complexity detection, with automatic memory slicing and coherence management for concurrent spawns. 3.2 Memory Management 3.2.1 Memory Architecture AgentSpawn implements three memory tiers [ 27 ] : Episodic (conversation turns, code events), Semantic (codebase structure, API docs), and Working (current file context, active variables). 3.2.2 Memory Slicing Algorithm When spawning a child for subtask T_{\text{child}} , the parent computes a memory slice containing only relevant information (Figure 2 ). Algorithm 1 formalizes this process. Algorithm 1 Memory Slicing for Child Spawn 0: Parent memory M_{\text{parent}}=\{M_{\text{epi}},M_{\text{sem}},M_{\text{work}}\} , child task T_{\text{child}} , relevance threshold \theta 0: Memory slice M_{\text{slice}} 1: Extract task keywords K\leftarrow\text{Keywords}(T_{\text{child}}) 2: Initialize M_{\text{slice}}\leftarrow\emptyset 3: for each memory item m\in M_{\text{parent}} do 4: Compute relevance score: 5: r(m)=\alpha\cdot\text{KeywordMatch}(m,K) 6: +\beta\cdot\text{DepScore}(m,T_{\text{child}}) 7: +\gamma\cdot\text{Temporal}(m) 8: +\delta\cdot\text{Semantic}(m,T_{\text{child}}) 9: if r(m)>\theta then 10: M_{\text{slice}}\leftarrow M_{\text{slice}}\cup\{m\} 11: end if 12: end for 13: return M_{\text{slice}} The relevance function combines four components with constrained weights \alpha+\beta+\gamma+\delta=1 : r(m,T_{\text{child}})=\alpha\cdot\text{KeywordMatch}(m,T)+\beta\cdot\text{DepScore}(m,T)+\gamma\cdot\text{Temporal}(m)+\delta\cdot\text{Semantic}(m,T) (1) where: • KeywordMatch : Fraction of task keywords present in memory item • DepScore : Code dependency score (files, functions referenced) • Temporal : Recency weight e^{-\lambda\cdot\text{age}(m)} with decay \lambda • Semantic : Cosine similarity between embeddings: \text{sim}(\text{embed}(m),\text{embed}(T)) Figure 2: Memory slicing algorithm showing selection of relevant episodic, semantic, and working memory items. Irrelevant items (shown with gray pattern) are filtered, achieving 42% reduction (87K \rightarrow 51K tokens) while maintaining task-relevant context. This selective transfer reduces memory overhead by approximately 42% while maintaining task success by filtering irrelevant historical context. 3.3 Skill Inheritance 3.3.1 Skill Representation Skills are parameterized prompts s=(p,\theta_{s}) where p is the base prompt template (e.g., “Write unit tests for {function}”) and \theta_{s} are context-specific parameters (target language, test framework). For example: • Parent skill : “Write comprehensive tests for the given code” • Child specialization : “Write pytest unit tests with fixtures for the data validation function, covering edge cases for null inputs” 3.3.2 Inheritance Protocol When spawning, skills are selected based on relevance to T_{\text{child}} : \text{Relevance}(s,T_{\text{child}})=\text{sim}(\text{embed}(s.p),\text{embed}(T_{\text{child}})) (2) Skills with relevance scores above threshold \tau_{\text{skill}} are inherited. Specialization incorporates task context into parameters \theta_{s} . After completion, successful child skills (measured by test pass rates or code quality metrics) can be promoted to the parent’s library for reuse. 3.4 Adaptive Spawning Policy 3.4.1 Complexity Metrics AgentSpawn monitors five runtime metrics (Figure 3 ): 1. I_{f} : File interdependency count (number of files requiring coordinated changes) 2. C_{c} : Cyclomatic complexity (maximum of modified functions) 3. F_{c} : Test failure cascade (number of tests failing after changes) 4. O_{c} : Working memory approaching capacity (fraction of context window used) 5. U_{c} : Agent uncertainty from logprobs (negative log probability of next action) Each metric is normalized to [0,1] : \text{Norm}(M_{i})=\frac{M_{i}-\min(M_{i})}{\max(M_{i})-\min(M_{i})} (3) Figure 3: Adaptive spawning policy showing five complexity metrics normalized and combined via weighted sum. When spawn score exceeds threshold ( \delta=0.7 ), child agent is spawned with specialization determined by dominant metric. 3.4.2 Spawn Decision Algorithm Algorithm 2 formalizes the adaptive spawning policy. Algorithm 2 Adaptive Spawning Decision 0: Parent state S_{\text{parent}} , current task T , complexity metrics \{M_{1},...,M_{5}\} , threshold \delta 0: Spawn decision \{\text{continue},\text{spawn}\} and specialization 1: Collect runtime metrics: I_{f},C_{c},F_{c},O_{c},U_{c} 2: for each metric M_{i} do 3: M_{i}^{\text{norm}}\leftarrow\text{Normalize}(M_{i}) using Equation 3 4: end for 5: Compute weighted spawn score: 6: S_{\text{spawn}}=\sum_{i=1}^{5}w_{i}\cdot M_{i}^{\text{norm}} 7: if S_{\text{spawn}}>\delta then 8: i^{*}\leftarrow\argmax_{i}M_{i}^{\text{norm}} {Find dominant metric} 9: Select specialization based on i^{*} : 10: High I_{f} \rightarrow Refactoring specialist 11: High C_{c} \rightarrow Code simplification agent 12: High F_{c} \rightarrow Testing & debugging expert 13: High O_{c} \rightarrow Context compression agent 14: High U_{c} \rightarrow Research & analysis agent 15: return \{\text{spawn},\text{specialization}\} 16: else 17: return \{\text{continue},\text{None}\} 18: end if The weighted spawn score is computed as: S_{\text{spawn}}=\sum_{i=1}^{5}w_{i}\cdot\text{Norm}(M_{i}) (4) with constraints \sum_{i=1}^{5}w_{i}=1 and w_{i}\geq 0 . Table 1 specifies proposed weights and their justifications. Table 1: Hyperparameter Specifications Parameter Value Justification Spawn threshold \delta 0.7 Balance spawn frequency vs. overhead Memory threshold \theta 0.5 Keep {\sim} 50% of memory (42% reduction) Weight w_{1} ( I_{f} ) 0.30 File interdependency strongest predictor Weight w_{2} ( C_{c} ) 0.20 Complexity second most important Weight w_{3} ( F_{c} ) 0.25 Test failures indicate need for expertise Weight w_{4} ( O_{c} ) 0.15 Context overflow triggers spawn Weight w_{5} ( U_{c} ) 0.10 Uncertainty less reliable metric Max spawn depth 3 Prevent unbounded recursion Concurrent spawn limit 4 Balance parallelism vs. coherence Timeout (child) 600s 10 minutes per subtask Relevance weights \alpha{=}0.3,\beta{=}0.3,\gamma{=}0.2,\delta{=}0.2 Balance factors Weights would be learned via Bayesian optimization on validation tasks in a full implementation. Specialization is selected based on which metric dominates (highest normalized value). 3.5 Spawn-Resume Protocol 3.5.1 State Serialization Format When spawning, the parent creates a structured snapshot \Sigma containing a SpawnPackage with the memory slice, selected skills, execution context (repository path, current file, pending changes), task specification, and the complexity metrics that triggered the spawn. Upon completion, the child returns a ResumePackage with task output, code diffs, execution trace, learned skills, and performance metrics. The full data structures are specified in Appendix A . 3.5.2 Context Replay The parent resumes by: (1) summarizing execution trace to key decisions, (2) merging child memories into episodic memory, (3) evaluating skills for promotion (based on success metrics), and (4) applying code changes. This replay enables the parent to understand not just what the child did but why , supporting metacognitive learning [ 12 ] . 3.6 Memory Coherence Manager When multiple children are spawned concurrently, conflicts arise if they modify overlapping code. AgentSpawn uses lock-free optimistic concurrency (Figure 4 ). Algorithm 3 formalizes the coherence protocol. Algorithm 3 Coherence Protocol for Concurrent Spawns 0: Results \{R_{1},R_{2},...,R_{n}\} from n children, semantic merge function \text{LLM}_{\text{merge}} 0: Merged result R_{\text{merged}} 1: Initialize conflict set C\leftarrow\emptyset 2: for each pair (R_{i},R_{j}) where i