@clearlysh/beehaven 0.8.24 → 0.8.25

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.
@@ -1,148 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
  // ============================================================================
3
3
  // Beehaven Office - Relay Setup (Login)
4
- // Browser-based OAuth login flow for Clearly account
4
+ // The daemon's interactive sign-in fallback, and logout.
5
5
  // ============================================================================
6
- import http from 'http';
7
- import crypto from 'crypto';
8
- import { createInterface } from 'readline';
9
- import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from 'fs';
6
+ import { existsSync, readFileSync, writeFileSync, chmodSync } from 'fs';
10
7
  import { homedir } from 'os';
11
8
  import { join } from 'path';
12
9
  import { spawnSync } from 'child_process';
13
10
  import { Relay, CLEARLY_RELAY_URL } from './relay.js';
14
- import { resolveRelaySelection } from './relay-presets.js';
15
11
  import { SESSION_KEYCHAIN_ACCOUNTS, KEYCHAIN_SERVICE } from './keychain.js';
16
- import { withUtm } from './utm.js';
17
12
  const CONFIG_DIR = join(homedir(), '.beehaven');
18
13
  const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
19
- const AUTH_BASE_URL = 'https://clearly.sh/auth/beehaven';
20
- const LOGIN_TIMEOUT_MS = 120_000; // 2 minutes
21
14
  /**
22
- * The raw config, for questions that must be answered BEFORE a token exists.
23
- * `Relay.loadConfig()` returns null without one, which is the state a first login is in.
15
+ * The daemon calls this when it starts interactively with no token, or when its session has
16
+ * expired (daemon.ts). It then re-reads the config, so all it needs is credentials on disk.
17
+ *
18
+ * ⚠⚠ AWO-224 — THIS USED TO BE A SECOND, DIFFERENT SIGN-IN. It opened
19
+ * `https://www.clearly.sh/auth/beehaven?callback=…`, waited for a single `token` and wrote its own
20
+ * config. The web app no longer serves that route (404 on prod and, via the apex redirect, the
21
+ * same 404), so the fallback opened a dead page and timed out after two minutes. It now runs the
22
+ * exact flow `beehaven login` runs (`browser-login.ts`: `/cli/auth`, the keychain idToken slot
23
+ * the daemon reads, both dial-target keys) and nothing else — it must NOT restart the daemon,
24
+ * because it runs inside it.
24
25
  */
