@agentuity/cli 2.0.11 → 2.0.13

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.
Files changed (274) hide show
  1. package/dist/cache/resource-region.d.ts.map +1 -1
  2. package/dist/cache/resource-region.js +48 -25
  3. package/dist/cache/resource-region.js.map +1 -1
  4. package/dist/cli.d.ts.map +1 -1
  5. package/dist/cli.js +15 -8
  6. package/dist/cli.js.map +1 -1
  7. package/dist/cmd/build/vite/bun-dev-server.d.ts +20 -0
  8. package/dist/cmd/build/vite/bun-dev-server.d.ts.map +1 -1
  9. package/dist/cmd/build/vite/bun-dev-server.js +62 -4
  10. package/dist/cmd/build/vite/bun-dev-server.js.map +1 -1
  11. package/dist/cmd/build/vite/index.d.ts +0 -1
  12. package/dist/cmd/build/vite/index.d.ts.map +1 -1
  13. package/dist/cmd/build/vite/index.js +0 -1
  14. package/dist/cmd/build/vite/index.js.map +1 -1
  15. package/dist/cmd/build/vite/static-renderer.d.ts +17 -0
  16. package/dist/cmd/build/vite/static-renderer.d.ts.map +1 -1
  17. package/dist/cmd/build/vite/static-renderer.js +18 -6
  18. package/dist/cmd/build/vite/static-renderer.js.map +1 -1
  19. package/dist/cmd/build/vite/vite-asset-server-config.d.ts.map +1 -1
  20. package/dist/cmd/build/vite/vite-asset-server-config.js +34 -27
  21. package/dist/cmd/build/vite/vite-asset-server-config.js.map +1 -1
  22. package/dist/cmd/build/vite/vite-asset-server.d.ts +9 -0
  23. package/dist/cmd/build/vite/vite-asset-server.d.ts.map +1 -1
  24. package/dist/cmd/build/vite/vite-asset-server.js +5 -1
  25. package/dist/cmd/build/vite/vite-asset-server.js.map +1 -1
  26. package/dist/cmd/build/vite/vite-builder.d.ts.map +1 -1
  27. package/dist/cmd/build/vite/vite-builder.js +12 -1
  28. package/dist/cmd/build/vite/vite-builder.js.map +1 -1
  29. package/dist/cmd/build/vite/ws-proxy.d.ts +15 -1
  30. package/dist/cmd/build/vite/ws-proxy.d.ts.map +1 -1
  31. package/dist/cmd/build/vite/ws-proxy.js +33 -0
  32. package/dist/cmd/build/vite/ws-proxy.js.map +1 -1
  33. package/dist/cmd/cloud/deploy.d.ts.map +1 -1
  34. package/dist/cmd/cloud/deploy.js +98 -39
  35. package/dist/cmd/cloud/deploy.js.map +1 -1
  36. package/dist/cmd/cloud/sandbox/checkpoint/create.d.ts.map +1 -1
  37. package/dist/cmd/cloud/sandbox/checkpoint/create.js +3 -4
  38. package/dist/cmd/cloud/sandbox/checkpoint/create.js.map +1 -1
  39. package/dist/cmd/cloud/sandbox/checkpoint/delete.d.ts.map +1 -1
  40. package/dist/cmd/cloud/sandbox/checkpoint/delete.js +3 -4
  41. package/dist/cmd/cloud/sandbox/checkpoint/delete.js.map +1 -1
  42. package/dist/cmd/cloud/sandbox/checkpoint/list.d.ts.map +1 -1
  43. package/dist/cmd/cloud/sandbox/checkpoint/list.js +3 -4
  44. package/dist/cmd/cloud/sandbox/checkpoint/list.js.map +1 -1
  45. package/dist/cmd/cloud/sandbox/checkpoint/restore.d.ts.map +1 -1
  46. package/dist/cmd/cloud/sandbox/checkpoint/restore.js +3 -4
  47. package/dist/cmd/cloud/sandbox/checkpoint/restore.js.map +1 -1
  48. package/dist/cmd/cloud/sandbox/create.d.ts.map +1 -1
  49. package/dist/cmd/cloud/sandbox/create.js +13 -4
  50. package/dist/cmd/cloud/sandbox/create.js.map +1 -1
  51. package/dist/cmd/cloud/sandbox/delete.d.ts.map +1 -1
  52. package/dist/cmd/cloud/sandbox/delete.js +3 -4
  53. package/dist/cmd/cloud/sandbox/delete.js.map +1 -1
  54. package/dist/cmd/cloud/sandbox/env.d.ts.map +1 -1
  55. package/dist/cmd/cloud/sandbox/env.js +3 -5
  56. package/dist/cmd/cloud/sandbox/env.js.map +1 -1
  57. package/dist/cmd/cloud/sandbox/exec.d.ts.map +1 -1
  58. package/dist/cmd/cloud/sandbox/exec.js +114 -41
  59. package/dist/cmd/cloud/sandbox/exec.js.map +1 -1
  60. package/dist/cmd/cloud/sandbox/execution/list.d.ts.map +1 -1
  61. package/dist/cmd/cloud/sandbox/execution/list.js +3 -5
  62. package/dist/cmd/cloud/sandbox/execution/list.js.map +1 -1
  63. package/dist/cmd/cloud/sandbox/fs/cp.d.ts.map +1 -1
  64. package/dist/cmd/cloud/sandbox/fs/cp.js +61 -113
  65. package/dist/cmd/cloud/sandbox/fs/cp.js.map +1 -1
  66. package/dist/cmd/cloud/sandbox/fs/download.d.ts.map +1 -1
  67. package/dist/cmd/cloud/sandbox/fs/download.js +11 -22
  68. package/dist/cmd/cloud/sandbox/fs/download.js.map +1 -1
  69. package/dist/cmd/cloud/sandbox/fs/ls.d.ts.map +1 -1
  70. package/dist/cmd/cloud/sandbox/fs/ls.js +3 -5
  71. package/dist/cmd/cloud/sandbox/fs/ls.js.map +1 -1
  72. package/dist/cmd/cloud/sandbox/fs/mkdir.d.ts.map +1 -1
  73. package/dist/cmd/cloud/sandbox/fs/mkdir.js +3 -5
  74. package/dist/cmd/cloud/sandbox/fs/mkdir.js.map +1 -1
  75. package/dist/cmd/cloud/sandbox/fs/rm.d.ts.map +1 -1
  76. package/dist/cmd/cloud/sandbox/fs/rm.js +3 -5
  77. package/dist/cmd/cloud/sandbox/fs/rm.js.map +1 -1
  78. package/dist/cmd/cloud/sandbox/fs/rmdir.d.ts.map +1 -1
  79. package/dist/cmd/cloud/sandbox/fs/rmdir.js +3 -5
  80. package/dist/cmd/cloud/sandbox/fs/rmdir.js.map +1 -1
  81. package/dist/cmd/cloud/sandbox/fs/upload.d.ts.map +1 -1
  82. package/dist/cmd/cloud/sandbox/fs/upload.js +7 -8
  83. package/dist/cmd/cloud/sandbox/fs/upload.js.map +1 -1
  84. package/dist/cmd/cloud/sandbox/get.d.ts.map +1 -1
  85. package/dist/cmd/cloud/sandbox/get.js +21 -7
  86. package/dist/cmd/cloud/sandbox/get.js.map +1 -1
  87. package/dist/cmd/cloud/sandbox/job/create.d.ts.map +1 -1
  88. package/dist/cmd/cloud/sandbox/job/create.js +3 -4
  89. package/dist/cmd/cloud/sandbox/job/create.js.map +1 -1
  90. package/dist/cmd/cloud/sandbox/job/destroy.d.ts.map +1 -1
  91. package/dist/cmd/cloud/sandbox/job/destroy.js +3 -4
  92. package/dist/cmd/cloud/sandbox/job/destroy.js.map +1 -1
  93. package/dist/cmd/cloud/sandbox/job/get.d.ts.map +1 -1
  94. package/dist/cmd/cloud/sandbox/job/get.js +3 -4
  95. package/dist/cmd/cloud/sandbox/job/get.js.map +1 -1
  96. package/dist/cmd/cloud/sandbox/job/list.d.ts.map +1 -1
  97. package/dist/cmd/cloud/sandbox/job/list.js +3 -4
  98. package/dist/cmd/cloud/sandbox/job/list.js.map +1 -1
  99. package/dist/cmd/cloud/sandbox/job/logs.d.ts.map +1 -1
  100. package/dist/cmd/cloud/sandbox/job/logs.js +4 -4
  101. package/dist/cmd/cloud/sandbox/job/logs.js.map +1 -1
  102. package/dist/cmd/cloud/sandbox/pause.d.ts.map +1 -1
  103. package/dist/cmd/cloud/sandbox/pause.js +21 -5
  104. package/dist/cmd/cloud/sandbox/pause.js.map +1 -1
  105. package/dist/cmd/cloud/sandbox/resume.d.ts.map +1 -1
  106. package/dist/cmd/cloud/sandbox/resume.js +3 -4
  107. package/dist/cmd/cloud/sandbox/resume.js.map +1 -1
  108. package/dist/cmd/cloud/sandbox/run.d.ts.map +1 -1
  109. package/dist/cmd/cloud/sandbox/run.js +36 -7
  110. package/dist/cmd/cloud/sandbox/run.js.map +1 -1
  111. package/dist/cmd/cloud/sandbox/snapshot/create.js +4 -4
  112. package/dist/cmd/cloud/sandbox/snapshot/create.js.map +1 -1
  113. package/dist/cmd/cloud/sandbox/util.d.ts +19 -0
  114. package/dist/cmd/cloud/sandbox/util.d.ts.map +1 -1
  115. package/dist/cmd/cloud/sandbox/util.js +40 -2
  116. package/dist/cmd/cloud/sandbox/util.js.map +1 -1
  117. package/dist/cmd/coder/create.js +7 -7
  118. package/dist/cmd/coder/create.js.map +1 -1
  119. package/dist/cmd/coder/start.d.ts.map +1 -1
  120. package/dist/cmd/coder/start.js +3 -0
  121. package/dist/cmd/coder/start.js.map +1 -1
  122. package/dist/cmd/coder/tui-init.js +1 -1
  123. package/dist/cmd/coder/tui-init.js.map +1 -1
  124. package/dist/cmd/coder/update.js +8 -8
  125. package/dist/cmd/coder/update.js.map +1 -1
  126. package/dist/cmd/coder/workspace/common.d.ts +29 -0
  127. package/dist/cmd/coder/workspace/common.d.ts.map +1 -0
  128. package/dist/cmd/coder/workspace/common.js +83 -0
  129. package/dist/cmd/coder/workspace/common.js.map +1 -0
  130. package/dist/cmd/coder/workspace/create.d.ts.map +1 -1
  131. package/dist/cmd/coder/workspace/create.js +57 -32
  132. package/dist/cmd/coder/workspace/create.js.map +1 -1
  133. package/dist/cmd/coder/workspace/get.d.ts.map +1 -1
  134. package/dist/cmd/coder/workspace/get.js +2 -5
  135. package/dist/cmd/coder/workspace/get.js.map +1 -1
  136. package/dist/cmd/coder/workspace/index.d.ts.map +1 -1
  137. package/dist/cmd/coder/workspace/index.js +11 -1
  138. package/dist/cmd/coder/workspace/index.js.map +1 -1
  139. package/dist/cmd/coder/workspace/list.d.ts.map +1 -1
  140. package/dist/cmd/coder/workspace/list.js +4 -0
  141. package/dist/cmd/coder/workspace/list.js.map +1 -1
  142. package/dist/cmd/coder/workspace/refresh.d.ts +2 -0
  143. package/dist/cmd/coder/workspace/refresh.d.ts.map +1 -0
  144. package/dist/cmd/coder/workspace/refresh.js +59 -0
  145. package/dist/cmd/coder/workspace/refresh.js.map +1 -0
  146. package/dist/cmd/coder/workspace/update.d.ts +2 -0
  147. package/dist/cmd/coder/workspace/update.d.ts.map +1 -0
  148. package/dist/cmd/coder/workspace/update.js +131 -0
  149. package/dist/cmd/coder/workspace/update.js.map +1 -0
  150. package/dist/cmd/coder/workspace/validate-dependencies.d.ts +2 -0
  151. package/dist/cmd/coder/workspace/validate-dependencies.d.ts.map +1 -0
  152. package/dist/cmd/coder/workspace/validate-dependencies.js +70 -0
  153. package/dist/cmd/coder/workspace/validate-dependencies.js.map +1 -0
  154. package/dist/cmd/dev/dev-lock.d.ts.map +1 -1
  155. package/dist/cmd/dev/dev-lock.js +43 -17
  156. package/dist/cmd/dev/dev-lock.js.map +1 -1
  157. package/dist/cmd/dev/index.d.ts.map +1 -1
  158. package/dist/cmd/dev/index.js +211 -125
  159. package/dist/cmd/dev/index.js.map +1 -1
  160. package/dist/cmd/dev/process-manager.d.ts +41 -1
  161. package/dist/cmd/dev/process-manager.d.ts.map +1 -1
  162. package/dist/cmd/dev/process-manager.js +160 -31
  163. package/dist/cmd/dev/process-manager.js.map +1 -1
  164. package/dist/cmd/project/create.d.ts.map +1 -1
  165. package/dist/cmd/project/create.js +0 -2
  166. package/dist/cmd/project/create.js.map +1 -1
  167. package/dist/cmd/project/index.d.ts.map +1 -1
  168. package/dist/cmd/project/index.js +0 -3
  169. package/dist/cmd/project/index.js.map +1 -1
  170. package/dist/cmd/project/random-name.d.ts +17 -0
  171. package/dist/cmd/project/random-name.d.ts.map +1 -0
  172. package/dist/cmd/project/random-name.js +144 -0
  173. package/dist/cmd/project/random-name.js.map +1 -0
  174. package/dist/cmd/project/template-flow.d.ts +0 -1
  175. package/dist/cmd/project/template-flow.d.ts.map +1 -1
  176. package/dist/cmd/project/template-flow.js +180 -275
  177. package/dist/cmd/project/template-flow.js.map +1 -1
  178. package/dist/composite-logger.d.ts.map +1 -1
  179. package/dist/composite-logger.js +19 -0
  180. package/dist/composite-logger.js.map +1 -1
  181. package/dist/config.d.ts +18 -16
  182. package/dist/config.d.ts.map +1 -1
  183. package/dist/config.js +46 -16
  184. package/dist/config.js.map +1 -1
  185. package/dist/tui/prompt.d.ts +29 -0
  186. package/dist/tui/prompt.d.ts.map +1 -1
  187. package/dist/tui/prompt.js +180 -8
  188. package/dist/tui/prompt.js.map +1 -1
  189. package/package.json +7 -7
  190. package/src/cache/resource-region.ts +68 -44
  191. package/src/cli.ts +30 -8
  192. package/src/cmd/ai/prompt/web.md +43 -17
  193. package/src/cmd/build/vite/bun-dev-server.ts +92 -6
  194. package/src/cmd/build/vite/index.ts +0 -1
  195. package/src/cmd/build/vite/static-renderer.ts +18 -7
  196. package/src/cmd/build/vite/vite-asset-server-config.ts +37 -27
  197. package/src/cmd/build/vite/vite-asset-server.ts +5 -1
  198. package/src/cmd/build/vite/vite-builder.ts +12 -1
  199. package/src/cmd/build/vite/ws-proxy.ts +52 -3
  200. package/src/cmd/cloud/deploy.ts +117 -49
  201. package/src/cmd/cloud/sandbox/checkpoint/create.ts +10 -4
  202. package/src/cmd/cloud/sandbox/checkpoint/delete.ts +10 -4
  203. package/src/cmd/cloud/sandbox/checkpoint/list.ts +10 -4
  204. package/src/cmd/cloud/sandbox/checkpoint/restore.ts +10 -4
  205. package/src/cmd/cloud/sandbox/create.ts +14 -4
  206. package/src/cmd/cloud/sandbox/delete.ts +10 -4
  207. package/src/cmd/cloud/sandbox/env.ts +10 -5
  208. package/src/cmd/cloud/sandbox/exec.ts +157 -42
  209. package/src/cmd/cloud/sandbox/execution/list.ts +10 -5
  210. package/src/cmd/cloud/sandbox/fs/cp.ts +94 -126
  211. package/src/cmd/cloud/sandbox/fs/download.ts +18 -25
  212. package/src/cmd/cloud/sandbox/fs/ls.ts +10 -5
  213. package/src/cmd/cloud/sandbox/fs/mkdir.ts +10 -5
  214. package/src/cmd/cloud/sandbox/fs/rm.ts +10 -5
  215. package/src/cmd/cloud/sandbox/fs/rmdir.ts +10 -5
  216. package/src/cmd/cloud/sandbox/fs/upload.ts +14 -8
  217. package/src/cmd/cloud/sandbox/get.ts +28 -7
  218. package/src/cmd/cloud/sandbox/job/create.ts +10 -4
  219. package/src/cmd/cloud/sandbox/job/destroy.ts +10 -4
  220. package/src/cmd/cloud/sandbox/job/get.ts +10 -4
  221. package/src/cmd/cloud/sandbox/job/list.ts +10 -4
  222. package/src/cmd/cloud/sandbox/job/logs.ts +11 -4
  223. package/src/cmd/cloud/sandbox/pause.ts +31 -5
  224. package/src/cmd/cloud/sandbox/resume.ts +10 -4
  225. package/src/cmd/cloud/sandbox/run.ts +49 -11
  226. package/src/cmd/cloud/sandbox/snapshot/create.ts +6 -6
  227. package/src/cmd/cloud/sandbox/util.ts +63 -2
  228. package/src/cmd/coder/create.ts +8 -8
  229. package/src/cmd/coder/start.ts +3 -0
  230. package/src/cmd/coder/tui-init.ts +1 -1
  231. package/src/cmd/coder/update.ts +7 -7
  232. package/src/cmd/coder/workspace/common.ts +103 -0
  233. package/src/cmd/coder/workspace/create.ts +84 -37
  234. package/src/cmd/coder/workspace/get.ts +2 -5
  235. package/src/cmd/coder/workspace/index.ts +13 -1
  236. package/src/cmd/coder/workspace/list.ts +4 -0
  237. package/src/cmd/coder/workspace/refresh.ts +63 -0
  238. package/src/cmd/coder/workspace/update.ts +154 -0
  239. package/src/cmd/coder/workspace/validate-dependencies.ts +75 -0
  240. package/src/cmd/dev/dev-lock.ts +50 -16
  241. package/src/cmd/dev/index.ts +249 -134
  242. package/src/cmd/dev/process-manager.ts +173 -33
  243. package/src/cmd/project/create.ts +0 -2
  244. package/src/cmd/project/index.ts +0 -3
  245. package/src/cmd/project/random-name.ts +152 -0
  246. package/src/cmd/project/template-flow.ts +196 -305
  247. package/src/composite-logger.ts +20 -0
  248. package/src/config.ts +69 -19
  249. package/src/tui/prompt.ts +214 -8
  250. package/dist/cmd/build/vite/public-asset-path-plugin.d.ts +0 -45
  251. package/dist/cmd/build/vite/public-asset-path-plugin.d.ts.map +0 -1
  252. package/dist/cmd/build/vite/public-asset-path-plugin.js +0 -166
  253. package/dist/cmd/build/vite/public-asset-path-plugin.js.map +0 -1
  254. package/dist/cmd/project/auth/generate.d.ts +0 -5
  255. package/dist/cmd/project/auth/generate.d.ts.map +0 -1
  256. package/dist/cmd/project/auth/generate.js +0 -102
  257. package/dist/cmd/project/auth/generate.js.map +0 -1
  258. package/dist/cmd/project/auth/index.d.ts +0 -2
  259. package/dist/cmd/project/auth/index.d.ts.map +0 -1
  260. package/dist/cmd/project/auth/index.js +0 -21
  261. package/dist/cmd/project/auth/index.js.map +0 -1
  262. package/dist/cmd/project/auth/init.d.ts +0 -2
  263. package/dist/cmd/project/auth/init.d.ts.map +0 -1
  264. package/dist/cmd/project/auth/init.js +0 -213
  265. package/dist/cmd/project/auth/init.js.map +0 -1
  266. package/dist/cmd/project/auth/shared.d.ts +0 -93
  267. package/dist/cmd/project/auth/shared.d.ts.map +0 -1
  268. package/dist/cmd/project/auth/shared.js +0 -475
  269. package/dist/cmd/project/auth/shared.js.map +0 -1
  270. package/src/cmd/build/vite/public-asset-path-plugin.ts +0 -209
  271. package/src/cmd/project/auth/generate.ts +0 -116
  272. package/src/cmd/project/auth/index.ts +0 -21
  273. package/src/cmd/project/auth/init.ts +0 -256
  274. package/src/cmd/project/auth/shared.ts +0 -591
