@polderlabs/bizar 4.2.2 → 4.2.3

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,129 @@
1
+ /**
2
+ * tests/memory-install-bootstrap.test.mjs
3
+ *
4
+ * Regression test for v4.2.2: the `bizar` CLI bootstrap path crashed with
5
+ * `ERR_AMBIGUOUS_MODULE_SYNTAX` on a fresh install because `promptAndInstallOptional`
6
+ * in `cli/install.mjs` referenced `__dirname` (a CommonJS global) without defining
7
+ * the ESM polyfill. This was only triggered by the bootstrap path, which the
8
+ * existing test suite never exercised.
9
+ *
10
+ * Test: spawn the CLI in a fresh temp project, with BIZAR_SKIP_INSTALL unset.
11
+ * The bootstrap should run and exit cleanly (rc 0 or rc 1 for missing auth, but
12
+ * NOT crash with ERR_AMBIGUOUS_MODULE_SYNTAX).
13
+ *
14
+ * Fix: move the `__dirname` polyfill to module scope in cli/install.mjs.
15
+ */
16
+
17
+ import { describe, it, beforeEach, afterEach } from 'node:test';
18
+ import assert from 'node:assert/strict';
19
+ import { tmpdir } from 'node:os';
20
+ import { join, dirname } from 'node:path';
21
+ import { mkdirSync, rmSync } from 'node:fs';
22
+ import { execFileSync } from 'node:child_process';
23
+ import { fileURLToPath } from 'node:url';
24
+
25
+ const __filename = fileURLToPath(import.meta.url);
26
+ const CLI_BIN = join(dirname(__filename), '..', '..', 'cli', 'bin.mjs');
27
+
28
+ const uid = () => `${Date.now()}-${Math.random().toString(36).slice(2)}`;
29
+
30
+ /**
31
+ * Spawn `bizar <subcmd>` in the given cwd, with the given env vars.
32
+ * Returns { code, stdout, stderr }.
33
+ *
34
+ * The bootstrap path is checked by NOT setting BIZAR_SKIP_INSTALL.
35
+ */
36
+ function runBizarWithoutSkip(args, cwd, extraEnv = {}) {
37
+ try {
38
+ const stdout = execFileSync('node', [CLI_BIN, ...args], {
39
+ cwd,
40
+ encoding: 'utf8',
41
+ stdio: 'pipe',
42
+ timeout: 30_000,
43
+ env: { ...process.env, ...extraEnv }, // no BIZAR_SKIP_INSTALL
44
+ });
45
+ return { code: 0, stdout, stderr: '' };
46
+ } catch (err) {
47
+ return {
48
+ code: err.status ?? 1,
49
+ stdout: err.stdout ? err.stdout.toString() : '',
50
+ stderr: err.stderr ? err.stderr.toString() : (err.message ?? ''),
51
+ };
52
+ }
53
+ }
54
+
55
+ describe('bizar CLI bootstrap (v4.2.3 regression)', () => {
56
+ let projectRoot;
57
+ let homeDir;
58
+
59
+ beforeEach(() => {
60
+ projectRoot = join(tmpdir(), `bizar-bootstrap-${uid()}`);
61
+ homeDir = join(tmpdir(), `bizar-bootstrap-home-${uid()}`);
62
+ mkdirSync(projectRoot, { recursive: true });
63
+ mkdirSync(homeDir, { recursive: true });
64
+ });
65
+
66
+ afterEach(() => {
67
+ try { rmSync(projectRoot, { recursive: true, force: true }); } catch { /* ignore */ }
68
+ try { rmSync(homeDir, { recursive: true, force: true }); } catch { /* ignore */ }
69
+ });
70
+
71
+ it('bizar --version does not crash with ERR_AMBIGUOUS_MODULE_SYNTAX', () => {
72
+ const r = runBizarWithoutSkip(['--version'], projectRoot, { HOME: homeDir });
73
+ // Whatever exit code, the key thing is the bootstrap doesn't crash
74
+ assert.ok(
75
+ !r.stderr.includes('ERR_AMBIGUOUS_MODULE_SYNTAX'),
76
+ `bootstrap crashed with ERR_AMBIGUOUS_MODULE_SYNTAX. stderr=${r.stderr}`,
77
+ );
78
+ assert.ok(
79
+ !r.stderr.includes('Cannot determine intended module format'),
80
+ `bootstrap module-format error. stderr=${r.stderr}`,
81
+ );
82
+ });
83
+
84
+ it('bizar memory status does not crash with ERR_AMBIGUOUS_MODULE_SYNTAX', () => {
85
+ const r = runBizarWithoutSkip(['memory', 'status'], projectRoot, { HOME: homeDir });
86
+ assert.ok(
87
+ !r.stderr.includes('ERR_AMBIGUOUS_MODULE_SYNTAX'),
88
+ `bootstrap crashed with ERR_AMBIGUOUS_MODULE_SYNTAX. stderr=${r.stderr}`,
89
+ );
90
+ assert.ok(
91
+ !r.stderr.includes('Cannot determine intended module format'),
92
+ `bootstrap module-format error. stderr=${r.stderr}`,
93
+ );
94
+ });
95
+
96
+ it('bizar memory doctor does not crash with ERR_AMBIGUOUS_MODULE_SYNTAX', () => {
97
+ const r = runBizarWithoutSkip(['memory', 'doctor'], projectRoot, { HOME: homeDir });
98
+ assert.ok(
99
+ !r.stderr.includes('ERR_AMBIGUOUS_MODULE_SYNTAX'),
100
+ `bootstrap crashed with ERR_AMBIGUOUS_MODULE_SYNTAX. stderr=${r.stderr}`,
101
+ );
102
+ });
103
+
104
+ it('install.mjs source defines __dirname at module scope (regression guard)', () => {
105
+ // Read the source of cli/install.mjs and check that __dirname is
106
+ // defined at module scope (not only inside runInstaller).
107
+ const installSrc = execFileSync('node', ['-e', `
108
+ const fs = require('fs');
109
+ const src = fs.readFileSync(${JSON.stringify(join(dirname(CLI_BIN), 'install.mjs'))}, 'utf8');
110
+ // Find first __dirname definition (should be at module scope, not inside a function)
111
+ const lines = src.split('\\n');
112
+ let firstDefine = -1;
113
+ for (let i = 0; i < lines.length; i++) {
114
+ if (lines[i].includes('__dirname = dirname(fileURLToPath')) {
115
+ firstDefine = i;
116
+ break;
117
+ }
118
+ }
119
+ // The first definition must be BEFORE any function definition that uses __dirname
120
+ // Simple heuristic: it must be within the first 20 lines
121
+ process.stdout.write(JSON.stringify({ firstDefine, totalLines: lines.length }));
122
+ `], { encoding: 'utf8' });
123
+ const { firstDefine, totalLines } = JSON.parse(installSrc);
124
+ assert.ok(
125
+ firstDefine >= 0 && firstDefine < 20,
126
+ `__dirname polyfill should be at module scope (first 20 lines). Found at line ${firstDefine + 1} of ${totalLines}.`,
127
+ );
128
+ });
129
+ });
package/cli/install.mjs CHANGED
@@ -1,7 +1,17 @@
1
1
  import chalk from 'chalk';
