@@ -8,12 +8,14 @@ import (
88 "io"
99 "os"
1010 "strings"
11+ "time"
1112 "unicode/utf8"
1213
1314 "cyberagent-workbench/internal/application"
1415 "cyberagent-workbench/internal/domain"
1516 "cyberagent-workbench/internal/idgen"
1617 "cyberagent-workbench/internal/projectconfig"
18+ "cyberagent-workbench/internal/redact"
1719 "cyberagent-workbench/internal/toolgateway"
1820 "cyberagent-workbench/internal/workspace"
1921)
@@ -39,6 +41,8 @@ func (a *App) execCommand(ctx context.Context, args []string) error {
3941 timeout := fs .Duration ("timeout" , 0 , "whole-execution deadline; zero means no deadline" )
4042 stdin := fs .Bool ("stdin" , false , "read the prompt from stdin instead of the positional argument" )
4143 jsonOutput := fs .Bool ("json" , false , "emit a structured JSON result" )
44+ instructions := fs .String ("instructions" , "" ,
45+ "fix extra operator instructions as a Thread context pin for this invocation" )
4246 enablePermissionControl := fs .Bool ("enable-permission-control" , false ,
4347 "enable the operator-approval runtime for this process" )
4448 enableFullAccess := fs .Bool ("enable-danger-full-access" , false ,
@@ -49,6 +53,7 @@ func (a *App) execCommand(ctx context.Context, args []string) error {
4953 "workspace" : true , "profile" : true , "route" : true ,
5054 "max-turns" : true , "max-tokens" : true , "max-tool-calls" : true ,
5155 "max-cost-usd" : true , "timeout" : true , "stdin" : false , "json" : false ,
56+ "instructions" : true ,
5257 "enable-permission-control" : false , "enable-danger-full-access" : false ,
5358 "confirm-full" : false ,
5459 })); err != nil {
@@ -64,11 +69,19 @@ func (a *App) execCommand(ctx context.Context, args []string) error {
6469 }
6570 prompt = strings .TrimSpace (prompt )
6671 if prompt == "" {
67- return errors .New (`usage: cyberagent exec --workspace <name> "prompt" [--profile code|review|learn|script] [--route <route>] [--timeout 30m] [--json]` )
72+ return errors .New (`usage: cyberagent exec --workspace <name> "prompt" [--profile code|review|learn|script] [--route <route>] [--instructions <text>] [-- timeout 30m] [--json]` )
6873 }
6974 if strings .TrimSpace (* workspaceName ) == "" {
7075 return errors .New ("exec requires --workspace <name>" )
7176 }
77+ // Validate the fixed instructions before any Run exists: a late pin
78+ // failure would otherwise strand a created Run, and the operator gets the
79+ // same text contract (UTF-8, no NUL, 8192-rune cap, no redacted secrets)
80+ // the interactive Thread pin surface enforces.
81+ fixedInstructions , err := normalizeExecInstructions (* instructions )
82+ if err != nil {
83+ return err
84+ }
7285 if * timeout > 0 {
7386 var cancel context.CancelFunc
7487 ctx , cancel = context .WithTimeout (ctx , * timeout )
@@ -151,6 +164,21 @@ func (a *App) execCommand(ctx context.Context, args []string) error {
151164 turns := application .NewThreadTurnService (a .store ,
152165 application .NewRunLifecycleControlService (a .store ).WithLifecycleHooks (a .newLifecycleHookEngine ()),
153166 application .NewRunExecutionHandoffWithRuntime (a .store , a .router , a .checker , dependencies ))
167+ if fixedInstructions != "" {
168+ if err := pinExecInstructions (ctx , turns , domain .InitialThreadID (run .ID ),
169+ fixedInstructions ); err != nil {
170+ // A created Run with a failed pin would sit idle until its budget
171+ // expires. Cancel it on a bounded detached context so the
172+ // operator gets a terminal Run, and report the Run ID if even
173+ // the cancellation fails. The pin error stays primary.
174+ cancelCtx , cancel := context .WithTimeout (context .WithoutCancel (ctx ), 5 * time .Second )
175+ if _ , cancelErr := application .NewRunService (a .store ).Cancel (cancelCtx , run .ID ); cancelErr != nil {
176+ err = fmt .Errorf ("%w (run %s could not be cancelled: %v)" , err , run .ID , cancelErr )
177+ }
178+ cancel ()
179+ return err
180+ }
181+ }
154182 oneShot := application .NewOneShotService (a .store , turns )
155183 result , err := oneShot .Run (ctx , application.OneShotRequest {
156184 Version : domain .ThreadMessageProtocolVersion ,
@@ -196,3 +224,51 @@ func clipExecGoal(prompt string) string {
196224 }
197225 return clipped
198226}
227+
228+ // normalizeExecInstructions validates the exec --instructions value before any
229+ // Run is created. An unset flag means no fixed instructions; a whitespace-only
230+ // value is an operator mistake and fails loudly. Otherwise the text must
231+ // satisfy the Thread context pin contract and carry no protected secret
232+ // content, mirroring the interactive pin surface's storage gate.
233+ func normalizeExecInstructions (raw string ) (string , error ) {
234+ if raw == "" {
235+ return "" , nil
236+ }
237+ text := strings .TrimSpace (raw )
238+ if text == "" {
239+ return "" , errors .New ("exec --instructions requires nonempty text" )
240+ }
241+ if err := domain .ValidateContextPinText (text ); err != nil {
242+ return "" , fmt .Errorf ("exec --instructions: %w" , err )
243+ }
244+ if text != redact .String (text ) {
245+ return "" , errors .New ("exec --instructions: remove protected secrets before fixing them as context guidance" )
246+ }
247+ return text , nil
248+ }
249+
250+ // pinExecInstructions fixes the invocation's extra operator instructions as a
251+ // Thread context pin before the first turn. The ordinary pin delivery — an
252+ // integrity-checked, operator-confirmed fixed requirement selected as a
253+ // protected section every turn — carries them for this Run and any successor
254+ // the Thread creates, with the same authority ceiling as every other pin: the
255+ // text never grants tools, permissions, or policy exceptions.
256+ func pinExecInstructions (ctx context.Context , turns * application.ThreadTurnService ,
257+ threadID , text string ) error {
258+ state , err := turns .InspectContextPins (ctx , threadID )
259+ if err != nil {
260+ return fmt .Errorf ("exec instructions pin inspect: %w" , err )
261+ }
262+ if _ , err := turns .ControlContextPin (ctx , domain.ThreadContextPinControl {
263+ Version : domain .ThreadContextPinsVersion ,
264+ ThreadID : threadID ,
265+ Action : "add" ,
266+ ExpectedRevision : state .Revision ,
267+ Text : text ,
268+ OperationKey : idgen .New ("cli-exec-pin" ),
269+ RequestedBy : "cli_exec_operator" ,
270+ }); err != nil {
271+ return fmt .Errorf ("exec instructions pin: %w" , err )
272+ }
273+ return nil
274+ }
0 commit comments