@deeplake/hivemind 0.7.35 → 0.7.36

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.
@@ -66830,7 +66830,6 @@ function sqlIdent(name) {
66830
66830
 
66831
66831
  // dist/src/embeddings/columns.js
66832
66832
  var SUMMARY_EMBEDDING_COL = "summary_embedding";
66833
- var MESSAGE_EMBEDDING_COL = "message_embedding";
66834
66833
 
66835
66834
  // dist/src/utils/client-header.js
66836
66835
  var DEEPLAKE_CLIENT_HEADER = "X-Deeplake-Client";
@@ -66841,6 +66840,123 @@ function deeplakeClientHeader() {
66841
66840
  return { [DEEPLAKE_CLIENT_HEADER]: deeplakeClientValue() };
66842
66841
  }
66843
66842
 
66843
+ // dist/src/deeplake-schema.js
66844
+ var MEMORY_COLUMNS = Object.freeze([
66845
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
66846
+ { name: "path", sql: "TEXT NOT NULL DEFAULT ''" },
66847
+ { name: "filename", sql: "TEXT NOT NULL DEFAULT ''" },
66848
+ { name: "summary", sql: "TEXT NOT NULL DEFAULT ''" },
66849
+ { name: "summary_embedding", sql: "FLOAT4[]" },
66850
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
66851
+ { name: "mime_type", sql: "TEXT NOT NULL DEFAULT 'text/plain'" },
66852
+ { name: "size_bytes", sql: "BIGINT NOT NULL DEFAULT 0" },
66853
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
66854
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
66855
+ { name: "agent", sql: "TEXT NOT NULL DEFAULT ''" },
66856
+ { name: "plugin_version", sql: "TEXT NOT NULL DEFAULT ''" },
66857
+ { name: "creation_date", sql: "TEXT NOT NULL DEFAULT ''" },
66858
+ { name: "last_update_date", sql: "TEXT NOT NULL DEFAULT ''" }
66859
+ ]);
66860
+ var SESSIONS_COLUMNS = Object.freeze([
66861
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
66862
+ { name: "path", sql: "TEXT NOT NULL DEFAULT ''" },
66863
+ { name: "filename", sql: "TEXT NOT NULL DEFAULT ''" },
66864
+ { name: "message", sql: "JSONB" },
66865
+ { name: "message_embedding", sql: "FLOAT4[]" },
66866
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
66867
+ { name: "mime_type", sql: "TEXT NOT NULL DEFAULT 'application/json'" },
66868
+ { name: "size_bytes", sql: "BIGINT NOT NULL DEFAULT 0" },
66869
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
66870
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
66871
+ { name: "agent", sql: "TEXT NOT NULL DEFAULT ''" },
66872
+ { name: "plugin_version", sql: "TEXT NOT NULL DEFAULT ''" },
66873
+ { name: "creation_date", sql: "TEXT NOT NULL DEFAULT ''" },
66874
+ { name: "last_update_date", sql: "TEXT NOT NULL DEFAULT ''" }
66875
+ ]);
66876
+ var SKILLS_COLUMNS = Object.freeze([
66877
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
66878
+ { name: "name", sql: "TEXT NOT NULL DEFAULT ''" },
66879
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
66880
+ { name: "project_key", sql: "TEXT NOT NULL DEFAULT ''" },
66881
+ { name: "local_path", sql: "TEXT NOT NULL DEFAULT ''" },
66882
+ { name: "install", sql: "TEXT NOT NULL DEFAULT 'project'" },
66883
+ { name: "source_sessions", sql: "TEXT NOT NULL DEFAULT '[]'" },
66884
+ { name: "source_agent", sql: "TEXT NOT NULL DEFAULT ''" },
66885
+ { name: "scope", sql: "TEXT NOT NULL DEFAULT 'me'" },
66886
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
66887
+ { name: "contributors", sql: "TEXT NOT NULL DEFAULT '[]'" },
66888
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
66889
+ { name: "trigger_text", sql: "TEXT NOT NULL DEFAULT ''" },
66890
+ { name: "body", sql: "TEXT NOT NULL DEFAULT ''" },
66891
+ { name: "version", sql: "BIGINT NOT NULL DEFAULT 1" },
66892
+ { name: "created_at", sql: "TEXT NOT NULL DEFAULT ''" },
66893
+ { name: "updated_at", sql: "TEXT NOT NULL DEFAULT ''" }
66894
+ ]);
66895
+ function validateSchema(label, cols) {
66896
+ const seen = /* @__PURE__ */ new Set();
66897
+ for (const col of cols) {
66898
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(col.name)) {
66899
+ throw new Error(`${label}: column name "${col.name}" is not a valid SQL identifier`);
66900
+ }
66901
+ if (seen.has(col.name)) {
66902
+ throw new Error(`${label}: duplicate column "${col.name}"`);
66903
+ }
66904
+ seen.add(col.name);
66905
+ const notNull = /\bNOT\s+NULL\b/i.test(col.sql);
66906
+ const hasDefault = /\bDEFAULT\b/i.test(col.sql);
66907
+ if (notNull && !hasDefault) {
66908
+ throw new Error(`${label}: column "${col.name}" is NOT NULL but has no DEFAULT \u2014 ALTER TABLE ADD COLUMN on a populated table would fail.`);
66909
+ }
66910
+ }
66911
+ }
66912
+ validateSchema("MEMORY_COLUMNS", MEMORY_COLUMNS);
66913
+ validateSchema("SESSIONS_COLUMNS", SESSIONS_COLUMNS);
66914
+ validateSchema("SKILLS_COLUMNS", SKILLS_COLUMNS);
66915
+ function buildCreateTableSql(tableName, cols) {
66916
+ const safe = sqlIdent(tableName);
66917
+ const colSql = cols.map((c15) => `${c15.name} ${c15.sql}`).join(", ");
66918
+ return `CREATE TABLE IF NOT EXISTS "${safe}" (${colSql}) USING deeplake`;
66919
+ }
66920
+ function buildIntrospectionSql(tableName, workspaceId) {
66921
+ return `SELECT column_name FROM information_schema.columns WHERE table_name = '${sqlStr(tableName)}' AND table_schema = '${sqlStr(workspaceId)}'`;
66922
+ }
66923
+ async function healMissingColumns(args) {
66924
+ const safeTable = sqlIdent(args.tableName);
66925
+ const introspectSql = buildIntrospectionSql(args.tableName, args.workspaceId);
66926
+ const rows = await args.query(introspectSql);
66927
+ const existing = /* @__PURE__ */ new Set();
66928
+ for (const row of rows) {
66929
+ const v27 = row?.column_name;
66930
+ if (typeof v27 === "string")
66931
+ existing.add(v27.toLowerCase());
66932
+ }
66933
+ const missingCols = args.columns.filter((c15) => !existing.has(c15.name.toLowerCase()));
66934
+ const missing = missingCols.map((c15) => c15.name);
66935
+ if (missingCols.length === 0)
66936
+ return { missing, altered: [] };
66937
+ const altered = [];
66938
+ for (const col of missingCols) {
66939
+ try {
66940
+ await args.query(`ALTER TABLE "${safeTable}" ADD COLUMN ${col.name} ${col.sql}`);
66941
+ altered.push(col.name);
66942
+ args.log?.(`schema-heal: added "${args.tableName}"."${col.name}"`);
66943
+ } catch (e6) {
66944
+ const msg = e6 instanceof Error ? e6.message : String(e6);
66945
+ if (!/already exists/i.test(msg))
66946
+ throw e6;
66947
+ const recheck = await args.query(introspectSql);
66948
+ const present = recheck.some((r10) => {
66949
+ const v27 = r10?.column_name;
66950
+ return typeof v27 === "string" && v27.toLowerCase() === col.name.toLowerCase();
66951
+ });
66952
+ if (!present)
66953
+ throw e6;
66954
+ args.log?.(`schema-heal: "${args.tableName}"."${col.name}" appeared via race, treating as success`);
66955
+ }
66956
+ }
66957
+ return { missing, altered };
66958
+ }
66959
+
66844
66960
  // dist/src/notifications/queue.js
