@@ -103,7 +103,7 @@ preferred_auth_method = "apikey"
103103
104104[sandbox_workspace_write]
105105network_access = true
106- writable_roots = ["${ARG_CODEX_START_DIRECTORY} "]
106+ writable_roots = ["${ARG_CODEX_START_DIRECTORY} ", " $HOME /.codex" ]
107107
108108EOF
109109}
@@ -150,33 +150,31 @@ function populate_config_toml() {
150150
151151function add_instruction_prompt_if_exists() {
152152 if [ -n " ${ARG_CODEX_INSTRUCTION_PROMPT:- } " ]; then
153- if [ -d " ${ARG_CODEX_START_DIRECTORY} " ]; then
154- printf " Directory '%s' exists. Changing to it.\\ n" " ${ARG_CODEX_START_DIRECTORY} "
155- cd " ${ARG_CODEX_START_DIRECTORY} " || {
156- printf " Error: Could not change to directory '%s'.\\ n" " ${ARG_CODEX_START_DIRECTORY} "
157- exit 1
158- }
153+ # Create AGENTS.md in .codex directory instead of polluting the working directory
154+ AGENTS_PATH=" $HOME /.codex/AGENTS.md"
155+ printf " Creating AGENTS.md in .codex directory: %s\\ n" " ${AGENTS_PATH} "
156+
157+ # Ensure .codex directory exists
158+ mkdir -p " $HOME /.codex"
159+
160+ # Check if AGENTS.md contains the instruction prompt already
161+ if [ -f " ${AGENTS_PATH} " ] && grep -Fxq " ${ARG_CODEX_INSTRUCTION_PROMPT} " " ${AGENTS_PATH} " ; then
162+ printf " AGENTS.md already contains the instruction prompt. Skipping append.\n"
159163 else
160- printf " Directory '%s' does not exist. Creating and changing to it.\\ n" " ${ARG_CODEX_START_DIRECTORY} "
164+ printf " Appending instruction prompt to AGENTS.md in .codex directory\n"
165+ echo -e " \n${ARG_CODEX_INSTRUCTION_PROMPT} " >> " ${AGENTS_PATH} "
166+ fi
167+
168+ # Ensure the working directory exists for Codex to run in
169+ if [ ! -d " ${ARG_CODEX_START_DIRECTORY} " ]; then
170+ printf " Creating start directory '%s'\\ n" " ${ARG_CODEX_START_DIRECTORY} "
161171 mkdir -p " ${ARG_CODEX_START_DIRECTORY} " || {
162172 printf " Error: Could not create directory '%s'.\\ n" " ${ARG_CODEX_START_DIRECTORY} "
163173 exit 1
164174 }
165- cd " ${ARG_CODEX_START_DIRECTORY} " || {
166- printf " Error: Could not change to directory '%s'.\\ n" " ${ARG_CODEX_START_DIRECTORY} "
167- exit 1
168- }
169- fi
170-
171- # Check if AGENTS.md contains the instruction prompt already
172- if [ -f AGENTS.md ] && grep -Fxq " ${ARG_CODEX_INSTRUCTION_PROMPT} " AGENTS.md; then
173- printf " AGENTS.md already contains the instruction prompt. Skipping append.\n"
174- else
175- printf " Appending instruction prompt to AGENTS.md\n"
176- echo -e " \n${ARG_CODEX_INSTRUCTION_PROMPT} " >> AGENTS.md
177175 fi
178176 else
179- printf " AGENTS.md is not set.\n"
177+ printf " AGENTS.md instruction prompt is not set.\n"
180178 fi
181179}
182180
0 commit comments