adapters/Delegation.ps1

function New-PraxoFuncSubagentRunner {
    param([Parameter(Mandatory = $true)][scriptblock]$Run)
    Import-PraxoCoreAssembly
    if (-not ('Praxo.Core.PowerShellSubagentRunner' -as [type])) {
        Add-Type -TypeDefinition @'
using System;
using System.Threading.Tasks;
using System.Management.Automation.Runspaces;
using Praxo.Core.Delegation;

namespace Praxo.Core
{
    public sealed class PowerShellSubagentRunner : SubagentRunner
    {
        private readonly Func<SubagentRequest, Task<SubagentResult>> _run;
        private readonly Runspace _runspace;

        public PowerShellSubagentRunner(Func<SubagentRequest, Task<SubagentResult>> run)
        {
            _run = run;
            _runspace = Runspace.DefaultRunspace;
        }

        public Task<SubagentResult> RunAsync(SubagentRequest request)
        {
            var previous = Runspace.DefaultRunspace;
            try
            {
                Runspace.DefaultRunspace = _runspace;
                return _run(request);
            }
            finally
            {
                Runspace.DefaultRunspace = previous;
            }
        }
    }
}
'@
 -ReferencedAssemblies @($script:PraxoCoreDll, [System.Management.Automation.Runspaces.Runspace].Assembly.Location) -CompilerOptions "/nowarn:1701"
    }
    $wrapped = {
        param($request)
        $result = & $Run $request
        if ($result -is [System.Threading.Tasks.Task[Praxo.Core.Delegation.SubagentResult]]) {
            return $result
        }
        [System.Threading.Tasks.Task[Praxo.Core.Delegation.SubagentResult]]::FromResult(
            [Praxo.Core.Delegation.SubagentResult]$result)
    }.GetNewClosure()
    $delegate = [System.Management.Automation.LanguagePrimitives]::ConvertTo(
        $wrapped,
        [System.Func[Praxo.Core.Delegation.SubagentRequest, System.Threading.Tasks.Task[Praxo.Core.Delegation.SubagentResult]]]
    )
    [Praxo.Core.PowerShellSubagentRunner]::new($delegate)
}

function New-PraxoSubagentManager {
    param([Parameter(Mandatory = $true)]$Runner)
    Import-PraxoCoreAssembly
    [Praxo.Core.Delegation.SubagentManager]::new([Praxo.Core.Delegation.SubagentRunner]$Runner)
}

function New-PraxoSubagentRequest {
    param(
        [Parameter(Mandatory = $true)][string]$ParentSessionId,
        [Parameter(Mandatory = $true)][string]$AgentId,
        [string]$Prompt = '',
        [ValidateSet('isolated', 'fork')][string]$Mode = 'isolated',
        [hashtable]$Context = @{},
        [object[]]$History = @()
    )
    Import-PraxoCoreAssembly
    $request = [Praxo.Core.Delegation.SubagentRequest]::new()
    $request.ParentSessionId = $ParentSessionId
    $request.AgentId = $AgentId
    $request.Prompt = $Prompt
    if ($Mode -eq 'fork') {
        $request.Mode = [Praxo.Core.Delegation.SubagentMode]::Fork
    } else {
        $request.Mode = [Praxo.Core.Delegation.SubagentMode]::Isolated
    }
    foreach ($key in $Context.Keys) {
        $request.Context[$key] = $Context[$key]
    }
    foreach ($message in $History) {
        $request.History.Add([Praxo.Core.Message]$message)
    }
    $request
}

function Invoke-PraxoSubagent {
    param(
        [Parameter(Mandatory = $true)][scriptblock]$Runner,
        [Parameter(Mandatory = $true)]$Request
    )
    Import-PraxoCoreAssembly
    if ([string]::IsNullOrEmpty($Request.ParentSessionId) `
            -or [string]::IsNullOrEmpty($Request.AgentId)) {
        throw [System.ArgumentException]::new(
            'agent: subagent parent session and agent id are required')
    }
    $task = [Praxo.Core.Delegation.SubagentTask]::new()
    $type = $task.GetType()
    $now = [DateTimeOffset]::UtcNow
    $type.GetProperty('Id').SetValue($task, 'subagent')
    $type.GetProperty('ParentSessionId').SetValue($task, $Request.ParentSessionId)
    $type.GetProperty('AgentId').SetValue($task, $Request.AgentId)
    $type.GetProperty('Mode').SetValue($task, $Request.Mode)
    $type.GetProperty('CreatedAt').SetValue($task, $now)
    $task.Status = [Praxo.Core.Delegation.SubagentStatus]::Running
    $task.UpdatedAt = $now
    try {
        $result = & $Runner $Request
        if ($result -is [System.Threading.Tasks.Task[Praxo.Core.Delegation.SubagentResult]]) {
            $result = $result.GetAwaiter().GetResult()
        }
        $task.Status = [Praxo.Core.Delegation.SubagentStatus]::Completed
        $task.Output = [string]$result.Output
    } catch {
        $task.Status = [Praxo.Core.Delegation.SubagentStatus]::Failed
        $task.Error = [string]$_.Exception.Message
    }
    $task.UpdatedAt = [DateTimeOffset]::UtcNow
    $task
}

$script:PraxoAdapterExports = @($script:PraxoAdapterExports) + @(
    'New-PraxoFuncSubagentRunner',
    'New-PraxoSubagentManager',
    'New-PraxoSubagentRequest',
    'Invoke-PraxoSubagent'
)