1- """The shipped Devin Desktop hooks.json, run the way Cascade runs it (bash -c) .
1+ """The shipped Devin Desktop hooks.json, run the way Cascade runs it.
22
3- Cascade treats exit code 2 from a pre-hook as "block" (pre_user_prompt
4- blocks the prompt). Every shipped command must exit 0 with empty stdout,
5- also when the emitter or python3 is missing.
3+ Cascade runs a hook's ``command`` with ``bash -c`` and passes the event JSON on
4+ stdin (https://docs.devin.ai/desktop/cascade/hooks, Configuration Options). It
5+ treats exit code 2 from a pre-hook as "block" (pre_user_prompt blocks the
6+ prompt). Every shipped command must exit 0 with empty stdout, also when the
7+ emitter or python3 is missing.
8+
9+ Each command is also run with the payload as a trailing heredoc
10+ (``<command> <<'EOF'``), the transport some hook hosts use: the subshell guard
11+ ``( ... || true)`` keeps the heredoc on the emitter instead of on ``true``.
612"""
713from __future__ import annotations
814
1420import unittest
1521from pathlib import Path
1622
17- from tests .helpers import ROOT , Receiver , all_spans
18-
19- BASH = shutil .which ("bash" ) or "/bin/bash"
23+ from tests .helpers import ROOT , Receiver
2024
2125APP_DIR = ROOT .parent / "devin-desktop"
2226HOOKS = json .loads ((APP_DIR / "hooks.json" ).read_text (encoding = "utf-8" ))["hooks" ]
2327EXPECTED = {"pre_user_prompt" , "post_read_code" , "post_write_code" , "post_run_command" ,
2428 "post_mcp_tool_use" , "post_cascade_response" }
29+ # Cascade uses bash; /bin/sh is the POSIX baseline.
30+ SHELLS = [sh for sh in ("/bin/sh" , shutil .which ("bash" ) or "/bin/bash" ) if os .path .exists (sh )]
31+ TRANSPORTS = ("stdin" , "heredoc" )
32+ # Span each hook exports once the emitter is installed.
33+ SPAN_FOR = {"pre_user_prompt" : None , "post_read_code" : "read_code" , "post_write_code" : "write_code" ,
34+ "post_run_command" : "run_command" , "post_mcp_tool_use" : "list_commits" ,
35+ "post_cascade_response" : "Cascade turn" }
2536
2637
27- def payload (event : str ) -> str :
28- return json .dumps ({"agent_action_name" : event , "trajectory_id" : "cfg-traj" , "execution_id" : "cfg-exec" ,
29- "model_name" : "Claude Sonnet 4" , "tool_info" : {"file_path" : "/tmp/x" }})
38+ def payload (event : str , trajectory : str = "cfg-traj" ) -> str :
39+ info = {"file_path" : "/tmp/x" , "edits" : [], "command_line" : "ls" , "mcp_server_name" : "github" ,
40+ "mcp_tool_name" : "list_commits" , "user_prompt" : "p" , "response" : "r" }
41+ return json .dumps ({"agent_action_name" : event , "trajectory_id" : trajectory , "execution_id" : "cfg-exec" ,
42+ "model_name" : "Claude Sonnet 4" , "tool_info" : info })
3043
3144
32- def run (command : str , home : str , stdin : str , ** env : str ) -> subprocess .CompletedProcess :
45+ def run (command : str , shell : str , transport : str , home : str , stdin : str , ** env : str ) -> subprocess .CompletedProcess :
3346 full = {"PATH" : os .environ .get ("PATH" , "/usr/bin:/bin" ), "HOME" : home }
3447 full .update (env )
35- return subprocess .run ([BASH , "-c" , command ], input = stdin , capture_output = True , text = True ,
36- env = full , timeout = 30 )
48+ if transport == "heredoc" :
49+ script , stdin = f"{ command } <<'HOOK_EOF'\n { stdin } \n HOOK_EOF\n " , ""
50+ else :
51+ script = command
52+ return subprocess .run ([shell , "-c" , script ], input = stdin , capture_output = True , text = True ,
53+ env = full , cwd = home , timeout = 30 )
3754
3855
3956class DevinDesktopHooksConfigTest (unittest .TestCase ):
@@ -43,54 +60,73 @@ def setUp(self):
4360 def tearDown (self ):
4461 shutil .rmtree (self .home , ignore_errors = True )
4562
46- def test_registered_hooks (self ):
63+ def test_registered_hooks_are_guarded (self ):
4764 self .assertEqual (set (HOOKS ), EXPECTED )
4865 self .assertNotIn ("post_cascade_response_with_transcript" , HOOKS )
4966 for event , entries in HOOKS .items ():
5067 self .assertEqual (len (entries ), 1 , event )
5168 entry = entries [0 ]
5269 self .assertIs (entry ["show_output" ], False , event )
53- self .assertTrue (entry ["command" ].startswith ("python3 -B " ), event )
54- self .assertTrue (entry ["command" ].endswith (f" devin_desktop { event } 2>/dev/null || true" ), event )
70+ self .assertTrue (entry ["command" ].startswith ("( python3 -B " ), event )
71+ self .assertTrue (entry ["command" ].endswith (f" devin_desktop { event } 2>/dev/null || true) " ), event )
5572
5673 def test_page_shows_the_shipped_hooks_json (self ):
5774 page = (ROOT .parents [2 ] / "src/pages/docs/integrations/coding-agents/devin-desktop.mdx" ).read_text (encoding = "utf-8" )
5875 block = page .split ("```json\n " , 1 )[1 ].split ("\n ```" , 1 )[0 ]
5976 self .assertEqual (json .loads (block )["hooks" ], HOOKS )
6077
6178 def test_missing_emitter_never_blocks (self ):
62- for event , entries in HOOKS .items ():
63- result = run (entries [0 ]["command" ], self .home , payload (event ))
64- self .assertEqual ((result .returncode , result .stdout , result .stderr ), (0 , "" , "" ), event )
79+ for shell in SHELLS :
80+ for transport in TRANSPORTS :
81+ for event , entries in HOOKS .items ():
82+ result = run (entries [0 ]["command" ], shell , transport , self .home , payload (event ))
83+ self .assertEqual ((result .returncode , result .stdout , result .stderr ), (0 , "" , "" ),
84+ f"{ shell } { transport } { event } " )
6585
6686 def test_missing_python_never_blocks (self ):
6787 empty_bin = tempfile .mkdtemp ()
6888 try :
69- for event , entries in HOOKS .items ():
70- result = run (entries [0 ]["command" ], self .home , payload (event ), PATH = empty_bin )
71- self .assertEqual ((result .returncode , result .stdout ), (0 , "" ), event )
89+ for shell in SHELLS :
90+ for transport in TRANSPORTS :
91+ for event , entries in HOOKS .items ():
92+ result = run (entries [0 ]["command" ], shell , transport , self .home , payload (event ),
93+ PATH = empty_bin )
94+ self .assertEqual ((result .returncode , result .stdout ), (0 , "" ), f"{ shell } { transport } { event } " )
7295 finally :
7396 shutil .rmtree (empty_bin , ignore_errors = True )
7497
75- def test_installed_commands_export_and_print_nothing (self ):
76- install = subprocess .run ([BASH , str (APP_DIR / "install.sh" )], capture_output = True , text = True ,
98+ def test_installed_commands_read_the_payload_and_print_nothing (self ):
99+ install = subprocess .run ([shutil .which ("bash" ) or "/bin/bash" , str (APP_DIR / "install.sh" )],
100+ capture_output = True , text = True ,
77101 env = {"PATH" : os .environ .get ("PATH" , "" ), "HOME" : self .home }, timeout = 30 )
78102 self .assertEqual (install .returncode , 0 , install .stderr )
79103 installed = json .loads (Path (self .home , ".codeium/windsurf/hooks.json" ).read_text ())["hooks" ]
80104 self .assertEqual (installed , HOOKS )
81- receiver = Receiver ()
82- try :
83- for event in ("pre_user_prompt" , "post_read_code" , "post_cascade_response" ):
84- result = run (HOOKS [event ][0 ]["command" ], self .home , payload (event ),
85- FI_API_KEY = "k" , FI_SECRET_KEY = "s" , FI_HOOK_SYNC = "1" ,
86- FI_OTLP_ENDPOINT = f"{ receiver .url } /v1/traces" ,
87- FI_HOOK_STATE_DIR = os .path .join (self .home , "state" ))
88- self .assertEqual ((result .returncode , result .stdout ), (0 , "" ), event )
89- receiver .wait_for (2 )
90- names = {s ["name" ] for _ , _ , body in receiver .requests for s in all_spans (body )}
91- self .assertEqual (names , {"read_code" , "Cascade turn" , "Cascade session" })
92- finally :
93- receiver .close ()
105+ for shell in SHELLS :
106+ for transport in TRANSPORTS :
107+ receiver = Receiver ()
108+ trajectory = f"cfg-{ Path (shell ).name } -{ transport } "
109+ try :
110+ for event in HOOKS :
111+ before = len (receiver .requests )
112+ result = run (HOOKS [event ][0 ]["command" ], shell , transport , self .home ,
113+ payload (event , trajectory ),
114+ FI_API_KEY = "k" , FI_SECRET_KEY = "s" , FI_HOOK_SYNC = "1" ,
115+ FI_OTLP_ENDPOINT = f"{ receiver .url } /v1/traces" ,
116+ FI_HOOK_STATE_DIR = os .path .join (self .home , "state" ))
117+ label = f"{ shell } { transport } { event } "
118+ self .assertEqual ((result .returncode , result .stdout ), (0 , "" ), label )
119+ expected = SPAN_FOR [event ]
120+ if expected is None : # the turn start exports nothing yet
121+ self .assertEqual (len (receiver .requests ), before , label )
122+ continue
123+ receiver .wait_for (before + 1 )
124+ names = {s ["name" ] for _ , _ , body in receiver .requests [before :]
125+ for rs in body ["resourceSpans" ] for ss in rs ["scopeSpans" ] for s in ss ["spans" ]}
126+ self .assertIn (expected , names , label )
127+ finally :
128+ receiver .close ()
129+ self .assertEqual (list (Path (self .home ).rglob ("__pycache__" )), [], "-B writes no bytecode" )
94130
95131
96132if __name__ == "__main__" :
0 commit comments