Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

feat(desktop): delete a sandbox's Clerk test account on prune

leepokaie47a6d7f3067
diff --git a/apps/desktop/tools/dev-account.mjs b/apps/desktop/tools/dev-account.mjs
index 36b001b8..71b3a821 100644
--- a/apps/desktop/tools/dev-account.mjs
+++ b/apps/desktop/tools/dev-account.mjs
@@ -1,16 +1,18 @@
// A Clerk test account for `npm run dev` sandboxes: finds or creates `sandbox-<name>[email protected]`
// in the Clerk *development* instance and mints a one-time sign-in token the app trades for a session
-// (account.ts, `CODYNC_DEV_SIGN_IN_TICKET`). The secret key comes from CLERK_SECRET_KEY or the git-ignored
-// apps/desktop/.env.local; only `sk_test_` keys are used, so production is never touched.
+// (account.ts, `CODYNC_DEV_SIGN_IN_TICKET`). The secret key comes from CLERK_SECRET_KEY, the git-ignored
+// apps/desktop/.env.local, or ~/.codync-release/clerk-dev.env (shared by every worktree); only `sk_test_`
+// keys are used, so production is never touched.
import { existsSync, readFileSync } from 'node:fs'
+import { homedir } from 'node:os'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const API = 'https://api.clerk.com/v1'
export function secretKey() {
- const file = join(dirname(fileURLToPath(import.meta.url)), '..', '.env.local')
- const fromFile = existsSync(file) ? /^CLERK_SECRET_KEY=(.+)$/m.exec(readFileSync(file, 'utf8'))?.[1] : undefined
+ const files = [join(dirname(fileURLToPath(import.meta.url)), '..', '.env.local'), join(homedir(), '.codync-release/clerk-dev.env')]
+ const fromFile = files.map((f) => (existsSync(f) ? /^CLERK_SECRET_KEY=(.+)$/m.exec(readFileSync(f, 'utf8'))?.[1] : undefined)).find(Boolean)
const key = (process.env.CLERK_SECRET_KEY ?? fromFile ?? '').trim().replace(/^["']|["']$/g, '')
if (key && !key.startsWith('sk_test_')) throw new Error('CLERK_SECRET_KEY must be the development instance key (sk_test_...).')
return key || null
@@ -28,13 +30,24 @@ async function clerk(key, method, path, body) {
return json
}
+const emailOf = (sandbox) => `sandbox-${sandbox}[email protected]`
+const findUser = (key, email) => clerk(key, 'GET', `/users?email_address=${encodeURIComponent(email)}`).then((found) => found[0])
+
/** A fresh sign-in token for the sandbox's test account, or null without a secret key. */
export async function mintTicket(sandbox) {
const key = secretKey()
if (!key) return null
- const email = `sandbox-${sandbox}[email protected]`
- const found = await clerk(key, 'GET', `/users?email_address=${encodeURIComponent(email)}`)
- const user = found[0] ?? (await clerk(key, 'POST', '/users', { email_address: [email], skip_password_requirement: true }))
+ const email = emailOf(sandbox)
+ const user = (await findUser(key, email)) ?? (await clerk(key, 'POST', '/users', { email_address: [email], skip_password_requirement: true }))
const { token } = await clerk(key, 'POST', '/sign_in_tokens', { user_id: user.id, expires_in_seconds: 600 })
return { token, email }
}
+
+/** Deletes the sandbox's test account; true when there was one. Without a secret key there is nothing to delete. */
+export async function deleteTestAccount(sandbox) {
+ const key = secretKey()
+ const user = key && (await findUser(key, emailOf(sandbox)))
+ if (!user) return false
+ await clerk(key, 'DELETE', `/users/${user.id}`)
+ return true
+}
diff --git a/apps/desktop/tools/dev.mjs b/apps/desktop/tools/dev.mjs
index 060a451d..47e701f0 100644
--- a/apps/desktop/tools/dev.mjs
+++ b/apps/desktop/tools/dev.mjs
@@ -5,7 +5,8 @@
// Codync Dev from a worktree, --sandbox runs a sandbox from the main checkout, --name <name> runs
// the sandbox of that name, --cloud lets the sandbox host use the dev cloud, --cdp opens the
// DevTools protocol on the sandbox port + 10000, --onboarding shows the welcome flow (a sandbox
-// starts past it), --list shows every sandbox, --prune deletes the ones whose worktree is gone.
+// starts past it), --list shows every sandbox, --prune deletes the ones whose worktree is gone
+// (and their Clerk test accounts).
import { spawn, spawnSync } from 'node:child_process'
import { existsSync, mkdirSync, openSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
import { createServer } from 'node:net'
@@ -14,7 +15,7 @@ import { basename, dirname, join, resolve } from 'node:path'
import { setTimeout as delay } from 'node:timers/promises'
import { fileURLToPath } from 'node:url'
import { parseArgs } from 'node:util'
-import { mintTicket } from './dev-account.mjs'
+import { deleteTestAccount, mintTicket } from './dev-account.mjs'
import { SANDBOX_PREFIX, sandboxName, sandboxPorts } from '../src/shared/dev-sandbox.ts'
const desktop = resolve(dirname(fileURLToPath(import.meta.url)), '..')
@@ -91,8 +92,9 @@ async function listAndPrune() {
const running = await answers(s.port)
const gone = !existsSync(s.worktree)
if (flags.prune && gone && !running) {
+ const account = await deleteTestAccount(s.name).catch((e) => console.error(`Test account of ${s.name} kept: ${e.message}`))
rmSync(s.home, { recursive: true, force: true })
- console.log(`Deleted the sandbox ${s.name} (${s.home})`)
+ console.log(`Deleted the sandbox ${s.name} (${s.home})${account ? ' and its Clerk test account' : ''}`)
} else if (flags.list) {
console.log(`${s.name.padEnd(28)} ${String(s.port).padEnd(6)} ${(running ? 'running' : 'stopped').padEnd(8)} ${s.worktree}${gone ? ' (gone)' : ''}`)
}
diff --git a/docs/guides/dev-app.md b/docs/guides/dev-app.md
index c14f73a0..f087dc7e 100644
--- a/docs/guides/dev-app.md
+++ b/docs/guides/dev-app.md
@@ -28,9 +28,9 @@ Xcode configurations separate environment from optimization: `Debug` and `Releas
- A host built from the worktree's own code (`node tools/native-host.mjs dev`; on Linux `cargo build`, honoring `CARGO_TARGET_DIR`; or `CODYNC_HOST_BIN`), started and stopped with the app, logging to `host.log`. No launchd/systemd service, and the dev cloud is off.
- The run prints the commands that pair a phone or simulator (`codync-host pair`) and open the terminal UI against the sandbox. Give each sandbox its own simulator: a simulator holds one Codync Dev.
-Flags go after `npm run dev --`: `--shared` takes the default Codync Dev from a worktree (quit the main checkout's first); `--sandbox` runs a sandbox from the main checkout, for a second session on `dev`; `--name <name>` runs the sandbox of that name, for another copy of the same checkout; `--cloud` lets the sandbox host use the dev cloud (each sandbox then shows as another computer); `--cdp` opens the DevTools protocol on the sandbox port + 10000; `--list` shows every sandbox; `--prune` deletes the ones whose worktree is gone, so run it after removing worktrees.
+Flags go after `npm run dev --`: `--shared` takes the default Codync Dev from a worktree (quit the main checkout's first); `--sandbox` runs a sandbox from the main checkout, for a second session on `dev`; `--name <name>` runs the sandbox of that name, for another copy of the same checkout; `--cloud` lets the sandbox host use the dev cloud (each sandbox then shows as another computer); `--cdp` opens the DevTools protocol on the sandbox port + 10000; `--list` shows every sandbox; `--prune` deletes the ones whose worktree is gone (and their Clerk test accounts), so run it after removing worktrees.
-A sandbox starts past onboarding (welcome and computer-access setup, only where the window hasn't chosen yet); `--onboarding` brings the flow back. With `CLERK_SECRET_KEY` (the Clerk **development** instance's `sk_test_...` key) in the git-ignored `apps/desktop/.env.local` or the environment, it also signs in its own Clerk test account `sandbox-<name>[email protected]`: `tools/dev-account.mjs` finds or creates that user through the Backend API and mints a one-time sign-in token, which `account.ts` trades for a session (`ticket` strategy, unpackaged Dev runs only). The session stays in the sandbox's `desktop/`, so the token is minted only while `desktop/account.bin` is missing. Keys that aren't `sk_test_` are refused. Without a key a sandbox starts signed out; it never borrows Codync Dev's login.
+A sandbox starts past onboarding (welcome and computer-access setup, only where the window hasn't chosen yet); `--onboarding` brings the flow back. With `CLERK_SECRET_KEY` (the Clerk **development** instance's `sk_test_...` key) in the git-ignored `apps/desktop/.env.local` or `~/.codync-release/clerk-dev.env` (one copy for every worktree) or the environment, it also signs in its own Clerk test account `sandbox-<name>[email protected]`: `tools/dev-account.mjs` finds or creates that user through the Backend API and mints a one-time sign-in token, which `account.ts` trades for a session (`ticket` strategy, unpackaged Dev runs only). The session stays in the sandbox's `desktop/`, so the token is minted only while `desktop/account.bin` is missing. Keys that aren't `sk_test_` are refused. Without a key a sandbox starts signed out; it never borrows Codync Dev's login.
Still shared: macOS sees every unpackaged run as the same Electron app, so permissions and notifications are shared and a `codync-dev://` sign-in callback may reach another copy (test sign-in on the default Codync Dev); Codync Screen and computer use need the packaged `Codync Dev.app`; a global shortcut (the approval island's) goes to whichever app registered it first.