2
2
  import boxen from 'boxen';
3
3
  import { existsSync, lstatSync, rmSync } from 'node:fs';
4
- import { join } from 'node:path';
4
+ import { join, dirname } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ // ── ESM `__dirname` polyfill ────────────────────────────────────────────────
8
+ // `__dirname` is a CommonJS global. ESM modules don't have it. We define it
9
+ // at module scope so all functions in this file can use it without each
10
+ // having to recreate the polyfill. (v4.2.3 — previously only `runInstaller`
11
+ // had it, which caused `promptAndInstallOptional` to crash with
12
+ // `ERR_AMBIGUOUS_MODULE_SYNTAX` when the bootstrap path fired on a fresh
13
+ // install.)
14
+ const __dirname = dirname(fileURLToPath(import.meta.url));
5
15
 
6
16
  import { showBanner, showPantheon, sectionHeading } from './banner.mjs';
7
17
  import { promptComponents, promptInstallMode, promptAgents, promptSkillPacks, promptApiKeys, promptConfirmInstall, promptRestartOpenCode } from './prompts.mjs';
@@ -232,11 +242,10 @@ export async function installPluginFromGlobal(opts = {}) {
232
242
  */
233
243
  export async function runInstaller() {
234
244
  const { existsSync } = await import('node:fs');
235
- const { join, dirname } = await import('node:path');
236
- const { fileURLToPath } = await import('node:url');
245
+ const { join } = await import('node:path');
237
246
  const { spawnSync } = await import('node:child_process');
238
247
 
239
- const __dirname = dirname(fileURLToPath(import.meta.url));
248
+ // __dirname is defined at module scope (see top of file)
240
249
  // cli/install.mjs → ../install.sh
241
250
  const installSh = join(__dirname, '..', 'install.sh');
242
251
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polderlabs/bizar",
3
- "version": "4.2.2",
3
+ "version": "4.2.3",
4
4
  "description": "Norse-pantheon multi-agent system for opencode — 13 agents across 4 cost tiers with cost-aware routing, plans, and a configurable agent harness. v4 ships as a single npm package bundling the dashboard server, opencode plugin, and typed SDK.",
5
5
  "type": "module",
6
6
  "bin": {