@ornexus/neocortex 4.0.1 → 4.0.2
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.
- package/install.ps1 +92 -33
- package/install.sh +15 -1
- package/package.json +3 -3
- package/packages/client/dist/adapters/adapter-registry.js +1 -106
- package/packages/client/dist/adapters/antigravity-adapter.js +2 -77
- package/packages/client/dist/adapters/claude-code-adapter.js +3 -79
- package/packages/client/dist/adapters/codex-adapter.js +2 -80
- package/packages/client/dist/adapters/cursor-adapter.js +4 -115
- package/packages/client/dist/adapters/gemini-adapter.js +2 -71
- package/packages/client/dist/adapters/index.js +1 -21
- package/packages/client/dist/adapters/platform-detector.js +1 -106
- package/packages/client/dist/adapters/target-adapter.js +0 -12
- package/packages/client/dist/adapters/vscode-adapter.js +2 -72
- package/packages/client/dist/agent/refresh-stubs.js +2 -234
- package/packages/client/dist/agent/update-agent-yaml.js +1 -102
- package/packages/client/dist/agent/update-description.js +1 -251
- package/packages/client/dist/cache/crypto-utils.js +1 -76
- package/packages/client/dist/cache/encrypted-cache.js +1 -94
- package/packages/client/dist/cache/in-memory-asset-cache.js +1 -70
- package/packages/client/dist/cache/index.js +1 -13
- package/packages/client/dist/cli.js +2 -163
- package/packages/client/dist/commands/activate.js +8 -390
- package/packages/client/dist/commands/cache-status.js +2 -112
- package/packages/client/dist/commands/invoke.js +28 -490
- package/packages/client/dist/config/resolver-selection.js +1 -278
- package/packages/client/dist/config/secure-config.js +12 -269
- package/packages/client/dist/constants.js +1 -25
- package/packages/client/dist/context/context-collector.js +2 -222
- package/packages/client/dist/context/context-sanitizer.js +1 -145
- package/packages/client/dist/index.js +1 -38
- package/packages/client/dist/license/index.js +1 -5
- package/packages/client/dist/license/license-client.js +1 -257
- package/packages/client/dist/machine/fingerprint.js +2 -160
- package/packages/client/dist/machine/index.js +1 -5
- package/packages/client/dist/resilience/circuit-breaker.js +1 -170
- package/packages/client/dist/resilience/degradation-manager.js +1 -164
- package/packages/client/dist/resilience/freshness-indicator.js +1 -100
- package/packages/client/dist/resilience/index.js +1 -8
- package/packages/client/dist/resilience/recovery-detector.js +1 -74
- package/packages/client/dist/resolvers/asset-resolver.js +0 -13
- package/packages/client/dist/resolvers/local-resolver.js +8 -218
- package/packages/client/dist/resolvers/remote-resolver.js +1 -282
- package/packages/client/dist/telemetry/index.js +1 -5
- package/packages/client/dist/telemetry/offline-queue.js +1 -131
- package/packages/client/dist/tier/index.js +1 -5
- package/packages/client/dist/tier/tier-aware-client.js +1 -260
- package/packages/client/dist/types/index.js +1 -38
- package/targets-stubs/antigravity/gemini.md +1 -1
- package/targets-stubs/antigravity/install-antigravity.sh +49 -3
- package/targets-stubs/antigravity/skill/SKILL.md +23 -4
- package/targets-stubs/claude-code/neocortex.agent.yaml +19 -1
- package/targets-stubs/claude-code/neocortex.md +64 -29
- package/targets-stubs/codex/agents.md +20 -3
- package/targets-stubs/codex/config-mcp.toml +5 -0
- package/targets-stubs/cursor/agent.md +23 -5
- package/targets-stubs/cursor/install-cursor.sh +51 -3
- package/targets-stubs/cursor/mcp.json +7 -0
- package/targets-stubs/gemini-cli/agent.md +37 -6
- package/targets-stubs/gemini-cli/install-gemini.sh +50 -17
- package/targets-stubs/vscode/agent.md +47 -10
- package/targets-stubs/vscode/install-vscode.sh +50 -3
- package/targets-stubs/vscode/mcp.json +8 -0
|
@@ -1,278 +1 @@
|
|
|
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
|
-
}
|
|
1
|
+
import{access as E,readFile as h,writeFile as R,mkdir as C}from"node:fs/promises";import{join as i,resolve as w}from"node:path";import{homedir as d}from"node:os";import{createHash as g}from"node:crypto";import{LocalResolver as l}from"../resolvers/local-resolver.js";import{RemoteResolver as u}from"../resolvers/remote-resolver.js";import{DEFAULT_SERVER_URL as O}from"../constants.js";const v=i(d(),".neocortex","feature-flags.json"),y=36e5;async function _(e){try{const r=process.env.NEOCORTEX_REMOTE_PERCENTAGE;if(r!==void 0){const n=parseInt(r,10);return isNaN(n)||n<=0?"skip":f(n)?"remote":"local"}const t=await F();if(t)return t.forceLocal?"local":t.forceRemote?"remote":t.remotePercentage<=0?"skip":f(t.remotePercentage)?"remote":"local";const c=s(e),o=await L(c);return o?(await p(o),o.forceLocal?"local":o.forceRemote?"remote":o.remotePercentage<=0?"skip":f(o.remotePercentage)?"remote":"local"):"skip"}catch{return"skip"}}function f(e){const r=process.env.NEOCORTEX_MACHINE_ID??"default";return g("sha256").update(r).digest().readUInt16BE(0)%100<e}async function F(){try{const e=await h(v,"utf-8"),r=JSON.parse(e);return Date.now()-r.fetchedAt<y?r:null}catch{return null}}async function p(e){try{await C(i(d(),".neocortex"),{recursive:!0}),await R(v,JSON.stringify(e),"utf-8")}catch{}}async function L(e){try{const r=new AbortController,t=setTimeout(()=>r.abort(),5e3),c=await fetch(`${e}/api/v1/config`,{signal:r.signal});if(clearTimeout(t),!c.ok)return null;const o=await c.json();return{remotePercentage:o.remotePercentage??0,forceRemote:o.forceRemote??!1,forceLocal:o.forceLocal??!1,fetchedAt:Date.now()}}catch{return null}}async function N(e){try{return await E(i(e,"core")),!0}catch{return!1}}function m(e){return e?.licenseKey||process.env.NEOCORTEX_LICENSE_KEY||void 0}function s(e){return e?.serverUrl||process.env.NEOCORTEX_SERVER_URL||O}async function M(e){return(await k(e)).resolver}async function k(e){const r=w(e?.projectRoot||process.cwd()),t=process.env.NEOCORTEX_MODE?.toLowerCase();if(e?.forceLocal)return{resolver:new l({projectRoot:r}),reason:"Forced local mode via --local flag",mode:"local"};if(t==="local")return{resolver:new l({projectRoot:r}),reason:"NEOCORTEX_MODE=local environment variable",mode:"local"};if(t==="remote"){const a=m(e);return a?{resolver:new u({serverUrl:s(e),licenseKey:a,cacheProvider:e?.cacheProvider,licenseClient:e?.licenseClient}),reason:"NEOCORTEX_MODE=remote environment variable",mode:"remote"}:{resolver:new l({projectRoot:r}),reason:"NEOCORTEX_MODE=remote but no license key found, falling back to local",mode:"local"}}if(await N(r))return{resolver:new l({projectRoot:r}),reason:"Auto-detected development mode (core/ directory exists)",mode:"local"};if(await _(e)==="remote"){const a=m(e);if(a)return{resolver:new u({serverUrl:s(e),licenseKey:a,cacheProvider:e?.cacheProvider,licenseClient:e?.licenseClient}),reason:"Feature flag cutover: machine in remote bucket",mode:"remote"}}const n=m(e);return n?{resolver:new u({serverUrl:s(e),licenseKey:n,cacheProvider:e?.cacheProvider,licenseClient:e?.licenseClient}),reason:"Auto-detected production mode (license key present, no core/ directory)",mode:"remote"}:{resolver:new l({projectRoot:r}),reason:"Default fallback to local mode",mode:"local"}}export{M as createResolver,k as selectResolver};
|
|
@@ -1,269 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
}
|
|
1
|
+
import{existsSync as p,readFileSync as U,writeFileSync as R,mkdirSync as S,chmodSync as y}from"node:fs";import{execSync as d}from"node:child_process";import{join as a}from"node:path";import{homedir as x}from"node:os";import{encrypt as L,decrypt as I}from"../cache/crypto-utils.js";import{getMachineFingerprint as l}from"../machine/fingerprint.js";import{DEFAULT_SERVER_URL as K}from"../constants.js";const o=a(x(),".neocortex"),s=a(o,"config.json"),u=1;function f(r){const e=l();return L(r,e)}function g(r){try{const e=l();return I(r,e).plaintext}catch{return null}}function O(r){try{if(process.platform==="win32"){const e=process.env.USERNAME||process.env.USER||"";if(!e)return;d(`icacls "${r}" /inheritance:r /grant:r "${e}:(F)"`,{stdio:"pipe",timeout:5e3})}else y(r,384)}catch{}}function m(r){try{if(process.platform==="win32"){const e=process.env.USERNAME||process.env.USER||"";if(!e)return;d(`icacls "${r}" /inheritance:r /grant:r "${e}:(OI)(CI)(F)"`,{stdio:"pipe",timeout:5e3})}else y(r,448)}catch{}}let v=!1;function C(r){if(v)return;const e=r.serverUrl;if(!e)return;/^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?/i.test(e)&&(v=!0,console.warn(`
|
|
2
|
+
[!] Warning: connected to local server (`+e+`).
|
|
3
|
+
To use production, run: neocortex activate YOUR-LICENSE-KEY
|
|
4
|
+
Get yours at https://neocortex.ornexus.com/login
|
|
5
|
+
`))}function $(){try{if(!p(s))return null;const r=U(s,"utf-8"),e=JSON.parse(r.replace(/^\uFEFF/,""));if(e.licenseKey&&!e.encryptedLicenseKey){const c=f(e.licenseKey),t={...e},E=t.licenseKey;return delete t.licenseKey,t.encryptedLicenseKey=c,t.configVersion||(t.configVersion=u),h(t),{serverUrl:e.serverUrl,mode:e.mode,machineId:e.machineId,activatedAt:e.activatedAt,tier:e.tier,licenseKey:E,keyType:e.keyType}}let i;if(e.encryptedLicenseKey){const c=g(e.encryptedLicenseKey);c&&(i=c)}const n={serverUrl:e.serverUrl,mode:e.mode,machineId:e.machineId,activatedAt:e.activatedAt,tier:e.tier,licenseKey:i,keyType:e.keyType};if(e.machineId&&!i&&e.encryptedLicenseKey){const c=l();e.machineId!==c&&console.warn(`
|
|
6
|
+
[!] Machine fingerprint changed (was: ${e.machineId.slice(0,12)}..., now: ${c.slice(0,12)}...).
|
|
7
|
+
Your encrypted credentials are no longer valid.
|
|
8
|
+
Re-activate with: neocortex activate YOUR-LICENSE-KEY
|
|
9
|
+
`)}if(C(n),n.serverUrl&&!process.env.NEOCORTEX_SERVER_URL&&/^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?/i.test(n.serverUrl)){const t=K;return console.warn(`[neocortex] Auto-repairing serverUrl: ${n.serverUrl} -> ${t}
|
|
10
|
+
[neocortex] To keep localhost, set NEOCORTEX_SERVER_URL=${n.serverUrl}
|
|
11
|
+
`),{...n,serverUrl:t}}if(n.serverUrl&&!process.env.NEOCORTEX_SERVER_URL&&/^https?:\/\/api\.neocortex\.ornexus\.com/i.test(n.serverUrl)){const t=n.serverUrl.replace(/^(https?:\/\/)api\.neocortex\.ornexus\.com/i,"$1api.neocortex.sh");return{...n,serverUrl:t}}return n}catch{return null}}function h(r){S(o,{recursive:!0}),m(o);const e=a(o,"cache");p(e)&&m(e),R(s,JSON.stringify(r,null,2)+`
|
|
12
|
+
`,"utf-8"),O(s)}function V(r){const e=f(r.licenseKey),i=r.keyType??(r.licenseKey.startsWith("nxk_")?"api_key":"license"),n={configVersion:u,serverUrl:r.serverUrl,mode:r.mode,machineId:r.machineId,activatedAt:r.activatedAt,tier:r.tier,encryptedLicenseKey:e,keyType:i};h(n)}export{g as decryptLicenseKey,f as encryptLicenseKey,$ as loadSecureConfig,V as saveSecureConfig,m as setSecureDirPermissions,O as setSecureFilePermissions};
|
|
@@ -1,25 +1 @@
|
|
|
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';
|
|
1
|
+
const t="https://api.neocortex.sh";export{t as DEFAULT_SERVER_URL};
|