@@ -7,6 +7,14 @@
7
7
  * - Graceful shutdown (SIGINT/SIGTERM)
8
8
  *
9
9
  * This prevents orphan processes and port conflicts between dev sessions.
10
+ *
11
+ * Key design decisions:
12
+ * - Process tree killing: Uses process.kill(-pid) to kill entire process groups,
13
+ * preventing orphaned child processes (e.g., Bun backend spawning workers).
14
+ * - Per-process SIGTERM→SIGKILL escalation: Each process gets its own grace
15
+ * period instead of waiting for all processes to exit before force-killing.
16
+ * - Last-resort synchronous cleanup: forceKillAllSync() can be called from
17
+ * process.on('exit') handlers where async operations are not possible.
10
18
  */
11
19
 
12
20
  import type { Logger } from '../../types';
@@ -39,6 +47,12 @@ export interface ManagedServer {
39
47
  description: string;
40
48
  /** The port this server uses */
41
49
  port?: number;
50
+ /**
51
+ * Maximum time (ms) to wait for this server's close() to resolve before
52
+ * moving on. Defaults to 1000ms. Servers like Vite that own filesystem
53
+ * watchers and HMR sockets may need more time.
54
+ */
55
+ closeTimeoutMs?: number;
42
56
  }
43
57
 
