@ornexus/neocortex 4.0.1

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.

Potentially problematic release.


This version of @ornexus/neocortex might be problematic. Click here for more details.

Files changed (121) hide show
  1. package/LICENSE +56 -0
  2. package/README.md +32 -0
  3. package/install.js +486 -0
  4. package/install.ps1 +1790 -0
  5. package/install.sh +1587 -0
  6. package/package.json +104 -0
  7. package/packages/client/dist/adapters/adapter-registry.d.ts +61 -0
  8. package/packages/client/dist/adapters/adapter-registry.js +106 -0
  9. package/packages/client/dist/adapters/antigravity-adapter.d.ts +18 -0
  10. package/packages/client/dist/adapters/antigravity-adapter.js +77 -0
  11. package/packages/client/dist/adapters/claude-code-adapter.d.ts +19 -0
  12. package/packages/client/dist/adapters/claude-code-adapter.js +79 -0
  13. package/packages/client/dist/adapters/codex-adapter.d.ts +19 -0
  14. package/packages/client/dist/adapters/codex-adapter.js +80 -0
  15. package/packages/client/dist/adapters/cursor-adapter.d.ts +19 -0
  16. package/packages/client/dist/adapters/cursor-adapter.js +115 -0
  17. package/packages/client/dist/adapters/gemini-adapter.d.ts +18 -0
  18. package/packages/client/dist/adapters/gemini-adapter.js +71 -0
  19. package/packages/client/dist/adapters/index.d.ts +19 -0
  20. package/packages/client/dist/adapters/index.js +21 -0
  21. package/packages/client/dist/adapters/platform-detector.d.ts +46 -0
  22. package/packages/client/dist/adapters/platform-detector.js +106 -0
  23. package/packages/client/dist/adapters/target-adapter.d.ts +70 -0
  24. package/packages/client/dist/adapters/target-adapter.js +12 -0
  25. package/packages/client/dist/adapters/vscode-adapter.d.ts +19 -0
  26. package/packages/client/dist/adapters/vscode-adapter.js +72 -0
  27. package/packages/client/dist/agent/refresh-stubs.d.ts +65 -0
  28. package/packages/client/dist/agent/refresh-stubs.js +234 -0
  29. package/packages/client/dist/agent/update-agent-yaml.d.ts +26 -0
  30. package/packages/client/dist/agent/update-agent-yaml.js +102 -0
  31. package/packages/client/dist/agent/update-description.d.ts +45 -0
  32. package/packages/client/dist/agent/update-description.js +251 -0
  33. package/packages/client/dist/cache/crypto-utils.d.ts +30 -0
  34. package/packages/client/dist/cache/crypto-utils.js +76 -0
  35. package/packages/client/dist/cache/encrypted-cache.d.ts +30 -0
  36. package/packages/client/dist/cache/encrypted-cache.js +94 -0
  37. package/packages/client/dist/cache/in-memory-asset-cache.d.ts +59 -0
  38. package/packages/client/dist/cache/in-memory-asset-cache.js +70 -0
  39. package/packages/client/dist/cache/index.d.ts +13 -0
  40. package/packages/client/dist/cache/index.js +13 -0
  41. package/packages/client/dist/cli.d.ts +14 -0
  42. package/packages/client/dist/cli.js +194 -0
  43. package/packages/client/dist/commands/activate.d.ts +55 -0
  44. package/packages/client/dist/commands/activate.js +390 -0
  45. package/packages/client/dist/commands/cache-status.d.ts +39 -0
  46. package/packages/client/dist/commands/cache-status.js +112 -0
  47. package/packages/client/dist/commands/invoke.d.ts +70 -0
  48. package/packages/client/dist/commands/invoke.js +490 -0
  49. package/packages/client/dist/config/resolver-selection.d.ts +40 -0
  50. package/packages/client/dist/config/resolver-selection.js +278 -0
  51. package/packages/client/dist/config/secure-config.d.ts +78 -0
  52. package/packages/client/dist/config/secure-config.js +269 -0
  53. package/packages/client/dist/constants.d.ts +25 -0
  54. package/packages/client/dist/constants.js +25 -0
  55. package/packages/client/dist/context/context-collector.d.ts +28 -0
  56. package/packages/client/dist/context/context-collector.js +222 -0
  57. package/packages/client/dist/context/context-sanitizer.d.ts +28 -0
  58. package/packages/client/dist/context/context-sanitizer.js +145 -0
  59. package/packages/client/dist/index.d.ts +55 -0
  60. package/packages/client/dist/index.js +38 -0
  61. package/packages/client/dist/license/index.d.ts +5 -0
  62. package/packages/client/dist/license/index.js +5 -0
  63. package/packages/client/dist/license/license-client.d.ts +79 -0
  64. package/packages/client/dist/license/license-client.js +257 -0
  65. package/packages/client/dist/machine/fingerprint.d.ts +34 -0
  66. package/packages/client/dist/machine/fingerprint.js +160 -0
  67. package/packages/client/dist/machine/index.d.ts +5 -0
  68. package/packages/client/dist/machine/index.js +5 -0
  69. package/packages/client/dist/resilience/circuit-breaker.d.ts +70 -0
  70. package/packages/client/dist/resilience/circuit-breaker.js +170 -0
  71. package/packages/client/dist/resilience/degradation-manager.d.ts +67 -0
  72. package/packages/client/dist/resilience/degradation-manager.js +164 -0
  73. package/packages/client/dist/resilience/freshness-indicator.d.ts +59 -0
  74. package/packages/client/dist/resilience/freshness-indicator.js +100 -0
  75. package/packages/client/dist/resilience/index.d.ts +8 -0
  76. package/packages/client/dist/resilience/index.js +8 -0
  77. package/packages/client/dist/resilience/recovery-detector.d.ts +59 -0
  78. package/packages/client/dist/resilience/recovery-detector.js +74 -0
  79. package/packages/client/dist/resolvers/asset-resolver.d.ts +79 -0
  80. package/packages/client/dist/resolvers/asset-resolver.js +13 -0
  81. package/packages/client/dist/resolvers/local-resolver.d.ts +26 -0
  82. package/packages/client/dist/resolvers/local-resolver.js +218 -0
  83. package/packages/client/dist/resolvers/remote-resolver.d.ts +91 -0
  84. package/packages/client/dist/resolvers/remote-resolver.js +282 -0
  85. package/packages/client/dist/telemetry/index.d.ts +5 -0
  86. package/packages/client/dist/telemetry/index.js +5 -0
  87. package/packages/client/dist/telemetry/offline-queue.d.ts +57 -0
  88. package/packages/client/dist/telemetry/offline-queue.js +131 -0
  89. package/packages/client/dist/tier/index.d.ts +5 -0
  90. package/packages/client/dist/tier/index.js +5 -0
  91. package/packages/client/dist/tier/tier-aware-client.d.ts +97 -0
  92. package/packages/client/dist/tier/tier-aware-client.js +260 -0
  93. package/packages/client/dist/types/index.d.ts +140 -0
  94. package/packages/client/dist/types/index.js +38 -0
  95. package/postinstall.js +272 -0
  96. package/targets-stubs/antigravity/README.md +36 -0
  97. package/targets-stubs/antigravity/gemini.md +22 -0
  98. package/targets-stubs/antigravity/install-antigravity.sh +44 -0
  99. package/targets-stubs/antigravity/mcp-config.json +9 -0
  100. package/targets-stubs/antigravity/skill/SKILL.md +67 -0
  101. package/targets-stubs/claude-code/README.md +20 -0
  102. package/targets-stubs/claude-code/neocortex.agent.yaml +24 -0
  103. package/targets-stubs/claude-code/neocortex.md +125 -0
  104. package/targets-stubs/codex/README.md +32 -0
  105. package/targets-stubs/codex/agents.md +61 -0
  106. package/targets-stubs/codex/config-mcp.toml +6 -0
  107. package/targets-stubs/codex/install-codex.sh +61 -0
  108. package/targets-stubs/cursor/README.md +33 -0
  109. package/targets-stubs/cursor/agent.md +94 -0
  110. package/targets-stubs/cursor/install-cursor.sh +35 -0
  111. package/targets-stubs/cursor/mcp.json +11 -0
  112. package/targets-stubs/gemini-cli/README.md +34 -0
  113. package/targets-stubs/gemini-cli/agent.md +101 -0
  114. package/targets-stubs/gemini-cli/gemini.md +16 -0
  115. package/targets-stubs/gemini-cli/install-gemini.sh +56 -0
  116. package/targets-stubs/gemini-cli/settings-mcp.json +11 -0
  117. package/targets-stubs/vscode/README.md +34 -0
  118. package/targets-stubs/vscode/agent.md +102 -0
  119. package/targets-stubs/vscode/copilot-instructions.md +16 -0
  120. package/targets-stubs/vscode/install-vscode.sh +42 -0
  121. package/targets-stubs/vscode/mcp.json +13 -0
