@leocm/flowcraft 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -2
- package/bin/cli.js +108 -108
- package/flowcraft.yaml +30 -30
- package/install.sh +24 -24
- package/lib/args.js +38 -38
- package/lib/config.js +20 -20
- package/lib/copy.js +131 -131
- package/lib/fix.js +110 -110
- package/lib/fsutil.js +104 -104
- package/lib/global.js +229 -229
- package/lib/init.js +43 -43
- package/lib/runtime.js +84 -84
- package/lib/version.js +55 -55
- package/lib/wizard.js +125 -125
- package/package.json +33 -33
- package/presets/backend.yaml +20 -20
- package/presets/default.yaml +15 -15
- package/presets/frontend.yaml +20 -20
- package/presets/quick-fix.yaml +15 -15
- package/setup/.claude/hooks/check-phase-test.py +152 -152
- package/setup/.claude/hooks/check-phase-write.py +235 -235
- package/setup/.claude/hooks/dispatch.py +67 -67
- package/setup/.claude/hooks/file-suggestion.py +45 -45
- package/setup/.claude/hooks/flowcraft_env.py +162 -137
- package/setup/.claude/hooks/permission-request.py +132 -125
- package/setup/.claude/hooks/post-tool-failure.py +36 -36
- package/setup/.claude/hooks/pre-compact.py +66 -66
- package/setup/.claude/hooks/review-runner.py +846 -846
- package/setup/.claude/hooks/setup-keys.py +282 -282
- package/setup/.claude/hooks/statusline.py +63 -63
- package/setup/.claude/hooks/stop-check.py +186 -171
- package/setup/.claude/hooks/subagent-start.py +50 -50
- package/setup/.claude/hooks/subagent-stop.py +63 -63
- package/setup/.claude/hooks/task-completed.py +57 -57
- package/setup/.claude/hooks/task_manager.py +592 -592
- package/setup/.claude/hooks/user-prompt-submit.py +91 -85
- package/setup/.claude/settings.json +233 -237
- package/setup/.claude/skills/Pstatus/SKILL.md +68 -68
- package/setup/.claude/skills/archive/SKILL.md +195 -195
- package/setup/.claude/skills/brainstorming/SKILL.md +5 -5
- package/setup/.claude/skills/brainstorming/scripts/server.cjs +723 -723
- package/setup/.claude/skills/brainstorming/scripts/start-server.py +245 -245
- package/setup/.claude/skills/brainstorming/scripts/stop-server.py +178 -178
- package/setup/.claude/skills/brainstorming/visual-companion.md +298 -298
- package/setup/.claude/skills/chrome-integration/SKILL.md +66 -66
- package/setup/.claude/skills/code-review/SKILL.md +264 -264
- package/setup/.claude/skills/executing-plans/SKILL.md +1 -1
- package/setup/.claude/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
- package/setup/.claude/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
- package/setup/.claude/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
- package/setup/.claude/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
- package/setup/.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
- package/setup/.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
- package/setup/.claude/skills/methodology/SKILL.md +124 -0
- package/setup/.claude/skills/phase/SKILL.md +62 -62
- package/setup/.claude/skills/playwright/SKILL.md +85 -85
- package/setup/.claude/skills/review/SKILL.md +97 -97
- package/setup/.claude/skills/task/SKILL.md +26 -26
- package/setup/.claude/skills/test-driven-development/SKILL.md +1 -1
- package/setup/.claude/skills/ui-ux-pro-max/SKILL.md +1 -1
- package/setup/.claude/skills/verification-before-completion/SKILL.md +1 -1
- package/setup/.claude/skills/writing-plans/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/archive/SKILL.md +195 -195
- package/setup/.codex/.agents/skills/brainstorming/SKILL.md +5 -5
- package/setup/.codex/.agents/skills/brainstorming/scripts/server.cjs +723 -723
- package/setup/.codex/.agents/skills/brainstorming/scripts/start-server.py +245 -245
- package/setup/.codex/.agents/skills/brainstorming/scripts/stop-server.py +178 -178
- package/setup/.codex/.agents/skills/brainstorming/visual-companion.md +298 -298
- package/setup/.codex/.agents/skills/chrome-integration/SKILL.md +66 -66
- package/setup/.codex/.agents/skills/code-review/SKILL.md +264 -264
- package/setup/.codex/.agents/skills/executing-plans/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-cli/SKILL.md +67 -67
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-debugging/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-exploring/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-guide/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/gitnexus/gitnexus-refactoring/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/methodology/SKILL.md +124 -0
- package/setup/.codex/.agents/skills/phase/SKILL.md +62 -62
- package/setup/.codex/.agents/skills/playwright/SKILL.md +85 -85
- package/setup/.codex/.agents/skills/review/SKILL.md +97 -97
- package/setup/.codex/.agents/skills/task/SKILL.md +27 -27
- package/setup/.codex/.agents/skills/test-driven-development/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/ui-ux-pro-max/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/verification-before-completion/SKILL.md +1 -1
- package/setup/.codex/.agents/skills/writing-plans/SKILL.md +1 -1
- package/setup/.codex/hooks/review-runner.py +851 -851
- package/setup/.codex/hooks/setup-keys.py +287 -287
- package/setup/AGENTS.md +11 -1
- package/setup/CLAUDE.md +11 -1
- package/setup/install.sh +24 -24
- package/setup/tests/test_decision_timeout.py +241 -241
- package/setup/tests/test_hook_guards.py +357 -357
- package/setup/tests/test_sdlc_bootstrap.py +167 -167
- package/setup/tests/test_sessionlog_switch.py +199 -0
- package/setup/tests/test_task_manager.py +148 -148
|
@@ -1,245 +1,245 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""Start the brainstorm server and output connection info.
|
|
3
|
-
|
|
4
|
-
Usage: start-server.py [--project-dir <path>] [--host <bind-host>] [--url-host <display-host>] [--foreground] [--background]
|
|
5
|
-
|
|
6
|
-
Starts server on a random high port, outputs JSON with URL.
|
|
7
|
-
Each session gets its own directory to avoid conflicts.
|
|
8
|
-
|
|
9
|
-
Options:
|
|
10
|
-
--project-dir <path> Store session files under <path>/.superpowers/brainstorm/
|
|
11
|
-
instead of the temp dir. Files persist after the server stops.
|
|
12
|
-
--host <bind-host> Host/interface to bind (default: 127.0.0.1).
|
|
13
|
-
Use 0.0.0.0 in remote/containerized environments.
|
|
14
|
-
--url-host <host> Hostname shown in returned URL JSON.
|
|
15
|
-
--idle-timeout-minutes <n> Shut down after n minutes idle (default 240 = 4h).
|
|
16
|
-
--open Auto-open the browser on the first screen (use only
|
|
17
|
-
after the user approves the visual companion).
|
|
18
|
-
--foreground Run server in the current terminal (no backgrounding).
|
|
19
|
-
--background Force background mode.
|
|
20
|
-
"""
|
|
21
|
-
import os
|
|
22
|
-
import random
|
|
23
|
-
import re
|
|
24
|
-
import shutil
|
|
25
|
-
import signal
|
|
26
|
-
import subprocess
|
|
27
|
-
import sys
|
|
28
|
-
import tempfile
|
|
29
|
-
import time
|
|
30
|
-
from pathlib import Path
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def is_windows() -> bool:
|
|
34
|
-
return os.name == "nt"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def server_id_value() -> str:
|
|
38
|
-
"""Generate a 32-64 char [A-Za-z0-9_-] server instance id."""
|
|
39
|
-
sid = os.urandom(24).hex() # 48 hex chars
|
|
40
|
-
if re.fullmatch(r"[A-Za-z0-9_-]{32,64}", sid):
|
|
41
|
-
return sid
|
|
42
|
-
return f"{os.getpid():08x}{int(time.time()):08x}{random.getrandbits(32):08x}{random.getrandbits(32):08x}"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def resolve_owner_pid() -> str:
|
|
46
|
-
"""Resolve the harness PID (grandparent of this script)."""
|
|
47
|
-
if is_windows():
|
|
48
|
-
return ""
|
|
49
|
-
try:
|
|
50
|
-
# grandparent of this process
|
|
51
|
-
r = subprocess.run(
|
|
52
|
-
["ps", "-o", "ppid=", "-p", str(os.getppid())],
|
|
53
|
-
capture_output=True, text=True, timeout=5,
|
|
54
|
-
)
|
|
55
|
-
pp = r.stdout.strip()
|
|
56
|
-
if pp and pp != "1":
|
|
57
|
-
return pp
|
|
58
|
-
except Exception:
|
|
59
|
-
pass
|
|
60
|
-
return str(os.getppid())
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def main(argv=None) -> int:
|
|
64
|
-
argv = sys.argv[1:] if argv is None else argv
|
|
65
|
-
project_dir = ""
|
|
66
|
-
foreground = False
|
|
67
|
-
force_background = False
|
|
68
|
-
bind_host = "127.0.0.1"
|
|
69
|
-
url_host = ""
|
|
70
|
-
idle_timeout_minutes = ""
|
|
71
|
-
do_open = False
|
|
72
|
-
|
|
73
|
-
i = 0
|
|
74
|
-
while i < len(argv):
|
|
75
|
-
a = argv[i]
|
|
76
|
-
if a == "--project-dir" and i + 1 < len(argv):
|
|
77
|
-
project_dir = argv[i + 1]; i += 2
|
|
78
|
-
elif a == "--host" and i + 1 < len(argv):
|
|
79
|
-
bind_host = argv[i + 1]; i += 2
|
|
80
|
-
elif a == "--url-host" and i + 1 < len(argv):
|
|
81
|
-
url_host = argv[i + 1]; i += 2
|
|
82
|
-
elif a == "--idle-timeout-minutes" and i + 1 < len(argv):
|
|
83
|
-
idle_timeout_minutes = argv[i + 1]; i += 2
|
|
84
|
-
elif a == "--open":
|
|
85
|
-
do_open = True; i += 1
|
|
86
|
-
elif a in ("--foreground", "--no-daemon"):
|
|
87
|
-
foreground = True; i += 1
|
|
88
|
-
elif a in ("--background", "--daemon"):
|
|
89
|
-
force_background = True; i += 1
|
|
90
|
-
else:
|
|
91
|
-
print(json_dumps({"error": f"Unknown argument: {a}"}))
|
|
92
|
-
return 1
|
|
93
|
-
|
|
94
|
-
if not url_host:
|
|
95
|
-
url_host = "localhost" if bind_host in ("127.0.0.1", "localhost") else bind_host
|
|
96
|
-
|
|
97
|
-
if idle_timeout_minutes:
|
|
98
|
-
if not idle_timeout_minutes.isdigit() or int(idle_timeout_minutes) < 1:
|
|
99
|
-
print(json_dumps({"error": "--idle-timeout-minutes must be a positive integer"}))
|
|
100
|
-
return 1
|
|
101
|
-
idle_timeout_ms = int(idle_timeout_minutes) * 60 * 1000
|
|
102
|
-
else:
|
|
103
|
-
idle_timeout_ms = None
|
|
104
|
-
|
|
105
|
-
# Some environments reap detached/background processes: auto-foreground.
|
|
106
|
-
if os.environ.get("CODEX_CI") and not foreground and not force_background:
|
|
107
|
-
foreground = True
|
|
108
|
-
if is_windows() and not foreground and not force_background:
|
|
109
|
-
foreground = True
|
|
110
|
-
|
|
111
|
-
# Generate a unique session directory
|
|
112
|
-
session_id = f"{os.getpid()}-{int(time.time())}"
|
|
113
|
-
if project_dir:
|
|
114
|
-
session_dir = Path(project_dir) / ".superpowers" / "brainstorm" / session_id
|
|
115
|
-
# Persist the bound port and key per project so a restart reuses them.
|
|
116
|
-
port_file = Path(project_dir) / ".superpowers" / "brainstorm" / ".last-port"
|
|
117
|
-
token_file = Path(project_dir) / ".superpowers" / "brainstorm" / ".last-token"
|
|
118
|
-
else:
|
|
119
|
-
session_dir = Path(tempfile.gettempdir()) / f"brainstorm-{session_id}"
|
|
120
|
-
port_file = token_file = None
|
|
121
|
-
|
|
122
|
-
state_dir = session_dir / "state"
|
|
123
|
-
pid_file = state_dir / "server.pid"
|
|
124
|
-
log_file = state_dir / "server.log"
|
|
125
|
-
server_id_file = state_dir / "server-instance-id"
|
|
126
|
-
|
|
127
|
-
# Create fresh session directory with content and state peers (owner-only)
|
|
128
|
-
for d in (session_dir / "content", state_dir):
|
|
129
|
-
d.mkdir(parents=True, exist_ok=True)
|
|
130
|
-
try:
|
|
131
|
-
os.chmod(session_dir, 0o700)
|
|
132
|
-
os.chmod(state_dir, 0o700)
|
|
133
|
-
except Exception:
|
|
134
|
-
pass
|
|
135
|
-
|
|
136
|
-
server_id = server_id_value()
|
|
137
|
-
server_id_file.write_text(server_id + "\n", encoding="utf-8")
|
|
138
|
-
try:
|
|
139
|
-
os.chmod(server_id_file, 0o600)
|
|
140
|
-
except Exception:
|
|
141
|
-
pass
|
|
142
|
-
|
|
143
|
-
# Kill any existing server
|
|
144
|
-
if pid_file.is_file():
|
|
145
|
-
try:
|
|
146
|
-
old_pid = int(pid_file.read_text(encoding="utf-8").strip() or "0")
|
|
147
|
-
except Exception:
|
|
148
|
-
old_pid = 0
|
|
149
|
-
if old_pid > 0:
|
|
150
|
-
try:
|
|
151
|
-
os.kill(old_pid, signal.SIGTERM)
|
|
152
|
-
except Exception:
|
|
153
|
-
pass
|
|
154
|
-
try:
|
|
155
|
-
pid_file.unlink()
|
|
156
|
-
except OSError:
|
|
157
|
-
pass
|
|
158
|
-
|
|
159
|
-
script_dir = Path(__file__).resolve().parent
|
|
160
|
-
owner_pid = resolve_owner_pid()
|
|
161
|
-
|
|
162
|
-
# Build the node invocation
|
|
163
|
-
node = shutil.which("node") or "node"
|
|
164
|
-
env = dict(os.environ)
|
|
165
|
-
env["BRAINSTORM_DIR"] = str(session_dir)
|
|
166
|
-
env["BRAINSTORM_HOST"] = bind_host
|
|
167
|
-
env["BRAINSTORM_URL_HOST"] = url_host
|
|
168
|
-
env["BRAINSTORM_OWNER_PID"] = owner_pid or ""
|
|
169
|
-
if port_file is not None:
|
|
170
|
-
env["BRAINSTORM_PORT_FILE"] = str(port_file)
|
|
171
|
-
env["BRAINSTORM_TOKEN_FILE"] = str(token_file)
|
|
172
|
-
if idle_timeout_ms is not None:
|
|
173
|
-
env["BRAINSTORM_IDLE_TIMEOUT_MS"] = str(idle_timeout_ms)
|
|
174
|
-
if do_open:
|
|
175
|
-
env["BRAINSTORM_OPEN"] = "1"
|
|
176
|
-
cmd = [node, "server.cjs", f"--brainstorm-server-id={server_id}"]
|
|
177
|
-
|
|
178
|
-
if foreground:
|
|
179
|
-
proc = subprocess.Popen(cmd, cwd=str(script_dir), env=env)
|
|
180
|
-
pid_file.write_text(str(proc.pid) + "\n", encoding="utf-8")
|
|
181
|
-
return proc.wait()
|
|
182
|
-
|
|
183
|
-
# Background mode: capture output to the log file
|
|
184
|
-
try:
|
|
185
|
-
lf = open(log_file, "wb")
|
|
186
|
-
except Exception as e:
|
|
187
|
-
print(json_dumps({"error": f"Cannot open log file: {e}"}))
|
|
188
|
-
return 1
|
|
189
|
-
try:
|
|
190
|
-
proc = subprocess.Popen(cmd, cwd=str(script_dir), env=env, stdout=lf, stderr=subprocess.STDOUT)
|
|
191
|
-
except Exception as e:
|
|
192
|
-
lf.close()
|
|
193
|
-
print(json_dumps({"error": f"Failed to start server: {e}"}))
|
|
194
|
-
return 1
|
|
195
|
-
pid_file.write_text(str(proc.pid) + "\n", encoding="utf-8")
|
|
196
|
-
|
|
197
|
-
# Wait for the server-started message (check the log file)
|
|
198
|
-
for _ in range(50):
|
|
199
|
-
if log_file.is_file():
|
|
200
|
-
try:
|
|
201
|
-
log_text = log_file.read_text(encoding="utf-8", errors="replace")
|
|
202
|
-
except Exception:
|
|
203
|
-
log_text = ""
|
|
204
|
-
if "server-started" in log_text:
|
|
205
|
-
# Verify the server is still alive after a short window
|
|
206
|
-
alive = True
|
|
207
|
-
for _ in range(20):
|
|
208
|
-
if not is_alive(proc.pid):
|
|
209
|
-
alive = False
|
|
210
|
-
break
|
|
211
|
-
time.sleep(0.1)
|
|
212
|
-
if not alive:
|
|
213
|
-
retry = f"{script_dir}/start-server.py"
|
|
214
|
-
if project_dir:
|
|
215
|
-
retry += f" --project-dir {project_dir}"
|
|
216
|
-
retry += f" --host {bind_host} --url-host {url_host} --foreground"
|
|
217
|
-
print(json_dumps({"error": f"Server started but was killed. Retry in a persistent terminal with: {retry}"}))
|
|
218
|
-
return 1
|
|
219
|
-
for line in log_text.splitlines():
|
|
220
|
-
if "server-started" in line:
|
|
221
|
-
print(line)
|
|
222
|
-
break
|
|
223
|
-
return 0
|
|
224
|
-
time.sleep(0.1)
|
|
225
|
-
|
|
226
|
-
# Timeout - server didn't start
|
|
227
|
-
print(json_dumps({"error": "Server failed to start within 5 seconds"}))
|
|
228
|
-
return 1
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
def is_alive(pid: int) -> bool:
|
|
232
|
-
try:
|
|
233
|
-
os.kill(pid, 0)
|
|
234
|
-
return True
|
|
235
|
-
except OSError:
|
|
236
|
-
return False
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
def json_dumps(obj) -> str:
|
|
240
|
-
import json
|
|
241
|
-
return json.dumps(obj, ensure_ascii=False)
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if __name__ == "__main__":
|
|
245
|
-
sys.exit(main())
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Start the brainstorm server and output connection info.
|
|
3
|
+
|
|
4
|
+
Usage: start-server.py [--project-dir <path>] [--host <bind-host>] [--url-host <display-host>] [--foreground] [--background]
|
|
5
|
+
|
|
6
|
+
Starts server on a random high port, outputs JSON with URL.
|
|
7
|
+
Each session gets its own directory to avoid conflicts.
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
--project-dir <path> Store session files under <path>/.superpowers/brainstorm/
|
|
11
|
+
instead of the temp dir. Files persist after the server stops.
|
|
12
|
+
--host <bind-host> Host/interface to bind (default: 127.0.0.1).
|
|
13
|
+
Use 0.0.0.0 in remote/containerized environments.
|
|
14
|
+
--url-host <host> Hostname shown in returned URL JSON.
|
|
15
|
+
--idle-timeout-minutes <n> Shut down after n minutes idle (default 240 = 4h).
|
|
16
|
+
--open Auto-open the browser on the first screen (use only
|
|
17
|
+
after the user approves the visual companion).
|
|
18
|
+
--foreground Run server in the current terminal (no backgrounding).
|
|
19
|
+
--background Force background mode.
|
|
20
|
+
"""
|
|
21
|
+
import os
|
|
22
|
+
import random
|
|
23
|
+
import re
|
|
24
|
+
import shutil
|
|
25
|
+
import signal
|
|
26
|
+
import subprocess
|
|
27
|
+
import sys
|
|
28
|
+
import tempfile
|
|
29
|
+
import time
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def is_windows() -> bool:
|
|
34
|
+
return os.name == "nt"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def server_id_value() -> str:
|
|
38
|
+
"""Generate a 32-64 char [A-Za-z0-9_-] server instance id."""
|
|
39
|
+
sid = os.urandom(24).hex() # 48 hex chars
|
|
40
|
+
if re.fullmatch(r"[A-Za-z0-9_-]{32,64}", sid):
|
|
41
|
+
return sid
|
|
42
|
+
return f"{os.getpid():08x}{int(time.time()):08x}{random.getrandbits(32):08x}{random.getrandbits(32):08x}"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def resolve_owner_pid() -> str:
|
|
46
|
+
"""Resolve the harness PID (grandparent of this script)."""
|
|
47
|
+
if is_windows():
|
|
48
|
+
return ""
|
|
49
|
+
try:
|
|
50
|
+
# grandparent of this process
|
|
51
|
+
r = subprocess.run(
|
|
52
|
+
["ps", "-o", "ppid=", "-p", str(os.getppid())],
|
|
53
|
+
capture_output=True, text=True, timeout=5,
|
|
54
|
+
)
|
|
55
|
+
pp = r.stdout.strip()
|
|
56
|
+
if pp and pp != "1":
|
|
57
|
+
return pp
|
|
58
|
+
except Exception:
|
|
59
|
+
pass
|
|
60
|
+
return str(os.getppid())
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def main(argv=None) -> int:
|
|
64
|
+
argv = sys.argv[1:] if argv is None else argv
|
|
65
|
+
project_dir = ""
|
|
66
|
+
foreground = False
|
|
67
|
+
force_background = False
|
|
68
|
+
bind_host = "127.0.0.1"
|
|
69
|
+
url_host = ""
|
|
70
|
+
idle_timeout_minutes = ""
|
|
71
|
+
do_open = False
|
|
72
|
+
|
|
73
|
+
i = 0
|
|
74
|
+
while i < len(argv):
|
|
75
|
+
a = argv[i]
|
|
76
|
+
if a == "--project-dir" and i + 1 < len(argv):
|
|
77
|
+
project_dir = argv[i + 1]; i += 2
|
|
78
|
+
elif a == "--host" and i + 1 < len(argv):
|
|
79
|
+
bind_host = argv[i + 1]; i += 2
|
|
80
|
+
elif a == "--url-host" and i + 1 < len(argv):
|
|
81
|
+
url_host = argv[i + 1]; i += 2
|
|
82
|
+
elif a == "--idle-timeout-minutes" and i + 1 < len(argv):
|
|
83
|
+
idle_timeout_minutes = argv[i + 1]; i += 2
|
|
84
|
+
elif a == "--open":
|
|
85
|
+
do_open = True; i += 1
|
|
86
|
+
elif a in ("--foreground", "--no-daemon"):
|
|
87
|
+
foreground = True; i += 1
|
|
88
|
+
elif a in ("--background", "--daemon"):
|
|
89
|
+
force_background = True; i += 1
|
|
90
|
+
else:
|
|
91
|
+
print(json_dumps({"error": f"Unknown argument: {a}"}))
|
|
92
|
+
return 1
|
|
93
|
+
|
|
94
|
+
if not url_host:
|
|
95
|
+
url_host = "localhost" if bind_host in ("127.0.0.1", "localhost") else bind_host
|
|
96
|
+
|
|
97
|
+
if idle_timeout_minutes:
|
|
98
|
+
if not idle_timeout_minutes.isdigit() or int(idle_timeout_minutes) < 1:
|
|
99
|
+
print(json_dumps({"error": "--idle-timeout-minutes must be a positive integer"}))
|
|
100
|
+
return 1
|
|
101
|
+
idle_timeout_ms = int(idle_timeout_minutes) * 60 * 1000
|
|
102
|
+
else:
|
|
103
|
+
idle_timeout_ms = None
|
|
104
|
+
|
|
105
|
+
# Some environments reap detached/background processes: auto-foreground.
|
|
106
|
+
if os.environ.get("CODEX_CI") and not foreground and not force_background:
|
|
107
|
+
foreground = True
|
|
108
|
+
if is_windows() and not foreground and not force_background:
|
|
109
|
+
foreground = True
|
|
110
|
+
|
|
111
|
+
# Generate a unique session directory
|
|
112
|
+
session_id = f"{os.getpid()}-{int(time.time())}"
|
|
113
|
+
if project_dir:
|
|
114
|
+
session_dir = Path(project_dir) / ".superpowers" / "brainstorm" / session_id
|
|
115
|
+
# Persist the bound port and key per project so a restart reuses them.
|
|
116
|
+
port_file = Path(project_dir) / ".superpowers" / "brainstorm" / ".last-port"
|
|
117
|
+
token_file = Path(project_dir) / ".superpowers" / "brainstorm" / ".last-token"
|
|
118
|
+
else:
|
|
119
|
+
session_dir = Path(tempfile.gettempdir()) / f"brainstorm-{session_id}"
|
|
120
|
+
port_file = token_file = None
|
|
121
|
+
|
|
122
|
+
state_dir = session_dir / "state"
|
|
123
|
+
pid_file = state_dir / "server.pid"
|
|
124
|
+
log_file = state_dir / "server.log"
|
|
125
|
+
server_id_file = state_dir / "server-instance-id"
|
|
126
|
+
|
|
127
|
+
# Create fresh session directory with content and state peers (owner-only)
|
|
128
|
+
for d in (session_dir / "content", state_dir):
|
|
129
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
130
|
+
try:
|
|
131
|
+
os.chmod(session_dir, 0o700)
|
|
132
|
+
os.chmod(state_dir, 0o700)
|
|
133
|
+
except Exception:
|
|
134
|
+
pass
|
|
135
|
+
|
|
136
|
+
server_id = server_id_value()
|
|
137
|
+
server_id_file.write_text(server_id + "\n", encoding="utf-8")
|
|
138
|
+
try:
|
|
139
|
+
os.chmod(server_id_file, 0o600)
|
|
140
|
+
except Exception:
|
|
141
|
+
pass
|
|
142
|
+
|
|
143
|
+
# Kill any existing server
|
|
144
|
+
if pid_file.is_file():
|
|
145
|
+
try:
|
|
146
|
+
old_pid = int(pid_file.read_text(encoding="utf-8").strip() or "0")
|
|
147
|
+
except Exception:
|
|
148
|
+
old_pid = 0
|
|
149
|
+
if old_pid > 0:
|
|
150
|
+
try:
|
|
151
|
+
os.kill(old_pid, signal.SIGTERM)
|
|
152
|
+
except Exception:
|
|
153
|
+
pass
|
|
154
|
+
try:
|
|
155
|
+
pid_file.unlink()
|
|
156
|
+
except OSError:
|
|
157
|
+
pass
|
|
158
|
+
|
|
159
|
+
script_dir = Path(__file__).resolve().parent
|
|
160
|
+
owner_pid = resolve_owner_pid()
|
|
161
|
+
|
|
162
|
+
# Build the node invocation
|
|
163
|
+
node = shutil.which("node") or "node"
|
|
164
|
+
env = dict(os.environ)
|
|
165
|
+
env["BRAINSTORM_DIR"] = str(session_dir)
|
|
166
|
+
env["BRAINSTORM_HOST"] = bind_host
|
|
167
|
+
env["BRAINSTORM_URL_HOST"] = url_host
|
|
168
|
+
env["BRAINSTORM_OWNER_PID"] = owner_pid or ""
|
|
169
|
+
if port_file is not None:
|
|
170
|
+
env["BRAINSTORM_PORT_FILE"] = str(port_file)
|
|
171
|
+
env["BRAINSTORM_TOKEN_FILE"] = str(token_file)
|
|
172
|
+
if idle_timeout_ms is not None:
|
|
173
|
+
env["BRAINSTORM_IDLE_TIMEOUT_MS"] = str(idle_timeout_ms)
|
|
174
|
+
if do_open:
|
|
175
|
+
env["BRAINSTORM_OPEN"] = "1"
|
|
176
|
+
cmd = [node, "server.cjs", f"--brainstorm-server-id={server_id}"]
|
|
177
|
+
|
|
178
|
+
if foreground:
|
|
179
|
+
proc = subprocess.Popen(cmd, cwd=str(script_dir), env=env)
|
|
180
|
+
pid_file.write_text(str(proc.pid) + "\n", encoding="utf-8")
|
|
181
|
+
return proc.wait()
|
|
182
|
+
|
|
183
|
+
# Background mode: capture output to the log file
|
|
184
|
+
try:
|
|
185
|
+
lf = open(log_file, "wb")
|
|
186
|
+
except Exception as e:
|
|
187
|
+
print(json_dumps({"error": f"Cannot open log file: {e}"}))
|
|
188
|
+
return 1
|
|
189
|
+
try:
|
|
190
|
+
proc = subprocess.Popen(cmd, cwd=str(script_dir), env=env, stdout=lf, stderr=subprocess.STDOUT)
|
|
191
|
+
except Exception as e:
|
|
192
|
+
lf.close()
|
|
193
|
+
print(json_dumps({"error": f"Failed to start server: {e}"}))
|
|
194
|
+
return 1
|
|
195
|
+
pid_file.write_text(str(proc.pid) + "\n", encoding="utf-8")
|
|
196
|
+
|
|
197
|
+
# Wait for the server-started message (check the log file)
|
|
198
|
+
for _ in range(50):
|
|
199
|
+
if log_file.is_file():
|
|
200
|
+
try:
|
|
201
|
+
log_text = log_file.read_text(encoding="utf-8", errors="replace")
|
|
202
|
+
except Exception:
|
|
203
|
+
log_text = ""
|
|
204
|
+
if "server-started" in log_text:
|
|
205
|
+
# Verify the server is still alive after a short window
|
|
206
|
+
alive = True
|
|
207
|
+
for _ in range(20):
|
|
208
|
+
if not is_alive(proc.pid):
|
|
209
|
+
alive = False
|
|
210
|
+
break
|
|
211
|
+
time.sleep(0.1)
|
|
212
|
+
if not alive:
|
|
213
|
+
retry = f"{script_dir}/start-server.py"
|
|
214
|
+
if project_dir:
|
|
215
|
+
retry += f" --project-dir {project_dir}"
|
|
216
|
+
retry += f" --host {bind_host} --url-host {url_host} --foreground"
|
|
217
|
+
print(json_dumps({"error": f"Server started but was killed. Retry in a persistent terminal with: {retry}"}))
|
|
218
|
+
return 1
|
|
219
|
+
for line in log_text.splitlines():
|
|
220
|
+
if "server-started" in line:
|
|
221
|
+
print(line)
|
|
222
|
+
break
|
|
223
|
+
return 0
|
|
224
|
+
time.sleep(0.1)
|
|
225
|
+
|
|
226
|
+
# Timeout - server didn't start
|
|
227
|
+
print(json_dumps({"error": "Server failed to start within 5 seconds"}))
|
|
228
|
+
return 1
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def is_alive(pid: int) -> bool:
|
|
232
|
+
try:
|
|
233
|
+
os.kill(pid, 0)
|
|
234
|
+
return True
|
|
235
|
+
except OSError:
|
|
236
|
+
return False
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def json_dumps(obj) -> str:
|
|
240
|
+
import json
|
|
241
|
+
return json.dumps(obj, ensure_ascii=False)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
if __name__ == "__main__":
|
|
245
|
+
sys.exit(main())
|