44
58
  /**
@@ -114,11 +128,71 @@ export class ProcessManager {
114
128
  return ports;
115
129
  }
116
130
 
131
+ /**
132
+ * Whether cleanup has already completed.
133
+ * Used by forceKillAllSync() to avoid redundant work.
134
+ */
135
+ get isCleanedUp(): boolean {
136
+ return this.cleaningUp && this.processes.length === 0 && this.servers.length === 0;
137
+ }
138
+
139
+ /**
140
+ * Kill a process and its entire process tree.
141
+ *
142
+ * Uses process.kill(-pid) to send the signal to the entire process group.
143
+ * This ensures child processes (e.g., workers spawned by Bun) are also killed.
144
+ * Falls back to direct PID kill if process group kill fails (e.g., EPERM or
145
+ * the process is not a group leader).
146
+ */
147
+ private killProcessTree(pid: number, signal: NodeJS.Signals): boolean {
148
+ // Safety: never send signals to PID 0 (own process group), PID 1 (init/systemd),
149
+ // or negative PIDs (which would be double-negated). process.kill(-1) is
150
+ // especially dangerous as it signals every process the user owns.
151
+ if (pid <= 1) {
152
+ this.logger.debug('Refusing to kill dangerous pid %d, skipping process tree kill', pid);
153
+ return false;
154
+ }
155
+
156
+ // Try killing the entire process group first (negative PID)
157
+ try {
158
+ process.kill(-pid, signal);
159
+ this.logger.debug('Sent %s to process group -%d', signal, pid);
160
+ return true;
161
+ } catch (err) {
162
+ const error = err as NodeJS.ErrnoException;
163
+ // ESRCH = no such process/group, EPERM = not a group leader or no permission
164
+ if (error.code !== 'ESRCH') {
165
+ this.logger.debug(
166
+ 'Process group kill failed for pid %d (%s), falling back to direct kill',
167
+ pid,
168
+ error.code
169
+ );
170
+ }
171
+ }
172
+
173
+ // Fall back to direct PID kill
174
+ try {
175
+ process.kill(pid, signal);
176
+ this.logger.debug('Sent %s to pid %d (direct)', signal, pid);
177
+ return true;
178
+ } catch (err) {
179
+ const error = err as NodeJS.ErrnoException;
180
+ if (error.code !== 'ESRCH') {
181
+ this.logger.debug('Direct kill failed for pid %d: %s', pid, error.code);
182
+ }
183
+ return false;
184
+ }
185
+ }
186
+
117
187
  /**
118
188
  * Clean up all tracked processes and servers.
119
189
  *
190
+ * Uses per-process SIGTERM→SIGKILL escalation: each process gets up to
191
+ * `timeout` ms to exit gracefully after SIGTERM. Processes that exit early
192
+ * don't delay cleanup of other processes.
193
+ *
120
194
  * @param reason - Why cleanup is happening (for logging)
121
- * @param timeout - Max time to wait for graceful shutdown (ms)
195
+ * @param timeout - Max time to wait for graceful shutdown per process (ms)
122
196
  */