25
- function readConfigFile() {
26
- if (!existsSync(CONFIG_FILE))
27
- return {};
28
- try {
29
- return JSON.parse(readFileSync(CONFIG_FILE, 'utf8'));
30
- }
31
- catch {
32
- return {};
33
- }
34
- }
35
- function saveRelayConfig(token, endpoint) {
36
- mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
37
- let existing = {};
38
- if (existsSync(CONFIG_FILE)) {
39
- try {
40
- existing = JSON.parse(readFileSync(CONFIG_FILE, 'utf8'));
41
- }
42
- catch (err) {
43
- console.warn(`[config] Failed to parse ${CONFIG_FILE}:`, err.message);
44
- }
45
- }
46
- existing.token = token;
47
- // ⚠ BOTH KEYS, as `beehaven env` writes them — `endpoint` is dialled, `relayUrl` is the alias
48
- // other surfaces read. Updating one leaves the other naming a different environment.
49
- existing.endpoint = endpoint;
50
- existing.relayUrl = endpoint;
51
- existing.tier = 'connected';
52
- writeFileSync(CONFIG_FILE, JSON.stringify(existing, null, 2));
53
- chmodSync(CONFIG_FILE, 0o600);
54
- }
55
- function prompt(question) {
56
- const rl = createInterface({ input: process.stdin, output: process.stdout });
57
- return new Promise((resolve) => {
58
- rl.question(question, (answer) => {
59
- rl.close();
60
- resolve(answer.trim());
61
- });
62
- });
63
- }
64
- /**
65
- * Start a local HTTP server on a random port to receive the OAuth callback.
66
- * Returns a promise for the token, plus helpers to get the port and close.
67
- */
68
- function startCallbackServer() {
69
- let resolveToken;
70
- let rejectToken;
71
- let serverRef;
72
- let settled = false;
73
- const oauthState = crypto.randomBytes(32).toString('hex');
74
- const promise = new Promise((resolve, reject) => {
75
- resolveToken = resolve;
76
- rejectToken = reject;
77
- serverRef = http.createServer((req, res) => {
78
- if (settled)
79
- return;
80
- const url = new URL(req.url, `http://localhost`);
81
- if (url.pathname === '/callback') {
82
- // Validate OAuth state parameter to prevent CSRF (skip if server doesn't echo it back yet)
83
- const returnedState = url.searchParams.get('state');
84
- if (returnedState && returnedState !== oauthState) {
85
- res.writeHead(403, { 'Content-Type': 'text/html' });
86
- res.end('<!DOCTYPE html><html><body><h1>Invalid state parameter</h1></body></html>');
87
- return;
88
- }
89
- const token = url.searchParams.get('token');
90
- if (token) {
91
- settled = true;
92
- res.writeHead(200, { 'Content-Type': 'text/html' });
93
- res.end([
94
- '<!DOCTYPE html><html><head>',
95
- '<meta charset="utf-8">',
96
- '<title>Beehaven - Logged In</title>',
97
- '<style>',
98
- 'body{font-family:system-ui,-apple-system,sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0;background:#fef9ef}',
99
- '.card{text-align:center;padding:48px;border-radius:16px;background:white;box-shadow:0 2px 12px rgba(0,0,0,0.08)}',
100
- 'h1{margin:0 0 8px;font-size:24px}',
101
- 'p{margin:0;color:#666;font-size:15px}',
102
- '.bee{font-size:48px;margin-bottom:16px}',
103
- '</style>',
104
- '</head><body>',
105
- '<div class="card">',
106
- '<div class="bee">&#x1F41D;</div>',
107
- '<h1>Logged in!</h1>',
108
- '<p>You can close this tab and return to your terminal.</p>',
109
- '</div>',
110
- '</body></html>',
111
- ].join(''));
112
- serverRef.close();
113
- resolve(token);
114
- }
115
- else {
116
- res.writeHead(400, { 'Content-Type': 'text/html' });
117
- res.end('<!DOCTYPE html><html><body><h1>Missing token</h1></body></html>');
118
- }
119
- }
120
- else {
121
- res.writeHead(404);
122
- res.end();
123
- }
124
- });
125
- serverRef.listen(0, '127.0.0.1');
126
- // Timeout after 2 minutes
127
- const timer = setTimeout(() => {
128
- if (!settled) {
129
- settled = true;
130
- serverRef.close();
131
- reject(new Error('Login timed out after 2 minutes.'));
132
- }
133
- }, LOGIN_TIMEOUT_MS);
134
- serverRef.on('close', () => clearTimeout(timer));
135
- });
136
- return {
137
- promise,
138
- getPort: () => serverRef.address()?.port || 0,
139
- close: () => {
140
- settled = true;
141
- serverRef?.close();
142
- },
143
- state: oauthState,
144
- };
145
- }
146
26
  export async function login() {
147
27
  console.log('');
148
28
  console.log(' Clearly account required to use Beehaven.');
@@ -165,114 +45,19 @@ export async function login() {
165
45
  console.log(' Session expired. Please log in again.');
166
46
  console.log('');
167
47
  }
168
- // Start local callback server
169
- const callbackServer = startCallbackServer();
170
- // Small delay to let server bind
171
- await new Promise((r) => setTimeout(r, 100));
172
- const port = callbackServer.getPort();
173
- if (!port) {
174
- console.log(' Failed to start auth server.');
175
- callbackServer.close();
176
- return false;
177
- }
178
- const callbackUrl = `http://localhost:${port}/callback`;
179
- const authUrl = withUtm(`${AUTH_BASE_URL}?callback=${encodeURIComponent(callbackUrl)}&state=${callbackServer.state}`);
180
- // Try to open browser
181
- let browserOpened = false;
182
- try {
183
- const openModule = await import('open');
184
- await openModule.default(authUrl);
185
- browserOpened = true;
186
- }
187
- catch {
188
- browserOpened = false;
189
- }
190
- if (browserOpened) {
191
- console.log(' Opening browser to sign in with Google...');
192
- console.log('');
193
- console.log(' Waiting for login... (press Ctrl+C to cancel)');
194
- }
195
- else {
196
- // Fallback: show URL and offer manual token paste
197
- console.log(' Could not open browser. Sign in manually:');
198
- console.log('');
199
- console.log(` 1. Open: ${authUrl}`);
200
- console.log(' 2. Sign in with Google');
201
- console.log('');
202
- console.log(' Waiting for login callback...');
203
- console.log(' Or paste your relay token below:');
204
- console.log('');
205
- // Race: callback server vs manual token paste
206
- const manualToken = prompt(' Relay token: ').then((token) => {
207
- if (token) {
208
- callbackServer.close();
209
- return token;
210
- }
211
- return null;
212
- });
213
- try {
214
- const token = await Promise.race([
215
- callbackServer.promise,
216
- manualToken.then((t) => {
217
- if (t)
218
- return t;
219
- return new Promise(() => { });
220
- }),
221
- ]);
222
- if (token) {
223
- return await verifyAndSave(token);
224
- }
225
- }
226
- catch (err) {
227
- console.log(` ${err.message}`);
228
- return false;
229
- }
230
- }
231
- // Wait for callback
232
48
  try {
233
- const token = await callbackServer.promise;
234
- return await verifyAndSave(token);
235
- }
236
- catch (err) {
237
- console.log(` ${err.message}`);
238
- return false;
239
- }
240
- }
241
- async function verifyAndSave(token) {
242
- // Validate token format before sending to server
243
- if (!token || !/^[a-zA-Z0-9_\-.]{20,}$/.test(token)) {
244
- console.log(' Invalid token format.');
245
- return false;
246
- }
247
- console.log('');
248
- console.log(' Verifying...');
249
- const relay = new Relay();
250
- /**
251
- * ⚠⚠ THE ACTIVE RELAY, NOT A HARDCODED PROD CONSTANT. Both of these were `CLEARLY_RELAY_URL`, so
252
- * a staging or dev user who signed in here was VERIFIED against prod and then had `endpoint`
253
- * OVERWRITTEN to prod — silently moving them to another environment as a side effect of logging
254
- * in. `beehaven env staging` writes the target; a login must honour it, never redefine it.
255
- *
256
- * ⚠ Read from the file rather than `Relay.loadConfig()`: that returns null without a token, which
257
- * is exactly the state a first login is in. `resolveRelaySelection` defaults to prod when the
258
- * config names nothing, so the no-config case is unchanged.
259
- */
260
- const dialUrl = resolveRelaySelection(readConfigFile()).url;
261
- const profile = await relay.verifyToken(token, dialUrl);
262
- if (profile) {
263
- saveRelayConfig(token, dialUrl);
264
- if (profile.displayName) {
265
- console.log(` Logged in as ${profile.displayName} (${profile.subscriptionPlan})`);
266
- }
267
- else {
268
- console.log(' Logged in.');
269
- }
49
+ // ⚠ Imported lazily: the Mac app loads this file for `logout` alone and should not pull in
50
+ // the sign-in flow's dependencies to do it.
51
+ const { browserLogin } = await import('./browser-login.js');
52
+ const who = await browserLogin();
53
+ console.log(` Logged in as ${who.email ?? who.userId}.`);
270
54
  console.log(' Config saved to ~/.beehaven/config.json');
271
55
  console.log('');
272
56
  return true;
273
57
  }
274
- else {
275
- console.log(' Token verification failed. Please try again.');
58
+ catch (err) {
59
+ console.log(` ${err?.message ?? err}`);
60
+ console.log(' Run `beehaven login`, then start again.');
276
61
  console.log('');
277
62
  return false;
278
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clearlysh/beehaven",
3
- "version": "0.8.24",
3
+ "version": "0.8.25",
4
4
  "description": "Beehaven CLI — command your Clearly workspace, agents, and canvas from the terminal",
5
5
  "private": false,
6
6
  "type": "module",
@@ -195,6 +195,15 @@ runs. The same steps sent as hundreds of separate calls each re-read the whole s
195
195
  call fails with a dropped connection, read the scene (`composition-detail
196
196
  {id, includeScene:true}`) before retrying: the work may have landed.
197
197
 
198
+ ⚠ **One batch per canvas at a time.** A reply that times out ("outcome unknown") or drops does
199
+ not stop the batch — it is still running on the workspace. Resending at once used to start a
200
+ second, slow, unprotected batch and could half-apply both (CA-43). The workspace now queues a
201
+ second writer behind the first and answers `code: "canvas-busy"` (with the running batch's
202
+ progress) if it waits too long — nothing from that call was applied. Either way: read the scene,
203
+ then send only what is missing. A ~1,100-step build takes about 40 s; the CLI waits 180 s by
204
+ default and `--timeout <ms>` (or `BEEHAVEN_RPC_TIMEOUT_MS`) raises it. Never place component
205
+ instances after a build whose reply you did not see.
206
+
198
207
  Keep review notes and status outside the delivered artwork. Use
199
208
  [communications.md](communications.md) only when posting those blocks is useful.
200
209
 
package/skills/index.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "project-plans.md",
18
18
  "review.mjs"
19
19
  ],
20
- "sha": "9b34f0b8326257d0"
20
+ "sha": "df3ef3200c38abdf"
21
21
  },
22
22
  {
23
23
  "name": "agent-home",