forked from paradigmxyz/centaur
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
267 lines (239 loc) · 11.4 KB
/
entrypoint.sh
File metadata and controls
267 lines (239 loc) · 11.4 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
#!/bin/bash
set -e
HOME_DIR="$(eval echo ~)"
FIREWALL_HOSTNAME="${FIREWALL_HOST:-firewall}"
STATE_DIR="${CENTAUR_STATE_DIR:-$HOME_DIR/state}"
if [ -d "$STATE_DIR" ] && [ -w "$STATE_DIR" ]; then
mkdir -p "$STATE_DIR/workspace" "$STATE_DIR/uploads" "$STATE_DIR/branches" "$STATE_DIR/codex" "$STATE_DIR/claude"
rm -rf "$HOME_DIR/.codex" "$HOME_DIR/.claude" "$HOME_DIR/uploads" "$HOME_DIR/branches"
ln -s "$STATE_DIR/codex" "$HOME_DIR/.codex"
ln -s "$STATE_DIR/claude" "$HOME_DIR/.claude"
ln -s "$STATE_DIR/uploads" "$HOME_DIR/uploads"
ln -s "$STATE_DIR/branches" "$HOME_DIR/branches"
export CENTAUR_PERSISTENT_STATE=1
fi
mkdir -p "$HOME_DIR/.config/amp"
# ── Write harness configs (no MCP — adds ~10s startup overhead) ───────────────
cat > "$HOME_DIR/.config/amp/settings.json" <<EOF
{
"amp.experimental.compaction": 95,
"amp.proxy": "http://${FIREWALL_HOSTNAME}:8080",
"amp.git.commit.coauthor.enabled": false
}
EOF
# ── Mock Google ADC for sandbox-only SDK initialization ─────────────────────
# Some Google client libraries refuse to initialize without ADC, even when the
# per-sandbox proxy is responsible for attaching the real auth headers.
if [ -z "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]; then
GOOGLE_APPLICATION_CREDENTIALS="$HOME_DIR/.config/gcloud/application_default_credentials.json"
export GOOGLE_APPLICATION_CREDENTIALS
mkdir -p "$(dirname "$GOOGLE_APPLICATION_CREDENTIALS")"
if [ ! -f "$GOOGLE_APPLICATION_CREDENTIALS" ]; then
# Some SDKs parse ADC into service-account credentials locally before any
# outbound request reaches the proxy, so the stub must look real enough
# to pass key loading.
_mock_gcp_private_key="$(openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 2>/dev/null)"
MOCK_GCP_PRIVATE_KEY="$_mock_gcp_private_key" python3 - "$GOOGLE_APPLICATION_CREDENTIALS" <<'PYEOF'
import json
import os
import sys
path = sys.argv[1]
client_email = "mock@creds.com"
with open(path, "w") as f:
json.dump(
{
"type": "service_account",
"project_id": "centaur-sandbox",
"private_key_id": "0000000000000000000000000000000000000000",
"private_key": os.environ["MOCK_GCP_PRIVATE_KEY"].rstrip("\n") + "\n",
"client_email": client_email,
"client_id": "100000000000000000000",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": f"https://www.googleapis.com/robot/v1/metadata/x509/{client_email.replace('@', '%40')}",
"universe_domain": "googleapis.com",
},
f,
indent=2,
)
f.write("\n")
PYEOF
unset _mock_gcp_private_key
fi
fi
# ── Codex settings ──────────────────────────────────────────────────────────
# CODEX_AUTH_MODE selects how codex authenticates with the upstream:
# - api_key (default): codex uses an OPENAI_API_KEY against api.openai.com.
# The entrypoint runs `codex login --with-api-key` below, which overwrites
# auth.json.
# - access_token: codex uses a ChatGPT-style access token against
# chatgpt.com. The default auth.json (auth_mode: chatgpt) is always
# installed and the api-key login step is skipped so iron-proxy can
# inject the brokered Bearer + chatgpt-account-id headers.
CODEX_AUTH_MODE="${CODEX_AUTH_MODE:-api_key}"
mkdir -p "$HOME_DIR/.codex"
if [ "$CODEX_AUTH_MODE" = "access_token" ] && [ -f /etc/centaur/codex-auth.default.json ]; then
cp /etc/centaur/codex-auth.default.json "$HOME_DIR/.codex/auth.json"
chmod 600 "$HOME_DIR/.codex/auth.json"
elif [ ! -f "$HOME_DIR/.codex/auth.json" ] && [ -f /etc/centaur/codex-auth.default.json ]; then
cp /etc/centaur/codex-auth.default.json "$HOME_DIR/.codex/auth.json"
chmod 600 "$HOME_DIR/.codex/auth.json"
fi
if [ -n "${CENTAUR_TRACE_ID:-}" ]; then
printf '%s' "$CENTAUR_TRACE_ID" > "$HOME_DIR/.trace_id"
fi
HARNESS_CONFIG_DIR="${CENTAUR_HARNESS_CONFIG_DIR:-$HOME_DIR/harness}"
if [ -f "$HARNESS_CONFIG_DIR/codex/config.toml" ]; then
cp "$HARNESS_CONFIG_DIR/codex/config.toml" "$HOME_DIR/.codex/config.toml"
else
echo "missing Codex harness config: $HARNESS_CONFIG_DIR/codex/config.toml" >&2
exit 1
fi
# ── Claude Code settings ────────────────────────────────────────────────────
mkdir -p "$HOME_DIR/.claude"
if [ -f "$HARNESS_CONFIG_DIR/claude/settings.json" ]; then
cp "$HARNESS_CONFIG_DIR/claude/settings.json" "$HOME_DIR/.claude/settings.json"
fi
# CLAUDE_CODE_AUTH_MODE selects how Claude Code authenticates with the upstream
# (mirrors CODEX_AUTH_MODE):
# - api_key (default): Claude Code uses ANTHROPIC_API_KEY against
# api.anthropic.com. The harness stub key is left in the env; iron-proxy's
# ANTHROPIC_API_KEY HttpSecret rewrites the X-Api-Key header on the wire.
# - access_token: Claude Code runs as a Claude.ai Pro or Max subscription
# user. We install a dummy ~/.claude/.credentials.json so the CLI emits
# OAuth-shaped requests, unset the API-key stub so it does not fall back
# to X-Api-Key, and let iron-token-broker mint a real Bearer at request
# time via the anthropic-claude brokered_token secret.
CLAUDE_CODE_AUTH_MODE="${CLAUDE_CODE_AUTH_MODE:-api_key}"
case "$CLAUDE_CODE_AUTH_MODE" in
api_key)
:
;;
access_token)
unset ANTHROPIC_API_KEY
if [ -f /etc/centaur/claude-credentials.default.json ]; then
cp /etc/centaur/claude-credentials.default.json "$HOME_DIR/.claude/.credentials.json"
chmod 600 "$HOME_DIR/.claude/.credentials.json"
fi
;;
*)
echo "unknown CLAUDE_CODE_AUTH_MODE: $CLAUDE_CODE_AUTH_MODE (expected api_key or access_token)" >&2
exit 1
;;
esac
# ── Pi-mono settings ─────────────────────────────────────────────────────────
mkdir -p "$HOME_DIR/.pi/agent/extensions"
cat > "$HOME_DIR/.pi/agent/settings.json" <<EOF
{
"provider": "anthropic",
"model": "claude-sonnet-4-20250514",
"thinkingLevel": "medium",
"autoCompaction": true
}
EOF
# ── Per-session workspace clone (no shared worktree metadata) ────────────────
if [ "${CENTAUR_PERSISTENT_STATE:-0}" = "1" ]; then
WORKSPACE_DIR="$STATE_DIR/workspace"
else
WORKSPACE_DIR="$HOME_DIR/workspace"
fi
if [ -n "${AGENT_REPO:-}" ]; then
REPO_PATH="$HOME_DIR/github/$AGENT_REPO"
if ! git -C "$REPO_PATH" rev-parse --git-dir >/dev/null 2>&1; then
echo "AGENT_REPO is not a valid git repository: $REPO_PATH" >&2
exit 1
fi
if ! git -C "$WORKSPACE_DIR" rev-parse --git-dir >/dev/null 2>&1; then
rm -rf "$WORKSPACE_DIR"
if ! git clone --quiet --shared "$REPO_PATH" "$WORKSPACE_DIR"; then
echo "shared clone failed for $REPO_PATH; retrying with regular clone" >&2
rm -rf "$WORKSPACE_DIR"
git clone --quiet "$REPO_PATH" "$WORKSPACE_DIR"
fi
BRANCH="agent-$(date +%s)-${RANDOM}-${RANDOM}"
git -C "$WORKSPACE_DIR" checkout -q -b "$BRANCH" || true
fi
else
mkdir -p "$WORKSPACE_DIR"
fi
# ── Ensure uploads directory exists ──────────────────────────────────────────
mkdir -p "$HOME_DIR/uploads"
# ── Copy project skills into workspace (so `skill` tool discovers them) ──────
BAKED_IN_CENTAUR_SKILLS="$HOME_DIR/.agents/skills"
MOUNTED_CENTAUR_SKILLS="$HOME_DIR/centaur-skills"
MOUNTED_ORG_SKILLS="$HOME_DIR/centaur-overlay-skills"
OVERLAY_TREE_SKILLS=""
if [ -n "${CENTAUR_OVERLAY_DIR:-}" ] && [ -d "${CENTAUR_OVERLAY_DIR}/.agents/skills" ]; then
OVERLAY_TREE_SKILLS="${CENTAUR_OVERLAY_DIR}/.agents/skills"
fi
CENTAUR_SKILLS=""
if [ -d "$HOME_DIR/github" ]; then
CENTAUR_SKILLS="$(find "$HOME_DIR/github" -path '*/centaur/.agents/skills' -type d -print -quit 2>/dev/null || true)"
fi
WS_SKILLS="$WORKSPACE_DIR/.agents/skills"
for SKILLS_SRC in "$BAKED_IN_CENTAUR_SKILLS" "$MOUNTED_CENTAUR_SKILLS" "$CENTAUR_SKILLS" "$MOUNTED_ORG_SKILLS" "$OVERLAY_TREE_SKILLS"; do
if [ -d "$SKILLS_SRC" ]; then
mkdir -p "$WS_SKILLS"
cp -r "$SKILLS_SRC"/. "$WS_SKILLS"/
fi
done
if [ -d "$WS_SKILLS" ]; then
mkdir -p "$WORKSPACE_DIR/.claude"
rm -rf "$WORKSPACE_DIR/.claude/skills"
ln -sf "$WS_SKILLS" "$WORKSPACE_DIR/.claude/skills"
fi
# ── Assemble system prompt from bind mounts ──────────────────────────────────
# Base prompt: mounted as AGENTS_BASE.md when present, fallback to baked-in AGENTS.md.
# Org/persona overlays are mounted alongside the base prompt when present.
TARGET_PROMPT="$WORKSPACE_DIR/AGENTS.md"
if [ -f "$HOME_DIR/AGENTS_BASE.md" ]; then
cp "$HOME_DIR/AGENTS_BASE.md" "$TARGET_PROMPT"
elif [ -f "$HOME_DIR/AGENTS.md" ]; then
cp "$HOME_DIR/AGENTS.md" "$TARGET_PROMPT"
fi
if [ -f "$HOME_DIR/AGENTS_OVERLAY.md" ] && [ -f "$TARGET_PROMPT" ]; then
printf '\n\n---\n\n' >> "$TARGET_PROMPT"
cat "$HOME_DIR/AGENTS_OVERLAY.md" >> "$TARGET_PROMPT"
fi
# Persona prompt injection is done by the API when it writes AGENTS_BASE.md.
# Switch to workspace so the harness reads workspace/AGENTS.md (with persona overlay)
cd "$WORKSPACE_DIR"
HARNESS_ADAPTER="${CENTAUR_HARNESS_ADAPTER:-/usr/local/bin/harness-adapter}"
if [ -x "$HARNESS_ADAPTER" ]; then
"$HARNESS_ADAPTER" "${1:-}" "$TARGET_PROMPT"
fi
# Codex reads its auth file when the app server starts. Complete this before
# signaling readiness, otherwise warm pods can be claimed with no auth loaded.
# Skipped under access_token mode — that path relies on the chatgpt auth.json
# installed above plus iron-proxy injecting the real Bearer at request time.
if [ "$CODEX_AUTH_MODE" != "access_token" ]; then
CODEX_KEY="${CODEX_API_KEY:-${OPENAI_API_KEY:-}}"
if [ -n "$CODEX_KEY" ]; then
echo "$CODEX_KEY" | codex login --with-api-key 2>/dev/null || true
fi
fi
# Wait for the tool-server sidecar before signalling readiness, so the harness
# doesn't issue its first tool call before the server is listening.
if [ -n "${CENTAUR_TOOLS_URL:-}" ]; then
_tools_deadline=$(( $(date +%s) + ${CENTAUR_TOOLS_WAIT_SECONDS:-10} ))
until curl -fsS --noproxy '*' --max-time 2 "${CENTAUR_TOOLS_URL}/healthz" >/dev/null 2>&1; do
if [ "$(date +%s)" -ge "$_tools_deadline" ]; then
echo "tool-server /healthz not ready after ${CENTAUR_TOOLS_WAIT_SECONDS:-10}s; continuing" >&2
break
fi
sleep 0.5
done
fi
# Signal readiness
touch "$HOME_DIR/.ready"
# ── Background: slow auth tasks ─────────────────────────────────────────────
{
if [ -n "${GITHUB_TOKEN:-}" ]; then
git config --global credential.helper store
printf 'https://oauth2:%s@github.com\n' "$GITHUB_TOKEN" > "$HOME_DIR/.git-credentials"
echo "${GITHUB_TOKEN}" | gh auth login --with-token 2>/dev/null || true
gh auth setup-git 2>/dev/null || true
fi
} &
exec "$@"