123
197
  async cleanup(reason: string, timeout = 3000): Promise<void> {
124
198
  if (this.cleaningUp) {
@@ -129,61 +203,94 @@ export class ProcessManager {
129
203
 
130
204
  this.logger.debug('Starting cleanup (reason: %s)', reason);
131
205
 
132
- // Kill processes in reverse order (LIFO)
133
- for (let i = this.processes.length - 1; i >= 0; i--) {
134
- const proc = this.processes[i];
135
- if (!proc) continue;
136
-
137
- try {
138
- if (proc.process.exitCode === null) {
139
- this.logger.debug(
140
- 'Killing process %s (pid=%s)',
141
- proc.id,
142
- proc.process.pid ?? 'unknown'
143
- );
144
- proc.process.kill('SIGTERM');
145
- }
146
- } catch (err) {
147
- this.logger.debug('Error killing process %s: %s', proc.id, err);
148
- }
149
- }
206
+ // Snapshot processes and servers before cleanup so we can clear tracking
207
+ // lists early. This prevents the exit handler from re-killing already
208
+ // handled processes.
209
+ const processSnapshot = [...this.processes];
210
+ const serverSnapshot = [...this.servers];
150
211
 
151
- // Close servers
152
- for (let i = this.servers.length - 1; i >= 0; i--) {
153
- const server = this.servers[i];
212
+ // Close servers first (reverse order, LIFO)
213
+ for (let i = serverSnapshot.length - 1; i >= 0; i--) {
214
+ const server = serverSnapshot[i];
154
215
  if (!server) continue;
155
216
 
217
+ const closeTimeout = server.closeTimeoutMs ?? 1000;
156
218
  try {
157
- this.logger.debug('Closing server %s', server.id);
219
+ this.logger.debug('Closing server %s (timeout=%dms)', server.id, closeTimeout);
158
220
  const closePromise = server.server.close();
159
221
  if (closePromise instanceof Promise) {
222
+ let timedOut = false;
160
223
  await Promise.race([
161
224
  closePromise,
162
- new Promise<void>((resolve) => setTimeout(resolve, 1000)),
225
+ new Promise<void>((resolve) =>
226
+ setTimeout(() => {
227
+ timedOut = true;
228
+ resolve();
229
+ }, closeTimeout)
230
+ ),
163
231
  ]);
232
+ if (timedOut) {
233
+ this.logger.debug(
234
+ 'Server %s did not close within %dms, continuing cleanup',
235
+ server.id,
236
+ closeTimeout
237
+ );
238
+ }
164
239
  }
165
240
  } catch (err) {
166
241
  this.logger.debug('Error closing server %s: %s', server.id, err);
167
242
  }
168
243
  }
169
244
 
170
- // Wait for processes to exit, then force-kill if needed
245
+ // Send SIGTERM to all processes in reverse order (LIFO), targeting
246
+ // process trees so child processes also receive the signal.
247
+ for (let i = processSnapshot.length - 1; i >= 0; i--) {
248
+ const proc = processSnapshot[i];
249
+ if (!proc) continue;
250
+
251
+ try {
252
+ if (proc.process.exitCode === null) {
253
+ const pid = proc.process.pid;
254
+ this.logger.debug('Killing process %s (pid=%s)', proc.id, pid ?? 'unknown');
255
+ if (pid) {
256
+ this.killProcessTree(pid, 'SIGTERM');
257
+ } else {
258
+ proc.process.kill('SIGTERM');
259
+ }
260
+ }
261
+ } catch (err) {
262
+ this.logger.debug('Error killing process %s: %s', proc.id, err);
263
+ }
264
+ }
265
+
266
+ // Wait for processes to exit, then force-kill individually.
267
+ // Each process gets up to `timeout` ms from the initial SIGTERM.
171
268
  const startTime = Date.now();
172
269
  while (Date.now() - startTime < timeout) {
173
- const allExited = this.processes.every((p) => p.process.exitCode !== null);
270
+ const allExited = processSnapshot.every((p) => p.process.exitCode !== null);
174
271
  if (allExited) break;
175
272
  await new Promise((resolve) => setTimeout(resolve, 100));
176
273
  }
177
274
 
178
- // Force kill any remaining
179
- for (const proc of this.processes) {
180
- if (proc.process.exitCode === null) {
181
- try {
182
- this.logger.debug('Force killing process %s', proc.id);
275
+ // Force kill any remaining processes and their process trees.
276
+ // When a PID is available, always attempt process-group SIGKILL even if
277
+ // the leader has already exited: on Unix, process groups persist after
278
+ // the leader exits and signaling via negative PGID still reaches
279
+ // remaining members. killProcessTree() handles ESRCH gracefully.
280
+ for (const proc of processSnapshot) {
281
+ const pid = proc.process.pid;
282
+ const shouldForceTreeKill = typeof pid === 'number' && pid > 1;
283
+ if (!shouldForceTreeKill && proc.process.exitCode !== null) continue;
284
+
285
+ try {
286
+ this.logger.debug('Force killing process %s (pid=%s)', proc.id, pid ?? 'unknown');
287
+ if (shouldForceTreeKill) {
288
+ this.killProcessTree(pid, 'SIGKILL');
289
+ } else {
183
290
  proc.process.kill('SIGKILL');
184
- } catch (err) {
185
- this.logger.debug('Error force killing process %s: %s', proc.id, err);
186
291
  }
292
+ } catch (err) {
293
+ this.logger.debug('Error force killing process %s: %s', proc.id, err);
187
294
  }
188
295
  }
189
296
 
@@ -192,6 +299,39 @@ export class ProcessManager {
192
299
  this.servers = [];
193
300
  }
194
301
 
302
+ /**
303
+ * Synchronous last-resort cleanup for use in process.on('exit') handlers.
304
+ *
305
+ * Sends SIGKILL to all tracked process trees. This is intentionally
306
+ * aggressive because it's the final opportunity to prevent orphans.
307
+ * Only runs if async cleanup() hasn't already completed.
308
+ */
309
+ forceKillAllSync(): void {
310
+ if (this.isCleanedUp) return;
311
+
312
+ for (const proc of this.processes) {
313
+ if (proc.process.exitCode !== null) continue;
314
+ const pid = proc.process.pid;
315
+ try {
316
+ if (pid && pid > 1) {
317
+ // Try process group kill first, fall back to direct
318
+ try {
319
+ process.kill(-pid, 'SIGKILL');
320
+ } catch {
321
+ process.kill(pid, 'SIGKILL');
322
+ }
323
+ } else {
324
+ proc.process.kill('SIGKILL');
325
+ }
326
+ } catch {
327
+ // Best effort in exit handler — nothing else we can do
328
+ }
329
+ }
330
+
331
+ this.processes = [];
332
+ this.servers = [];
333
+ }
334
+
195
335
  /**
196
336
  * Verify that all ports used by tracked processes are released.
197
337
  * Used after cleanup to ensure no orphan processes remain.
@@ -80,7 +80,6 @@ export const createProjectSubcommand = createSubcommand({
80
80
  .string()
81
81
  .optional()
82
82
  .describe('Storage action: "skip", "new", or existing bucket name'),
83
- enableAuth: z.boolean().optional().describe('Enable Agentuity Auth'),
84
83
  }),
85
84
  response: ProjectCreateResponseSchema,
86
85
  },
@@ -115,7 +114,6 @@ export const createProjectSubcommand = createSubcommand({
115
114
  region,
116
115
  database: opts.database,
117
116
  storage: opts.storage,
118
- enableAuth: opts.enableAuth,
119
117
  });
120
118
 
121
119
  // Exit with error code if setup failed and not in JSON mode
@@ -4,7 +4,6 @@ import { importSubcommand } from './import';
4
4
  import { listSubcommand } from './list';
5
5
  import { deleteSubcommand } from './delete';
6
6
  import { showSubcommand } from './show';
7
- import { authCommand } from './auth';
8
7
  import { addCommand } from './add';
9
8
  import { hostnameCommand } from './hostname';
10
9
  import { domainCommand } from './domain';
@@ -18,7 +17,6 @@ export const command = createCommand({
18
17
  { command: getCommand('project create my-agent'), description: 'Create a new project' },
19
18
  { command: getCommand('project import'), description: 'Import an existing project' },
20
19
  { command: getCommand('project list'), description: 'List all projects' },
21
- { command: getCommand('project auth init'), description: 'Set up Agentuity Auth' },
22
20
  { command: getCommand('project add database'), description: 'Link an existing database' },
23
21
  {
24
22
  command: getCommand('project add storage'),
@@ -39,7 +37,6 @@ export const command = createCommand({
39
37
  listSubcommand,
40
38
  deleteSubcommand,
41
39
  showSubcommand,
42
- authCommand,
43
40
  addCommand,
44
41
  hostnameCommand,
45
42
  domainCommand,
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Generates project-derived suggestions for resource names (DB / S3 bucket).
3
+ *
4
+ * The CLI shows these as a dim "press Enter to use ..." default in the create flow.
5
+ * If the user presses Enter, the suggestion is sent to the server. Otherwise the
6
+ * server is responsible for assigning a name when none is provided.
7
+ *
8
+ * Both suggestions are validated against the same rules the server enforces
9
+ * (`validateBucketName` / `validateDatabaseName` from `@agentuity/server`) so the
10
+ * happy path can never produce an invalid suggestion.
11
+ */
12
+ import { validateBucketName, validateDatabaseName } from '@agentuity/server';
13
+
14
+ const BUCKET_MAX = 63;
15
+ const BUCKET_MIN = 3;
16
+ const DB_MAX = 63;
17
+
18
+ /** 3 lowercase alphanumeric chars, e.g. "k7p". */
19
+ function shortSuffix(): string {
20
+ // toString(36) yields [0-9a-z]; slice 3 chars after the "0." prefix.
21
+ const s = Math.random().toString(36).slice(2, 5);
22
+ // Pad in the (extremely unlikely) case the slice is shorter than 3 chars.
23
+ return s.length === 3 ? s : (s + '000').slice(0, 3);
24
+ }
25
+
26
+ /**
27
+ * Sanitize a project name into the bucket-name alphabet:
28
+ * - lowercase
29
+ * - spaces / underscores / dots → hyphens
30
+ * - drop anything else
31
+ * - collapse and trim hyphens
32
+ * - strip reserved prefixes (`agentuity*`, `ag-*`, `ago-*`, `xn--`)
33
+ */
34
+ function sanitizeForBucket(name: string): string {
35
+ let out = name
36
+ .toLowerCase()
37
+ .trim()
38
+ .replace(/[\s_.]+/g, '-')
39
+ .replace(/[^a-z0-9-]/g, '')
40
+ .replace(/-+/g, '-')
41
+ .replace(/^-+|-+$/g, '');
42
+
43
+ // Strip reserved prefixes (rules from validateBucketName).
44
+ while (
45
+ out.startsWith('agentuity') ||
46
+ out.startsWith('ag-') ||
47
+ out.startsWith('ago-') ||
48
+ out.startsWith('xn--')
49
+ ) {
50
+ if (out.startsWith('agentuity')) out = out.slice('agentuity'.length);
51
+ else if (out.startsWith('ago-')) out = out.slice('ago-'.length);
52
+ else if (out.startsWith('ag-')) out = out.slice('ag-'.length);
53
+ else if (out.startsWith('xn--')) out = out.slice('xn--'.length);
54
+ out = out.replace(/^-+/, '');
55
+ }
56
+ return out;
57
+ }
58
+
59
+ /**
60
+ * Sanitize a project name into the database-name alphabet:
61
+ * - lowercase
62
+ * - non `[a-z0-9_]` → `_`
63
+ * - collapse and trim underscores
64
+ * - ensure it starts with a letter or underscore (prepend `p_` otherwise)
65
+ * - strip reserved `pg_` prefix
66
+ */
67
+ function sanitizeForDatabase(name: string): string {
68
+ let out = name
69
+ .toLowerCase()
70
+ .trim()
71
+ .replace(/[^a-z0-9_]+/g, '_')
72
+ .replace(/_+/g, '_')
73
+ .replace(/^_+|_+$/g, '');
74
+
75
+ if (!/^[a-z_]/.test(out)) {
76
+ out = out.length > 0 ? `p_${out}` : '';
77
+ }
78
+ while (out.startsWith('pg_')) {
79
+ out = out.slice(3).replace(/^_+/, '');
80
+ if (!/^[a-z_]/.test(out) && out.length > 0) out = `p_${out}`;
81
+ }
82
+ return out;
83
+ }
84
+
85
+ /**
86
+ * Truncate so the final string (`<base>-<suffixWithDash>`) fits within `max` chars
87
+ * while keeping the suffix intact and not ending on a hyphen.
88
+ */
89
+ function truncateBaseHyphen(base: string, suffixWithDash: string, max: number): string {
90
+ const room = max - suffixWithDash.length;
91
+ if (room <= 0) return '';
92
+ let out = base.slice(0, room);
93
+ out = out.replace(/-+$/, '');
94
+ return out;
95
+ }
96
+
97
+ /** Same as `truncateBaseHyphen` but for underscore-joined names (database). */
98
+ function truncateBaseUnderscore(base: string, suffixWithUnderscore: string, max: number): string {
99
+ const room = max - suffixWithUnderscore.length;
100
+ if (room <= 0) return '';
101
+ let out = base.slice(0, room);
102
+ out = out.replace(/_+$/, '');
103
+ return out;
104
+ }
105
+
106
+ /**
107
+ * Generate a suggested S3 bucket name derived from the project name.
108
+ * Format: `<sanitized-project>-storage-<3char>` (≤ 63 chars).
109
+ *
110
+ * Falls back to `bucket-<3char><3char>` if the project name produces nothing usable.
111
+ * Always returns a value that passes `validateBucketName`.
112
+ */
113
+ export function suggestBucketName(projectName: string): string {
114
+ const sanitized = sanitizeForBucket(projectName);
115
+
116
+ // Try a few times in case sanitization + suffix happens to land on something invalid.
117
+ for (let attempt = 0; attempt < 5; attempt++) {
118
+ const suffix = `-storage-${shortSuffix()}`;
119
+ const base = truncateBaseHyphen(sanitized, suffix, BUCKET_MAX);
120
+ const candidate = base.length > 0 ? `${base}${suffix}` : `bucket${suffix}`;
121
+ if (candidate.length >= BUCKET_MIN && validateBucketName(candidate).valid) {
122
+ return candidate;
123
+ }
124
+ }
125
+
126
+ // Pure fallback: short, always-valid generic name.
127
+ const fallback = `bucket-${shortSuffix()}${shortSuffix()}`;
128
+ return validateBucketName(fallback).valid ? fallback : `bucket-${shortSuffix()}aaa`;
129
+ }
130
+
131
+ /**
132
+ * Generate a suggested PostgreSQL database name derived from the project name.
133
+ * Format: `<sanitized-project>_db_<3char>` (≤ 63 chars).
134
+ *
135
+ * Falls back to `db_<3char><3char>` if the project name produces nothing usable.
136
+ * Always returns a value that passes `validateDatabaseName`.
137
+ */
138
+ export function suggestDatabaseName(projectName: string): string {
139
+ const sanitized = sanitizeForDatabase(projectName);
140
+
141
+ for (let attempt = 0; attempt < 5; attempt++) {
142
+ const suffix = `_db_${shortSuffix()}`;
143
+ const base = truncateBaseUnderscore(sanitized, suffix, DB_MAX);
144
+ const candidate = base.length > 0 ? `${base}${suffix}` : `db${suffix}`;
145
+ if (validateDatabaseName(candidate).valid) {
146
+ return candidate;
147
+ }
148
+ }
149
+
150
+ const fallback = `db_${shortSuffix()}${shortSuffix()}`;
151
+ return validateDatabaseName(fallback).valid ? fallback : `db_${shortSuffix()}aaa`;
152
+ }