@@ -0,0 +1,278 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ *
5
+ * This file is part of Neocortex CLI, licensed under the
6
+ * Functional Source License, Version 1.1 (FSL-1.1).
7
+ *
8
+ * Change Date: February 20, 2029
9
+ * Change License: MIT
10
+ *
11
+ * See the LICENSE file in the project root for full license text.
12
+ */
13
+ /**
14
+ * @neocortex/client - Resolver Selection Factory
15
+ *
16
+ * Factory function that selects the appropriate AssetResolver
17
+ * based on CLI flags, environment variables, and auto-detection.
18
+ *
19
+ * Decision chain (priority order):
20
+ * 1. forceLocal option (--local flag) -> LocalResolver
21
+ * 2. NEOCORTEX_MODE=local env -> LocalResolver
22
+ * 3. NEOCORTEX_MODE=remote env -> RemoteResolver
23
+ * 4. core/ directory exists locally -> LocalResolver (dev mode)
24
+ * 4.5. Feature flag cutover (hash(machine) % 100 < remotePercentage) -> RemoteResolver
25
+ * 5. Valid license key present + no core/ -> RemoteResolver
26
+ * 6. Default -> LocalResolver (safe fallback)
27
+ */
28
+ import { access, readFile, writeFile, mkdir } from 'node:fs/promises';
29
+ import { join, resolve } from 'node:path';
30
+ import { homedir } from 'node:os';
31
+ import { createHash } from 'node:crypto';
32
+ import { LocalResolver } from '../resolvers/local-resolver.js';
33
+ import { RemoteResolver } from '../resolvers/remote-resolver.js';
34
+ import { DEFAULT_SERVER_URL } from '../constants.js';
35
+ // ── Feature Flag (Story 43.7) ───────────────────────────────────────────
36
+ const CONFIG_CACHE_FILE = join(homedir(), '.neocortex', 'feature-flags.json');
37
+ const CONFIG_CACHE_TTL_MS = 3600_000; // 1 hour
38
+ /**
39
+ * Check if this machine should use remote mode based on server feature flags.
40
+ * Uses hash(machine_fingerprint) % 100 < remotePercentage to determine bucket.
41
+ * Caches the server config for 1 hour.
42
+ *
43
+ * Returns 'remote' | 'local' | 'skip' (skip = no flag applies)
44
+ */
45
+ async function checkFeatureFlag(options) {
46
+ try {
47
+ // Check env override
48
+ const envPercentage = process.env['NEOCORTEX_REMOTE_PERCENTAGE'];
49
+ if (envPercentage !== undefined) {
50
+ const pct = parseInt(envPercentage, 10);
51
+ if (isNaN(pct) || pct <= 0)
52
+ return 'skip';
53
+ return isInRemoteBucket(pct) ? 'remote' : 'local';
54
+ }
55
+ // Try cached config
56
+ const cached = await loadFeatureFlagCache();
57
+ if (cached) {
58
+ if (cached.forceLocal)
59
+ return 'local';
60
+ if (cached.forceRemote)
61
+ return 'remote';
62
+ if (cached.remotePercentage <= 0)
63
+ return 'skip';
64
+ return isInRemoteBucket(cached.remotePercentage) ? 'remote' : 'local';
65
+ }
66
+ // Fetch from server (non-blocking, fail gracefully)
67
+ const serverUrl = getServerUrl(options);
68
+ const config = await fetchFeatureFlags(serverUrl);
69
+ if (config) {
70
+ await saveFeatureFlagCache(config);
71
+ if (config.forceLocal)
72
+ return 'local';
73
+ if (config.forceRemote)
74
+ return 'remote';
75
+ if (config.remotePercentage <= 0)
76
+ return 'skip';
77
+ return isInRemoteBucket(config.remotePercentage) ? 'remote' : 'local';
78
+ }
79
+ return 'skip'; // No flag info available
80
+ }
81
+ catch {
82
+ return 'skip'; // Never block on feature flag errors
83
+ }
84
+ }
85
+ /**
86
+ * Deterministic bucket assignment using machine fingerprint hash.
87
+ * hash(machine_id) % 100 < percentage = in remote bucket
88
+ */
89
+ function isInRemoteBucket(percentage) {
90
+ const machineId = process.env['NEOCORTEX_MACHINE_ID'] ?? 'default';
91
+ const hash = createHash('sha256').update(machineId).digest();
92
+ const bucket = hash.readUInt16BE(0) % 100;
93
+ return bucket < percentage;
94
+ }
95
+ async function loadFeatureFlagCache() {
96
+ try {
97
+ const raw = await readFile(CONFIG_CACHE_FILE, 'utf-8');
98
+ const cached = JSON.parse(raw);
99
+ if (Date.now() - cached.fetchedAt < CONFIG_CACHE_TTL_MS) {
100
+ return cached;
101
+ }
102
+ return null; // Expired
103
+ }
104
+ catch {
105
+ return null;
106
+ }
107
+ }
108
+ async function saveFeatureFlagCache(config) {
109
+ try {
110
+ await mkdir(join(homedir(), '.neocortex'), { recursive: true });
111
+ await writeFile(CONFIG_CACHE_FILE, JSON.stringify(config), 'utf-8');
112
+ }
113
+ catch {
114
+ // Non-critical - ignore
115
+ }
116
+ }
117
+ async function fetchFeatureFlags(serverUrl) {
118
+ try {
119
+ const controller = new AbortController();
120
+ const timeout = setTimeout(() => controller.abort(), 5000);
121
+ const response = await fetch(`${serverUrl}/api/v1/config`, {
122
+ signal: controller.signal,
123
+ });
124
+ clearTimeout(timeout);
125
+ if (!response.ok)
126
+ return null;
127
+ const data = (await response.json());
128
+ return {
129
+ remotePercentage: data.remotePercentage ?? 0,
130
+ forceRemote: data.forceRemote ?? false,
131
+ forceLocal: data.forceLocal ?? false,
132
+ fetchedAt: Date.now(),
133
+ };
134
+ }
135
+ catch {
136
+ return null;
137
+ }
138
+ }
139
+ // ── Detection Helpers ───────────────────────────────────────────────────
140
+ /**
141
+ * Check if the core/ directory exists at the given project root.
142
+ * This indicates development mode.
143
+ */
144
+ async function detectLocalMode(projectRoot) {
145
+ try {
146
+ await access(join(projectRoot, 'core'));
147
+ return true;
148
+ }
149
+ catch {
150
+ return false;
151
+ }
152
+ }
153
+ /**
154
+ * Get license key from environment or options.
155
+ */
156
+ function getLicenseKey(options) {
157
+ return options?.licenseKey || process.env['NEOCORTEX_LICENSE_KEY'] || undefined;
158
+ }
159
+ /**
160
+ * Get server URL from environment or options.
161
+ */
162
+ function getServerUrl(options) {
163
+ return (options?.serverUrl ||
164
+ process.env['NEOCORTEX_SERVER_URL'] ||
165
+ DEFAULT_SERVER_URL);
166
+ }
167
+ // ── Factory Function ────────────────────────────────────────────────────
168
+ /**
169
+ * Create the appropriate AssetResolver based on configuration.
170
+ *
171
+ * Selection follows a strict priority chain:
172
+ * 1. forceLocal option -> LocalResolver
173
+ * 2. NEOCORTEX_MODE=local -> LocalResolver
174
+ * 3. NEOCORTEX_MODE=remote -> RemoteResolver
175
+ * 4. core/ exists locally -> LocalResolver
176
+ * 5. License key + no core/ -> RemoteResolver
177
+ * 6. Default -> LocalResolver
178
+ *
179
+ * @param options - Optional configuration overrides
180
+ * @returns Configured AssetResolver ready for use
181
+ */
182
+ export async function createResolver(options) {
183
+ const result = await selectResolver(options);
184
+ return result.resolver;
185
+ }
186
+ /**
187
+ * Select resolver with full result including reason.
188
+ * Useful for logging/debugging why a specific resolver was chosen.
189
+ */
190
+ export async function selectResolver(options) {
191
+ const projectRoot = resolve(options?.projectRoot || process.cwd());
192
+ const neocortexMode = process.env['NEOCORTEX_MODE']?.toLowerCase();
193
+ // 1. forceLocal option (--local CLI flag)
194
+ if (options?.forceLocal) {
195
+ return {
196
+ resolver: new LocalResolver({ projectRoot }),
197
+ reason: 'Forced local mode via --local flag',
198
+ mode: 'local',
199
+ };
200
+ }
201
+ // 2. NEOCORTEX_MODE=local
202
+ if (neocortexMode === 'local') {
203
+ return {
204
+ resolver: new LocalResolver({ projectRoot }),
205
+ reason: 'NEOCORTEX_MODE=local environment variable',
206
+ mode: 'local',
207
+ };
208
+ }
209
+ // 3. NEOCORTEX_MODE=remote
210
+ if (neocortexMode === 'remote') {
211
+ const licenseKey = getLicenseKey(options);
212
+ if (!licenseKey) {
213
+ // Fall back to local if no license key for remote mode
214
+ return {
215
+ resolver: new LocalResolver({ projectRoot }),
216
+ reason: 'NEOCORTEX_MODE=remote but no license key found, falling back to local',
217
+ mode: 'local',
218
+ };
219
+ }
220
+ return {
221
+ resolver: new RemoteResolver({
222
+ serverUrl: getServerUrl(options),
223
+ licenseKey,
224
+ cacheProvider: options?.cacheProvider,
225
+ licenseClient: options?.licenseClient,
226
+ }),
227
+ reason: 'NEOCORTEX_MODE=remote environment variable',
228
+ mode: 'remote',
229
+ };
230
+ }
231
+ // 4. Auto-detect: core/ directory exists -> dev mode
232
+ const hasLocalCore = await detectLocalMode(projectRoot);
233
+ if (hasLocalCore) {
234
+ return {
235
+ resolver: new LocalResolver({ projectRoot }),
236
+ reason: 'Auto-detected development mode (core/ directory exists)',
237
+ mode: 'local',
238
+ };
239
+ }
240
+ // 4.5. Feature flag cutover check (Story 43.7)
241
+ // If server config says remotePercentage > 0, check if this machine is in the bucket
242
+ const featureFlagResult = await checkFeatureFlag(options);
243
+ if (featureFlagResult === 'remote') {
244
+ const licenseKey = getLicenseKey(options);
245
+ if (licenseKey) {
246
+ return {
247
+ resolver: new RemoteResolver({
248
+ serverUrl: getServerUrl(options),
249
+ licenseKey,
250
+ cacheProvider: options?.cacheProvider,
251
+ licenseClient: options?.licenseClient,
252
+ }),
253
+ reason: 'Feature flag cutover: machine in remote bucket',
254
+ mode: 'remote',
255
+ };
256
+ }
257
+ }
258
+ // 5. License key present + no core/ -> production mode
259
+ const licenseKey = getLicenseKey(options);
260
+ if (licenseKey) {
261
+ return {
262
+ resolver: new RemoteResolver({
263
+ serverUrl: getServerUrl(options),
264
+ licenseKey,
265
+ cacheProvider: options?.cacheProvider,
266
+ licenseClient: options?.licenseClient,
267
+ }),
268
+ reason: 'Auto-detected production mode (license key present, no core/ directory)',
269
+ mode: 'remote',
270
+ };
271
+ }
272
+ // 6. Default -> LocalResolver (safe fallback)
273
+ return {
274
+ resolver: new LocalResolver({ projectRoot }),
275
+ reason: 'Default fallback to local mode',
276
+ mode: 'local',
277
+ };
278
+ }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ *
5
+ * This file is part of Neocortex CLI, licensed under the
6
+ * Functional Source License, Version 1.1 (FSL-1.1).
7
+ *
8
+ * Change Date: February 20, 2029
9
+ * Change License: MIT
10
+ *
11
+ * See the LICENSE file in the project root for full license text.
12
+ */
13
+ /** Key type discriminator (Story 29.04) */
14
+ export type KeyType = 'license' | 'api_key';
15
+ /** Resolved config with decrypted license key */
16
+ export interface SecureConfig {
17
+ readonly serverUrl?: string;
18
+ readonly mode?: string;
19
+ readonly machineId?: string;
20
+ readonly activatedAt?: string;
21
+ readonly tier?: string;
22
+ readonly licenseKey?: string;
23
+ /** Key type: 'license' (NX-...) or 'api_key' (nxk_...). undefined = legacy (treat as license). Story 29.04 */
24
+ readonly keyType?: KeyType;
25
+ }
26
+ /**
27
+ * Encrypt a license key using the machine fingerprint as passphrase.
28
+ * Returns the encrypted envelope string.
29
+ */
30
+ export declare function encryptLicenseKey(licenseKey: string): string;
31
+ /**
32
+ * Decrypt a license key using the machine fingerprint as passphrase.
33
+ * Returns the plaintext key or null if decryption fails (hardware changed).
34
+ */
35
+ export declare function decryptLicenseKey(encryptedKey: string): string | null;
36
+ /**
37
+ * Set restrictive file permissions on config files.
38
+ * Unix: chmod 600 (owner read/write only).
39
+ * Windows: icacls to remove inheritance and grant Full Control only to current user.
40
+ * Story 61.4 - F4 remediation.
41
+ * Story 66.1 - Windows ACL via icacls.
42
+ */
43
+ export declare function setSecureFilePermissions(filePath: string): void;
44
+ /**
45
+ * Set restrictive directory permissions on config directories.
46
+ * Unix: chmod 700 (owner read/write/execute only).
47
+ * Windows: icacls with (OI)(CI) for propagation to child objects.
48
+ * Story 61.4 - F4 remediation.
49
+ * Story 66.1 - Windows ACL via icacls.
50
+ */
51
+ export declare function setSecureDirPermissions(dirPath: string): void;
52
+ /**
53
+ * Load config from ~/.neocortex/config.json with automatic migration.
54
+ *
55
+ * If the config contains a plaintext `licenseKey` (old format), it is:
56
+ * 1. Encrypted using machine fingerprint
57
+ * 2. Stored as `encryptedLicenseKey`
58
+ * 3. Old `licenseKey` field removed
59
+ * 4. Config rewritten to disk
60
+ *
61
+ * If decryption of `encryptedLicenseKey` fails (hardware change),
62
+ * returns config with licenseKey = undefined.
63
+ */
64
+ export declare function loadSecureConfig(): SecureConfig | null;
65
+ /**
66
+ * Save config after activation with encrypted license key.
67
+ * This is the primary write path called from activate.ts.
68
+ */
69
+ export declare function saveSecureConfig(config: {
70
+ serverUrl: string;
71
+ mode: string;
72
+ machineId: string;
73
+ activatedAt: string;
74
+ tier?: string;
75
+ licenseKey: string;
76
+ /** Key type: 'license' (NX-...) or 'api_key' (nxk_...). Auto-detected from key prefix if omitted. Story 29.04 */
77
+ keyType?: KeyType;
78
+ }): void;
@@ -0,0 +1,269 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ *
5
+ * This file is part of Neocortex CLI, licensed under the
6
+ * Functional Source License, Version 1.1 (FSL-1.1).
7
+ *
8
+ * Change Date: February 20, 2029
9
+ * Change License: MIT
10
+ *
11
+ * See the LICENSE file in the project root for full license text.
12
+ */
13
+ /**
14
+ * @neocortex/client - Secure Config
15
+ *
16
+ * Handles reading and writing config.json with encrypted license key.
17
+ * Uses machine fingerprint as encryption seed so the key is bound
18
+ * to the physical machine.
19
+ *
20
+ * Story 61.2 - F2 remediation: license key no longer stored in plaintext.
21
+ *
22
+ * Migration: if config has plaintext `licenseKey`, it is silently
23
+ * migrated to `encryptedLicenseKey` on first read.
24
+ *
25
+ * If decryption fails (e.g. hardware change), returns null for
26
+ * licenseKey, requiring re-activation.
27
+ */
28
+ import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from 'node:fs';
29
+ import { execSync } from 'node:child_process';
30
+ import { join } from 'node:path';
31
+ import { homedir } from 'node:os';
32
+ import { encrypt, decrypt } from '../cache/crypto-utils.js';
33
+ import { getMachineFingerprint } from '../machine/fingerprint.js';
34
+ import { DEFAULT_SERVER_URL } from '../constants.js';
35
+ // ── Constants ────────────────────────────────────────────────────────────
36
+ const CONFIG_DIR = join(homedir(), '.neocortex');
37
+ const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
38
+ // ── Types ────────────────────────────────────────────────────────────────
39
+ /** Current config schema version (Epic 62 - FR8) */
40
+ const CURRENT_CONFIG_VERSION = 1;
41
+ // ── License Key Encryption ──────────────────────────────────────────────
42
+ /**
43
+ * Encrypt a license key using the machine fingerprint as passphrase.
44
+ * Returns the encrypted envelope string.
45
+ */
46
+ export function encryptLicenseKey(licenseKey) {
47
+ const fingerprint = getMachineFingerprint();
48
+ return encrypt(licenseKey, fingerprint);
49
+ }
50
+ /**
51
+ * Decrypt a license key using the machine fingerprint as passphrase.
52
+ * Returns the plaintext key or null if decryption fails (hardware changed).
53
+ */
54
+ export function decryptLicenseKey(encryptedKey) {
55
+ try {
56
+ const fingerprint = getMachineFingerprint();
57
+ const result = decrypt(encryptedKey, fingerprint);
58
+ // expired flag is not relevant for license keys (no TTL)
59
+ return result.plaintext;
60
+ }
61
+ catch {
62
+ return null;
63
+ }
64
+ }
65
+ // ── Secure File Permissions ─────────────────────────────────────────────
66
+ /**
67
+ * Set restrictive file permissions on config files.
68
+ * Unix: chmod 600 (owner read/write only).
69
+ * Windows: icacls to remove inheritance and grant Full Control only to current user.
70
+ * Story 61.4 - F4 remediation.
71
+ * Story 66.1 - Windows ACL via icacls.
72
+ */
73
+ export function setSecureFilePermissions(filePath) {
74
+ try {
75
+ if (process.platform === 'win32') {
76
+ const username = process.env.USERNAME || process.env.USER || '';
77
+ if (!username)
78
+ return;
79
+ execSync(`icacls "${filePath}" /inheritance:r /grant:r "${username}:(F)"`, { stdio: 'pipe', timeout: 5000 });
80
+ }
81
+ else {
82
+ chmodSync(filePath, 0o600);
83
+ }
84
+ }
85
+ catch {
86
+ // Fail-open: ACL/chmod is hardening, not blocking
87
+ }
88
+ }
89
+ /**
90
+ * Set restrictive directory permissions on config directories.
91
+ * Unix: chmod 700 (owner read/write/execute only).
92
+ * Windows: icacls with (OI)(CI) for propagation to child objects.
93
+ * Story 61.4 - F4 remediation.
94
+ * Story 66.1 - Windows ACL via icacls.
95
+ */
96
+ export function setSecureDirPermissions(dirPath) {
97
+ try {
98
+ if (process.platform === 'win32') {
99
+ const username = process.env.USERNAME || process.env.USER || '';
100
+ if (!username)
101
+ return;
102
+ execSync(`icacls "${dirPath}" /inheritance:r /grant:r "${username}:(OI)(CI)(F)"`, { stdio: 'pipe', timeout: 5000 });
103
+ }
104
+ else {
105
+ chmodSync(dirPath, 0o700);
106
+ }
107
+ }
108
+ catch {
109
+ // Fail-open: ACL/chmod is hardening, not blocking
110
+ }
111
+ }
112
+ // ── Localhost Warning ────────────────────────────────────────────────────
113
+ /**
114
+ * Story 32.08: Warn if serverUrl points to localhost.
115
+ * Informative only -- does NOT modify config.
116
+ */
117
+ let localhostWarningShown = false;
118
+ function warnIfLocalhost(config) {
119
+ if (localhostWarningShown)
120
+ return;
121
+ const url = config.serverUrl;
122
+ if (!url)
123
+ return;
124
+ const isLocal = /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?/i.test(url);
125
+ if (isLocal) {
126
+ localhostWarningShown = true;
127
+ console.warn('\n[!] Warning: connected to local server (' + url + ').\n' +
128
+ ' To use production, run: neocortex activate YOUR-LICENSE-KEY\n' +
129
+ ' Get yours at https://neocortex.ornexus.com/login\n');
130
+ }
131
+ }
132
+ // ── Config Read/Write ───────────────────────────────────────────────────
133
+ /**
134
+ * Load config from ~/.neocortex/config.json with automatic migration.
135
+ *
136
+ * If the config contains a plaintext `licenseKey` (old format), it is:
137
+ * 1. Encrypted using machine fingerprint
138
+ * 2. Stored as `encryptedLicenseKey`
139
+ * 3. Old `licenseKey` field removed
140
+ * 4. Config rewritten to disk
141
+ *
142
+ * If decryption of `encryptedLicenseKey` fails (hardware change),
143
+ * returns config with licenseKey = undefined.
144
+ */
145
+ export function loadSecureConfig() {
146
+ try {
147
+ if (!existsSync(CONFIG_FILE))
148
+ return null;
149
+ const raw = readFileSync(CONFIG_FILE, 'utf-8');
150
+ // Strip UTF-8 BOM if present (PowerShell 5.1 writes BOM via Out-File -Encoding utf8)
151
+ // Defense in depth: handles configs written by old PS 5.1 installers (Epic 63 - Story 63.1)
152
+ const config = JSON.parse(raw.replace(/^\uFEFF/, ''));
153
+ // Migration path: plaintext licenseKey -> encrypted
154
+ if (config.licenseKey && !config.encryptedLicenseKey) {
155
+ const encrypted = encryptLicenseKey(config.licenseKey);
156
+ const migratedConfig = { ...config };
157
+ const plainKey = migratedConfig.licenseKey;
158
+ delete migratedConfig.licenseKey;
159
+ migratedConfig.encryptedLicenseKey = encrypted;
160
+ // Ensure configVersion is preserved or added during migration
161
+ if (!migratedConfig.configVersion) {
162
+ migratedConfig.configVersion = CURRENT_CONFIG_VERSION;
163
+ }
164
+ writeSecureConfig(migratedConfig);
165
+ return {
166
+ serverUrl: config.serverUrl,
167
+ mode: config.mode,
168
+ machineId: config.machineId,
169
+ activatedAt: config.activatedAt,
170
+ tier: config.tier,
171
+ licenseKey: plainKey,
172
+ keyType: config.keyType,
173
+ };
174
+ }
175
+ // Decrypt encrypted license key
176
+ let licenseKey;
177
+ if (config.encryptedLicenseKey) {
178
+ const decrypted = decryptLicenseKey(config.encryptedLicenseKey);
179
+ if (decrypted) {
180
+ licenseKey = decrypted;
181
+ }
182
+ // If decryption fails, licenseKey remains undefined -> requires re-activation
183
+ // Story P26.06 below handles the fingerprint change warning
184
+ }
185
+ const result = {
186
+ serverUrl: config.serverUrl,
187
+ mode: config.mode,
188
+ machineId: config.machineId,
189
+ activatedAt: config.activatedAt,
190
+ tier: config.tier,
191
+ licenseKey,
192
+ keyType: config.keyType,
193
+ };
194
+ // Story P26.04 + P26.06: Detect fingerprint change early and warn
195
+ // This catches the issue BEFORE the user tries to invoke and gets a confusing error
196
+ if (config.machineId && !licenseKey && config.encryptedLicenseKey) {
197
+ const currentFp = getMachineFingerprint();
198
+ if (config.machineId !== currentFp) {
199
+ console.warn(`\n[!] Machine fingerprint changed (was: ${config.machineId.slice(0, 12)}..., now: ${currentFp.slice(0, 12)}...).` +
200
+ '\n Your encrypted credentials are no longer valid.' +
201
+ '\n Re-activate with: neocortex activate YOUR-LICENSE-KEY\n');
202
+ }
203
+ }
204
+ // Story 32.08: Warn if serverUrl points to localhost
205
+ warnIfLocalhost(result);
206
+ // Story 70.05: Auto-repair localhost serverUrl if not intentional.
207
+ // If serverUrl points to localhost and NEOCORTEX_SERVER_URL env is not set
208
+ // to localhost (i.e., user didn't explicitly request local), repair to production.
209
+ // This is an in-memory repair only -- does NOT rewrite the config file on disk.
210
+ if (result.serverUrl && !process.env['NEOCORTEX_SERVER_URL']) {
211
+ const isLocal = /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?/i.test(result.serverUrl);
212
+ if (isLocal) {
213
+ const repairedUrl = DEFAULT_SERVER_URL;
214
+ console.warn(`[neocortex] Auto-repairing serverUrl: ${result.serverUrl} -> ${repairedUrl}\n` +
215
+ `[neocortex] To keep localhost, set NEOCORTEX_SERVER_URL=${result.serverUrl}\n`);
216
+ return { ...result, serverUrl: repairedUrl };
217
+ }
218
+ }
219
+ // Story P46.02: Auto-repair old ornexus.com URL to neocortex.sh.
220
+ // The old domain api.neocortex.ornexus.com remains as a CNAME, but new installs
221
+ // should use the canonical api.neocortex.sh. In-memory only -- does NOT rewrite disk.
222
+ // Suppressed by NEOCORTEX_SERVER_URL env var (developer override).
223
+ if (result.serverUrl && !process.env['NEOCORTEX_SERVER_URL']) {
224
+ const isOldDomain = /^https?:\/\/api\.neocortex\.ornexus\.com/i.test(result.serverUrl);
225
+ if (isOldDomain) {
226
+ const repairedUrl = result.serverUrl.replace(/^(https?:\/\/)api\.neocortex\.ornexus\.com/i, '$1api.neocortex.sh');
227
+ return { ...result, serverUrl: repairedUrl };
228
+ }
229
+ }
230
+ return result;
231
+ }
232
+ catch {
233
+ return null;
234
+ }
235
+ }
236
+ /**
237
+ * Write config to disk with encrypted license key and secure permissions.
238
+ * The licenseKey field should already be encrypted as encryptedLicenseKey.
239
+ */
240
+ function writeSecureConfig(config) {
241
+ mkdirSync(CONFIG_DIR, { recursive: true });
242
+ setSecureDirPermissions(CONFIG_DIR);
243
+ const cacheDir = join(CONFIG_DIR, 'cache');
244
+ if (existsSync(cacheDir)) {
245
+ setSecureDirPermissions(cacheDir);
246
+ }
247
+ writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2) + '\n', 'utf-8');
248
+ setSecureFilePermissions(CONFIG_FILE);
249
+ }
250
+ /**
251
+ * Save config after activation with encrypted license key.
252
+ * This is the primary write path called from activate.ts.
253
+ */
254
+ export function saveSecureConfig(config) {
255
+ const encrypted = encryptLicenseKey(config.licenseKey);
256
+ // Auto-detect keyType from key prefix if not explicitly provided
257
+ const keyType = config.keyType ?? (config.licenseKey.startsWith('nxk_') ? 'api_key' : 'license');
258
+ const diskConfig = {
259
+ configVersion: CURRENT_CONFIG_VERSION,
260
+ serverUrl: config.serverUrl,
261
+ mode: config.mode,
262
+ machineId: config.machineId,
263
+ activatedAt: config.activatedAt,
264
+ tier: config.tier,
265
+ encryptedLicenseKey: encrypted,
266
+ keyType,
267
+ };
268
+ writeSecureConfig(diskConfig);
269
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ *
5
+ * This file is part of Neocortex CLI, licensed under the
6
+ * Functional Source License, Version 1.1 (FSL-1.1).
7
+ *
8
+ * Change Date: February 20, 2029
9
+ * Change License: MIT
10
+ *
11
+ * See the LICENSE file in the project root for full license text.
12
+ */
13
+ /**
14
+ * Production server URL.
15
+ *
16
+ * Single Source of Truth for the default Neocortex IP Protection Server URL.
17
+ * All client-side code MUST import this constant instead of hardcoding the URL.
18
+ *
19
+ * Shell scripts (install.sh, install.ps1) cannot import from TypeScript,
20
+ * so they maintain a hardcoded copy with a comment referencing this file.
21
+ *
22
+ * Epic 70 - Story 70.03
23
+ * Epic P46 - Story P46.01: migrated from api.neocortex.ornexus.com to api.neocortex.sh
24
+ */
25
+ export declare const DEFAULT_SERVER_URL = "https://api.neocortex.sh";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ *
5
+ * This file is part of Neocortex CLI, licensed under the
6
+ * Functional Source License, Version 1.1 (FSL-1.1).
7
+ *
8
+ * Change Date: February 20, 2029
9
+ * Change License: MIT
10
+ *
11
+ * See the LICENSE file in the project root for full license text.
12
+ */
13
+ /**
14
+ * Production server URL.
15
+ *
16
+ * Single Source of Truth for the default Neocortex IP Protection Server URL.
17
+ * All client-side code MUST import this constant instead of hardcoding the URL.
18
+ *
19
+ * Shell scripts (install.sh, install.ps1) cannot import from TypeScript,
20
+ * so they maintain a hardcoded copy with a comment referencing this file.
21
+ *
22
+ * Epic 70 - Story 70.03
23
+ * Epic P46 - Story P46.01: migrated from api.neocortex.ornexus.com to api.neocortex.sh
24
+ */
25
+ export const DEFAULT_SERVER_URL = 'https://api.neocortex.sh';