@adhdev/daemon-core 0.9.82-rc.332 → 0.9.82-rc.333

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.
@@ -0,0 +1,21 @@
1
+ import type BetterSqlite3 from 'better-sqlite3';
2
+ /**
3
+ * Load the `better-sqlite3` constructor in a way that survives every runtime the
4
+ * daemon ships in.
5
+ *
6
+ * The naive `typeof require === 'function' ? require : createRequire(import.meta.url)`
7
+ * is unsafe inside the daemon-cloud bundle: esbuild emits CJS output but, for any
8
+ * chunk that touches `import.meta.url`, it shims the local `require` with a stub
9
+ * that THROWS `Dynamic require of "..." is not supported`. That stub is still
10
+ * `typeof === 'function'`, so the ternary picks it and the call throws — it never
11
+ * reaches the `createRequire` fallback. The failure surfaced as `mesh_send_task`
12
+ * crashing the whole tool call instead of gracefully degrading.
13
+ *
14
+ * The robust approach is to ATTEMPT the real `require` and CATCH the esbuild stub's
15
+ * throw, then fall back to `createRequire`. We try multiple resolution bases so the
16
+ * load works whether the code runs as:
17
+ * - a genuine CJS module (bare `require` works),
18
+ * - an esbuild CJS bundle with the throwing `require` shim (createRequire(import.meta.url)),
19
+ * - a pure ESM module where `require` is undefined (createRequire(import.meta.url)).
20
+ */
21
+ export declare function loadBetterSqlite3(): typeof BetterSqlite3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adhdev/daemon-core",
3
- "version": "0.9.82-rc.332",
3
+ "version": "0.9.82-rc.333",
4
4
  "description": "ADHDev daemon core — CDP, IDE detection, providers, command execution",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,7 +46,7 @@
46
46
  "author": "vilmire",
47
47
  "license": "AGPL-3.0-or-later",
48
48
  "dependencies": {
49
- "@adhdev/mesh-shared": "0.9.82-rc.332",
49
+ "@adhdev/mesh-shared": "0.9.82-rc.333",
50
50
  "@adhdev/session-host-core": "*",
51
51
  "@agentclientprotocol/sdk": "^0.16.1",
52
52
  "ajv": "^8.20.0",
@@ -1,6 +1,6 @@
1
1
  import { existsSync, mkdirSync, readFileSync, renameSync, statSync } from 'fs';
2
2
  import { dirname, join } from 'path';
3
- import { createRequire } from 'module';
3
+ import { loadBetterSqlite3 } from '../system/load-better-sqlite3.js';
4
4
  import { getLedgerDir } from './mesh-ledger.js';
5
5
  import { nodeSatisfiesRequiredTags } from './mesh-work-queue.js';
6
6
  import type { MeshTaskStatus, MeshWorkQueueEntry } from './mesh-work-queue.js';
@@ -11,10 +11,7 @@ let DatabaseCtor: typeof BetterSqlite3 | undefined;
11
11
 
12
12
  function loadDatabaseCtor(): typeof BetterSqlite3 {
13
13
  if (DatabaseCtor) return DatabaseCtor;
14
- const runtimeRequire = typeof require === 'function'
15
- ? require
16
- : createRequire(import.meta.url);
17
- DatabaseCtor = runtimeRequire('better-sqlite3') as typeof BetterSqlite3;
14
+ DatabaseCtor = loadBetterSqlite3() as typeof BetterSqlite3;
18
15
  return DatabaseCtor;
19
16
  }
20
17
 
@@ -19,6 +19,7 @@
19
19
  import * as fs from 'node:fs';
20
20
  import * as path from 'node:path';
21
21
  import * as os from 'node:os';
22
+ import { loadBetterSqlite3 } from '../../system/load-better-sqlite3.js';
22
23
 
23
24
  export interface NativeHistoryMessage {
24
25
  id: string;
@@ -61,8 +62,7 @@ function statMtimeMs(p: string): number {
61
62
  function openDb(): any | null {
62
63
  if (!fs.existsSync(HERMES_STATE_DB)) return null;
63
64
  try {
64
- // eslint-disable-next-line @typescript-eslint/no-require-imports
65
- const Database = require('better-sqlite3');
65
+ const Database = loadBetterSqlite3();
66
66
  return new Database(HERMES_STATE_DB, { readonly: true, fileMustExist: true });
67
67
  } catch {
68
68
  return null;
@@ -22,6 +22,7 @@ import * as fs from 'node:fs';
22
22
  import * as os from 'node:os';
23
23
  import * as path from 'node:path';
24
24
  import { LOG } from '../../logging/logger.js';
25
+ import { loadBetterSqlite3 } from '../../system/load-better-sqlite3.js';
25
26
  import type {
26
27
  NativeHistoryConfig,
27
28
  NativeHistoryJsonlSource,
@@ -253,8 +254,7 @@ function executeSqlite(src: NativeHistorySqliteSource, input: NativeHistoryInput
253
254
 
254
255
  let Database: any;
255
256
  try {
256
- // eslint-disable-next-line @typescript-eslint/no-require-imports
257
- Database = require('better-sqlite3');
257
+ Database = loadBetterSqlite3();
258
258
  } catch { return null; }
259
259
 
260
260
  let db: any;
@@ -0,0 +1,68 @@
1
+ import { createRequire } from 'module';
2
+ import type BetterSqlite3 from 'better-sqlite3';
3
+
4
+ let cached: typeof BetterSqlite3 | undefined;
5
+
6
+ /**
7
+ * Load the `better-sqlite3` constructor in a way that survives every runtime the
8
+ * daemon ships in.
9
+ *
10
+ * The naive `typeof require === 'function' ? require : createRequire(import.meta.url)`
11
+ * is unsafe inside the daemon-cloud bundle: esbuild emits CJS output but, for any
12
+ * chunk that touches `import.meta.url`, it shims the local `require` with a stub
13
+ * that THROWS `Dynamic require of "..." is not supported`. That stub is still
14
+ * `typeof === 'function'`, so the ternary picks it and the call throws — it never
15
+ * reaches the `createRequire` fallback. The failure surfaced as `mesh_send_task`
16
+ * crashing the whole tool call instead of gracefully degrading.
17
+ *
18
+ * The robust approach is to ATTEMPT the real `require` and CATCH the esbuild stub's
19
+ * throw, then fall back to `createRequire`. We try multiple resolution bases so the
20
+ * load works whether the code runs as:
21
+ * - a genuine CJS module (bare `require` works),
22
+ * - an esbuild CJS bundle with the throwing `require` shim (createRequire(import.meta.url)),
23
+ * - a pure ESM module where `require` is undefined (createRequire(import.meta.url)).
24
+ */
25
+ export function loadBetterSqlite3(): typeof BetterSqlite3 {
26
+ if (cached) return cached;
27
+
28
+ const errors: unknown[] = [];
29
+
30
+ // 1) Real CJS require, when present and not the esbuild throwing shim.
31
+ if (typeof require === 'function') {
32
+ try {
33
+ cached = require('better-sqlite3') as typeof BetterSqlite3;
34
+ return cached;
35
+ } catch (e) {
36
+ // Either the esbuild "Dynamic require is not supported" shim, or a
37
+ // genuine resolution failure. Fall through to createRequire.
38
+ errors.push(e);
39
+ }
40
+ }
41
+
42
+ // 2) createRequire anchored at this module's URL (works in ESM and in esbuild
43
+ // CJS bundles, where import.meta.url is rewritten to a usable value).
44
+ try {
45
+ const metaUrl = typeof import.meta?.url === 'string' ? import.meta.url : undefined;
46
+ if (metaUrl) {
47
+ cached = createRequire(metaUrl)('better-sqlite3') as typeof BetterSqlite3;
48
+ return cached;
49
+ }
50
+ } catch (e) {
51
+ errors.push(e);
52
+ }
53
+
54
+ // 3) Last resort: createRequire anchored at the current working directory.
55
+ try {
56
+ cached = createRequire(`${process.cwd()}/__adhdev_better_sqlite3_loader__.js`)(
57
+ 'better-sqlite3',
58
+ ) as typeof BetterSqlite3;
59
+ return cached;
60
+ } catch (e) {
61
+ errors.push(e);
62
+ }
63
+
64
+ const detail = errors
65
+ .map((e) => (e instanceof Error ? e.message : String(e)))
66
+ .join('; ');
67
+ throw new Error(`Failed to load better-sqlite3: ${detail}`);
68
+ }