@@ -34,6 +34,7 @@ import { createTestPreparedRunAdmission } from "../admitted-run-context.test-sup
3434import { clearRuntimeAuthProfileStoreSnapshots } from "../auth-profiles/runtime-snapshots.js" ;
3535import { saveAuthProfileStore } from "../auth-profiles/store.js" ;
3636import { testing as cliBackendsTesting } from "../cli-backends.test-support.js" ;
37+ import { buildCliMcpGrantContext } from "../cli-runner/mcp-grant-context.js" ;
3738import { createCronCreatorAuthorityCapability } from "../cron-creator-authority-context.js" ;
3839import type { RunEmbeddedAgentInternalParams } from "../embedded-agent-runner/run/internal-params.js" ;
3940import type { EmbeddedAgentRunResult } from "../embedded-agent.js" ;
@@ -2952,6 +2953,97 @@ describe("CLI attempt execution", () => {
29522953 } ) ;
29532954 } ) ;
29542955
2956+ it . each ( [
2957+ {
2958+ label : "a fallback completion report" ,
2959+ isFallbackRetry : true ,
2960+ inputProvenance : { kind : "inter_session" , sourceTool : "subagent_announce" } ,
2961+ expected : "report_only" ,
2962+ } ,
2963+ {
2964+ label : "a fallback answering ordinary user input" ,
2965+ isFallbackRetry : true ,
2966+ inputProvenance : { kind : "external_user" } ,
2967+ expected : undefined ,
2968+ } ,
2969+ {
2970+ label : "a primary completion report" ,
2971+ isFallbackRetry : false ,
2972+ inputProvenance : { kind : "inter_session" , sourceTool : "subagent_announce" } ,
2973+ expected : undefined ,
2974+ } ,
2975+ ] ) (
2976+ "stamps the command-fallback CLI grant delegation capability for $label" ,
2977+ async ( { isFallbackRetry, inputProvenance, expected } ) => {
2978+ const runId = `run-command-fallback-delegation-${ String ( isFallbackRetry ) } -${ expected } ` ;
2979+ const sessionKey = `agent:main:direct:${ runId } ` ;
2980+ const sessionEntry : SessionEntry = {
2981+ sessionId : `session-${ runId } ` ,
2982+ updatedAt : Date . now ( ) ,
2983+ } ;
2984+ const cfg = {
2985+ session : { store : storePath } ,
2986+ agents : {
2987+ defaults : {
2988+ models : {
2989+ "anthropic/claude-opus-4-7" : { agentRuntime : { id : "claude-cli" } } ,
2990+ } ,
2991+ } ,
2992+ } ,
2993+ } as OpenClawConfig ;
2994+ await writeSessionStoreSeed ( { [ sessionKey ] : sessionEntry } ) ;
2995+ runCliAgentMock . mockResolvedValueOnce ( makeCliResult ( "delegation gate" ) ) ;
2996+
2997+ await runAgentAttempt ( {
2998+ providerOverride : "anthropic" ,
2999+ originalProvider : "anthropic" ,
3000+ modelOverride : "claude-opus-4-7" ,
3001+ cfg,
3002+ sessionEntry,
3003+ sessionId : sessionEntry . sessionId ,
3004+ sessionKey,
3005+ sessionAgentId : "main" ,
3006+ sessionFile : path . join ( tmpDir , `${ runId } .jsonl` ) ,
3007+ workspaceDir : tmpDir ,
3008+ body : "report the completion" ,
3009+ isFallbackRetry,
3010+ resolvedThinkLevel : "medium" ,
3011+ timeoutMs : 1_000 ,
3012+ runId,
3013+ opts : {
3014+ message : "report the completion" ,
3015+ inputProvenance,
3016+ } as RunAgentAttemptParams [ "opts" ] ,
3017+ runContext : { } as RunAgentAttemptParams [ "runContext" ] ,
3018+ spawnedBy : undefined ,
3019+ messageChannel : "telegram" ,
3020+ skillsSnapshot : undefined ,
3021+ resolvedVerboseLevel : undefined ,
3022+ agentDir,
3023+ onAgentEvent : vi . fn ( ) ,
3024+ authProfileProvider : "anthropic" ,
3025+ sessionStore : { [ sessionKey ] : sessionEntry } ,
3026+ storePath,
3027+ sessionHasHistory : false ,
3028+ } ) ;
3029+
3030+ // The command loop is a second fallback entry point; its CLI grant must
3031+ // carry the same gate as the auto-reply candidate or the loopback surface
3032+ // resolves to full.
3033+ const grantContext = buildCliMcpGrantContext ( {
3034+ run : firstRunCliAgentArg ( ) as unknown as Parameters <
3035+ typeof buildCliMcpGrantContext
3036+ > [ 0 ] [ "run" ] ,
3037+ config : cfg ,
3038+ requireExplicitMessageTarget : false ,
3039+ agentId : "main" ,
3040+ modelProvider : "anthropic" ,
3041+ modelId : "claude-opus-4-7" ,
3042+ } ) ;
3043+ expect ( grantContext . delegationCapability ) . toBe ( expected ) ;
3044+ } ,
3045+ ) ;
3046+
29553047 it ( "routes canonical Anthropic models through the configured Claude CLI runtime" , async ( ) => {
29563048 const sessionKey = "agent:main:direct:canonical-claude-cli" ;
29573049 const sessionEntry = makeSessionEntry ( "openclaw-session-canonical-cli" ) ;
0 commit comments