@clagentic/console 1.7.0 → 1.8.0-beta.1

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 (58) hide show
  1. package/bin/cli.js +198 -2542
  2. package/lib/build-sha.json +4 -0
  3. package/lib/cli/daemon-launch.js +797 -0
  4. package/lib/cli/ipc-subcommands.js +186 -0
  5. package/lib/cli/menus.js +849 -0
  6. package/lib/cli/net-detect.js +209 -0
  7. package/lib/cli/tui.js +589 -0
  8. package/lib/daemon.js +15 -6
  9. package/lib/memory-shed.js +81 -2
  10. package/lib/pages.js +13 -17
  11. package/lib/project-connection.js +13 -0
  12. package/lib/project-loop.js +4 -0
  13. package/lib/project-sessions.js +9 -3
  14. package/lib/project.js +3 -2
  15. package/lib/public/css/diagnostics.css +29 -1
  16. package/lib/public/css/home-hub.css +10 -0
  17. package/lib/public/css/overlays.css +19 -0
  18. package/lib/public/index.html +12 -11
  19. package/lib/public/manifest.json +1 -1
  20. package/lib/public/modules/app-connection.js +150 -4
  21. package/lib/public/modules/app-history-replay.js +155 -0
  22. package/lib/public/modules/app-home-hub.js +24 -14
  23. package/lib/public/modules/app-messages.js +1246 -1469
  24. package/lib/public/modules/app-notifications.js +4 -4
  25. package/lib/public/modules/app-rate-limit.js +128 -27
  26. package/lib/public/modules/command-palette.js +11 -22
  27. package/lib/public/modules/context-sources.js +1 -6
  28. package/lib/public/modules/filebrowser.js +25 -13
  29. package/lib/public/modules/input.js +47 -12
  30. package/lib/public/modules/mcp-ui.js +2 -2
  31. package/lib/public/modules/notifications.js +22 -1
  32. package/lib/public/modules/playbook.js +6 -6
  33. package/lib/public/modules/project-settings.js +5 -13
  34. package/lib/public/modules/qrcode.js +2 -2
  35. package/lib/public/modules/rate-limit-state.js +133 -0
  36. package/lib/public/modules/relative-time.js +27 -0
  37. package/lib/public/modules/scheduler-history.js +1 -5
  38. package/lib/public/modules/server-settings.js +28 -15
  39. package/lib/public/modules/sidebar-mobile.js +8 -3
  40. package/lib/public/modules/sidebar-projects.js +1 -1
  41. package/lib/public/modules/sidebar-sessions.js +1 -15
  42. package/lib/public/modules/sidebar.js +3 -3
  43. package/lib/public/modules/utils.js +1 -0
  44. package/lib/public/sw.js +24 -4
  45. package/lib/sdk-bridge.js +22 -0
  46. package/lib/sdk-message-processor.js +27 -1
  47. package/lib/server-auth.js +48 -2
  48. package/lib/server-palette.js +17 -1
  49. package/lib/server-settings.js +70 -4
  50. package/lib/server.js +200 -4
  51. package/lib/session-search.js +20 -5
  52. package/lib/sessions.js +84 -3
  53. package/lib/smtp.js +6 -6
  54. package/lib/yoke/adapters/codex.js +1 -1
  55. package/package.json +4 -2
  56. package/scripts/install-local-test.js +97 -0
  57. package/scripts/verify-installed-build.js +80 -0
  58. package/scripts/write-build-sha.js +53 -0
