forked from NVIDIA/NemoClaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·447 lines (417 loc) · 18.9 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·447 lines (417 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/bash -p
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# NemoClaw sandbox entrypoint for LangChain Deep Agents Code.
set -euo pipefail
unset BASH_ENV ENV
export HOME=/sandbox
export PATH="/usr/local/bin:/opt/venv/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin"
readonly NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE="/usr/local/lib/nemoclaw/dcode-login-profile.sh"
verify_dcode_login_profile() {
[ -d /sandbox ] \
&& [ ! -L /sandbox ] \
&& [ -f "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" ] \
&& [ ! -L "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" ] \
&& [ "$(stat -c '%U:%G:%a' "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" 2>/dev/null || true)" = "root:root:444" ] \
&& [ ! -L /sandbox/.bash_profile ] \
&& [ "$(stat -c '%U:%G:%a' /sandbox 2>/dev/null || true)" = "root:sandbox:1775" ] \
&& [ "$(stat -c '%U:%G:%a' /sandbox/.bash_profile 2>/dev/null || true)" = "root:root:444" ] \
&& cmp -s "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" /sandbox/.bash_profile
}
protect_dcode_login_profile() {
local source_metadata
source_metadata="$(stat -c '%U:%G:%a' "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" 2>/dev/null || true)"
if [ ! -f "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" ] \
|| [ -L "$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" ] \
|| [ "$source_metadata" != "root:root:444" ]; then
printf '%s\n' '[SECURITY] Managed DCode login profile is missing or unsafe.' >&2
exit 1
fi
chown root:sandbox /sandbox
chmod 1775 /sandbox
rm -f -- /sandbox/.bash_profile
install -o root -g root -m 0444 \
"$NEMOCLAW_DCODE_LOGIN_PROFILE_SOURCE" /sandbox/.bash_profile
if ! verify_dcode_login_profile; then
printf '%s\n' '[SECURITY] Could not protect the managed DCode login profile.' >&2
exit 1
fi
}
# managed-entrypoint-env-wrapper begin
_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER="/usr/local/lib/nemoclaw/entrypoint-env-wrapper.sh"
if [ ! -f "$_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER" ]; then
_DCODE_ENTRYPOINT_SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER="${_DCODE_ENTRYPOINT_SOURCE_DIR}/../../scripts/lib/entrypoint-env-wrapper.sh"
unset _DCODE_ENTRYPOINT_SOURCE_DIR
fi
if [ ! -f "$_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER" ]; then
printf '%s\n' '[SECURITY] Required entrypoint env-wrapper normalizer is missing.' >&2
exit 1
fi
# shellcheck source=scripts/lib/entrypoint-env-wrapper.sh
source "$_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER"
nemoclaw_normalize_entrypoint_env_wrapper "$@"
if [ "$NEMOCLAW_ENTRYPOINT_NORMALIZED_ARGC" -eq 0 ]; then
set --
else
set -- "${NEMOCLAW_ENTRYPOINT_NORMALIZED_ARGV[@]}"
fi
unset NEMOCLAW_ENTRYPOINT_NORMALIZED_ARGC NEMOCLAW_ENTRYPOINT_NORMALIZED_ARGV \
_NEMOCLAW_ENTRYPOINT_ENV_WRAPPER
unset -f nemoclaw_normalize_entrypoint_env_wrapper
# managed-entrypoint-env-wrapper end
# The published managed image uses uid 0 as its OCI entry user so every start
# can repair the protected login-profile boundary before immediately dropping
# to the legacy sandbox-user path. A sandbox-user image still verifies the
# image-baked boundary before continuing.
if [ "$(id -u)" -eq 0 ]; then
protect_dcode_login_profile
exec /usr/bin/setpriv --reuid=sandbox --regid=sandbox --init-groups -- \
/usr/local/bin/nemoclaw-start "$@"
fi
if ! verify_dcode_login_profile; then
printf '%s\n' '[SECURITY] DCode login profile is not protected; rebuild this sandbox.' >&2
exit 1
fi
while IFS= read -r _nemoclaw_auto_approval_env; do
unset "$_nemoclaw_auto_approval_env"
done < <(compgen -A variable NEMOCLAW_DCODE_AUTO_APPROVAL || true)
unset _nemoclaw_auto_approval_env
export DEEPAGENTS_CODE_NO_UPDATE_CHECK=1
export LANGGRAPH_NO_VERSION_CHECK=true
export LANGGRAPH_CLI_NO_ANALYTICS=1
export OTEL_ENABLED=false
export DEEPAGENTS_CODE_AUTO_UPDATE=0
export DEEPAGENTS_CODE_LANGSMITH_TRACING=false
export DEEPAGENTS_CODE_LANGSMITH_TRACING_V2=false
export DEEPAGENTS_CODE_LANGCHAIN_TRACING=false
export DEEPAGENTS_CODE_LANGCHAIN_TRACING_V2=false
export LANGSMITH_TRACING=false
export LANGSMITH_TRACING_V2=false
export LANGCHAIN_TRACING=false
export LANGCHAIN_TRACING_V2=false
export DEEPAGENTS_CODE_OFFLINE=1
export DEEPAGENTS_CODE_RIPGREP_INSTALLER=system
export DEEPAGENTS_CODE_OPENAI_API_KEY="${DEEPAGENTS_CODE_OPENAI_API_KEY:-nemoclaw-managed-inference}"
export OPENAI_BASE_URL="${OPENAI_BASE_URL:-https://inference.local/v1}"
# Harden RLIMITs (nproc + nofile) for the long-running Deep Agents Code process
# tree. Unlike the root-supervised OpenClaw/Hermes entrypoints this runs as the
# non-root sandbox user, which can still lower the inherited caps. Connect and
# exec shells are hardened independently by the system-wide profile hooks.
_NEMOCLAW_SANDBOX_RLIMITS="/usr/local/lib/nemoclaw/sandbox-rlimits.sh"
if [ ! -f "$_NEMOCLAW_SANDBOX_RLIMITS" ]; then
_NEMOCLAW_SANDBOX_RLIMITS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../scripts/lib/sandbox-rlimits.sh"
fi
if [ ! -f "$_NEMOCLAW_SANDBOX_RLIMITS" ]; then
printf '%s\n' '[SECURITY] Required sandbox-rlimits.sh is missing; refusing to start unhardened.' >&2
exit 1
fi
# shellcheck source=scripts/lib/sandbox-rlimits.sh
. "$_NEMOCLAW_SANDBOX_RLIMITS"
# shellcheck disable=SC2119 # harden_resource_limits' optional $1 selects
# quiet mode; it is not this entrypoint's own argument vector.
harden_resource_limits
# shellcheck disable=SC2119 # optional $1 selects quiet mode, not entrypoint args.
if ! verify_resource_limits_exact; then
printf '%s\n' '[SECURITY] Effective sandbox resource limits do not match policy; refusing to start unhardened.' >&2
exit 1
fi
unset _NEMOCLAW_SANDBOX_RLIMITS
# Invalid state: OpenShell's sandbox-create environment contains the host proxy
# seed, including NO_PROXY=inference.local, so dcode bypasses the managed proxy
# and attempts direct DNS resolution that is not part of the dcode contract.
# Source boundary: that seed remains correct for OpenShell's host-side proxy
# chaining; this agent-owned runtime boundary is the first safe place to replace
# it without changing OpenClaw, Hermes, or global OpenShell route provisioning.
# Source-fix constraint: inference.local is an L7 managed-proxy route, so adding
# sandbox DNS/hosts state or changing the shared seed would widen this fix and
# break the host chaining contract. Direct DNS/hosts resolution is not required.
# Regression: focused tests and the live check cover login-shell, direct dcode,
# and connect paths when the direct DNS/hosts lookup is absent.
# Removal condition: remove this normalization only when OpenShell guarantees
# the managed proxy and normalized NO_PROXY for every sandbox exec/login process,
# or when dcode no longer uses inference.local.
readonly MANAGED_PROXY_HOST_FILE="/usr/local/share/nemoclaw/dcode-proxy-host"
readonly MANAGED_PROXY_PORT_FILE="/usr/local/share/nemoclaw/dcode-proxy-port"
if [ -e /run/nemoclaw/managed-startup-ca-bundle.pem ] \
|| [ -L /run/nemoclaw/managed-startup-ca-bundle.pem ]; then
readonly MANAGED_FETCH_CA_BUNDLE_FILE="/run/nemoclaw/managed-startup-ca-bundle.pem"
else
readonly MANAGED_FETCH_CA_BUNDLE_FILE="/etc/openshell-tls/ca-bundle.pem"
fi
readonly MANAGED_PROXY_OWNER_UID=0
managed_proxy_file_metadata() {
local file="$1"
local metadata
if metadata="$(stat -c '%u:%a' "$file" 2>/dev/null)"; then
printf '%s' "$metadata"
else
stat -f '%u:%Lp' "$file" 2>/dev/null
fi
}
read_managed_proxy_value() {
local file="$1"
local name="$2"
local metadata
local value
if [ ! -f "$file" ] || [ -L "$file" ] || [ ! -r "$file" ]; then
printf 'Missing or unsafe trusted managed proxy %s file.\n' "$name" >&2
return 1
fi
metadata="$(managed_proxy_file_metadata "$file")" || {
printf 'Cannot inspect trusted managed proxy %s file.\n' "$name" >&2
return 1
}
if [ "$metadata" != "${MANAGED_PROXY_OWNER_UID}:444" ]; then
printf 'Unsafe ownership or mode on trusted managed proxy %s file.\n' "$name" >&2
return 1
fi
value="$(<"$file")"
printf '%s' "$value"
}
managed_fetch_ca_bundle_metadata() {
local file="$1"
local metadata
if metadata="$(stat -c '%u:%a:%s' "$file" 2>/dev/null)"; then
printf '%s' "$metadata"
else
stat -f '%u:%Lp:%z' "$file" 2>/dev/null
fi
}
validate_managed_fetch_ca_bundle() {
local file="$MANAGED_FETCH_CA_BUNDLE_FILE"
local metadata owner mode size extra
if [ -L "$file" ]; then
printf '%s\n' 'Missing or unsafe managed fetch CA bundle file.' >&2
return 1
fi
[ -e "$file" ] || return 0
if [ ! -f "$file" ] || [ ! -r "$file" ]; then
printf '%s\n' 'Missing or unsafe managed fetch CA bundle file.' >&2
return 1
fi
metadata="$(managed_fetch_ca_bundle_metadata "$file")" || {
printf '%s\n' 'Cannot inspect managed fetch CA bundle file.' >&2
return 1
}
IFS=: read -r owner mode size extra <<<"$metadata"
if [ -n "${extra:-}" ] \
|| [[ ! "$owner" =~ ^[0-9]+$ ]] \
|| [[ ! "$mode" =~ ^[0-7]{3,4}$ ]] \
|| [[ ! "$size" =~ ^[0-9]+$ ]] \
|| [ "$owner" != "$MANAGED_PROXY_OWNER_UID" ] \
|| [ "$size" -le 0 ] \
|| (((8#$mode & 0022) != 0)); then
printf '%s\n' 'Unsafe ownership or mode on managed fetch CA bundle file.' >&2
return 1
fi
}
# Fail closed if the root-owned image contract is missing. Process-level
# NEMOCLAW_PROXY_* values are not a trusted runtime routing source.
PROXY_HOST="$(read_managed_proxy_value "$MANAGED_PROXY_HOST_FILE" "host")"
PROXY_PORT="$(read_managed_proxy_value "$MANAGED_PROXY_PORT_FILE" "port")"
validate_managed_fetch_ca_bundle
unset NEMOCLAW_PROXY_HOST NEMOCLAW_PROXY_PORT
# Generic proxy fallbacks are outside the managed dcode contract and may carry
# host credentials even after the scheme-specific proxy values are normalized.
unset ALL_PROXY all_proxy OPENAI_PROXY
# Keep this validator behavior identical to the host-side TypeScript boundary.
# It is applied only to image-baked values that onboard writes into root-owned
# files at build time; runtime env is explicitly unset above and never reaches
# this check. Underscores remain accepted for controlled internal/container
# aliases such as proxy_name; public DNS hostnames should remain RFC 1123
# names without them. Schemes, credentials, separators, and whitespace are
# still rejected.
is_valid_proxy_host() {
local value="$1"
[[ "$value" =~ ^[A-Za-z0-9._-]+$ ]]
}
is_valid_proxy_port() {
local value="$1"
[[ "$value" =~ ^[0-9]{1,5}$ ]] || return 1
((10#$value >= 1 && 10#$value <= 65535))
}
if ! is_valid_proxy_host "$PROXY_HOST"; then
printf '%s\n' 'Invalid NEMOCLAW_PROXY_HOST for the managed runtime proxy.' >&2
exit 1
fi
if ! is_valid_proxy_port "$PROXY_PORT"; then
printf '%s\n' 'Invalid NEMOCLAW_PROXY_PORT for the managed runtime proxy.' >&2
exit 1
fi
_PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}"
_NO_PROXY_VAL="localhost,127.0.0.1,::1,${PROXY_HOST}"
# Deep Agents Code 0.1.34 intentionally ignores environment proxies in
# fetch_url so it can pin direct DNS results against rebinding. OpenShell's
# sandbox instead requires all ordinary egress, including DNS resolution for a
# destination, to stay behind its policy proxy. This explicit variable opts the
# managed package patch into that trusted proxy boundary without teaching the
# upstream tool to trust arbitrary ambient HTTP_PROXY values. It is derived
# only from the root-owned image files validated above.
export DEEPAGENTS_CODE_FETCH_URL_TRUSTED_PROXY_URL="$_PROXY_URL"
export HTTP_PROXY="$_PROXY_URL"
export HTTPS_PROXY="$_PROXY_URL"
export NO_PROXY="$_NO_PROXY_VAL"
export http_proxy="$_PROXY_URL"
export https_proxy="$_PROXY_URL"
export no_proxy="$_NO_PROXY_VAL"
write_export_if_set() {
local name="$1"
local value="${!name:-}"
[ -n "$value" ] || return 0
printf 'export %s=%q\n' "$name" "$value"
}
prepare_runtime_env() {
# Unlike prepare_observability_marker below, this file is intentionally
# volatile: every stateful start rebuilds it from root-owned proxy inputs.
local target=/tmp/nemoclaw-proxy-env.sh
local tmp
tmp="$(mktemp /tmp/nemoclaw-proxy-env.XXXXXX)"
{
printf '%s\n' 'export HOME=/sandbox'
printf '%s\n' 'export PATH="/usr/local/bin:/opt/venv/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin"'
printf '%s\n' 'export DEEPAGENTS_CODE_NO_UPDATE_CHECK=1'
printf '%s\n' 'export LANGGRAPH_NO_VERSION_CHECK=true'
printf '%s\n' 'export LANGGRAPH_CLI_NO_ANALYTICS=1'
printf '%s\n' 'export OTEL_ENABLED=false'
printf '%s\n' 'export DEEPAGENTS_CODE_AUTO_UPDATE=0'
printf '%s\n' 'export DEEPAGENTS_CODE_LANGSMITH_TRACING=false'
printf '%s\n' 'export DEEPAGENTS_CODE_LANGSMITH_TRACING_V2=false'
printf '%s\n' 'export DEEPAGENTS_CODE_LANGCHAIN_TRACING=false'
printf '%s\n' 'export DEEPAGENTS_CODE_LANGCHAIN_TRACING_V2=false'
printf '%s\n' 'export LANGSMITH_TRACING=false'
printf '%s\n' 'export LANGSMITH_TRACING_V2=false'
printf '%s\n' 'export LANGCHAIN_TRACING=false'
printf '%s\n' 'export LANGCHAIN_TRACING_V2=false'
printf '%s\n' 'export DEEPAGENTS_CODE_OFFLINE=1'
printf '%s\n' 'export DEEPAGENTS_CODE_RIPGREP_INSTALLER=system'
# Intentionally omit the trusted proxy when unset: its absence signals
# unmanaged mode, where the upstream fetch transport remains authoritative.
write_export_if_set DEEPAGENTS_CODE_FETCH_URL_TRUSTED_PROXY_URL
# shellcheck disable=SC2016
printf '%s\n' 'export DEEPAGENTS_CODE_OPENAI_API_KEY="${DEEPAGENTS_CODE_OPENAI_API_KEY:-nemoclaw-managed-inference}"'
# shellcheck disable=SC2016
printf '%s\n' 'export OPENAI_BASE_URL="${OPENAI_BASE_URL:-https://inference.local/v1}"'
printf '%s\n' 'unset ALL_PROXY all_proxy OPENAI_PROXY'
write_export_if_set HTTP_PROXY
write_export_if_set HTTPS_PROXY
write_export_if_set NO_PROXY
write_export_if_set http_proxy
write_export_if_set https_proxy
write_export_if_set no_proxy
printf '_nemoclaw_dcode_ca_bundle=%q\n' "$MANAGED_FETCH_CA_BUNDLE_FILE"
printf '_nemoclaw_dcode_ca_owner_uid=%q\n' "$MANAGED_PROXY_OWNER_UID"
cat <<'NEMOCLAW_DCODE_CA_GUARD'
if [ -L "$_nemoclaw_dcode_ca_bundle" ]; then
printf "%s\n" "Missing or unsafe managed fetch CA bundle file." >&2
return 1 2>/dev/null || exit 1
elif [ -e "$_nemoclaw_dcode_ca_bundle" ]; then
if [ ! -f "$_nemoclaw_dcode_ca_bundle" ] || [ ! -r "$_nemoclaw_dcode_ca_bundle" ]; then
printf "%s\n" "Missing or unsafe managed fetch CA bundle file." >&2
return 1 2>/dev/null || exit 1
fi
_nemoclaw_dcode_ca_meta="$(stat -c "%u:%a:%s" "$_nemoclaw_dcode_ca_bundle" 2>/dev/null || stat -f "%u:%Lp:%z" "$_nemoclaw_dcode_ca_bundle" 2>/dev/null || true)"
IFS=: read -r _nemoclaw_dcode_ca_owner _nemoclaw_dcode_ca_mode _nemoclaw_dcode_ca_size _nemoclaw_dcode_ca_extra <<NEMOCLAW_DCODE_CA_METADATA
$_nemoclaw_dcode_ca_meta
NEMOCLAW_DCODE_CA_METADATA
_nemoclaw_dcode_ca_valid=1
case "$_nemoclaw_dcode_ca_owner" in
'' | *[!0-9]*) _nemoclaw_dcode_ca_valid=0 ;;
esac
case "$_nemoclaw_dcode_ca_owner_uid" in
'' | *[!0-9]*) _nemoclaw_dcode_ca_valid=0 ;;
esac
case "$_nemoclaw_dcode_ca_mode" in
[0-7][0-7][0-7] | [0-7][0-7][0-7][0-7]) ;;
*) _nemoclaw_dcode_ca_valid=0 ;;
esac
case "$_nemoclaw_dcode_ca_size" in
'' | *[!0-9]*) _nemoclaw_dcode_ca_valid=0 ;;
esac
if [ "$_nemoclaw_dcode_ca_valid" -ne 1 ] \
|| [ -n "${_nemoclaw_dcode_ca_extra:-}" ] \
|| [ "$_nemoclaw_dcode_ca_owner" != "$_nemoclaw_dcode_ca_owner_uid" ] \
|| [ "$_nemoclaw_dcode_ca_size" -le 0 ] \
|| [ "$((0$_nemoclaw_dcode_ca_mode & 022))" -ne 0 ]; then
printf "%s\n" "Unsafe ownership or mode on managed fetch CA bundle file." >&2
return 1 2>/dev/null || exit 1
fi
fi
unset _nemoclaw_dcode_ca_bundle _nemoclaw_dcode_ca_owner_uid _nemoclaw_dcode_ca_meta _nemoclaw_dcode_ca_owner _nemoclaw_dcode_ca_mode _nemoclaw_dcode_ca_size _nemoclaw_dcode_ca_extra _nemoclaw_dcode_ca_valid
NEMOCLAW_DCODE_CA_GUARD
write_export_if_set SSL_CERT_FILE
write_export_if_set REQUESTS_CA_BUNDLE
write_export_if_set NODE_EXTRA_CA_CERTS
# LangSmith values are intentionally excluded because any inherited variable
# can be misconfigured with a token and this shared file is readable.
write_export_if_set NEMOCLAW_SANDBOX_NAME
} >"$tmp"
# Dcode intentionally runs as the non-root sandbox user, unlike the
# root-supervised OpenClaw/Hermes startup path. This atomic, sandbox-user-owned
# file is credential-free convenience state for independent login/exec shells,
# not an integrity boundary: the dcode launcher re-derives trusted proxy values
# from the root-owned image files. Secret scans guard its contents; mode 0444
# removes write bits so ordinary accidental writes fail.
chmod 444 "$tmp"
mv -f "$tmp" "$target"
}
prepare_observability_marker() {
local marker_dir=/sandbox/.deepagents
local target="${marker_dir}/.nemoclaw-observability-enabled"
local tmp
# OpenShell policy replacement can reset the sandbox's ephemeral /tmp while
# preserving its /sandbox workspace. Keep this credential-free convenience
# bit with the managed DCode state so independent exec/login shells retain
# the host-selected observability setting across policy updates. Reject a
# symlinked state directory before creating a same-directory temporary file;
# the marker remains non-authoritative and the network policy controls OTLP.
if [ -L "$marker_dir" ] || { [ -e "$marker_dir" ] && [ ! -d "$marker_dir" ]; }; then
printf '%s\n' 'Unsafe managed Deep Agents Code state directory.' >&2
return 1
fi
if [ -d "$marker_dir" ] \
&& { [ -L "$target" ] || { [ -e "$target" ] && [ ! -f "$target" ]; }; }; then
printf '%s\n' 'Unsafe managed observability marker target.' >&2
return 1
fi
# Policy replacement restarts the entrypoint without the sandbox-create
# environment. Absent therefore preserves the validated durable state;
# NemoClaw create/rebuild paths pass an explicit authoritative 1 or 0.
if [ -z "${NEMOCLAW_OBSERVABILITY+x}" ]; then
return 0
fi
if [ "$NEMOCLAW_OBSERVABILITY" != "1" ]; then
[ -d "$marker_dir" ] || return 0
rm -f "$target"
return 0
fi
mkdir -p "$marker_dir"
if [ -L "$marker_dir" ] || [ ! -d "$marker_dir" ]; then
printf '%s\n' 'Unsafe managed Deep Agents Code state directory.' >&2
return 1
fi
if [ -L "$target" ] || { [ -e "$target" ] && [ ! -f "$target" ]; }; then
printf '%s\n' 'Unsafe managed observability marker target.' >&2
return 1
fi
tmp="$(mktemp "${target}.XXXXXX")"
printf '%s\n' '1' >"$tmp"
chmod 444 "$tmp"
mv -fT -- "$tmp" "$target"
}
prepare_runtime_env
prepare_observability_marker
# With no command, this invocation IS the sandbox's long-running entrypoint.
# Deep Agents Code is a terminal-runtime agent invoked on demand via
# `openshell sandbox exec`, so the entrypoint has no daemon to run and must
# stay alive as a stable foreground process. A bare `/bin/bash` exits
# immediately in a non-interactive sandbox (no TTY, EOF on stdin), leaving the
# sandbox with no persistent process: OpenShell then flaps it into the Error
# phase, which breaks the Docker GPU-patch supervisor reconnect and leaves GPU
# posture unreliable (#5717). Idle forever instead so the sandbox stays Ready.
if [ "$#" -eq 0 ]; then
printf '%s\n' 'Setting up NemoClaw Deep Agents Code runtime...'
exec -a nemoclaw-dcode-entrypoint tail -f /dev/null
fi
exec "$@"