@pellux/goodvibes-sdk 0.21.27 → 0.21.28

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.
@@ -18,19 +18,19 @@ export interface CompanionTokenRecord {
18
18
  readonly createdAt: number;
19
19
  }
20
20
  /**
21
- * Load the stored companion token for a surface, or generate and persist a new one.
21
+ * Load the stored companion token, or generate and persist a new one.
22
+ * Token is always written to <daemonHomeDir>/operator-tokens.json at mode 0600.
22
23
  */
23
- export declare function getOrCreateCompanionToken(surface: string, options?: {
24
- basePath?: string;
25
- daemonHomeDir?: string;
24
+ export declare function getOrCreateCompanionToken(surface: string, options: {
25
+ daemonHomeDir: string;
26
26
  regenerate?: boolean;
27
27
  }): CompanionPairingResult;
28
28
  /**
29
- * Regenerate the companion token for a surface, replacing any existing token.
29
+ * Regenerate the companion token, replacing any existing token.
30
+ * Written to <daemonHomeDir>/operator-tokens.json at mode 0600.
30
31
  */
31
- export declare function regenerateCompanionToken(surface: string, options?: {
32
- basePath?: string;
33
- daemonHomeDir?: string;
32
+ export declare function regenerateCompanionToken(surface: string, options: {
33
+ daemonHomeDir: string;
34
34
  }): CompanionPairingResult;
35
35
  /**
36
36
  * Build a CompanionConnectionInfo object from raw parameters.
@@ -1 +1 @@
1
- {"version":3,"file":"companion-token.d.ts","sourceRoot":"","sources":["../../../../src/_internal/platform/pairing/companion-token.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,mFAAmF;IACnF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AA4CD;;GAEG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GAC5E,sBAAsB,CAyBxB;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,sBAAsB,CAExB;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE;IACpD,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,uBAAuB,CAS1B;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,uBAAuB,GAAG,MAAM,CAS7E"}
1
+ {"version":3,"file":"companion-token.d.ts","sourceRoot":"","sources":["../../../../src/_internal/platform/pairing/companion-token.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QA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E"}
@@ -1,5 +1,5 @@
1
1
  import { randomBytes } from 'node:crypto';
2
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
2
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, chmodSync } from 'node:fs';
3
3
  import { join, dirname } from 'node:path';
4
4
  const TOKEN_PREFIX = 'gv_';
5
5
  function generateTokenValue() {
@@ -9,41 +9,25 @@ function generatePeerId() {
9
9
  return randomBytes(12).toString('hex');
10
10
  }
11
11
  /**
12
- * Resolve the token store path.
12
+ * Resolve the operator token store path.
13
13
  *
14
- * Resolution order (first match wins):
15
- * 1. daemonHomeDir daemon-identity-scoped path (0.21.19+, recommended)
16
- * 2. basePath workspace-scoped path (0.21.17–0.21.18 layout, legacy fallback)
17
- * 3. process.cwd() — absolute fallback
14
+ * The only valid location is <daemonHomeDir>/operator-tokens.json.
15
+ * Operator tokens are global (daemon-home scoped) since 0.21.28.
16
+ * No workspace-scoped fallback exists.
18
17
  *
19
- * Storing tokens under daemonHomeDir means workspace swaps never invalidate
20
- * paired companions — a token paired once works regardless of which working
21
- * directory the daemon is currently serving.
18
+ * @throws {Error} when daemonHomeDir is not provided all callers must supply it.
22
19
  */
23
- function resolveSharedTokenPath(basePath, daemonHomeDir) {
24
- if (daemonHomeDir) {
25
- return join(daemonHomeDir, 'operator-tokens.json');
26
- }
27
- const base = basePath ?? process.cwd();
28
- return join(base, '.goodvibes', 'operator-tokens.json');
20
+ function resolveSharedTokenPath(daemonHomeDir) {
21
+ return join(daemonHomeDir, 'operator-tokens.json');
29
22
  }
30
23
  /**
31
- * Resolve the path to the companion token file.
32
- *
33
- * Always resolves to the shared workspace-level path regardless of surface,
34
- * so that tokens are portable across TUI-embedded and standalone daemon postures.
35
- *
36
- * @deprecated `surface` parameter is ignored; use {@link resolveSharedTokenPath} directly.
37
- */
38
- function resolveTokenPath(_surface, basePath, daemonHomeDir) {
39
- return resolveSharedTokenPath(basePath, daemonHomeDir);
40
- }
41
- /**
42
- * Load the stored companion token for a surface, or generate and persist a new one.
24
+ * Load the stored companion token, or generate and persist a new one.
25
+ * Token is always written to <daemonHomeDir>/operator-tokens.json at mode 0600.
43
26
  */
44
27
  export function getOrCreateCompanionToken(surface, options) {
45
- const tokenPath = resolveTokenPath(surface, options?.basePath, options?.daemonHomeDir);
46
- if (!options?.regenerate && existsSync(tokenPath)) {
28
+ void surface; // surface parameter retained for API compatibility; token path is global
29
+ const tokenPath = resolveSharedTokenPath(options.daemonHomeDir);
30
+ if (!options.regenerate && existsSync(tokenPath)) {
47
31
  try {
48
32
  const raw = readFileSync(tokenPath, 'utf-8');
49
33
  const record = JSON.parse(raw);
@@ -60,12 +44,19 @@ export function getOrCreateCompanionToken(surface, options) {
60
44
  peerId: generatePeerId(),
61
45
  createdAt: Date.now(),
62
46
  };
63
- mkdirSync(dirname(tokenPath), { recursive: true });
64
- writeFileSync(tokenPath, JSON.stringify(record, null, 2), 'utf-8');
47
+ const dir = dirname(tokenPath);
48
+ mkdirSync(dir, { recursive: true });
49
+ // Write with mode 0600 (owner read/write only) and enforce after write
50
+ writeFileSync(tokenPath, JSON.stringify(record, null, 2), { encoding: 'utf-8', mode: 0o600 });
51
+ try {
52
+ chmodSync(tokenPath, 0o600);
53
+ }
54
+ catch { /* best-effort */ }
65
55
  return { token: record.token, peerId: record.peerId, createdAt: record.createdAt };
66
56
  }
67
57
  /**
68
- * Regenerate the companion token for a surface, replacing any existing token.
58
+ * Regenerate the companion token, replacing any existing token.
59
+ * Written to <daemonHomeDir>/operator-tokens.json at mode 0600.
69
60
  */
70
61
  export function regenerateCompanionToken(surface, options) {
71
62
  return getOrCreateCompanionToken(surface, { ...options, regenerate: true });
@@ -1,6 +1,6 @@
1
1
  import { readFileSync } from 'node:fs';
2
2
  import { join } from 'node:path';
3
- let version = '0.21.27';
3
+ let version = '0.21.28';
4
4
  try {
5
5
  const pkg = JSON.parse(readFileSync(join(import.meta.dir, '..', '..', 'package.json'), 'utf-8'));
6
6
  version = pkg.version ?? version;
@@ -18,7 +18,8 @@
18
18
  * - If ~/.goodvibes/daemon/ does not exist:
19
19
  * - ~/.goodvibes/tui/auth-users.json → ~/.goodvibes/daemon/auth-users.json
20
20
  * - ~/.goodvibes/tui/auth-bootstrap.txt → ~/.goodvibes/daemon/auth-bootstrap.txt
21
- * - <cwd>/.goodvibes/operator-tokens.json ~/.goodvibes/daemon/operator-tokens.json (F3 revision)
21
+ * - Operator tokens are global-only: read/written exclusively at
22
+ * <daemonHomeDir>/operator-tokens.json. No workspace-scoped paths.
22
23
  * - Old paths are left intact (never deleted) to avoid breaking older binaries.
23
24
  */
24
25
  import type { RuntimeEventBus } from '../runtime/events/index.js';
@@ -31,8 +32,6 @@ export interface DaemonHomeDirs {
31
32
  export interface DaemonHomeOptions {
32
33
  /** Value of --daemon-home CLI flag, if provided. */
33
34
  readonly daemonHomeArg?: string | undefined;
34
- /** Current working directory, used as base for operator-tokens migration. */
35
- readonly cwd?: string;
36
35
  /** Override process.env for testing. */
37
36
  readonly env?: NodeJS.ProcessEnv;
38
37
  }
@@ -48,14 +47,36 @@ export interface DaemonHomeMigrationDeps {
48
47
  * Resolve the daemon home directory from CLI flag, environment variable, or default.
49
48
  */
50
49
  export declare function resolveDaemonHomeDir(options?: DaemonHomeOptions): string;
50
+ /**
51
+ * Returns the single canonical path for operator tokens.
52
+ * All reads and writes MUST use this path. No workspace-scoped fallback exists.
53
+ */
54
+ export declare function resolveOperatorTokenPath(daemonHomeDir: string): string;
51
55
  /**
52
56
  * Run migration if the daemon home directory does not yet exist.
53
57
  * Creates the directory and copies identity files from legacy paths if found.
54
58
  * Old files are NOT deleted.
55
59
  *
60
+ * Operator tokens are NOT migrated from workspace-scoped paths — the global
61
+ * daemon-home path is canonical since 0.21.28. If tokens are missing,
62
+ * the first pairing operation will create them at the global path.
63
+ *
56
64
  * Returns `freshInstall: true` when migration ran, `false` when the dir already existed.
57
65
  */
58
66
  export declare function runDaemonHomeMigration(daemonHomeDir: string, options?: DaemonHomeOptions, deps?: DaemonHomeMigrationDeps): DaemonHomeDirs;
67
+ /**
68
+ * Write operator tokens to the global daemon-home path with mode 0600.
69
+ * All token provisioning MUST go through this function.
70
+ *
71
+ * Uses a write-to-tmp-then-rename pattern for atomicity.
72
+ * Applies chmod 0600 after rename so the file is never world-readable.
73
+ */
74
+ export declare function writeOperatorTokenFile(daemonHomeDir: string, content: string): void;
75
+ /**
76
+ * Read operator tokens from the global daemon-home path.
77
+ * Returns undefined when the file does not exist or cannot be parsed.
78
+ */
79
+ export declare function readOperatorTokenFile(daemonHomeDir: string): string | undefined;
59
80
  /**
60
81
  * Read a single key from daemon-settings.json, or return undefined if missing.
61
82
  */
@@ -1 +1 @@
1
- {"version":3,"file":"daemon-home.d.ts","sourceRoot":"","sources":["../../../../src/_internal/platform/workspace/daemon-home.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAOH,OAAO,KAAK,EAAE,eAAe,EAAwB,MAAM,4BAA4B,CAAC;AAOxF,MAAM,WAAW,cAAc;IAC7B,6EAA6E;IAC7E,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,iFAAiF;IACjF,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;CAChC;AAED,MAAM,WAAW,iBAAiB;IAChC,oDAAoD;IACpD,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,6EAA6E;IAC7E,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,wCAAwC;IACxC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CAClC;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,eAAe,CAAC;CACvC;AAMD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAiB5E;AAMD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC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,uBAA4B,GACjC,cAAc,CA+BhB;AAMD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CASnF;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAQ/E;AAMD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAWxF;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,aAAa,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAc1F"}
@@ -18,10 +18,11 @@
18
18
  * - If ~/.goodvibes/daemon/ does not exist:
19
19
  * - ~/.goodvibes/tui/auth-users.json → ~/.goodvibes/daemon/auth-users.json
20
20
  * - ~/.goodvibes/tui/auth-bootstrap.txt → ~/.goodvibes/daemon/auth-bootstrap.txt
21
- * - <cwd>/.goodvibes/operator-tokens.json ~/.goodvibes/daemon/operator-tokens.json (F3 revision)
21
+ * - Operator tokens are global-only: read/written exclusively at
22
+ * <daemonHomeDir>/operator-tokens.json. No workspace-scoped paths.
22
23
  * - Old paths are left intact (never deleted) to avoid breaking older binaries.
23
24
  */
24
- import { existsSync, mkdirSync, copyFileSync, readFileSync, writeFileSync, renameSync, readdirSync } from 'node:fs';
25
+ import { existsSync, mkdirSync, copyFileSync, readFileSync, writeFileSync, renameSync, chmodSync } from 'node:fs';
25
26
  import { join, isAbsolute, resolve, dirname } from 'node:path';
26
27
  import { homedir } from 'node:os';
27
28
  import { logger } from '../utils/logger.js';
@@ -49,6 +50,16 @@ export function resolveDaemonHomeDir(options = {}) {
49
50
  return join(homedir(), '.goodvibes', 'daemon');
50
51
  }
51
52
  // ---------------------------------------------------------------------------
53
+ // Global operator token path
54
+ // ---------------------------------------------------------------------------
55
+ /**
56
+ * Returns the single canonical path for operator tokens.
57
+ * All reads and writes MUST use this path. No workspace-scoped fallback exists.
58
+ */
59
+ export function resolveOperatorTokenPath(daemonHomeDir) {
60
+ return join(daemonHomeDir, 'operator-tokens.json');
61
+ }
62
+ // ---------------------------------------------------------------------------
52
63
  // One-time migration
53
64
  // ---------------------------------------------------------------------------
54
65
  /**
@@ -56,6 +67,10 @@ export function resolveDaemonHomeDir(options = {}) {
56
67
  * Creates the directory and copies identity files from legacy paths if found.
57
68
  * Old files are NOT deleted.
58
69
  *
70
+ * Operator tokens are NOT migrated from workspace-scoped paths — the global
71
+ * daemon-home path is canonical since 0.21.28. If tokens are missing,
72
+ * the first pairing operation will create them at the global path.
73
+ *
59
74
  * Returns `freshInstall: true` when migration ran, `false` when the dir already existed.
60
75
  */
61
76
  export function runDaemonHomeMigration(daemonHomeDir, options = {}, deps = {}) {
@@ -66,7 +81,6 @@ export function runDaemonHomeMigration(daemonHomeDir, options = {}, deps = {}) {
66
81
  // Create the daemon home directory tree
67
82
  mkdirSync(daemonHomeDir, { recursive: true });
68
83
  const userGoodVibesRoot = join(homedir(), '.goodvibes');
69
- const cwd = options.cwd ?? process.cwd();
70
84
  // Migrate auth-users.json from tui surface path
71
85
  const legacyAuthUsers = join(userGoodVibesRoot, 'tui', 'auth-users.json');
72
86
  if (existsSync(legacyAuthUsers)) {
@@ -77,67 +91,52 @@ export function runDaemonHomeMigration(daemonHomeDir, options = {}, deps = {}) {
77
91
  if (existsSync(legacyBootstrap)) {
78
92
  safeCopy(legacyBootstrap, join(daemonHomeDir, 'auth-bootstrap.txt'));
79
93
  }
80
- // Migrate operator-tokens.json search multiple legacy paths (F3 revision).
81
- // 0.21.17 used <cwd>/.goodvibes/operator-tokens.json (workspace-scoped).
82
- // 0.21.16 and earlier used surface-scoped ~/.goodvibes/<surface>/companion-token.json.
83
- // 0.21.19+ canonical path is <daemonHomeDir>/operator-tokens.json.
84
- const destTokenPath = join(daemonHomeDir, 'operator-tokens.json');
85
- if (!existsSync(destTokenPath)) {
86
- // Priority 1: workspace-scoped path from 0.21.17
87
- const legacyWorkspaceTokens = join(cwd, '.goodvibes', 'operator-tokens.json');
88
- // Priority 2: surface-scoped legacy tokens from 0.21.16 and earlier
89
- const legacySurfaceToken = join(userGoodVibesRoot, 'tui', 'companion-token.json');
90
- // Priority 3: XDG data home if set
91
- const xdgDataHome = options.env?.['XDG_DATA_HOME'];
92
- const xdgToken = xdgDataHome ? join(xdgDataHome, 'goodvibes', 'operator-tokens.json') : null;
93
- // Scan for any surface-scoped companion-token.json files under ~/.goodvibes/
94
- const surfaceScopedTokens = [];
95
- try {
96
- const entries = readdirSync(userGoodVibesRoot, { withFileTypes: true });
97
- for (const entry of entries) {
98
- if (entry.isDirectory()) {
99
- const candidate = join(userGoodVibesRoot, entry.name, 'companion-token.json');
100
- if (existsSync(candidate))
101
- surfaceScopedTokens.push(candidate);
102
- }
103
- }
104
- }
105
- catch {
106
- // Best-effort scan
107
- }
108
- const tokenSources = [
109
- legacyWorkspaceTokens,
110
- ...(xdgToken ? [xdgToken] : []),
111
- legacySurfaceToken,
112
- ...surfaceScopedTokens,
113
- ];
114
- for (const src of tokenSources) {
115
- if (!existsSync(src))
116
- continue;
117
- // Validate JSON before copying — corrupt JSON must not be migrated.
118
- try {
119
- JSON.parse(readFileSync(src, 'utf-8'));
120
- }
121
- catch (parseErr) {
122
- const reason = parseErr instanceof Error ? parseErr.message : String(parseErr);
123
- logger.warn('daemon-home: skipping corrupt token file during migration', {
124
- sourcePath: src,
125
- reason,
126
- });
127
- _emitMigrationEvent(deps.runtimeBus, { type: 'WORKSPACE_IDENTITY_MIGRATION_FAILED', sourcePath: src, reason });
128
- safeCopy(src, destTokenPath, { skipIfInvalid: true });
129
- continue;
130
- }
131
- if (safeCopy(src, destTokenPath)) {
132
- logger.info('daemon-home: migrated operator token', { from: src, to: destTokenPath });
133
- _emitMigrationEvent(deps.runtimeBus, { type: 'WORKSPACE_IDENTITY_MIGRATED', from: src, to: destTokenPath });
134
- }
135
- break; // First valid source wins
136
- }
137
- }
94
+ // NOTE: Operator tokens are NOT migrated from legacy workspace-scoped paths.
95
+ // The canonical path is <daemonHomeDir>/operator-tokens.json (global, set at 0600).
96
+ // If no token file exists at the canonical path, the first pairing call will
97
+ // create it via getOrCreateCompanionToken (companion-token.ts).
98
+ void deps; // deps.runtimeBus reserved for future migration event emission
138
99
  return { daemonHomeDir, freshInstall: true };
139
100
  }
140
101
  // ---------------------------------------------------------------------------
102
+ // Operator token file write (global-only, mode 0600)
103
+ // ---------------------------------------------------------------------------
104
+ /**
105
+ * Write operator tokens to the global daemon-home path with mode 0600.
106
+ * All token provisioning MUST go through this function.
107
+ *
108
+ * Uses a write-to-tmp-then-rename pattern for atomicity.
109
+ * Applies chmod 0600 after rename so the file is never world-readable.
110
+ */
111
+ export function writeOperatorTokenFile(daemonHomeDir, content) {
112
+ const tokenPath = resolveOperatorTokenPath(daemonHomeDir);
113
+ mkdirSync(dirname(tokenPath), { recursive: true });
114
+ const tmpPath = tokenPath + '.tmp';
115
+ writeFileSync(tmpPath, content, { encoding: 'utf-8', mode: 0o600 });
116
+ chmodSync(tmpPath, 0o600);
117
+ renameSync(tmpPath, tokenPath);
118
+ // Apply chmod again after rename — some filesystems reset permissions on rename
119
+ try {
120
+ chmodSync(tokenPath, 0o600);
121
+ }
122
+ catch { /* best-effort */ }
123
+ }
124
+ /**
125
+ * Read operator tokens from the global daemon-home path.
126
+ * Returns undefined when the file does not exist or cannot be parsed.
127
+ */
128
+ export function readOperatorTokenFile(daemonHomeDir) {
129
+ const tokenPath = resolveOperatorTokenPath(daemonHomeDir);
130
+ if (!existsSync(tokenPath))
131
+ return undefined;
132
+ try {
133
+ return readFileSync(tokenPath, 'utf-8');
134
+ }
135
+ catch {
136
+ return undefined;
137
+ }
138
+ }
139
+ // ---------------------------------------------------------------------------
141
140
  // Daemon settings persistence
142
141
  // ---------------------------------------------------------------------------
143
142
  /**
@@ -182,30 +181,11 @@ export function writeDaemonSetting(daemonHomeDir, key, value) {
182
181
  // ---------------------------------------------------------------------------
183
182
  // Helpers
184
183
  // ---------------------------------------------------------------------------
185
- /**
186
- * Emit a workspace migration event on the runtime bus.
187
- * Never throws — bus emission must not interrupt migration.
188
- */
189
- function _emitMigrationEvent(bus, payload) {
190
- if (!bus)
191
- return;
192
- try {
193
- const envelope = createEventEnvelope(payload.type, payload, { sessionId: '', source: 'daemon-home-migration' });
194
- bus.emit('workspace',
195
- // WorkspaceEvent discriminated-union member; single widening cast is safe.
196
- envelope);
197
- }
198
- catch {
199
- // Swallow — never let event emission break migration
200
- }
201
- }
202
184
  /**
203
185
  * Copy src to dest. Returns true on success, false on failure.
204
186
  * Failures are logged at warn level. Never throws.
205
187
  */
206
- function safeCopy(src, dest, opts) {
207
- if (opts?.skipIfInvalid)
208
- return false;
188
+ function safeCopy(src, dest) {
209
189
  try {
210
190
  copyFileSync(src, dest);
211
191
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pellux/goodvibes-sdk",
3
- "version": "0.21.27",
3
+ "version": "0.21.28",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/mgd34msu/goodvibes-sdk.git"