66845
66961
  import { readFileSync as readFileSync2, writeFileSync, renameSync, mkdirSync, openSync, closeSync, unlinkSync, statSync as statSync2 } from "node:fs";
66846
66962
  import { join as join6, resolve as resolve4 } from "node:path";
@@ -67223,64 +67339,33 @@ var DeeplakeApi = class {
67223
67339
  }
67224
67340
  }
67225
67341
  /**
67226
- * Ensure a vector column exists on the given table.
67227
- *
67228
- * The previous implementation always issued `ALTER TABLE ADD COLUMN IF NOT
67229
- * EXISTS …` on every SessionStart. On a long-running workspace that's
67230
- * already migrated, every call returns 500 "Column already exists" — noisy
67231
- * in the log and a wasted round-trip. Worse, the very first call after the
67232
- * column is genuinely added triggers Deeplake's post-ALTER `vector::at`
67233
- * window (~30s) during which subsequent INSERTs fail; minimising the
67234
- * number of ALTER calls minimises exposure to that window.
67342
+ * Heal any missing columns on a table so it matches one of the schema
67343
+ * definitions in `deeplake-schema.ts`. One SELECT against
67344
+ * `information_schema.columns` per call, then `ALTER TABLE ADD COLUMN`
67345
+ * only the genuinely missing ones never blanket, never `IF NOT
67346
+ * EXISTS`.
67235
67347
  *
67236
- * New flow:
67237
- * 1. Check the local marker file (mirrors ensureLookupIndex). If fresh,
67238
- * return zero network calls.
67239
- * 2. SELECT 1 FROM information_schema.columns WHERE table_name = T AND
67240
- * column_name = C. Read-only, idempotent, can't tickle the post-ALTER
67241
- * bug. If the column is present mark + return.
67242
- * 3. Only if step 2 says the column is missing, fall back to ALTER ADD
67243
- * COLUMN IF NOT EXISTS. Mark on success, also mark if Deeplake reports
67244
- * "already exists" (race: another client added it between our SELECT
67245
- * and ALTER).
67246
- *
67247
- * Marker uses the same dir / TTL as ensureLookupIndex so both schema
67248
- * caches share an opt-out (HIVEMIND_INDEX_MARKER_DIR) and a TTL knob.
67348
+ * History: an earlier path used a local marker file (`col_<name>` under
67349
+ * the index-marker dir) to skip even the SELECT after the first
67350
+ * confirmation, plus per-column ALTERs for `summary_embedding`,
67351
+ * `message_embedding`, `agent`, `plugin_version`. The marker existed
67352
+ * because Deeplake used to expose a ~30s post-ALTER bug where
67353
+ * subsequent INSERTs failed, so we wanted to keep ALTER traffic to a
67354
+ * minimum. The bug was re-verified on 2026-05-18 against
67355
+ * `api.deeplake.ai` (`test_plugin` org) and no longer reproduces
67356
+ * (71/71 INSERTs OK, first success 2ms after ALTER). The single SELECT
67357
+ * + targeted ALTER pattern survives the marker removal because: each
67358
+ * ALTER still costs ~800ms (so blanket sweeps are wasteful) and the
67359
+ * diff produces clearer logs than "ALTER all with IF NOT EXISTS".
67249
67360
  */
67250
- async ensureEmbeddingColumn(table, column) {
67251
- await this.ensureColumn(table, column, "FLOAT4[]");
67252
- }
67253
- /**
67254
- * Generic marker-gated column migration. Same SELECT-then-ALTER flow as
67255
- * ensureEmbeddingColumn, parameterized by SQL type so it can patch up any
67256
- * column that was added to the schema after the table was originally
67257
- * created. Used today for `summary_embedding`, `message_embedding`, and
67258
- * the `agent` column (added 2026-04-11) — the latter has no fallback if
67259
- * a user upgraded over a pre-2026-04-11 table, so every INSERT fails
67260
- * with `column "agent" does not exist`.
67261
- */
67262
- async ensureColumn(table, column, sqlType) {
67263
- const markers = await getIndexMarkerStore();
67264
- const markerPath = markers.buildIndexMarkerPath(this.workspaceId, this.orgId, table, `col_${column}`);
67265
- if (markers.hasFreshIndexMarker(markerPath))
67266
- return;
67267
- const colCheck = `SELECT 1 FROM information_schema.columns WHERE table_name = '${sqlStr(table)}' AND column_name = '${sqlStr(column)}' AND table_schema = '${sqlStr(this.workspaceId)}' LIMIT 1`;
67268
- const rows = await this.query(colCheck);
67269
- if (rows.length > 0) {
67270
- markers.writeIndexMarker(markerPath);
67271
- return;
67272
- }
67273
- try {
67274
- await this.query(`ALTER TABLE "${table}" ADD COLUMN ${column} ${sqlType}`);
67275
- } catch (e6) {
67276
- const msg = e6 instanceof Error ? e6.message : String(e6);
67277
- if (!/already exists/i.test(msg))
67278
- throw e6;
67279
- const recheck = await this.query(colCheck);
67280
- if (recheck.length === 0)
67281
- throw e6;
67282
- }
67283
- markers.writeIndexMarker(markerPath);
67361
+ async healSchema(table, columns) {
67362
+ await healMissingColumns({
67363
+ query: (sql) => this.query(sql),
67364
+ tableName: table,
67365
+ workspaceId: this.workspaceId,
67366
+ columns,
67367
+ log: log3
67368
+ });
67284
67369
  }
67285
67370
  /** List all tables in the workspace (with retry). */
67286
67371
  async listTables(forceRefresh = false) {
@@ -67351,20 +67436,21 @@ var DeeplakeApi = class {
67351
67436
  }
67352
67437
  throw lastErr;
67353
67438
  }
67354
- /** Create the memory table if it doesn't already exist. Migrate columns on existing tables. */
67439
+ /** Create the memory table if it doesn't already exist. Heal missing columns on existing tables. */
67355
67440
  async ensureTable(name) {
67441
+ if (!MEMORY_COLUMNS.some((c15) => c15.name === SUMMARY_EMBEDDING_COL)) {
67442
+ throw new Error(`MEMORY_COLUMNS missing "${SUMMARY_EMBEDDING_COL}" (embeddings/columns.ts drift)`);
67443
+ }
67356
67444
  const tbl = sqlIdent(name ?? this.tableName);
67357
67445
  const tables = await this.listTables();
67358
67446
  if (!tables.includes(tbl)) {
67359
67447
  log3(`table "${tbl}" not found, creating`);
67360
- await this.createTableWithRetry(`CREATE TABLE IF NOT EXISTS "${tbl}" (id TEXT NOT NULL DEFAULT '', path TEXT NOT NULL DEFAULT '', filename TEXT NOT NULL DEFAULT '', summary TEXT NOT NULL DEFAULT '', summary_embedding FLOAT4[], author TEXT NOT NULL DEFAULT '', mime_type TEXT NOT NULL DEFAULT 'text/plain', size_bytes BIGINT NOT NULL DEFAULT 0, project TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '', agent TEXT NOT NULL DEFAULT '', plugin_version TEXT NOT NULL DEFAULT '', creation_date TEXT NOT NULL DEFAULT '', last_update_date TEXT NOT NULL DEFAULT '') USING deeplake`, tbl);
67448
+ await this.createTableWithRetry(buildCreateTableSql(tbl, MEMORY_COLUMNS), tbl);
67361
67449
  log3(`table "${tbl}" created`);
67362
67450
  if (!tables.includes(tbl))
67363
67451
  this._tablesCache = [...tables, tbl];
67364
67452
  }
67365
- await this.ensureEmbeddingColumn(tbl, SUMMARY_EMBEDDING_COL);
67366
- await this.ensureColumn(tbl, "agent", "TEXT NOT NULL DEFAULT ''");
67367
- await this.ensureColumn(tbl, "plugin_version", "TEXT NOT NULL DEFAULT ''");
67453
+ await this.healSchema(tbl, MEMORY_COLUMNS);
67368
67454
  }
67369
67455
  /** Create the sessions table (uses JSONB for message since every row is a JSON event). */
67370
67456
  async ensureSessionsTable(name) {
@@ -67372,14 +67458,12 @@ var DeeplakeApi = class {
67372
67458
  const tables = await this.listTables();
67373
67459
  if (!tables.includes(safe)) {
67374
67460
  log3(`table "${safe}" not found, creating`);
67375
- await this.createTableWithRetry(`CREATE TABLE IF NOT EXISTS "${safe}" (id TEXT NOT NULL DEFAULT '', path TEXT NOT NULL DEFAULT '', filename TEXT NOT NULL DEFAULT '', message JSONB, message_embedding FLOAT4[], author TEXT NOT NULL DEFAULT '', mime_type TEXT NOT NULL DEFAULT 'application/json', size_bytes BIGINT NOT NULL DEFAULT 0, project TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '', agent TEXT NOT NULL DEFAULT '', plugin_version TEXT NOT NULL DEFAULT '', creation_date TEXT NOT NULL DEFAULT '', last_update_date TEXT NOT NULL DEFAULT '') USING deeplake`, safe);
67461
+ await this.createTableWithRetry(buildCreateTableSql(safe, SESSIONS_COLUMNS), safe);
67376
67462
  log3(`table "${safe}" created`);
67377
67463
  if (!tables.includes(safe))
67378
67464
  this._tablesCache = [...tables, safe];
67379
67465
  }
67380
- await this.ensureEmbeddingColumn(safe, MESSAGE_EMBEDDING_COL);
67381
- await this.ensureColumn(safe, "agent", "TEXT NOT NULL DEFAULT ''");
67382
- await this.ensureColumn(safe, "plugin_version", "TEXT NOT NULL DEFAULT ''");
67466
+ await this.healSchema(safe, SESSIONS_COLUMNS);
67383
67467
  await this.ensureLookupIndex(safe, "path_creation_date", `("path", "creation_date")`);
67384
67468
  }
67385
67469
  /**
@@ -67397,11 +67481,12 @@ var DeeplakeApi = class {
67397
67481
  const tables = await this.listTables();
67398
67482
  if (!tables.includes(safe)) {
67399
67483
  log3(`table "${safe}" not found, creating`);
67400
- await this.createTableWithRetry(`CREATE TABLE IF NOT EXISTS "${safe}" (id TEXT NOT NULL DEFAULT '', name TEXT NOT NULL DEFAULT '', project TEXT NOT NULL DEFAULT '', project_key TEXT NOT NULL DEFAULT '', local_path TEXT NOT NULL DEFAULT '', install TEXT NOT NULL DEFAULT 'project', source_sessions TEXT NOT NULL DEFAULT '[]', source_agent TEXT NOT NULL DEFAULT '', scope TEXT NOT NULL DEFAULT 'me', author TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '', trigger_text TEXT NOT NULL DEFAULT '', body TEXT NOT NULL DEFAULT '', version BIGINT NOT NULL DEFAULT 1, created_at TEXT NOT NULL DEFAULT '', updated_at TEXT NOT NULL DEFAULT '') USING deeplake`, safe);
67484
+ await this.createTableWithRetry(buildCreateTableSql(safe, SKILLS_COLUMNS), safe);
67401
67485
  log3(`table "${safe}" created`);
67402
67486
  if (!tables.includes(safe))
67403
67487
  this._tablesCache = [...tables, safe];
67404
67488
  }
67489
+ await this.healSchema(safe, SKILLS_COLUMNS);
67405
67490
  await this.ensureLookupIndex(safe, "project_key_name", `("project_key", "name")`);
67406
67491
  }
67407
67492
  };
@@ -316,6 +316,9 @@ ${s.body}
316
316
  import { randomUUID } from "node:crypto";
317
317
 
318
318
  // dist/src/utils/sql.js
319
+ function sqlStr(value) {
320
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/\0/g, "").replace(/[\x01-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
321
+ }
319
322
  function sqlIdent(name) {
320
323
  if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
321
324
  throw new Error(`Invalid SQL identifier: ${JSON.stringify(name)}`);
@@ -323,29 +326,142 @@ function sqlIdent(name) {
323
326
  return name;
324
327
  }
325
328
 
326
- // dist/src/skillify/skills-table.js
327
- function createSkillsTableSql(tableName) {
328
- const safe = sqlIdent(tableName);
329
- return `CREATE TABLE IF NOT EXISTS "${safe}" (id TEXT NOT NULL DEFAULT '', name TEXT NOT NULL DEFAULT '', project TEXT NOT NULL DEFAULT '', project_key TEXT NOT NULL DEFAULT '', local_path TEXT NOT NULL DEFAULT '', install TEXT NOT NULL DEFAULT 'project', source_sessions TEXT NOT NULL DEFAULT '[]', source_agent TEXT NOT NULL DEFAULT '', scope TEXT NOT NULL DEFAULT 'me', author TEXT NOT NULL DEFAULT '', contributors TEXT NOT NULL DEFAULT '[]', description TEXT NOT NULL DEFAULT '', trigger_text TEXT NOT NULL DEFAULT '', body TEXT NOT NULL DEFAULT '', version BIGINT NOT NULL DEFAULT 1, created_at TEXT NOT NULL DEFAULT '', updated_at TEXT NOT NULL DEFAULT '') USING deeplake`;
329
+ // dist/src/deeplake-schema.js
330
+ var MEMORY_COLUMNS = Object.freeze([
331
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
332
+ { name: "path", sql: "TEXT NOT NULL DEFAULT ''" },
333
+ { name: "filename", sql: "TEXT NOT NULL DEFAULT ''" },
334
+ { name: "summary", sql: "TEXT NOT NULL DEFAULT ''" },
335
+ { name: "summary_embedding", sql: "FLOAT4[]" },
336
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
337
+ { name: "mime_type", sql: "TEXT NOT NULL DEFAULT 'text/plain'" },
338
+ { name: "size_bytes", sql: "BIGINT NOT NULL DEFAULT 0" },
339
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
340
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
341
+ { name: "agent", sql: "TEXT NOT NULL DEFAULT ''" },
342
+ { name: "plugin_version", sql: "TEXT NOT NULL DEFAULT ''" },
343
+ { name: "creation_date", sql: "TEXT NOT NULL DEFAULT ''" },
344
+ { name: "last_update_date", sql: "TEXT NOT NULL DEFAULT ''" }
345
+ ]);
346
+ var SESSIONS_COLUMNS = Object.freeze([
347
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
348
+ { name: "path", sql: "TEXT NOT NULL DEFAULT ''" },
349
+ { name: "filename", sql: "TEXT NOT NULL DEFAULT ''" },
350
+ { name: "message", sql: "JSONB" },
351
+ { name: "message_embedding", sql: "FLOAT4[]" },
352
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
353
+ { name: "mime_type", sql: "TEXT NOT NULL DEFAULT 'application/json'" },
354
+ { name: "size_bytes", sql: "BIGINT NOT NULL DEFAULT 0" },
355
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
356
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
357
+ { name: "agent", sql: "TEXT NOT NULL DEFAULT ''" },
358
+ { name: "plugin_version", sql: "TEXT NOT NULL DEFAULT ''" },
359
+ { name: "creation_date", sql: "TEXT NOT NULL DEFAULT ''" },
360
+ { name: "last_update_date", sql: "TEXT NOT NULL DEFAULT ''" }
361
+ ]);
362
+ var SKILLS_COLUMNS = Object.freeze([
363
+ { name: "id", sql: "TEXT NOT NULL DEFAULT ''" },
364
+ { name: "name", sql: "TEXT NOT NULL DEFAULT ''" },
365
+ { name: "project", sql: "TEXT NOT NULL DEFAULT ''" },
366
+ { name: "project_key", sql: "TEXT NOT NULL DEFAULT ''" },
367
+ { name: "local_path", sql: "TEXT NOT NULL DEFAULT ''" },
368
+ { name: "install", sql: "TEXT NOT NULL DEFAULT 'project'" },
369
+ { name: "source_sessions", sql: "TEXT NOT NULL DEFAULT '[]'" },
370
+ { name: "source_agent", sql: "TEXT NOT NULL DEFAULT ''" },
371
+ { name: "scope", sql: "TEXT NOT NULL DEFAULT 'me'" },
372
+ { name: "author", sql: "TEXT NOT NULL DEFAULT ''" },
373
+ { name: "contributors", sql: "TEXT NOT NULL DEFAULT '[]'" },
374
+ { name: "description", sql: "TEXT NOT NULL DEFAULT ''" },
375
+ { name: "trigger_text", sql: "TEXT NOT NULL DEFAULT ''" },
376
+ { name: "body", sql: "TEXT NOT NULL DEFAULT ''" },
377
+ { name: "version", sql: "BIGINT NOT NULL DEFAULT 1" },
378
+ { name: "created_at", sql: "TEXT NOT NULL DEFAULT ''" },
379
+ { name: "updated_at", sql: "TEXT NOT NULL DEFAULT ''" }
380
+ ]);
381
+ function validateSchema(label, cols) {
382
+ const seen = /* @__PURE__ */ new Set();
383
+ for (const col of cols) {
384
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(col.name)) {
385
+ throw new Error(`${label}: column name "${col.name}" is not a valid SQL identifier`);
386
+ }
387
+ if (seen.has(col.name)) {
388
+ throw new Error(`${label}: duplicate column "${col.name}"`);
389
+ }
390
+ seen.add(col.name);
391
+ const notNull = /\bNOT\s+NULL\b/i.test(col.sql);
392
+ const hasDefault = /\bDEFAULT\b/i.test(col.sql);
393
+ if (notNull && !hasDefault) {
394
+ throw new Error(`${label}: column "${col.name}" is NOT NULL but has no DEFAULT \u2014 ALTER TABLE ADD COLUMN on a populated table would fail.`);
395
+ }
396
+ }
330
397
  }
