11// Tests /steer target capture, accepted delivery, and visible fallback.
22import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
3+ import type { ChatType } from "../../channels/chat-type.js" ;
34import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
45import { buildCommandTestParams } from "./commands.test-harness.js" ;
56import type { ReplyBackendQueueMessageOptions , ReplyOperation } from "./reply-run-registry.js" ;
67import { createReplyOperation } from "./reply-run-registry.js" ;
8+ import {
9+ createFollowupRunToolAuthorityProjector ,
10+ resolveFollowupRunToolAuthorityFingerprint ,
11+ } from "./reply-tool-authority.js" ;
12+ import { createMockFollowupRun } from "./test-helpers.js" ;
713
814const { handleSteerCommand } = await import ( "./commands-steer.js" ) ;
915
@@ -24,8 +30,20 @@ function beginActiveOperation(
2430 sessionKey : string ,
2531 sessionId = "session-active" ,
2632 taskSuggestionDeliveryMode ?: "gateway" ,
33+ authorityRun = createMockFollowupRun ( { run : { sessionId, sessionKey } } ) ,
2734) {
2835 const operation = createReplyOperation ( { sessionKey, sessionId, resetTriggered : false } ) ;
36+ const authorityRoute = {
37+ provider : authorityRun . run . provider ,
38+ model : authorityRun . run . model ,
39+ } ;
40+ const toolAuthorityFingerprint = resolveFollowupRunToolAuthorityFingerprint (
41+ authorityRun ,
42+ authorityRoute ,
43+ ) ;
44+ operation . bindToolAuthorityProjector ( createFollowupRunToolAuthorityProjector ( authorityRun ) ) ;
45+ operation . bindToolAuthorityRoute ( authorityRoute ) ;
46+ operation . bindToolAuthorityFingerprint ( toolAuthorityFingerprint ) ;
2947 operation . setPhase ( "running" ) ;
3048 operation . attachBackend ( {
3149 kind : "embedded" ,
@@ -34,7 +52,47 @@ function beginActiveOperation(
3452 messageInjection : { isAvailable : ( ) => true , queueMessage } ,
3553 } ) ;
3654 operations . push ( operation ) ;
37- return operation ;
55+ return { operation, toolAuthorityFingerprint } ;
56+ }
57+
58+ function createCommandAuthorityRun ( params : ReturnType < typeof buildParams > ) {
59+ return createMockFollowupRun ( {
60+ originatingChannel : params . ctx . OriginatingChannel ,
61+ toolsAllow : params . opts ?. toolsAllow ,
62+ disableTools : params . opts ?. disableTools ,
63+ run : {
64+ agentId : params . agentId ?? "main" ,
65+ agentDir : params . agentDir ?? "/tmp/agent" ,
66+ sessionId : "session-active" ,
67+ sessionKey : params . sessionKey ,
68+ messageProvider : params . ctx . OriginatingChannel ?? params . ctx . Provider ?? params . ctx . Surface ,
69+ chatType : params . ctx . ChatType as ChatType | undefined ,
70+ agentAccountId : params . ctx . AccountId ,
71+ conversationToolPolicy : params . ctx . ConversationToolPolicy ,
72+ groupId : undefined ,
73+ groupChannel : undefined ,
74+ groupSpace : undefined ,
75+ memberRoleIds : params . ctx . MemberRoleIds ,
76+ spawnedBy : params . sessionEntry ?. spawnedBy ,
77+ senderId : params . ctx . SenderId ,
78+ senderName : params . ctx . SenderName ,
79+ senderUsername : params . ctx . SenderUsername ,
80+ senderE164 : params . ctx . SenderE164 ,
81+ senderIsOwner : params . command . senderIsOwner ,
82+ traceAuthorized :
83+ params . command . senderIsOwner ||
84+ ( params . ctx . GatewayClientScopes ?? [ ] ) . includes ( "operator.admin" ) ,
85+ approvalReviewerDeviceId : params . ctx . ApprovalReviewerDeviceId ,
86+ clientCaps : params . ctx . GatewayClientCaps ,
87+ toolBindings : params . ctx . GatewayRunToolBindings ,
88+ inputProvenance : params . ctx . InputProvenance ,
89+ workspaceDir : params . workspaceDir ,
90+ config : params . cfg ,
91+ toolOverrides : params . sessionEntry ?. toolOverrides ,
92+ provider : params . provider ,
93+ model : params . model ,
94+ } ,
95+ } ) ;
3896}
3997
4098describe ( "handleSteerCommand" , ( ) => {
@@ -46,23 +104,52 @@ describe("handleSteerCommand", () => {
46104 }
47105 } ) ;
48106
49- it ( "injects into the captured current text-command session" , async ( ) => {
50- beginActiveOperation ( "agent:main:main" ) ;
107+ it ( "matching authority /steer injects into the captured operation" , async ( ) => {
108+ const params = buildParams ( "/steer keep going" ) ;
109+ params . opts = { toolsAllow : [ "read" ] } ;
110+ const { toolAuthorityFingerprint } = beginActiveOperation (
111+ "agent:main:main" ,
112+ "session-active" ,
113+ undefined ,
114+ createCommandAuthorityRun ( params ) ,
115+ ) ;
51116
52- const result = await handleSteerCommand ( buildParams ( "/steer keep going" ) , true ) ;
117+ const result = await handleSteerCommand ( params , true ) ;
53118
54119 expect ( result ) . toEqual ( {
55120 shouldContinue : false ,
56121 reply : { text : "steered current session." } ,
57122 } ) ;
58123 expect ( queueMessage ) . toHaveBeenCalledWith ( "keep going" , {
59124 steeringMode : "all" ,
125+ isInboundUserMessage : true ,
126+ toolAuthorityFingerprint,
60127 debounceMs : 0 ,
61128 taskSuggestionDeliveryMode : undefined ,
62129 onQueueAccepted : expect . any ( Function ) ,
63130 } ) ;
64131 } ) ;
65132
133+ it ( "authorized sender with mismatched tool authority cannot inject via /steer" , async ( ) => {
134+ const activeParams = buildParams ( "/steer keep going" ) ;
135+ activeParams . opts = { toolsAllow : [ "exec" ] } ;
136+ beginActiveOperation (
137+ "agent:main:main" ,
138+ "session-active" ,
139+ undefined ,
140+ createCommandAuthorityRun ( activeParams ) ,
141+ ) ;
142+ const params = buildParams ( "/steer keep going" ) ;
143+ params . opts = { toolsAllow : [ "read" ] } ;
144+
145+ const result = await handleSteerCommand ( params , true ) ;
146+
147+ expect ( result ) . toEqual ( { shouldContinue : true } ) ;
148+ expect ( params . ctx . BodyForAgent ) . toBe ( "keep going" ) ;
149+ expect ( params . command . commandBodyNormalized ) . toBe ( "keep going" ) ;
150+ expect ( queueMessage ) . not . toHaveBeenCalled ( ) ;
151+ } ) ;
152+
66153 it ( "passes the initiating surface task capability into steering" , async ( ) => {
67154 beginActiveOperation ( "agent:main:main" , "session-active" , "gateway" ) ;
68155 const params = buildParams ( "/steer keep going" ) ;
0 commit comments