@@ -45,6 +45,12 @@ import { ProviderV2 } from "@opencode-ai/core/provider"
4545import { ModelV2 } from "@opencode-ai/core/model"
4646import { Provider } from "@/provider/provider"
4747import type { Command } from "@/command"
48+ import {
49+ type Contract as ChildAgentContract ,
50+ childAgentMetadata ,
51+ parseChildAgentContract ,
52+ requireGovernedChildBoundary ,
53+ } from "./child-agent"
4854
4955export const AuthMethodID = "redcode-login"
5056
@@ -78,6 +84,7 @@ export function make(input: {
7884 directory ?: Directory . Interface
7985 session ?: ACPSession . Interface
8086 usage ?: UsageService . Interface
87+ env ?: NodeJS . ProcessEnv
8188 eventSubscription ?: ( subscription : ACPEvent . Subscription ) => void
8289} ) : Interface {
8390 const session = input . session ?? makeSessionService ( )
@@ -162,6 +169,17 @@ export function make(input: {
162169
163170 const newSession = Effect . fn ( "ACP.newSession" ) ( function * ( params : NewSessionRequest ) {
164171 const started = performance . now ( )
172+ const childAgent = yield * Effect . try ( {
173+ try : ( ) => {
174+ const contract = parseChildAgentContract ( params . _meta )
175+ if ( contract ) requireGovernedChildBoundary ( contract , params . mcpServers , input . env ?? process . env )
176+ return contract
177+ } ,
178+ catch : ( error ) =>
179+ new ACPError . InvalidChildAgentBoundaryError ( {
180+ reason : error instanceof Error ? error . message : "Invalid governed child Agent boundary" ,
181+ } ) ,
182+ } )
165183 const snapshot = yield * directorySnapshot ( params . cwd )
166184 const selected = selectDefaultModel ( snapshot )
167185 const variant = selectVariant ( snapshot , selected )
@@ -190,6 +208,7 @@ export function make(input: {
190208 model : selected ,
191209 variant,
192210 modeId,
211+ childAgent,
193212 } )
194213 sessionSnapshots . set ( state . id , snapshot )
195214
@@ -198,6 +217,7 @@ export function make(input: {
198217
199218 const response = {
200219 sessionId : state . id ,
220+ ...( state . childAgent ? { _meta : childAgentMetadata ( state . childAgent ) } : { } ) ,
201221 configOptions : configOptions ( snapshot , {
202222 model : state . model ?? selected ,
203223 variant : state . variant ,
@@ -525,7 +545,7 @@ export function make(input: {
525545 "session" ,
526546 )
527547 yield * sendUsageUpdate ( input . usage , input . sdk , input . connection , current . id , current . cwd )
528- return yield * promptResponse ( response . info , params . messageId )
548+ return yield * promptResponse ( response . info , params . messageId , current . childAgent )
529549 }
530550
531551 const known = snapshot . availableCommands . find ( ( item ) => item . name === command . name )
@@ -549,7 +569,7 @@ export function make(input: {
549569 "session" ,
550570 )
551571 yield * sendUsageUpdate ( input . usage , input . sdk , input . connection , current . id , current . cwd )
552- return yield * promptResponse ( response . info , params . messageId )
572+ return yield * promptResponse ( response . info , params . messageId , current . childAgent )
553573 }
554574
555575 if ( command . name === "compact" ) {
@@ -571,7 +591,7 @@ export function make(input: {
571591 }
572592
573593 yield * sendUsageUpdate ( input . usage , input . sdk , input . connection , current . id , current . cwd )
574- return yield * promptResponse ( undefined , params . messageId )
594+ return yield * promptResponse ( undefined , params . messageId , current . childAgent )
575595 } ) ,
576596 cancel,
577597 }
@@ -824,20 +844,22 @@ function detectSlashCommand(parts: ReturnType<typeof promptContentToParts>) {
824844const promptResponse = Effect . fn ( "ACP.promptResponse" ) ( function * (
825845 info : AssistantInfo ,
826846 messageId : string | null | undefined ,
847+ childAgent ?: ChildAgentContract ,
827848) {
849+ const metadata = childAgent ? childAgentMetadata ( childAgent ) : { }
828850 if ( ! info ?. error ) {
829851 return {
830852 stopReason : "end_turn" as const ,
831853 ...( info ? { usage : UsageService . buildUsage ( info ) } : { } ) ,
832854 ...( messageId ? { userMessageId : messageId } : { } ) ,
833- _meta : { } ,
855+ _meta : metadata ,
834856 }
835857 }
836858
837859 const base = {
838860 usage : UsageService . buildUsage ( info ) ,
839861 ...( messageId ? { userMessageId : messageId } : { } ) ,
840- _meta : { } ,
862+ _meta : metadata ,
841863 }
842864
843865 if ( info . error . name === "MessageAbortedError" ) {
0 commit comments