331
- function addContributorsColumnSql(tableName) {
398
+ validateSchema("MEMORY_COLUMNS", MEMORY_COLUMNS);
399
+ validateSchema("SESSIONS_COLUMNS", SESSIONS_COLUMNS);
400
+ validateSchema("SKILLS_COLUMNS", SKILLS_COLUMNS);
401
+ function buildCreateTableSql(tableName, cols) {
332
402
  const safe = sqlIdent(tableName);
333
- return `ALTER TABLE "${safe}" ADD COLUMN IF NOT EXISTS contributors TEXT NOT NULL DEFAULT '[]'`;
403
+ const colSql = cols.map((c) => `${c.name} ${c.sql}`).join(", ");
404
+ return `CREATE TABLE IF NOT EXISTS "${safe}" (${colSql}) USING deeplake`;
334
405
  }
335
- function esc(s) {
336
- return s.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/[\x01-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
406
+ function buildIntrospectionSql(tableName, workspaceId) {
407
+ return `SELECT column_name FROM information_schema.columns WHERE table_name = '${sqlStr(tableName)}' AND table_schema = '${sqlStr(workspaceId)}'`;
408
+ }
409
+ async function healMissingColumns(args) {
410
+ const safeTable = sqlIdent(args.tableName);
411
+ const introspectSql = buildIntrospectionSql(args.tableName, args.workspaceId);
412
+ const rows = await args.query(introspectSql);
413
+ const existing = /* @__PURE__ */ new Set();
414
+ for (const row of rows) {
415
+ const v = row?.column_name;
416
+ if (typeof v === "string")
417
+ existing.add(v.toLowerCase());
418
+ }
419
+ const missingCols = args.columns.filter((c) => !existing.has(c.name.toLowerCase()));
420
+ const missing = missingCols.map((c) => c.name);
421
+ if (missingCols.length === 0)
422
+ return { missing, altered: [] };
423
+ const altered = [];
424
+ for (const col of missingCols) {
425
+ try {
426
+ await args.query(`ALTER TABLE "${safeTable}" ADD COLUMN ${col.name} ${col.sql}`);
427
+ altered.push(col.name);
428
+ args.log?.(`schema-heal: added "${args.tableName}"."${col.name}"`);
429
+ } catch (e) {
430
+ const msg = e instanceof Error ? e.message : String(e);
431
+ if (!/already exists/i.test(msg))
432
+ throw e;
433
+ const recheck = await args.query(introspectSql);
434
+ const present = recheck.some((r) => {
435
+ const v = r?.column_name;
436
+ return typeof v === "string" && v.toLowerCase() === col.name.toLowerCase();
437
+ });
438
+ if (!present)
439
+ throw e;
440
+ args.log?.(`schema-heal: "${args.tableName}"."${col.name}" appeared via race, treating as success`);
441
+ }
442
+ }
443
+ return { missing, altered };
337
444
  }
338
445
  function isMissingTableError(message) {
339
446
  if (!message)
340
447
  return false;
448
+ if (/permission denied|must be owner/i.test(message))
449
+ return false;
341
450
  if (/\bcolumn\b/i.test(message))
342
451
  return false;
343
452
  return /Table does not exist|relation .* does not exist|no such table/i.test(message);
344
453
  }
345
- function isMissingContributorsColumnError(message) {
454
+ function isMissingColumnError(message) {
346
455
  if (!message)
347
456
  return false;
348
- return /contributors.*(?:does not exist|not found|unknown)/i.test(message) || /(?:does not exist|unknown column).*contributors/i.test(message);
457
+ if (/permission denied|must be owner/i.test(message))
458
+ return false;
459
+ return /column ["']?[A-Za-z_][A-Za-z0-9_]*["']? .*does not exist/i.test(message) || /unknown column/i.test(message) || /no such column/i.test(message);
460
+ }
461
+
462
+ // dist/src/skillify/skills-table.js
463
+ function esc(s) {
464
+ return s.replace(/\\/g, "\\\\").replace(/'/g, "''").replace(/[\x01-\x08\x0b\x0c\x0e-\x1f\x7f]/g, "");
349
465
  }
350
466
  async function insertSkillRow(args) {
351
467
  const id = args.id ?? randomUUID();
@@ -354,14 +470,29 @@ async function insertSkillRow(args) {
354
470
  const sql = `INSERT INTO "${sqlIdent(args.tableName)}" (id, name, project, project_key, local_path, install, source_sessions, source_agent, scope, author, contributors, description, trigger_text, body, version, created_at, updated_at) VALUES ('${esc(id)}', '${esc(args.name)}', '${esc(args.project)}', '${esc(args.projectKey)}', '${esc(args.localPath)}', '${esc(args.install)}', '${esc(sourceSessionsJson)}', '${esc(args.sourceAgent)}', '${esc(args.scope)}', '${esc(args.author)}', '${esc(contributorsJson)}', '${esc(args.description)}', '${esc(args.trigger ?? "")}', '${esc(args.body)}', ${args.version}, '${esc(args.createdAt)}', '${esc(args.updatedAt)}')`;
355
471
  try {
356
472
  await args.query(sql);
473
+ return;
357
474
  } catch (e) {
358
- if (isMissingTableError(e?.message)) {
359
- await args.query(createSkillsTableSql(args.tableName));
475
+ const msg = e instanceof Error ? e.message : String(e);
476
+ if (isMissingTableError(msg)) {
477
+ await args.query(buildCreateTableSql(args.tableName, SKILLS_COLUMNS));
478
+ await healMissingColumns({
479
+ query: args.query,
480
+ tableName: args.tableName,
481
+ workspaceId: args.workspaceId,
482
+ columns: SKILLS_COLUMNS
483
+ });
360
484
  await args.query(sql);
361
485
  return;
362
486
  }
363
- if (isMissingContributorsColumnError(e?.message)) {
364
- await args.query(addContributorsColumnSql(args.tableName));
487
+ if (isMissingColumnError(msg)) {
488
+ const result = await healMissingColumns({
489
+ query: args.query,
490
+ tableName: args.tableName,
491
+ workspaceId: args.workspaceId,
492
+ columns: SKILLS_COLUMNS
493
+ });
494
+ if (result.missing.length === 0)
495
+ throw e;
365
496
  await args.query(sql);
366
497
  return;
367
498
  }
@@ -1001,6 +1132,7 @@ async function main() {
1001
1132
  await insertSkillRow({
1002
1133
  query,
1003
1134
  tableName: cfg.skillsTable,
1135
+ workspaceId: cfg.workspaceId,
1004
1136
  name: verdict2.name,
1005
1137
  project: cfg.project,
1006
1138
  projectKey: cfg.projectKey,