@bakapiano/ccsm 0.21.5 → 0.22.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/lib/config.js CHANGED
@@ -91,6 +91,13 @@ const DEFAULTS = {
91
91
  // remote browsers' approval records — stable. `devtunnel delete <id>`
92
92
  // is invoked when the user explicitly rotates via the Reset button.
93
93
  devtunnel: { tunnelId: null },
94
+ // Provider-agnostic tunnel prefs. When autoStart is on, the backend
95
+ // brings the tunnel up during its own startup (server.js boot hook) —
96
+ // NOT an OS-level autostart. token is persisted so share URLs survive
97
+ // a backend restart; it's written ONLY while autoStart is on and is
98
+ // stripped from /api/config so remote devices can't read it. provider
99
+ // is 'devtunnel' | 'cloudflared'.
100
+ tunnel: { autoStart: false, provider: null, token: null },
94
101
  };
95
102
 
96
103
  function ensureDataDir() {
@@ -122,9 +129,10 @@ migrateLegacyDataIfNeeded();
122
129
  // object so callers don't mutate DEFAULTS.
123
130
  function mergeWithDefaults(partial) {
124
131
  const out = { ...DEFAULTS, ...partial };
125
- // Deep-merge devtunnel so a partial save (just .tunnelId) doesn't
126
- // wipe sibling keys we may add later.
132
+ // Deep-merge devtunnel + tunnel so a partial save (just .tunnelId, or
133
+ // just .autoStart) doesn't wipe sibling keys.
127
134
  out.devtunnel = { ...DEFAULTS.devtunnel, ...(partial?.devtunnel || {}) };
135
+ out.tunnel = { ...DEFAULTS.tunnel, ...(partial?.tunnel || {}) };
128
136
  // Drop v0.x keys that the new architecture doesn't use.
129
137
  delete out.terminal;
130
138
  delete out.commandShell;
@@ -237,7 +245,19 @@ async function saveConfig(partial) {
237
245
  ensureDataDir();
238
246
  return withFileLock(CONFIG_PATH, async () => {
239
247
  const current = await loadConfig();
240
- const next = mergeWithDefaults({ ...current, ...partial });
248
+ // mergeWithDefaults re-merges nested objects (devtunnel, tunnel)
249
+ // against DEFAULTS only, so a partial save like
250
+ // saveConfig({ tunnel: { autoStart: true } }) would reset the
251
+ // sibling token/provider back to defaults. Pre-merge the nested
252
+ // blocks against `current` so a partial update preserves siblings.
253
+ const merged = { ...current, ...partial };
254
+ if (partial && partial.devtunnel) {
255
+ merged.devtunnel = { ...current.devtunnel, ...partial.devtunnel };
256
+ }
257
+ if (partial && partial.tunnel) {
258
+ merged.tunnel = { ...current.tunnel, ...partial.tunnel };
259
+ }
260
+ const next = mergeWithDefaults(merged);
241
261
  await atomicWriteJson(CONFIG_PATH, next);
242
262
  return next;
243
263
  });
package/lib/tunnel.js CHANGED
@@ -80,6 +80,12 @@ const PROVIDERS = {
80
80
  // In-memory state. Single tunnel at a time — switching providers tears
81
81
  // down the old one first.
82
82
  let current = null; // { provider, child, url, startedAt, log: string[] }
83
+ let starting = false; // True while start() is mid-spawn. devtunnel does
84
+ // ~10-20s of async create/configure BEFORE `current`
85
+ // is assigned, so the `if (current)` guard alone
86
+ // can't stop a second concurrent start() (boot
87
+ // auto-start racing a manual click) from spawning a
88
+ // duplicate child. This flag closes that window.
83
89
  let token = null; // Remote-access bearer token. Null = no remote
84
90
  // access enforced. Set via setToken() or by the
85
91
  // start() call. Server.js middleware reads via
@@ -319,6 +325,10 @@ function probeCachedSWR() {
319
325
  }
320
326
 
321
327
  async function status() {
328
+ // One config read serves both the persisted tunnelId and the
329
+ // auto-start prefs the Remote page's toggle reflects.
330
+ let cfg = null;
331
+ try { cfg = await loadConfig(); } catch { /* fall back to nulls below */ }
322
332
  return {
323
333
  providers: probeCachedSWR(),
324
334
  running: !!current,
@@ -328,10 +338,11 @@ async function status() {
328
338
  pid: current?.child?.pid || null,
329
339
  log: current?.log?.slice(-50) || [],
330
340
  token,
331
- tunnelId: current?.tunnelId || (await (async () => {
332
- try { return (await loadConfig())?.devtunnel?.tunnelId || null; }
333
- catch { return null; }
334
- })()),
341
+ tunnelId: current?.tunnelId || cfg?.devtunnel?.tunnelId || null,
342
+ // Persisted auto-start prefs surfaced so the toggle renders the
343
+ // right state across reloads (the page already polls /status).
344
+ autoStart: cfg?.tunnel?.autoStart ?? false,
345
+ autoStartProvider: cfg?.tunnel?.provider ?? null,
335
346
  // Token is echoed back so the Remote page can render the
336
347
  // pre-built share URL. The route itself is token-protected
337
348
  // (the middleware blocks non-loopback callers without it), so
@@ -487,36 +498,51 @@ function clearDevtunnelLogin() {
487
498
  // installed, the provider is unknown, or another tunnel is running.
488
499
  async function start({ provider, port }) {
489
500
  if (current) throw new Error('tunnel already running');
501
+ if (starting) throw new Error('tunnel is already starting');
490
502
  const p = PROVIDERS[provider];
491
503
  if (!p) throw new Error(`unknown provider: ${provider}`);
492
- const exe = await findBinary(provider);
493
- if (!exe) throw new Error(`${p.label} is not installed`);
494
- if (provider === 'devtunnel') {
495
- const { loggedIn } = await checkDevtunnelLogin(exe);
496
- if (!loggedIn) throw new Error('devtunnel requires login — run `devtunnel user login` first');
497
- }
498
504
 
499
- // Resolve devtunnel's persistent id BEFORE building args so the
500
- // CLI is invoked with `host <id>` and the public URL stays stable
501
- // across restarts. Cloudflared has no equivalent here quick
502
- // tunnels always rotate URLs and named tunnels require a CF
503
- // account + DNS setup that's outside ccsm's scope.
504
- let tunnelId = null;
505
- if (provider === 'devtunnel') {
506
- tunnelId = await ensureDevtunnelTunnelId(exe);
507
- if (tunnelId) {
508
- // Make sure the port is in the tunnel's port list and anonymous
509
- // access is enabled. `devtunnel host <id>` doesn't accept port /
510
- // access flags after the tunnel exists, so this has to be done
511
- // out of band before host.
512
- await configureDevtunnelTunnel(exe, tunnelId, port);
505
+ // Hold the `starting` flag across the async setup below. devtunnel's
506
+ // create/configure can take 10-20s, all of it BEFORE `current` is
507
+ // assigned without this flag a second start() (boot auto-start vs.
508
+ // a manual click) would slip past the `if (current)` guard and spawn
509
+ // a duplicate child. Cleared the moment `current` is set, after which
510
+ // the `if (current)` guard alone is sufficient.
511
+ starting = true;
512
+ let entry;
513
+ let child;
514
+ try {
515
+ const exe = await findBinary(provider);
516
+ if (!exe) throw new Error(`${p.label} is not installed`);
517
+ if (provider === 'devtunnel') {
518
+ const { loggedIn } = await checkDevtunnelLogin(exe);
519
+ if (!loggedIn) throw new Error('devtunnel requires login — run `devtunnel user login` first');
513
520
  }
514
- }
515
521
 
516
- const args = p.args(port, { tunnelId });
517
- const child = spawn(exe, args, { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
518
- const entry = { provider, child, url: null, startedAt: Date.now(), log: [], tunnelId };
519
- current = entry;
522
+ // Resolve devtunnel's persistent id BEFORE building args so the
523
+ // CLI is invoked with `host <id>` and the public URL stays stable
524
+ // across restarts. Cloudflared has no equivalent here quick
525
+ // tunnels always rotate URLs and named tunnels require a CF
526
+ // account + DNS setup that's outside ccsm's scope.
527
+ let tunnelId = null;
528
+ if (provider === 'devtunnel') {
529
+ tunnelId = await ensureDevtunnelTunnelId(exe);
530
+ if (tunnelId) {
531
+ // Make sure the port is in the tunnel's port list and anonymous
532
+ // access is enabled. `devtunnel host <id>` doesn't accept port /
533
+ // access flags after the tunnel exists, so this has to be done
534
+ // out of band before host.
535
+ await configureDevtunnelTunnel(exe, tunnelId, port);
536
+ }
537
+ }
538
+
539
+ const args = p.args(port, { tunnelId });
540
+ child = spawn(exe, args, { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
541
+ entry = { provider, child, url: null, startedAt: Date.now(), log: [], tunnelId };
542
+ current = entry;
543
+ } finally {
544
+ starting = false;
545
+ }
520
546
 
521
547
  const pushLog = (line) => {
522
548
  entry.log.push(line);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakapiano/ccsm",
3
- "version": "0.21.5",
3
+ "version": "0.22.0",
4
4
  "description": "Claude Code Session Manager — Windows web UI to manage many concurrent claude sessions: live list, snapshot/restore, focus existing window, new session in an isolated workspace with repo clones",
5
5
  "license": "MIT",
6
6
  "main": "server.js",
@@ -1838,6 +1838,32 @@
1838
1838
  Running: a status banner (state · provider · uptime · stop link)
1839
1839
  followed by a copyable share URL block and an optional collapsed
1840
1840
  CLI log. */
1841
+ .tunnel-autostart {
1842
+ display: flex;
1843
+ flex-direction: column;
1844
+ gap: 6px;
1845
+ margin-bottom: var(--s-4);
1846
+ }
1847
+ .tunnel-autostart-row {
1848
+ display: flex;
1849
+ align-items: center;
1850
+ gap: 9px;
1851
+ cursor: pointer;
1852
+ font-size: 13px;
1853
+ color: var(--ink-mid);
1854
+ }
1855
+ .tunnel-autostart-row input {
1856
+ flex: 0 0 auto;
1857
+ cursor: pointer;
1858
+ }
1859
+ .tunnel-autostart-label {
1860
+ user-select: none;
1861
+ }
1862
+ .tunnel-autostart-hint {
1863
+ /* indent under the checkbox so it lines up with the label text */
1864
+ padding-left: 26px;
1865
+ }
1866
+
1841
1867
  .tunnel-hero {
1842
1868
  display: flex;
1843
1869
  align-items: center;
@@ -38,16 +38,29 @@ function saveLaunchState(s) {
38
38
  try { localStorage.setItem(LS_KEY, JSON.stringify(s)); } catch {}
39
39
  }
40
40
 
41
- function initRepoSelection(repos, override) {
42
- if (override && Array.isArray(override)) {
43
- // Only honour names that still exist in the user's repo list;
44
- // anything else was deleted between sessions.
45
- const valid = new Set(repos.map((r) => r.name));
46
- selectedRepos.value = new Set(override.filter((n) => valid.has(n)));
47
- return;
41
+ function initRepoSelection(repos, saved) {
42
+ const valid = new Set(repos.map((r) => r.name));
43
+ const sel = new Set();
44
+ // Start from the persisted selection (last-used picks), keeping only
45
+ // repos that still exist.
46
+ if (saved && Array.isArray(saved.repos)) {
47
+ for (const n of saved.repos) if (valid.has(n)) sel.add(n);
48
48
  }
49
- const want = new Set(repos.filter((r) => r.defaultSelected).map((r) => r.name));
50
- selectedRepos.value = want;
49
+ // Auto-check any repo whose "pre-select on launch" default is newly
50
+ // active — i.e. it wasn't a default the last time we saved. This
51
+ // covers both a brand-new default repo and an existing repo the user
52
+ // just flipped to default in Configure. A default the user previously
53
+ // unchecked stays unchecked (it's an old default, already in
54
+ // knownDefaults). With no saved knownDefaults (fresh user / old
55
+ // state), every default applies.
56
+ const knownDefaults = saved && Array.isArray(saved.knownDefaults)
57
+ ? new Set(saved.knownDefaults) : null;
58
+ for (const r of repos) {
59
+ if (r.defaultSelected && (knownDefaults === null || !knownDefaults.has(r.name))) {
60
+ sel.add(r.name);
61
+ }
62
+ }
63
+ selectedRepos.value = sel;
51
64
  }
52
65
 
53
66
  function LaunchHero() {
@@ -86,17 +99,21 @@ function LaunchHero() {
86
99
  }, [folderId, folders.value.length]);
87
100
 
88
101
  // Persist every change. JSON-stringifying a Set isn't useful, so
89
- // we materialize selectedRepos to an array here.
102
+ // we materialize selectedRepos to an array here. knownDefaults records
103
+ // which repos were marked "pre-select" at save time so
104
+ // initRepoSelection can tell a newly-flipped default apart from one the
105
+ // user deliberately unchecked.
90
106
  useEffect(() => {
91
107
  saveLaunchState({
92
108
  cliId, folderId, mode, cwd,
93
109
  repos: [...selectedRepos.value],
110
+ knownDefaults: repos.filter((r) => r.defaultSelected).map((r) => r.name),
94
111
  });
95
112
  }, [cliId, folderId, mode, cwd, selectedRepos.value]);
96
113
 
97
114
 
98
115
  const sig = repos.map((r) => r.name + ':' + r.defaultSelected).join('|');
99
- useStateOnce(sig, () => initRepoSelection(repos, saved?.repos));
116
+ useStateOnce(sig, () => initRepoSelection(repos, saved));
100
117
 
101
118
  const cli = clis.find((c) => c.id === cliId) || clis[0];
102
119
  const folder = folders.value.find((f) => f.id === folderId);
@@ -423,11 +423,35 @@ export function RemotePage() {
423
423
  const fresh = genToken();
424
424
  setTokenLocal(fresh);
425
425
  try {
426
- const s = await api('POST', '/api/tunnel/token', { token: fresh });
426
+ // When auto-start is on the token must be PERSISTED, else the
427
+ // rotated token is lost on the next backend restart and every
428
+ // share URL built from it 401s. Route through the persisting
429
+ // endpoint in that case; otherwise the in-memory-only token
430
+ // endpoint is enough.
431
+ const s = status?.autoStart
432
+ ? await api('POST', '/api/tunnel/autostart', { autoStart: true, provider, token: fresh })
433
+ : await api('POST', '/api/tunnel/token', { token: fresh });
427
434
  setStatus(s);
428
435
  setToast('New token in effect', 'ok');
429
436
  } catch (e) { setToast(`token save failed · ${e.message}`, 'error'); }
430
437
  }
438
+ // Persist (or clear) the auto-start preference. On enable with no
439
+ // token yet, mint one first so the backend has something to reuse on
440
+ // its next startup. Approved devices keep working regardless of the
441
+ // token — it only gates NEW device registration.
442
+ async function onToggleAutoStart(next) {
443
+ setBusy(true);
444
+ try {
445
+ let tok = token;
446
+ if (next && (!tok || tok.length < 8)) { tok = genToken(); setTokenLocal(tok); }
447
+ const s = await api('POST', '/api/tunnel/autostart',
448
+ next ? { autoStart: true, provider, token: tok } : { autoStart: false });
449
+ setStatus(s);
450
+ setToast(next ? 'Auto-start on · tunnel comes up when ccsm starts' : 'Auto-start off', 'ok');
451
+ } catch (e) {
452
+ setToast(`auto-start ${next ? 'enable' : 'disable'} failed · ${e.message}`, 'error');
453
+ } finally { setBusy(false); }
454
+ }
431
455
  async function onInstall(p) {
432
456
  const ok = await ccsmConfirm(`Install ${p} via winget? Runs in the background — refresh after ~30s.`, {
433
457
  title: 'Install tunnel provider', okLabel: 'Install',
@@ -553,6 +577,18 @@ export function RemotePage() {
553
577
  meta=${running
554
578
  ? html`Provider <code>${status?.provider}</code> · started ${new Date(status.startedAt).toLocaleTimeString()}`
555
579
  : html`Not running.`}>
580
+ <div class="tunnel-autostart">
581
+ <label class="tunnel-autostart-row">
582
+ <input type="checkbox" checked=${!!status?.autoStart} disabled=${busy}
583
+ onChange=${(e) => onToggleAutoStart(e.target.checked)} />
584
+ <span class="tunnel-autostart-label">Start this tunnel automatically when ccsm starts</span>
585
+ </label>
586
+ ${status?.autoStart && provider === 'cloudflared' ? html`
587
+ <span class="hint tunnel-autostart-hint">
588
+ Cloudflare quick tunnels get a new URL each launch — the share URL will change on
589
+ restart and approved devices must re-register. Use Microsoft Dev Tunnel for a stable URL.
590
+ </span>` : null}
591
+ </div>
556
592
  ${!running ? html`
557
593
  <div class="tunnel-hero">
558
594
  <div class="tunnel-hero-body">
package/server.js CHANGED
@@ -456,13 +456,27 @@ function decorateConfigWithProbes(cfg) {
456
456
  };
457
457
  }
458
458
 
459
+ // The tunnel + devtunnel config blocks are managed exclusively through
460
+ // /api/tunnel/* (host-only) — they hold the persisted remote-access
461
+ // token and the named tunnelId. Strip them from /api/config so (a) the
462
+ // plaintext token never reaches an approved remote device reading config
463
+ // and (b) the frontend's whole-object config round-trip on save can't
464
+ // clobber tunnelId/token with a stale snapshot.
465
+ function stripTunnelKeys(cfg) {
466
+ const rest = { ...cfg };
467
+ delete rest.tunnel;
468
+ delete rest.devtunnel;
469
+ return rest;
470
+ }
459
471
  app.get('/api/config', asyncH(async (_req, res) => {
460
- res.json(decorateConfigWithProbes(await loadConfig()));
472
+ res.json(decorateConfigWithProbes(stripTunnelKeys(await loadConfig())));
461
473
  }));
462
474
 
463
475
  app.put('/api/config', asyncH(async (req, res) => {
464
- const cfg = await saveConfig(req.body || {});
465
- res.json(decorateConfigWithProbes(cfg));
476
+ const body = { ...(req.body || {}) };
477
+ delete body.tunnel;
478
+ delete body.devtunnel;
479
+ res.json(decorateConfigWithProbes(stripTunnelKeys(await saveConfig(body))));
466
480
  }));
467
481
 
468
482
  // ---- CLI probe / test ----
@@ -1162,6 +1176,28 @@ app.post('/api/tunnel/token', asyncH(async (req, res) => {
1162
1176
  tunnel.setToken(t);
1163
1177
  res.json(await tunnel.status());
1164
1178
  }));
1179
+ // Persist auto-start prefs. When ON, the backend brings this tunnel up
1180
+ // during its own startup (the boot hook in the listen IIFE below) using
1181
+ // the persisted token, so share URLs survive a backend restart. The
1182
+ // token is written to config ONLY while auto-start is on; turning it off
1183
+ // wipes the persisted token from disk. setToken keeps the in-memory copy
1184
+ // in lockstep so the share URL the page renders stays valid immediately.
1185
+ app.post('/api/tunnel/autostart', asyncH(async (req, res) => {
1186
+ const { autoStart, provider, token } = req.body || {};
1187
+ if (autoStart) {
1188
+ if (!token || String(token).length < 8) {
1189
+ return res.status(400).json({ error: 'token required (≥ 8 chars)' });
1190
+ }
1191
+ if (!['devtunnel', 'cloudflared'].includes(provider)) {
1192
+ return res.status(400).json({ error: 'valid provider required' });
1193
+ }
1194
+ tunnel.setToken(token);
1195
+ await saveConfig({ tunnel: { autoStart: true, provider, token } });
1196
+ } else {
1197
+ await saveConfig({ tunnel: { autoStart: false, provider: null, token: null } });
1198
+ }
1199
+ res.json(await tunnel.status());
1200
+ }));
1165
1201
  app.post('/api/tunnel/install', asyncH(async (req, res) => {
1166
1202
  const { provider } = req.body || {};
1167
1203
  try {
@@ -1675,6 +1711,20 @@ function openInBrowser(url) {
1675
1711
  // is warm by the time anyone clicks.
1676
1712
  try { tunnel.probe(true).catch(() => {}); } catch {}
1677
1713
 
1714
+ // Auto-start the tunnel if the user enabled it on the Remote page.
1715
+ // This is the BACKEND PROCESS bringing its own tunnel up on startup —
1716
+ // not an OS-level autostart (no registry / scheduled task). Reuses the
1717
+ // persisted token so share URLs stay valid across restarts. Strictly
1718
+ // fire-and-forget: a failure here (devtunnel not signed in, provider
1719
+ // uninstalled, etc.) must never crash boot — it just logs and the user
1720
+ // can start manually from the Remote page.
1721
+ if (cfg.tunnel?.autoStart && cfg.tunnel?.token && cfg.tunnel?.provider) {
1722
+ tunnel.setToken(cfg.tunnel.token);
1723
+ tunnel.start({ provider: cfg.tunnel.provider, port: currentPort })
1724
+ .then((s) => console.log(`[ccsm] tunnel auto-started · ${cfg.tunnel.provider} · ${s.url || 'URL pending'}`))
1725
+ .catch((e) => console.warn(`[ccsm] tunnel auto-start failed · ${e.message}`));
1726
+ }
1727
+
1678
1728
  if (webTerminal.available) {
1679
1729
  let WebSocketServer;
1680
1730
  try { ({ WebSocketServer } = require('ws')); } catch {}