Repository navigation
Expand file tree
/
Copy pathdev-fullstack.sh
More file actions
executable file
·181 lines (170 loc) · 8.08 KB
/
Copy pathdev-fullstack.sh
File metadata and controls
executable file
·181 lines (170 loc) · 8.08 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
#!/bin/bash
# Start the full KiroCrew dev stack in ONE terminal:
# 1. Backend gateway from live source (dev-backend.sh, port 6777, .kirocrew-dev home)
# 2. Vite dev server (hot reload) proxying to it
# 3. Mint a dashboard token and print the ready-to-open Vite URL
#
# Usage: ./dev-fullstack.sh
# Ctrl+C stops backend + Vite together. Logs stream into this terminal.
# Backend code changes: Ctrl+C and re-run. Frontend changes: hot-reload on save.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
GATEWAY_PORT="${KIROCREW_PORT:-6777}"
# Resolve the runtime Python ONCE, with the same precedence dev-backend.sh
# uses (RUNTIME_PYTHON override, else the repo venv), and reuse it for the
# token mint below — hardcoding .venv/bin/python here while dev-backend.sh
# honors RUNTIME_PYTHON would launch the stack fine but silently fail to
# mint the promised auth URL.
RUNTIME_PYTHON="${RUNTIME_PYTHON:-}"
if [ -z "$RUNTIME_PYTHON" ] || [ ! -x "$RUNTIME_PYTHON" ]; then
if [ -x "$SCRIPT_DIR/.venv/bin/python" ]; then
RUNTIME_PYTHON="$SCRIPT_DIR/.venv/bin/python"
fi
fi
if [ ! -x "$RUNTIME_PYTHON" ]; then
echo "[dev] ERROR: cannot find the KiroCrew venv Python at $SCRIPT_DIR/.venv/bin/python."
echo "[dev] Run 'bash minimal_install.sh' once to set up the venv, then try again."
exit 1
fi
# Fail fast if something already listens on the gateway port. Otherwise the
# spawned gateway dies on bind while the readiness probe below is satisfied
# by the OLD process — the script reports success, Vite proxies to an
# unrelated (or stale) service, and teardown kills nothing that matters.
if curl -fsS -o /dev/null --max-time 2 "http://127.0.0.1:$GATEWAY_PORT/" 2>/dev/null; then
echo "[dev] ERROR: something is already listening on :$GATEWAY_PORT."
echo "[dev] Stop it first (kirocrew stop --port $GATEWAY_PORT) or set KIROCREW_PORT to a free port."
exit 1
fi
# One EFFECTIVE data home for the whole stack. dev-backend.sh honors a
# caller-provided KIROCREW_HOME, so the token mint below must use the same
# home the backend booted with — hardcoding .kirocrew-dev for one of them
# yields a token signed with a different secret (auth silently fails).
# Same default + relative-path handling as dev-backend.sh.
KIROCREW_HOME="${KIROCREW_HOME:-.kirocrew-dev}"
case "$KIROCREW_HOME" in
/*) ;;
*) KIROCREW_HOME="$SCRIPT_DIR/$KIROCREW_HOME" ;;
esac
export KIROCREW_HOME
# Logs live under the dev data home (repo-local, gitignored — same place
# dev-backend.sh keeps its state) rather than the shared world-writable /tmp:
# no cross-user collisions on predictable names, no symlink-planting surface,
# and everything dev-stack-related stays in one directory.
LOG_DIR="$KIROCREW_HOME/logs"
mkdir -p "$LOG_DIR"
BACKEND_LOG="$LOG_DIR/gateway.log"
VITE_LOG="$LOG_DIR/vite.log"
# Teardown: terminate exactly the processes we started, by PID, including
# their descendants (npm spawns node; dev-backend execs python). The previous
# `kill -- -$$` assumed this script is its own process-group leader — true
# from an interactive shell, NOT when invoked from another script or an IDE —
# and silently leaked the stack when that assumption failed. macOS has no
# setsid to force group leadership, so explicit PID-tree kills it is.
PIDS=()
kill_tree() {
local child
for child in $(pgrep -P "$1" 2>/dev/null); do
kill_tree "$child"
done
kill -TERM "$1" 2>/dev/null || true
}
cleanup() {
trap - INT TERM EXIT
local pid
for pid in "${PIDS[@]}"; do
kill_tree "$pid"
done
wait 2>/dev/null || true
}
trap cleanup INT TERM EXIT
echo "[dev] starting backend (port $GATEWAY_PORT, home .kirocrew-dev) -> $BACKEND_LOG"
# --no-open: the gateway would otherwise auto-open its OWN url (:6777) — the
# bundled-snapshot surface. The live surface is the Vite url printed below.
#
# The gateway trusts no browser origin but its own until told to, so this
# script opts in the one port it starts Vite on. Trusting a port lets every
# page served there act as you in the dashboard, so the port must be free
# now, and Vite runs with --strictPort below so it fails instead of moving to
# 3001+ and leaving the trusted :3000 to whatever takes it next.
VITE_DEV_PORT=3000
for vite_host in 127.0.0.1 '[::1]'; do
# Only a refused connection (curl exit 7) means the port is free. A listener
# that accepts and then answers slowly, never answers, or closes at once is
# still a listener, so every other outcome counts as taken.
# The exit code is captured with `||` so `set -e` does not end the script
# on the refused connection that is the normal, free-port case, and no
# proxy is consulted, so only a real loopback listener can answer.
probe_rc=0
curl -gsS --noproxy '*' -o /dev/null --connect-timeout 2 --max-time 2 "http://$vite_host:$VITE_DEV_PORT/" 2>/dev/null || probe_rc=$?
if [ "$probe_rc" -ne 7 ]; then
echo "[dev] ERROR: something is already listening on :$VITE_DEV_PORT."
echo "[dev] Stop it first: the dev gateway trusts that port as a browser origin."
exit 1
fi
done
# Added to any ports the caller already opted in (an SSH tunnel, say).
export KIROCREW_ALLOWED_LOOPBACK_PORTS="${KIROCREW_ALLOWED_LOOPBACK_PORTS:+$KIROCREW_ALLOWED_LOOPBACK_PORTS,}$VITE_DEV_PORT"
./dev-backend.sh --no-open > "$BACKEND_LOG" 2>&1 &
PIDS+=($!)
# Wait for the gateway to accept connections (up to 90s: first boot loads MCP servers).
for i in $(seq 1 90); do
if curl -fsS -o /dev/null "http://127.0.0.1:$GATEWAY_PORT/" 2>/dev/null; then
break
fi
if [ "$i" -eq 90 ]; then
echo "[dev] ERROR: gateway did not come up on :$GATEWAY_PORT — see $BACKEND_LOG"
exit 1
fi
sleep 1
done
echo "[dev] backend up on :$GATEWAY_PORT"
echo "[dev] starting Vite -> $VITE_LOG"
( cd website && KIROCREW_PORT="$GATEWAY_PORT" exec npm run dev -- --port "$VITE_DEV_PORT" --strictPort > "$VITE_LOG" 2>&1 ) &
PIDS+=($!)
# --strictPort pins Vite to VITE_DEV_PORT; read it back from the banner anyway.
# If Vite exits (its strict bind failed), stop at once: the gateway trusting
# the port must not outlive the Vite that was meant to hold it.
VITE_PID=$!
VITE_PORT=""
for i in $(seq 1 30); do
VITE_PORT="$(grep -oE 'Local:.*localhost:[0-9]+' "$VITE_LOG" 2>/dev/null | grep -oE '[0-9]+$' | head -1)"
[ -n "$VITE_PORT" ] && break
if ! kill -0 "$VITE_PID" 2>/dev/null; then
echo "[dev] ERROR: Vite exited before it reported a port; see $VITE_LOG"
exit 1
fi
sleep 1
done
if [ -z "$VITE_PORT" ]; then
echo "[dev] ERROR: Vite did not report a port — see $VITE_LOG"
exit 1
fi
echo "[dev] Vite up on :$VITE_PORT"
# Mint a dashboard token against the DEV home and rewrite the URL to the Vite
# port (the Vite proxy forwards the token to the backend and sets the cookie).
# `kirocrew token` can print a SECOND, externally-advertised URL when
# dashboard.url is configured — always take the localhost one.
TOKEN_URL="$(PYTHONPATH="$SCRIPT_DIR/src" \
"$RUNTIME_PYTHON" -m kiro_crew token --port "$GATEWAY_PORT" 2>/dev/null \
| grep -oE 'http://localhost:[0-9]+[^ ]*' | head -1)"
if [ -n "$TOKEN_URL" ]; then
OPEN_URL="$(printf '%s' "$TOKEN_URL" | sed "s/:$GATEWAY_PORT/:$VITE_PORT/")"
echo ""
echo "[dev] ============================================================"
echo "[dev] open: $OPEN_URL"
echo "[dev] ============================================================"
echo ""
# Open the LIVE (Vite) surface in the browser — macOS `open`, Linux xdg-open.
# Best-effort: on a headless box the launcher exits nonzero, and under
# set -e that would tear down the whole stack we just brought up.
if command -v open >/dev/null 2>&1; then open "$OPEN_URL" || true
elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$OPEN_URL" || true
fi
else
echo "[dev] WARNING: could not mint a token automatically. Run:"
echo "[dev] KIROCREW_HOME=$KIROCREW_HOME PYTHONPATH=src .venv/bin/python -m kiro_crew token --port $GATEWAY_PORT"
echo "[dev] then open the URL with :$GATEWAY_PORT swapped to :$VITE_PORT"
fi
echo "[dev] streaming logs (Ctrl+C stops everything)"
tail -f "$BACKEND_LOG" "$VITE_LOG"