Skip to content

Cascade Agent kill API to SubAgents#1546

Merged
igordayen merged 2 commits intomainfrom
subagent-kill-propagation
Mar 30, 2026
Merged

Cascade Agent kill API to SubAgents#1546
igordayen merged 2 commits intomainfrom
subagent-kill-propagation

Conversation

@igordayen
Copy link
Copy Markdown
Contributor

@igordayen igordayen commented Mar 29, 2026

Overview

Please refer to issue:

#1509

When killing a parent agent process, all child subagents are now automatically killed recursively.

Child Process Discovery: Uses AgentProcessRepository.findByParentId() to locate children. Each
child process stores its parentId (set during createChildProcess()), enabling repository queries to
find all descendants.

Why Repository vs Parent Collection: Repository is the single source of truth for process
lifecycle. This works consistently across repository
implementations (in-memory, database), and leverages existing parentId field without bi-directional
references.

Changes

File: AgentProcess.kt
Change: Add isRootProcess property
────────────────────────────────────────
File: AgentProcessRepository.kt
Change: Add findByParentId(parentId: String): List
────────────────────────────────────────
File: AbstractAgentProcess.kt
Change: kill() cascades to children via repository lookup
────────────────────────────────────────
File: DefaultAgentPlatform.kt
Change: killAgentProcess() delegates to process.kill()
────────────────────────────────────────
File: InMemoryAgentProcessRepository.kt
Change: Implement findByParentId() and hierarchy-aware eviction
────────────────────────────────────────
File: SubagentExecutionTest.kt
Change: Add tests for parentId, cascading kill via platform and direct kill()
────────────────────────────────────────
File: InMemoryAgentProcessRepositoryTest.kt
Change: Add tests for hierarchy eviction rules

* Locate all child sub-processes (subagents) by searching in AgentProcessRepository
* Ammend eviction algorithm by blocking eviction for running agents (including subagents)
@jasperblues
Copy link
Copy Markdown
Contributor

The cascade kill implementation is clean. One suggestion worth considering: the hierarchy-aware eviction logic (isHierarchyFinished, evictHierarchy) currently lives inside InMemoryAgentProcessRepository, which gives the repository two concerns — storage/query and eviction policy.

Extracting the eviction policy to its own class would keep the repository focused and make the policy independently testable:

internal class HierarchyAwareEvictionPolicy(private val windowSize: Int) {

    fun evictIfNeeded(accessOrder: ArrayDeque<String>, map: MutableMap<String, AgentProcess>) {
        while (accessOrder.size > windowSize) {
            val oldestRootId = accessOrder.first()
            if (isHierarchyFinished(oldestRootId, map)) {
                accessOrder.removeFirst()
                evictHierarchy(oldestRootId, map)
            } else break
        }
    }

    private fun isHierarchyFinished(processId: String, map: Map<String, AgentProcess>): Boolean {
        val process = map[processId] ?: return true
        if (!process.finished) return false
        return map.values.filter { it.parentId == processId }
            .all { isHierarchyFinished(it.id, map) }
    }

    private fun evictHierarchy(processId: String, map: MutableMap<String, AgentProcess>) {
        map.values.filter { it.parentId == processId }.forEach { evictHierarchy(it.id, map) }
        map.remove(processId)
    }
}

save() then becomes:

if (agentProcess.isRootProcess) {
    accessOrder.offer(processId)
    evictionPolicy.evictIfNeeded(accessOrder, map)
}

As a further thought, eviction could eventually be applied as an aspect on save() — a true side effect, fully decoupled from the repository implementation. Not a blocker, but for consideration.

@igordayen
Copy link
Copy Markdown
Contributor Author

@jasperblues: Refactored InMemory Agent Process Repo per recommendation. thanks

@sonarqubecloud
Copy link
Copy Markdown

@igordayen igordayen merged commit a21ef06 into main Mar 30, 2026
17 checks passed
@igordayen igordayen deleted the subagent-kill-propagation branch March 30, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants