@@ -20,10 +20,12 @@ import (
2020 "testing"
2121 "time"
2222
23+ "github.com/BurntSushi/toml"
2324 "github.com/databricks/cli/experimental/ssh/internal/agentlauncher"
2425
2526 "github.com/databricks/cli/libs/cmdio"
2627 "github.com/databricks/cli/libs/dbr"
28+ "github.com/databricks/cli/libs/env"
2729 "github.com/databricks/cli/libs/telemetry/protos"
2830 "github.com/databricks/databricks-sdk-go"
2931 "github.com/databricks/databricks-sdk-go/config"
@@ -360,9 +362,8 @@ func TestEnsureToolchain(t *testing.T) {
360362func TestInjectAgentContext (t * testing.T ) {
361363 claude , _ := agentlauncher .AgentByName ("claude" )
362364 codex , _ := agentlauncher .AgentByName ("codex" )
363- noContext := agentlauncher.AgentSpec {Name : "none" } // an agent with neither mechanism
364365
365- t .Run ("flag agent writes a scratch file and returns the flag " , func (t * testing.T ) {
366+ t .Run ("claude gets the context as a scratch file" , func (t * testing.T ) {
366367 home := t .TempDir ()
367368 args , err := injectAgentContext (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , claude )
368369 require .NoError (t , err )
@@ -373,27 +374,20 @@ func TestInjectAgentContext(t *testing.T) {
373374 assert .Contains (t , string (data ), "databricks ssh connect" )
374375 })
375376
376- t .Run ("home-file agent writes the global instructions file, no args " , func (t * testing.T ) {
377+ t .Run ("codex gets the context as a TOML config override " , func (t * testing.T ) {
377378 home := t .TempDir ()
378- args , err := injectAgentContext (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , codex )
379+ ctx := env .Set (dbr .MockRuntime (t .Context (), serverlessRuntime ), workspaceHomeEnv , "/Workspace/Users/a\" b@example.com" )
380+ args , err := injectAgentContext (ctx , home , codex )
379381 require .NoError (t , err )
380- assert .Nil (t , args )
381- data , err := os .ReadFile (filepath .Join (home , ".codex" , "AGENTS.md" ))
382- require .NoError (t , err )
383- assert .Contains (t , string (data ), "databricks ssh connect" )
384- })
385-
386- t .Run ("home-file agent does not clobber an existing file" , func (t * testing.T ) {
387- home := t .TempDir ()
388- target := filepath .Join (home , ".codex" , "AGENTS.md" )
389- require .NoError (t , os .MkdirAll (filepath .Dir (target ), 0o755 ))
390- require .NoError (t , os .WriteFile (target , []byte ("user's own instructions" ), 0o644 ))
391- args , err := injectAgentContext (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , codex )
392- require .NoError (t , err )
393- assert .Nil (t , args )
394- data , err := os .ReadFile (target )
382+ require .Len (t , args , 2 )
383+ assert .Equal (t , "-c" , args [0 ])
384+ // codex splits the override on the first '=' and parses the rest as TOML
385+ var got map [string ]string
386+ require .NoError (t , toml .Unmarshal ([]byte (args [1 ]), & got ))
387+ assert .Equal (t , map [string ]string {"developer_instructions" : agentSystemContext ("/Workspace/Users/a\" b@example.com" , true )}, got )
388+ entries , err := os .ReadDir (home )
395389 require .NoError (t , err )
396- assert .Equal (t , "user's own instructions" , string ( data ) )
390+ assert .Empty (t , entries )
397391 })
398392
399393 t .Run ("context describes the compute the shim runs on" , func (t * testing.T ) {
@@ -413,16 +407,6 @@ func TestInjectAgentContext(t *testing.T) {
413407 assert .Contains (t , string (data ), c .want )
414408 }
415409 })
416-
417- t .Run ("agent without a mechanism writes nothing" , func (t * testing.T ) {
418- home := t .TempDir ()
419- args , err := injectAgentContext (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , noContext )
420- require .NoError (t , err )
421- assert .Nil (t , args )
422- entries , err := os .ReadDir (home )
423- require .NoError (t , err )
424- assert .Empty (t , entries )
425- })
426410}
427411
428412func TestAgentSystemContext (t * testing.T ) {
@@ -796,12 +780,21 @@ func TestPrepareAgentLaunch(t *testing.T) {
796780 agent , ok := agentlauncher .AgentByName ("claude" )
797781 require .True (t , ok )
798782
799- t .Run ("passes the context args before the user's agent args" , func (t * testing.T ) {
783+ t .Run ("passes the context args after the user's args" , func (t * testing.T ) {
800784 home := t .TempDir ()
801785 argv , environ , err := prepareAgentLaunch (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , agent , []string {"--resume" })
802786 require .NoError (t , err )
803787 require .NotEmpty (t , argv )
804- assert .Equal (t , []string {"claude" , "--" , "--append-system-prompt-file" , filepath .Join (home , agentRootDir , contextFile ), "--resume" }, argv [1 :])
788+ assert .Equal (t , []string {"claude" , "--" , "--resume" , "--append-system-prompt-file" , filepath .Join (home , agentRootDir , contextFile )}, argv [1 :])
789+ assert .Equal (t , os .Environ (), environ )
790+ })
791+
792+ t .Run ("passes the context args before a '--' arg" , func (t * testing.T ) {
793+ home := t .TempDir ()
794+ argv , environ , err := prepareAgentLaunch (dbr .MockRuntime (t .Context (), serverlessRuntime ), home , agent , []string {"--max-budget-usd" , "1" , "--" , "hello there" })
795+ require .NoError (t , err )
796+ require .NotEmpty (t , argv )
797+ assert .Equal (t , []string {"claude" , "--" , "--max-budget-usd" , "1" , "--append-system-prompt-file" , filepath .Join (home , agentRootDir , contextFile ), "--" , "hello there" }, argv [1 :])
805798 assert .Equal (t , os .Environ (), environ )
806799 })
807800
0 commit comments