@@ -0,0 +1,849 @@
1
+ // lib/cli/menus.js
2
+ //
3
+ // Interactive CLI menus for bin/cli.js: first-run setup wizard, project
4
+ // restore prompt, and the main/settings management menus shown once the
5
+ // daemon is up. Extracted verbatim from bin/cli.js (lr-4e49 Part 1), no
6
+ // behavior change.
7
+ //
8
+ // This module and lib/cli/daemon-launch.js reference each other's exports
9
+ // (menu actions re-fork/restart the daemon; daemon-launch shows the menu
10
+ // after a successful (re)start). Both requires are done lazily inside
11
+ // functions to avoid a load-time circular-require ordering issue.
12
+
13
+ var fs = require("fs");
14
+ var path = require("path");
15
+ var crypto = require("crypto");
16
+ var qrcode = require("qrcode-terminal");
17
+
18
+ var {
19
+ loadConfig, saveConfig, socketPath, logPath, clearStaleConfig,
20
+ loadClayrc, saveClayrc, REAL_HOME,
21
+ } = require("../config");
22
+ var { sendIPCCommand } = require("../ipc");
23
+ var { hasAdmin, getSetupCode, hashPin } = require("../users");
24
+ var {
25
+ log, sym, a, printLogo,
26
+ promptToggle, promptText, promptSelect, promptMultiSelect,
27
+ } = require("./tui");
28
+ var { getLocalIP, isPortFree } = require("./net-detect");
29
+
30
+ // ==============================
31
+ // Restore projects from ~/.clagentic-rc
32
+ // ==============================
33
+ function promptRestoreProjects(projects, callback) {
34
+ log(sym.bar);
35
+ log(sym.pointer + " " + a.bold + "Previous projects found" + a.reset);
36
+ log(sym.bar + " " + a.dim + "Restore projects from your last session?" + a.reset);
37
+ log(sym.bar);
38
+
39
+ var items = projects.map(function (p) {
40
+ var name = p.title || path.basename(p.path);
41
+ return {
42
+ label: a.bold + name + a.reset + " " + a.dim + p.path + a.reset,
43
+ value: p,
44
+ checked: true,
45
+ };
46
+ });
47
+
48
+ promptMultiSelect("Restore projects", items, function (selected) {
49
+ // Remove unselected projects from ~/.clagentic-rc
50
+ if (selected.length < projects.length) {
51
+ var selectedPaths = {};
52
+ for (var si = 0; si < selected.length; si++) {
53
+ selectedPaths[selected[si].path] = true;
54
+ }
55
+ try {
56
+ var rc = loadClayrc();
57
+ rc.recentProjects = (rc.recentProjects || []).filter(function (p) {
58
+ return selectedPaths[p.path];
59
+ });
60
+ saveClayrc(rc);
61
+ } catch (e) {}
62
+ }
63
+
64
+ log(sym.bar);
65
+ if (selected.length > 0) {
66
+ log(sym.done + " " + a.green + "Restoring " + selected.length + (selected.length === 1 ? " project" : " projects") + a.reset);
67
+ } else {
68
+ log(sym.done + " " + a.dim + "Starting fresh" + a.reset);
69
+ }
70
+ log(sym.end + " " + a.dim + "Starting relay..." + a.reset);
71
+ log("");
72
+ callback(selected);
73
+ });
74
+ }
75
+
76
+ // ==============================
77
+ // First-run setup (no daemon)
78
+ // ==============================
79
+ /**
80
+ * `defaultPort` seeds the port prompt (mirrors the CLI's --port / default
81
+ * value). `cliOpts`: { host, dangerouslySkipPermissions } — preserved onto
82
+ * the config when OS-user isolation is requested without root, mirroring
83
+ * the original bin/cli.js behavior of reading those from parsed CLI flags.
84
+ */
85
+ function setup(callback, defaultPort, cliOpts) {
86
+ cliOpts = cliOpts || {};
87
+ var port = defaultPort;
88
+ console.clear();
89
+ printLogo();
90
+ log("");
91
+ log(sym.pointer + " " + a.bold + "Clagentic: Console" + a.reset + a.dim + " · Unofficial, open-source project" + a.reset);
92
+ log(sym.bar);
93
+ log(sym.bar + " " + a.yellow + sym.warn + " Disclaimer" + a.reset);
94
+ log(sym.bar);
95
+ log(sym.bar + " " + a.dim + "This is an independent project and is not affiliated with Anthropic." + a.reset);
96
+ log(sym.bar + " " + a.dim + "Claude is a trademark of Anthropic." + a.reset);
97
+ log(sym.bar);
98
+ log(sym.bar + " " + a.dim + "Clagentic: Console is provided \"as is\" without warranty of any kind. Users are" + a.reset);
99
+ log(sym.bar + " " + a.dim + "responsible for complying with the terms of service of underlying AI" + a.reset);
100
+ log(sym.bar + " " + a.dim + "providers (e.g., Anthropic, OpenAI) and all applicable terms of any" + a.reset);
101
+ log(sym.bar + " " + a.dim + "third-party services." + a.reset);
102
+ log(sym.bar);
103
+ log(sym.bar + " " + a.dim + "Features such as multi-user mode are experimental and may involve" + a.reset);
104
+ log(sym.bar + " " + a.dim + "sharing access to API-based services. Before enabling such features," + a.reset);
105
+ log(sym.bar + " " + a.dim + "review your provider's usage policies regarding account sharing," + a.reset);
106
+ log(sym.bar + " " + a.dim + "acceptable use, and any applicable rate limits or restrictions." + a.reset);
107
+ log(sym.bar);
108
+ log(sym.bar + " " + a.dim + "The authors assume no liability for misuse or violations arising" + a.reset);
109
+ log(sym.bar + " " + a.dim + "from the use of this software." + a.reset);
110
+ log(sym.bar);
111
+ log(sym.bar + " Type " + a.bold + "agree" + a.reset + " to accept and continue.");
112
+ log(sym.bar);
113
+
114
+ promptText("", "", function (val) {
115
+ if (!val || val.trim().toLowerCase() !== "agree") {
116
+ log(sym.end + " " + a.dim + "Aborted." + a.reset);
117
+ log("");
118
+ process.exit(0);
119
+ return;
120
+ }
121
+ log(sym.bar);
122
+
123
+ function askPort() {
124
+ promptText("Port", String(port), function (val) {
125
+ if (val === null) {
126
+ log(sym.end + " " + a.dim + "Aborted." + a.reset);
127
+ log("");
128
+ process.exit(0);
129
+ return;
130
+ }
131
+ var p = parseInt(val, 10);
132
+ if (!p || p < 1 || p > 65535) {
133
+ log(sym.warn + " " + a.red + "Invalid port number" + a.reset);
134
+ askPort();
135
+ return;
136
+ }
137
+ isPortFree(p).then(function (free) {
138
+ if (!free) {
139
+ log(sym.warn + " " + a.yellow + "Port " + p + " is already in use" + a.reset);
140
+ askPort();
141
+ return;
142
+ }
143
+ port = p;
144
+ log(sym.bar);
145
+ // Single-user mode removed (lr-bd9f). All new installs are multi-user.
146
+ // Existing single-user installs continue to work until the full removal
147
+ // lands — see task lr-bd9f-redux.
148
+ askOsUsers("multi");
149
+ });
150
+ });
151
+ }
152
+
153
+ function askOsUsers(mode) {
154
+ // Only offer OS user isolation on Linux
155
+ if (process.platform !== "linux") {
156
+ finishSetup(mode, false);
157
+ return;
158
+ }
159
+ log(sym.bar);
160
+ promptSelect("Enable OS-level user isolation?", [
161
+ { label: "Yes", value: "yes" },
162
+ { label: "No", value: "no" },
163
+ ], function (choice) {
164
+ if (choice !== "yes") {
165
+ finishSetup(mode, false);
166
+ return;
167
+ }
168
+ log(sym.bar);
169
+ log(sym.bar + " " + a.yellow + sym.warn + " OS-Level User Isolation" + a.reset);
170
+ log(sym.bar);
171
+ log(sym.bar + " " + a.dim + "This feature maps each Clagentic: Console user to a Linux OS user account." + a.reset);
172
+ log(sym.bar + " " + a.dim + "The daemon must run as root and will spawn processes (SDK workers," + a.reset);
173
+ log(sym.bar + " " + a.dim + "terminals, file operations) as the mapped Linux user." + a.reset);
174
+ log(sym.bar);
175
+ log(sym.bar + " " + a.dim + "What this means:" + a.reset);
176
+ log(sym.bar + " " + a.dim + "- Each mapped user uses their own ~/.claude/ credentials" + a.reset);
177
+ log(sym.bar + " " + a.dim + "- Terminals and file access follow Linux permissions" + a.reset);
178
+ log(sym.bar + " " + a.dim + "- Linux user accounts are created automatically (clay-username)" + a.reset);
179
+ log(sym.bar);
180
+ log(sym.bar + " " + a.dim + "Recommended: Run on a dedicated Clagentic: Console server or cloud instance," + a.reset);
181
+ log(sym.bar + " " + a.dim + "not on a personal computer or general-purpose server." + a.reset);
182
+ log(sym.bar);
183
+ promptSelect("Confirm", [
184
+ { label: "Enable OS-level user isolation", value: "confirm" },
185
+ { label: "Cancel", value: "cancel" },
186
+ ], function (confirmChoice) {
187
+ if (confirmChoice !== "confirm") {
188
+ finishSetup(mode, false);
189
+ return;
190
+ }
191
+ var isRoot = typeof process.getuid === "function" && process.getuid() === 0;
192
+ if (!isRoot) {
193
+ // Merge into existing config (preserve projects, TLS, etc.)
194
+ var existingCfg = loadConfig() || {};
195
+ existingCfg.port = port;
196
+ existingCfg.host = cliOpts.host;
197
+ existingCfg.mode = "multi";
198
+ existingCfg.osUsers = true;
199
+ existingCfg.setupCompleted = true;
200
+ if (cliOpts.dangerouslySkipPermissions) existingCfg.dangerouslySkipPermissions = true;
201
+ saveConfig(existingCfg);
202
+ log(sym.bar);
203
+ log(sym.warn + " " + a.yellow + "OS user isolation requires root." + a.reset);
204
+ log(sym.bar + " Run:");
205
+ log(sym.bar + " " + a.bold + "sudo npx @clagentic/console" + a.reset);
206
+ log(sym.end);
207
+ log("");
208
+ process.exit(0);
209
+ return;
210
+ }
211
+ finishSetup(mode, true);
212
+ });
213
+ });
214
+ }
215
+
216
+ function finishSetup(mode, wantOsUsers) {
217
+ if (process.platform === "darwin") {
218
+ log(sym.bar);
219
+ promptToggle("Keep awake", "Prevent system sleep while relay is running", false, function (keepAwake) {
220
+ callback(mode, keepAwake, wantOsUsers, port);
221
+ });
222
+ } else {
223
+ callback(mode, false, wantOsUsers, port);
224
+ }
225
+ }
226
+
227
+ askPort();
228
+ });
229
+ }
230
+
231
+ // ==============================
232
+ // Show server started info
233
+ // ==============================
234
+ function showServerStarted(config, ip, setupCode) {
235
+ showMainMenu(config, ip, setupCode);
236
+ }
237
+
238
+ // ==============================
239
+ // Main management menu
240
+ // ==============================
241
+ function showMainMenu(config, ip, setupCode) {
242
+ var { startDaemonWatcher } = require("./daemon-launch");
243
+ startDaemonWatcher();
244
+ var protocol = config.tls ? "https" : "http";
245
+ var url = protocol + "://" + ip + ":" + config.port;
246
+ var currentVersion = require("../../package.json").version;
247
+
248
+ sendIPCCommand(socketPath(), { cmd: "get_status" }).then(function (status) {
249
+ var projs = (status && status.projects) || [];
250
+ var totalSessions = 0;
251
+ var totalAwaiting = 0;
252
+ for (var i = 0; i < projs.length; i++) {
253
+ totalSessions += projs[i].sessions || 0;
254
+ if (projs[i].isProcessing) totalAwaiting++;
255
+ }
256
+
257
+ console.clear();
258
+ printLogo();
259
+ log("");
260
+
261
+ function afterQr() {
262
+ // Status line
263
+ log(" " + a.dim + "Clagentic: Console" + a.reset + " " + a.dim + "v" + currentVersion + a.reset + a.dim + " — " + url + a.reset);
264
+ var parts = [];
265
+ parts.push(a.bold + projs.length + a.reset + a.dim + (projs.length === 1 ? " project" : " projects"));
266
+ parts.push(a.reset + a.bold + totalSessions + a.reset + a.dim + (totalSessions === 1 ? " session" : " sessions"));
267
+ if (totalAwaiting > 0) {
268
+ parts.push(a.reset + a.yellow + a.bold + totalAwaiting + a.reset + a.yellow + " awaiting" + a.reset + a.dim);
269
+ }
270
+ log(" " + a.dim + parts.join(a.reset + a.dim + " · ") + a.reset);
271
+ log(" " + a.dim + "~/.clagentic → " + path.join(REAL_HOME, ".clagentic") + a.reset);
272
+ log(" Press " + a.bold + "o" + a.reset + " to open in browser");
273
+ log("");
274
+
275
+ showMenuItems();
276
+ }
277
+
278
+ if (ip !== "localhost") {
279
+ qrcode.generate(url, { small: !require("./tui").isBasicTerm }, function (code) {
280
+ var lines = code.split("\n").map(function (l) { return " " + l; }).join("\n");
281
+ console.log(lines);
282
+ afterQr();
283
+ });
284
+ } else {
285
+ log(a.bold + " " + url + a.reset);
286
+ log("");
287
+ afterQr();
288
+ }
289
+
290
+ function showMenuItems() {
291
+ var items = [
292
+ { label: "Settings", value: "settings" },
293
+ { label: "Shut down server", value: "shutdown" },
294
+ { label: "Keep server alive & exit", value: "exit" },
295
+ ];
296
+
297
+ promptSelect("What would you like to do?", items, function (choice) {
298
+ switch (choice) {
299
+ case "settings":
300
+ showSettingsMenu(config, ip);
301
+ break;
302
+
303
+ case "shutdown":
304
+ log(sym.bar);
305
+ log(sym.bar + " " + a.yellow + "This will stop the server completely." + a.reset);
306
+ log(sym.bar + " " + a.dim + "All connected sessions will be disconnected." + a.reset);
307
+ log(sym.bar);
308
+ promptSelect("Are you sure?", [
309
+ { label: "Cancel", value: "cancel" },
310
+ { label: "Shut down", value: "confirm" },
311
+ ], function (confirm) {
312
+ if (confirm === "confirm") {
313
+ var { stopDaemonWatcher } = require("./daemon-launch");
314
+ stopDaemonWatcher();
315
+ sendIPCCommand(socketPath(), { cmd: "shutdown" }).then(function () {
316
+ log(sym.done + " " + a.green + "Server stopped." + a.reset);
317
+ log("");
318
+ clearStaleConfig();
319
+ process.exit(0);
320
+ });
321
+ } else {
322
+ showMainMenu(config, ip);
323
+ }
324
+ });
325
+ break;
326
+
327
+ case "exit":
328
+ log("");
329
+ log(" " + a.bold + "Bye!" + a.reset + " " + a.dim + "Server is still running in background." + a.reset);
330
+ log(" " + a.dim + "Run " + a.reset + "npx @clagentic/console" + a.dim + " to come back here." + a.reset);
331
+ log("");
332
+ process.exit(0);
333
+ break;
334
+ }
335
+ }, {
336
+ hint: [
337
+ "Run npx @clagentic/console in other directories to add more projects.", ],
338
+ keys: [
339
+ { key: "o", onKey: function () {
340
+ openUrl(url);
341
+ showMainMenu(config, ip);
342
+ }},
343
+ { key: "s", onKey: function () {
344
+ openUrl("https://github.com/clagentic/clagentic-console");
345
+ showMainMenu(config, ip);
346
+ }},
347
+ ],
348
+ });
349
+ }
350
+ });
351
+ }
352
+
353
+ /**
354
+ * Open a URL in the platform default browser (best-effort, ignores errors).
355
+ * Used by the "o" / "s" hotkeys in showMainMenu.
356
+ */
357
+ function openUrl(url) {
358
+ var spawn = require("child_process").spawn;
359
+ try {
360
+ if (process.platform === "win32") {
361
+ spawn("cmd", ["/c", "start", "", url], { stdio: "ignore", detached: true, windowsHide: true }).unref();
362
+ } else {
363
+ var cmd = process.platform === "darwin" ? "open" : "xdg-open";
364
+ spawn(cmd, [url], { stdio: "ignore", detached: true }).unref();
365
+ }
366
+ } catch (e) {}
367
+ }
368
+
369
+ // ==============================
370
+ // Settings sub-menu
371
+ // ==============================
372
+ function showSettingsMenu(config, ip) {
373
+ var cwd = process.cwd();
374
+ sendIPCCommand(socketPath(), { cmd: "get_status" }).then(function (status) {
375
+ var isAwake = status && status.keepAwake;
376
+ var isOsUsers = status && status.osUsers;
377
+
378
+ console.clear();
379
+ printLogo();
380
+ log("");
381
+ log(sym.pointer + " " + a.bold + "Settings" + a.reset);
382
+ log(sym.bar);
383
+
384
+ // Detect current state
385
+ var tlsStatus = config.tls
386
+ ? a.green + "Enabled" + a.reset
387
+ : a.dim + "Disabled" + a.reset;
388
+ var pinStatus = config.pinHash
389
+ ? a.green + "Enabled" + a.reset
390
+ : a.dim + "Off" + a.reset;
391
+ var awakeStatus = isAwake
392
+ ? a.green + "On" + a.reset
393
+ : a.dim + "Off" + a.reset;
394
+
395
+ log(sym.bar + " HTTPS " + tlsStatus);
396
+
397
+ // Multi-user mode is always on (single-user mode was removed, lr-ec2d).
398
+ log(sym.bar + " Mode " + a.indigo + "Multi-user" + a.reset);
399
+ log(sym.bar + " PIN " + pinStatus);
400
+ var osUsersStatus = isOsUsers
401
+ ? a.green + "Enabled" + a.reset
402
+ : a.dim + "Off" + a.reset;
403
+ log(sym.bar + " OS users " + osUsersStatus);
404
+ if (process.platform === "darwin") {
405
+ log(sym.bar + " Keep awake " + awakeStatus);
406
+ }
407
+ log(sym.bar);
408
+
409
+ // Build items
410
+ var items = [];
411
+
412
+ if (isOsUsers) {
413
+ items.push({ label: "Disable OS-level user isolation", value: "disable_os_users" });
414
+ } else {
415
+ items.push({ label: "Enable OS-level user isolation", value: "os_users" });
416
+ }
417
+ items.push({ label: "Show setup code", value: "show_setup_code" });
418
+ if (hasAdmin()) {
419
+ items.push({ label: "Recover admin password", value: "recover_admin" });
420
+ }
421
+ if (process.platform === "darwin") {
422
+ items.push({ label: isAwake ? "Disable keep awake" : "Enable keep awake", value: "awake" });
423
+ }
424
+ items.push({ label: "View logs", value: "logs" });
425
+ items.push({ label: "Re-run setup wizard", value: "rerun_setup" });
426
+ items.push({ label: "Back", value: "back" });
427
+
428
+ promptSelect("Select", items, function (choice) {
429
+ switch (choice) {
430
+ case "pin": {
431
+ var { promptPin } = require("./tui");
432
+ log(sym.bar);
433
+ promptPin(function (pin) {
434
+ if (pin) {
435
+ var hash = hashPin(pin);
436
+ sendIPCCommand(socketPath(), { cmd: "set_pin", pinHash: hash }).then(function () {
437
+ config.pinHash = hash;
438
+ log(sym.done + " " + a.green + "PIN updated" + a.reset);
439
+ log("");
440
+ showSettingsMenu(config, ip);
441
+ });
442
+ } else {
443
+ showSettingsMenu(config, ip);
444
+ }
445
+ });
446
+ break;
447
+ }
448
+
449
+ case "remove_pin":
450
+ sendIPCCommand(socketPath(), { cmd: "set_pin", pinHash: null }).then(function () {
451
+ config.pinHash = null;
452
+ log(sym.done + " " + a.dim + "PIN removed" + a.reset);
453
+ log("");
454
+ showSettingsMenu(config, ip);
455
+ });
456
+ break;
457
+
458
+ case "os_users":
459
+ if (process.platform === "win32") {
460
+ log(sym.bar);
461
+ log(sym.bar + " " + a.red + "OS-level user isolation is not supported on Windows." + a.reset);
462
+ log(sym.bar);
463
+ promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
464
+ showSettingsMenu(config, ip);
465
+ });
466
+ break;
467
+ }
468
+ if (process.getuid() !== 0) {
469
+ log(sym.bar);
470
+ log(sym.bar + " " + a.red + sym.warn + " OS user isolation requires root." + a.reset);
471
+ log(sym.bar + " " + a.dim + "Shut down this server, then restart with:" + a.reset);
472
+ log(sym.bar + " " + a.bold + "sudo npx @clagentic/console" + a.reset);
473
+ log(sym.bar);
474
+ promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
475
+ showSettingsMenu(config, ip);
476
+ });
477
+ break;
478
+ }
479
+ if (process.platform !== "linux") {
480
+ log(sym.bar);
481
+ log(sym.bar + " " + a.red + sym.warn + " OS-level user isolation requires Linux." + a.reset);
482
+ log(sym.bar + " " + a.dim + "This feature depends on setfacl, getent, and uid/gid process spawning." + a.reset);
483
+ log(sym.bar + " " + a.dim + "Use Docker or a Linux VM to run Clagentic: Console with OS user isolation." + a.reset);
484
+ log(sym.bar);
485
+ showSettingsMenu(config, ip);
486
+ return;
487
+ }
488
+ log(sym.bar);
489
+ log(sym.bar + " " + a.yellow + sym.warn + " OS-Level User Isolation" + a.reset);
490
+ log(sym.bar);
491
+ log(sym.bar + " " + a.dim + "This feature maps each Clagentic: Console user to a Linux OS user account." + a.reset);
492
+ log(sym.bar + " " + a.dim + "The daemon must run as root and will spawn processes (SDK workers," + a.reset);
493
+ log(sym.bar + " " + a.dim + "terminals, file operations) as the mapped Linux user." + a.reset);
494
+ log(sym.bar);
495
+ log(sym.bar + " " + a.dim + "What this means:" + a.reset);
496
+ log(sym.bar + " " + a.dim + "- Each mapped user uses their own ~/.claude/ credentials" + a.reset);
497
+ log(sym.bar + " " + a.dim + "- Terminals and file access follow Linux permissions" + a.reset);
498
+ log(sym.bar + " " + a.dim + "- Linux user accounts are created automatically (clay-username)" + a.reset);
499
+ log(sym.bar);
500
+ log(sym.bar + " " + a.dim + "Recommended: Run on a dedicated Clagentic: Console server or cloud instance," + a.reset);
501
+ log(sym.bar + " " + a.dim + "not on a personal computer or general-purpose server." + a.reset);
502
+ log(sym.bar);
503
+ promptSelect("Select", [
504
+ { label: "Enable OS-level user isolation", value: "confirm" },
505
+ { label: "Cancel", value: "cancel" },
506
+ ], function (confirmChoice) {
507
+ if (confirmChoice === "confirm") {
508
+ sendIPCCommand(socketPath(), { cmd: "set_os_users", value: true }).then(function (res) {
509
+ if (res.error === "acl_not_installed") {
510
+ log(sym.bar);
511
+ log(sym.bar + " " + a.red + sym.warn + " setfacl is not installed." + a.reset);
512
+ log(sym.bar);
513
+ log(sym.bar + " OS user isolation requires the ACL (Access Control List) package");
514
+ log(sym.bar + " to manage per-user file permissions on shared projects.");
515
+ log(sym.bar);
516
+ log(sym.bar + " " + a.bold + "Install it:" + a.reset);
517
+ log(sym.bar + " " + a.cyan + res.installCmd + a.reset);
518
+ log(sym.bar);
519
+ log(sym.bar + " " + a.dim + "Then try enabling OS user isolation again." + a.reset);
520
+ log(sym.bar);
521
+ showSettingsMenu(config, ip);
522
+ return;
523
+ } else if (res.error) {
524
+ log(sym.bar);
525
+ log(sym.bar + " " + a.red + sym.warn + " Failed to enable OS users: " + res.error + a.reset);
526
+ log(sym.bar);
527
+ showSettingsMenu(config, ip);
528
+ return;
529
+ } else if (!res.ok) {
530
+ log(sym.bar);
531
+ log(sym.bar + " " + a.red + sym.warn + " Unexpected response from daemon." + a.reset);
532
+ log(sym.bar + " " + a.dim + JSON.stringify(res) + a.reset);
533
+ log(sym.bar);
534
+ showSettingsMenu(config, ip);
535
+ return;
536
+ }
537
+ // Daemon saved the flag. Now provision from CLI with live progress.
538
+ config.osUsers = true;
539
+ log(sym.bar);
540
+ log(sym.done + " " + a.green + "OS-level user isolation enabled." + a.reset);
541
+ log(sym.bar);
542
+
543
+ // Provision Linux accounts from CLI (we have root + terminal)
544
+ var osUsersLib = require("../os-users");
545
+ var usersLib = require("../users");
546
+
547
+ try { osUsersLib.ensureProjectsDir(); } catch (e) {
548
+ log(sym.bar + " " + a.yellow + sym.warn + " Failed to create projects dir: " + e.message + a.reset);
549
+ }
550
+
551
+ var allUsers = usersLib.getAllUsers();
552
+ if (allUsers.length === 0) {
553
+ log(sym.bar + " " + a.dim + "No users to provision yet. Accounts will be created when users register." + a.reset);
554
+ } else {
555
+ log(sym.bar + " " + a.dim + "Provisioning " + allUsers.length + " user(s)..." + a.reset);
556
+ for (var ui = 0; ui < allUsers.length; ui++) {
557
+ var usr = allUsers[ui];
558
+ if (usr.linuxUser && osUsersLib.linuxUserExists(usr.linuxUser)) {
559
+ log(sym.bar + " " + a.dim + sym.done + " " + usr.username + " -> " + usr.linuxUser + " (exists)" + a.reset);
560
+ continue;
561
+ }
562
+ log(sym.bar + " " + a.dim + "Creating Linux account for " + usr.username + "..." + a.reset);
563
+ var provision = osUsersLib.provisionLinuxUser(usr.username);
564
+ if (provision.ok) {
565
+ usersLib.updateLinuxUser(usr.id, provision.linuxUser);
566
+ log(sym.bar + " " + a.green + sym.done + " " + usr.username + " -> " + provision.linuxUser + a.reset);
567
+ } else {
568
+ log(sym.bar + " " + a.red + sym.warn + " " + usr.username + ": " + (provision.error || "unknown error") + a.reset);
569
+ }
570
+ }
571
+ }
572
+
573
+ // Set up ACLs for existing projects
574
+ var cfg = loadConfig() || {};
575
+ var cfgProjects = cfg.projects || [];
576
+ if (cfgProjects.length > 0) {
577
+ log(sym.bar);
578
+ log(sym.bar + " " + a.dim + "Setting ACLs for " + cfgProjects.length + " project(s)..." + a.reset);
579
+ for (var pi = 0; pi < cfgProjects.length; pi++) {
580
+ var proj = cfgProjects[pi];
581
+ if (osUsersLib.isHomeDirectory(proj.path)) {
582
+ log(sym.bar + " " + a.dim + "~ " + (proj.slug || proj.path) + " (home dir, skipped)" + a.reset);
583
+ continue;
584
+ }
585
+ try {
586
+ if (proj.visibility === "public") {
587
+ osUsersLib.grantAllUsersAccess(proj.path, usersLib);
588
+ }
589
+ if (proj.ownerId) {
590
+ var ownerUser = usersLib.findUserById(proj.ownerId);
591
+ if (ownerUser && ownerUser.linuxUser) {
592
+ osUsersLib.grantProjectAccess(proj.path, ownerUser.linuxUser);
593
+ }
594
+ }
595
+ log(sym.bar + " " + a.dim + sym.done + " " + (proj.slug || proj.path) + a.reset);
596
+ } catch (aclErr) {
597
+ log(sym.bar + " " + a.yellow + sym.warn + " " + (proj.slug || proj.path) + ": " + aclErr.message + a.reset);
598
+ }
599
+ }
600
+ }
601
+
602
+ log(sym.bar);
603
+ log(sym.bar + " " + a.dim + "Restart the daemon for full effect." + a.reset);
604
+ log(sym.bar);
605
+ showSettingsMenu(config, ip);
606
+ }).catch(function (err) {
607
+ log(sym.bar);
608
+ log(sym.bar + " " + a.red + sym.warn + " IPC error: " + (err.message || err) + a.reset);
609
+ log(sym.bar);
610
+ showSettingsMenu(config, ip);
611
+ });
612
+ } else {
613
+ showSettingsMenu(config, ip);
614
+ }
615
+ });
616
+ break;
617
+
618
+ case "disable_os_users":
619
+ log(sym.bar);
620
+ log(sym.bar + " " + a.yellow + sym.warn + " Disable OS-level user isolation?" + a.reset);
621
+ log(sym.bar);
622
+ log(sym.bar + " " + a.dim + "Processes will no longer be spawned as mapped Linux users." + a.reset);
623
+ log(sym.bar + " " + a.dim + "User mappings will be preserved and restored if re-enabled." + a.reset);
624
+ log(sym.bar);
625
+ promptSelect("Confirm", [
626
+ { label: "Disable OS-level user isolation", value: "confirm" },
627
+ { label: "Cancel", value: "cancel" },
628
+ ], function (confirmChoice) {
629
+ if (confirmChoice === "confirm") {
630
+ sendIPCCommand(socketPath(), { cmd: "set_os_users", value: false }).then(function (res) {
631
+ if (res.ok) {
632
+ config.osUsers = false;
633
+ log(sym.bar);
634
+ log(sym.done + " " + a.green + "OS-level user isolation disabled." + a.reset);
635
+ log(sym.bar + " " + a.dim + "Restart the daemon for changes to take full effect." + a.reset);
636
+ log(sym.bar);
637
+ }
638
+ showSettingsMenu(config, ip);
639
+ });
640
+ } else {
641
+ showSettingsMenu(config, ip);
642
+ }
643
+ });
644
+ break;
645
+
646
+ case "rerun_setup":
647
+ log(sym.bar);
648
+ log(sym.bar + " " + a.yellow + sym.warn + " Re-run setup wizard?" + a.reset);
649
+ log(sym.bar);
650
+ log(sym.bar + " " + a.dim + "This will shut down the running daemon, reset your setup" + a.reset);
651
+ log(sym.bar + " " + a.dim + "preferences (mode, port), and walk you through the wizard again." + a.reset);
652
+ log(sym.bar + " " + a.dim + "Your projects and user accounts will be preserved." + a.reset);
653
+ log(sym.bar);
654
+ promptSelect("Confirm", [
655
+ { label: "Re-run setup wizard", value: "confirm" },
656
+ { label: "Cancel", value: "cancel" },
657
+ ], function (confirmChoice) {
658
+ if (confirmChoice === "confirm") {
659
+ var { forkDaemon, getCliOpts } = require("./daemon-launch");
660
+ // Save old PID before clearing, so we can force-kill if needed
661
+ var cfg = loadConfig() || {};
662
+ var oldPid = cfg.pid;
663
+ var oldPort = cfg.port || config.port;
664
+ // Clear setupCompleted so setup() runs fresh
665
+ delete cfg.setupCompleted;
666
+ delete cfg.mode;
667
+ cfg.pid = null;
668
+ saveConfig(cfg);
669
+
670
+ // Helper: wait for port to be free, force-kill if needed
671
+ function waitForPortFree(cb) {
672
+ var attempts = 0;
673
+ var maxAttempts = 12; // 6 seconds total
674
+ function check() {
675
+ isPortFree(oldPort).then(function (free) {
676
+ if (free) return cb();
677
+ attempts++;
678
+ if (attempts >= maxAttempts) {
679
+ // Port still busy, force-kill old daemon
680
+ if (oldPid) {
681
+ try { process.kill(oldPid, "SIGKILL"); } catch (e) {}
682
+ }
683
+ // Wait a bit more after SIGKILL
684
+ setTimeout(function () {
685
+ isPortFree(oldPort).then(function (free2) {
686
+ if (!free2) {
687
+ log(sym.warn + " " + a.yellow + "Port " + oldPort + " still in use. Kill the process manually:" + a.reset);
688
+ log(sym.bar + " " + a.bold + "lsof -ti:" + oldPort + " | xargs kill -9" + a.reset);
689
+ }
690
+ cb();
691
+ });
692
+ }, 1000);
693
+ return;
694
+ }
695
+ setTimeout(check, 500);
696
+ });
697
+ }
698
+ check();
699
+ }
700
+
701
+ // Helper: run setup wizard after daemon is dead
702
+ function proceedWithSetup() {
703
+ clearStaleConfig();
704
+ setup(function (mode, keepAwake, wantOsUsers, chosenPort) {
705
+ var rc = loadClayrc();
706
+ var restorable = (rc.recentProjects || []).filter(function (p) {
707
+ return p.path !== cwd && fs.existsSync(p.path);
708
+ });
709
+ // Reuse the CLI's original flags (--no-https, --local-cert, etc.)
710
+ // rather than assuming defaults — this is a re-fork, not a fresh
711
+ // process, so bin/cli.js's parsed argv is the source of truth.
712
+ var cliOpts = Object.assign({}, getCliOpts(), { port: chosenPort, cwd: cwd });
713
+ if (restorable.length > 0) {
714
+ promptRestoreProjects(restorable, function (selected) {
715
+ forkDaemon(mode, keepAwake, selected, false, wantOsUsers, cliOpts);
716
+ });
717
+ } else {
718
+ log(sym.bar);
719
+ log(sym.end + " " + a.dim + "Starting relay..." + a.reset);
720
+ log("");
721
+ forkDaemon(mode, keepAwake, undefined, true, wantOsUsers, cliOpts);
722
+ }
723
+ }, oldPort);
724
+ }
725
+
726
+ // Shut down the daemon, then wait for port to be free
727
+ sendIPCCommand(socketPath(), { cmd: "shutdown" }).then(function () {
728
+ waitForPortFree(proceedWithSetup);
729
+ }).catch(function () {
730
+ // IPC failed, daemon may be unresponsive. Try SIGTERM, then wait.
731
+ if (oldPid) {
732
+ try { process.kill(oldPid, "SIGTERM"); } catch (e) {}
733
+ }
734
+ waitForPortFree(proceedWithSetup);
735
+ });
736
+ } else {
737
+ showSettingsMenu(config, ip);
738
+ }
739
+ });
740
+ break;
741
+
742
+ case "show_setup_code":
743
+ // getSetupCode() auto-generates if multi-user is on and no code exists
744
+ var currentCode = getSetupCode();
745
+ log(sym.bar);
746
+ if (currentCode) {
747
+ log(sym.bar + " " + a.yellow + sym.warn + " Setup code: " + a.bold + currentCode + a.reset);
748
+ if (hasAdmin()) {
749
+ log(sym.bar + " " + a.dim + "Admin account exists. This code is for adding the next admin." + a.reset);
750
+ } else {
751
+ log(sym.bar + " " + a.dim + "Enter this code in the browser to create the admin account." + a.reset);
752
+ }
753
+ } else {
754
+ log(sym.bar + " " + a.dim + "Multi-user mode is not enabled." + a.reset);
755
+ }
756
+ log(sym.bar);
757
+ promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
758
+ showSettingsMenu(config, ip);
759
+ });
760
+ break;
761
+
762
+ case "logs":
763
+ console.clear();
764
+ log(a.bold + "Daemon logs" + a.reset + " " + a.dim + "(" + logPath() + ")" + a.reset);
765
+ log("");
766
+ try {
767
+ var logContent = fs.readFileSync(logPath(), "utf8");
768
+ var logLines = logContent.split("\n").slice(-30);
769
+ for (var li = 0; li < logLines.length; li++) {
770
+ log(a.dim + logLines[li] + a.reset);
771
+ }
772
+ } catch (e) {
773
+ log(a.dim + "(empty)" + a.reset);
774
+ }
775
+ log("");
776
+ promptSelect("Back?", [{ label: "Back", value: "back" }], function () {
777
+ showSettingsMenu(config, ip);
778
+ });
779
+ break;
780
+
781
+ case "awake":
782
+ sendIPCCommand(socketPath(), { cmd: "set_keep_awake", value: !isAwake }).then(function (res) {
783
+ if (res.ok) {
784
+ config.keepAwake = !isAwake;
785
+ }
786
+ showSettingsMenu(config, ip);
787
+ });
788
+ break;
789
+
790
+ case "recover_admin": {
791
+ var recoveryUrlPath = crypto.randomBytes(16).toString("hex");
792
+ var recoveryPassword = crypto.randomBytes(8).toString("base64url");
793
+ sendIPCCommand(socketPath(), { cmd: "enable_recovery", urlPath: recoveryUrlPath, password: recoveryPassword }).then(function (res) {
794
+ if (!res.ok) {
795
+ log(sym.bar + " " + a.red + "Failed to enable recovery mode." + a.reset);
796
+ log(sym.bar);
797
+ showSettingsMenu(config, ip);
798
+ return;
799
+ }
800
+ var protocol = config.tls ? "https" : "http";
801
+ var recoveryUrl = config.builtinCert
802
+ ? toClayStudioUrl(ip, config.port, protocol) + "/recover/" + recoveryUrlPath
803
+ : protocol + "://" + ip + ":" + config.port + "/recover/" + recoveryUrlPath;
804
+ log(sym.bar);
805
+ log(sym.bar + " " + a.yellow + sym.warn + " Admin Password Recovery" + a.reset);
806
+ log(sym.bar);
807
+ log(sym.bar + " " + a.dim + "Recovery URL:" + a.reset);
808
+ log(sym.bar + " " + a.bold + recoveryUrl + a.reset);
809
+ log(sym.bar);
810
+ log(sym.bar + " " + a.dim + "Recovery password:" + a.reset);
811
+ log(sym.bar + " " + a.bold + recoveryPassword + a.reset);
812
+ log(sym.bar);
813
+ log(sym.bar + " " + a.dim + "Open the URL in a browser and enter the password above." + a.reset);
814
+ log(sym.bar + " " + a.dim + "This link is single-use and will expire when the PIN is reset." + a.reset);
815
+ log(sym.bar);
816
+ promptSelect("Done?", [
817
+ { label: "Disable recovery link", value: "disable" },
818
+ { label: "Back (keep link active)", value: "back" },
819
+ ], function (rc) {
820
+ if (rc === "disable") {
821
+ sendIPCCommand(socketPath(), { cmd: "disable_recovery" }).then(function () {
822
+ log(sym.done + " " + a.dim + "Recovery link disabled." + a.reset);
823
+ log("");
824
+ showSettingsMenu(config, ip);
825
+ });
826
+ } else {
827
+ showSettingsMenu(config, ip);
828
+ }
829
+ });
830
+ });
831
+ break;
832
+ }
833
+
834
+ case "back":
835
+ showMainMenu(config, ip);
836
+ break;
837
+ }
838
+ });
839
+ });
840
+ }
841
+
842
+ module.exports = {
843
+ promptRestoreProjects: promptRestoreProjects,
844
+ setup: setup,
845
+ showServerStarted: showServerStarted,
846
+ showMainMenu: showMainMenu,
847
+ showSettingsMenu: showSettingsMenu,
848
+ openUrl: openUrl,
849
+ };