@codexa/cli 9.0.32 → 9.0.33

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/db/connection.ts CHANGED
@@ -4,6 +4,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
4
4
  import { join, dirname } from "path";
5
5
 
6
6
  let client: Client | null = null;
7
+ let dbProvisioned = false;
7
8
 
8
9
  // ═══════════════════════════════════════════════════════════════
9
10
  // Resolucao de DB URL: env → config local → derivacao do git
@@ -74,32 +75,39 @@ function saveConfig(configPath: string, url: string): void {
74
75
 
75
76
  // ═══════════════════════════════════════════════════════════════
76
77
  // Provisioning: cria o DB no Turso se nao existir
78
+ // Usa API-first (POST idempotente) para evitar bug de
79
+ // compatibilidade Bun no @libsql/hrana-client (resp.body.cancel)
77
80
  // ═══════════════════════════════════════════════════════════════
78
81
 
79
82
  export async function ensureDatabase(): Promise<void> {
83
+ if (dbProvisioned) return;
84
+
80
85
  const url = resolveDbUrl();
81
86
 
82
87
  // Somente provisionar para URLs Turso (nao para file: ou :memory:)
83
- if (!url.startsWith("libsql://")) return;
88
+ if (!url.startsWith("libsql://")) {
89
+ dbProvisioned = true;
90
+ return;
91
+ }
84
92
 
85
93
  const authToken = process.env.TURSO_AUTH_TOKEN;
86
94
  const org = process.env.TURSO_ORG;
87
- if (!authToken || !org) return;
88
-
89
- // Extrair nome do DB da URL: libsql://codexa-slug-org.turso.io → codexa-slug
90
- const hostMatch = url.match(/^libsql:\/\/(.+)-[^-]+\.turso\.io$/);
91
- if (!hostMatch) return;
92
- const dbName = hostMatch[1];
93
-
94
- // Tentar conectar primeiro — se o DB ja existe, nao precisa criar
95
- try {
96
- const testClient = createClient({ url, authToken });
97
- await testClient.execute("SELECT 1");
98
- testClient.close();
95
+ if (!authToken || !org) {
96
+ dbProvisioned = true;
99
97
  return;
100
- } catch { /* DB nao existe, criar */ }
98
+ }
101
99
 
102
- // Criar via Turso Platform API
100
+ // Extrair nome do DB da URL: libsql://codexa-slug-org.turso.io → codexa-slug
101
+ const hostname = url.replace("libsql://", "").replace(".turso.io", "");
102
+ // O nome do DB no Turso e o hostname completo (sem .turso.io)
103
+ // Mas a API espera o nome curto. O hostname = name-org, entao name = hostname sem -org no final
104
+ const dbName = hostname.endsWith(`-${org}`)
105
+ ? hostname.slice(0, -(org.length + 1))
106
+ : hostname;
107
+
108
+ // API-first: tenta criar. 409 = ja existe (ok). 200 = criado.
109
+ // Isso evita o bug Bun no @libsql/hrana-client que ocorre quando
110
+ // tentamos conectar a um DB inexistente (resp.body?.cancel not a function)
103
111
  const group = process.env.TURSO_GROUP || "default";
104
112
  try {
105
113
  const response = await fetch(
@@ -116,15 +124,19 @@ export async function ensureDatabase(): Promise<void> {
116
124
 
117
125
  if (response.ok) {
118
126
  console.log(`[codexa] Banco criado no Turso: ${dbName}`);
127
+ // Aguardar um momento para o DB ficar disponivel
128
+ await new Promise((r) => setTimeout(r, 2000));
119
129
  } else if (response.status === 409) {
120
130
  // Ja existe — tudo certo
121
131
  } else {
122
132
  const body = await response.text();
123
- console.error(`[codexa] Falha ao criar banco: ${response.status} ${body}`);
133
+ console.error(`[codexa] Falha ao criar banco (${response.status}): ${body}`);
124
134
  }
125
135
  } catch (e: any) {
126
136
  console.error(`[codexa] Erro ao provisionar banco: ${e.message}`);
127
137
  }
138
+
139
+ dbProvisioned = true;
128
140
  }
129
141
 
130
142
  // ═══════════════════════════════════════════════════════════════
@@ -153,10 +165,12 @@ export function closeDb(): void {
153
165
 
154
166
  export function resetClient(): void {
155
167
  client = null;
168
+ dbProvisioned = false;
156
169
  }
157
170
 
158
171
  export function setClient(c: Client): void {
159
172
  client = c;
173
+ dbProvisioned = true;
160
174
  }
161
175
 
162
176
  export function getResolvedDbUrl(): string {
package/db/schema.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { Client, Transaction } from "@libsql/client";
2
- import { getDb, setClient, dbGet, dbAll, dbRun, dbExec } from "./connection";
2
+ import { getDb, setClient, dbGet, dbAll, dbRun, dbExec, ensureDatabase } from "./connection";
3
3
 
4
4
  export async function initSchema(): Promise<void> {
5
+ await ensureDatabase();
5
6
  await dbExec(`
6
7
  CREATE TABLE IF NOT EXISTS specs (
7
8
  id TEXT PRIMARY KEY,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codexa/cli",
3
- "version": "9.0.32",
3
+ "version": "9.0.33",
4
4
  "description": "Orchestrated workflow system for Claude Code - manages feature development through parallel subagents with structured phases, gates, and quality enforcement.",
5
5
  "type": "module",
6
6
  "bin": {