@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,282 +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
|
-
import { ResolverMode, NoOpCache, } from '../types/index.js';
|
|
14
|
-
import { InMemoryAssetCache } from '../cache/in-memory-asset-cache.js';
|
|
15
|
-
// ── Constants ────────────────────────────────────────────────────────────
|
|
16
|
-
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
17
|
-
const DEFAULT_RETRY_COUNT = 3;
|
|
18
|
-
const BACKOFF_BASE_MS = 1_000;
|
|
19
|
-
const BACKOFF_MAX_MS = 10_000;
|
|
20
|
-
// ── Error Types ──────────────────────────────────────────────────────────
|
|
21
|
-
export class RemoteResolverError extends Error {
|
|
22
|
-
statusCode;
|
|
23
|
-
endpoint;
|
|
24
|
-
constructor(message, statusCode, endpoint) {
|
|
25
|
-
super(message);
|
|
26
|
-
this.statusCode = statusCode;
|
|
27
|
-
this.endpoint = endpoint;
|
|
28
|
-
this.name = 'RemoteResolverError';
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
// ── RemoteResolver Implementation ───────────────────────────────────────
|
|
32
|
-
export class RemoteResolver {
|
|
33
|
-
mode = ResolverMode.REMOTE;
|
|
34
|
-
serverUrl;
|
|
35
|
-
licenseKey;
|
|
36
|
-
timeout;
|
|
37
|
-
retryCount;
|
|
38
|
-
/**
|
|
39
|
-
* Persistent cache -- used for registry only.
|
|
40
|
-
* P70.06: NEVER used for step/skill/standard content (those live in
|
|
41
|
-
* `assetCache` and only in process memory).
|
|
42
|
-
*/
|
|
43
|
-
cache;
|
|
44
|
-
/**
|
|
45
|
-
* In-memory LRU cache for asset content (Epic P70.06).
|
|
46
|
-
* Volatile: discarded on process exit. Never persisted to disk.
|
|
47
|
-
*/
|
|
48
|
-
assetCache;
|
|
49
|
-
licenseClient;
|
|
50
|
-
constructor(options) {
|
|
51
|
-
this.serverUrl = options.serverUrl.replace(/\/+$/, '');
|
|
52
|
-
this.licenseKey = options.licenseKey;
|
|
53
|
-
this.timeout = options.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
54
|
-
this.retryCount = options.retryCount ?? DEFAULT_RETRY_COUNT;
|
|
55
|
-
this.cache = options.cacheProvider ?? new NoOpCache();
|
|
56
|
-
this.licenseClient = options.licenseClient ?? null;
|
|
57
|
-
// P70.06: asset content always lives in volatile memory cache.
|
|
58
|
-
this.assetCache = new InMemoryAssetCache();
|
|
59
|
-
}
|
|
60
|
-
async resolveStep(stepId, _context) {
|
|
61
|
-
// P70.06: step content in-memory only
|
|
62
|
-
const cacheKey = `step:${stepId}`;
|
|
63
|
-
return this.fetchWithInMemoryCache(cacheKey, {
|
|
64
|
-
method: 'GET',
|
|
65
|
-
path: `/api/v1/steps/${encodeURIComponent(stepId)}`,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
async resolveSkill(skillId, _context) {
|
|
69
|
-
// P70.06: skill content in-memory only
|
|
70
|
-
const cacheKey = `skill:${skillId}`;
|
|
71
|
-
return this.fetchWithInMemoryCache(cacheKey, {
|
|
72
|
-
method: 'GET',
|
|
73
|
-
path: `/api/v1/skills/${encodeURIComponent(skillId)}`,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
async resolveStandard(standardId) {
|
|
77
|
-
// P70.06: standard content in-memory only
|
|
78
|
-
const cacheKey = `standard:${standardId}`;
|
|
79
|
-
return this.fetchWithInMemoryCache(cacheKey, {
|
|
80
|
-
method: 'GET',
|
|
81
|
-
path: `/api/v1/standards/${encodeURIComponent(standardId)}`,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
async resolveRegistry() {
|
|
85
|
-
// Registry is NOT asset content -- it's tokenized metadata (P70.02).
|
|
86
|
-
// Persistent cache is acceptable here.
|
|
87
|
-
const cacheKey = 'registry';
|
|
88
|
-
return this.fetchWithCacheFallback(cacheKey, {
|
|
89
|
-
method: 'GET',
|
|
90
|
-
path: '/api/v1/registry',
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
async assemblePrompt(stepId, context) {
|
|
94
|
-
// assemblePrompt is not cached because it depends on dynamic context
|
|
95
|
-
return this.fetchWithRetry({
|
|
96
|
-
method: 'POST',
|
|
97
|
-
path: '/api/v1/prompts/assemble',
|
|
98
|
-
body: {
|
|
99
|
-
stepId,
|
|
100
|
-
context,
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
async isAvailable() {
|
|
105
|
-
try {
|
|
106
|
-
const controller = new AbortController();
|
|
107
|
-
const timeoutId = setTimeout(() => controller.abort(), 5_000);
|
|
108
|
-
const response = await fetch(`${this.serverUrl}/api/v1/health`, {
|
|
109
|
-
method: 'GET',
|
|
110
|
-
headers: await this.buildHeaders(),
|
|
111
|
-
signal: controller.signal,
|
|
112
|
-
});
|
|
113
|
-
clearTimeout(timeoutId);
|
|
114
|
-
return response.ok;
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
async dispose() {
|
|
121
|
-
await this.cache.clear();
|
|
122
|
-
// P70.06: explicit asset cache clear (defense-in-depth; process exit
|
|
123
|
-
// already releases it).
|
|
124
|
-
await this.assetCache.clear();
|
|
125
|
-
}
|
|
126
|
-
// ── Private Methods ─────────────────────────────────────────────────
|
|
127
|
-
/**
|
|
128
|
-
* Build authorization headers for API requests.
|
|
129
|
-
* When a LicenseClient is available, uses JWT token from it.
|
|
130
|
-
* Story 31.04: NEVER sends raw license key as Bearer token.
|
|
131
|
-
* If JWT is unavailable, omits Authorization header entirely.
|
|
132
|
-
* Server will return 401, which fetchWithRetry handles via forceRefresh.
|
|
133
|
-
*/
|
|
134
|
-
async buildHeaders() {
|
|
135
|
-
const headers = {
|
|
136
|
-
'Content-Type': 'application/json',
|
|
137
|
-
'X-Client-Version': '0.1.0',
|
|
138
|
-
};
|
|
139
|
-
if (this.licenseClient) {
|
|
140
|
-
const jwt = await this.licenseClient.getToken();
|
|
141
|
-
if (jwt) {
|
|
142
|
-
headers['Authorization'] = `Bearer ${jwt}`;
|
|
143
|
-
}
|
|
144
|
-
// No JWT available: omit Authorization header entirely
|
|
145
|
-
// Server will return 401, which fetchWithRetry handles via forceRefresh
|
|
146
|
-
}
|
|
147
|
-
return headers;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Execute HTTP request with retry and exponential backoff.
|
|
151
|
-
* Handles 401 specially: if a licenseClient is available, attempts
|
|
152
|
-
* forceRefresh() and retries once with the new token.
|
|
153
|
-
*/
|
|
154
|
-
async fetchWithRetry(options) {
|
|
155
|
-
let lastError;
|
|
156
|
-
let authRetried = false;
|
|
157
|
-
for (let attempt = 0; attempt <= this.retryCount; attempt++) {
|
|
158
|
-
try {
|
|
159
|
-
const controller = new AbortController();
|
|
160
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
161
|
-
const url = `${this.serverUrl}${options.path}`;
|
|
162
|
-
const fetchOptions = {
|
|
163
|
-
method: options.method,
|
|
164
|
-
headers: await this.buildHeaders(),
|
|
165
|
-
signal: controller.signal,
|
|
166
|
-
};
|
|
167
|
-
if (options.body) {
|
|
168
|
-
fetchOptions.body = JSON.stringify(options.body);
|
|
169
|
-
}
|
|
170
|
-
const response = await fetch(url, fetchOptions);
|
|
171
|
-
clearTimeout(timeoutId);
|
|
172
|
-
if (!response.ok) {
|
|
173
|
-
const errorBody = await response.text().catch(() => 'Unknown error');
|
|
174
|
-
// 401: attempt token refresh once if licenseClient is available
|
|
175
|
-
if (response.status === 401 && !authRetried && this.licenseClient) {
|
|
176
|
-
authRetried = true;
|
|
177
|
-
const newToken = await this.licenseClient.forceRefresh();
|
|
178
|
-
if (newToken) {
|
|
179
|
-
// Retry immediately with the new token (don't count as a normal retry)
|
|
180
|
-
attempt--; // offset the loop increment so this doesn't consume a retry
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
// forceRefresh failed - throw immediately
|
|
184
|
-
throw new RemoteResolverError(`API error: ${response.status} ${response.statusText} - ${errorBody}`, response.status, options.path);
|
|
185
|
-
}
|
|
186
|
-
// Don't retry on 4xx client errors (except 429 rate limit)
|
|
187
|
-
if (response.status >= 400 && response.status < 500 && response.status !== 429) {
|
|
188
|
-
throw new RemoteResolverError(`API error: ${response.status} ${response.statusText} - ${errorBody}`, response.status, options.path);
|
|
189
|
-
}
|
|
190
|
-
// Retry on 5xx server errors and 429 rate limit
|
|
191
|
-
throw new RemoteResolverError(`Server error: ${response.status} ${response.statusText}`, response.status, options.path);
|
|
192
|
-
}
|
|
193
|
-
return (await response.json());
|
|
194
|
-
}
|
|
195
|
-
catch (error) {
|
|
196
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
197
|
-
// Don't retry on non-retryable errors (4xx except 429)
|
|
198
|
-
if (error instanceof RemoteResolverError && error.statusCode && error.statusCode >= 400 && error.statusCode < 500 && error.statusCode !== 429) {
|
|
199
|
-
throw error;
|
|
200
|
-
}
|
|
201
|
-
// Apply backoff before next retry (except on last attempt)
|
|
202
|
-
if (attempt < this.retryCount) {
|
|
203
|
-
const backoff = Math.min(BACKOFF_BASE_MS * Math.pow(2, attempt), BACKOFF_MAX_MS);
|
|
204
|
-
// Add jitter (0-25% of backoff)
|
|
205
|
-
const jitter = Math.random() * backoff * 0.25;
|
|
206
|
-
await this.sleep(backoff + jitter);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
throw lastError ?? new RemoteResolverError('All retry attempts failed', undefined, options.path);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Fetch with persistent cache fallback: try HTTP first, fall back to cache
|
|
214
|
-
* on failure. On successful HTTP response, update the cache.
|
|
215
|
-
*
|
|
216
|
-
* P70.06: ONLY the registry uses this path. Asset content (step/skill/
|
|
217
|
-
* standard) uses {@link fetchWithInMemoryCache} so it never touches disk.
|
|
218
|
-
*/
|
|
219
|
-
async fetchWithCacheFallback(cacheKey, options) {
|
|
220
|
-
try {
|
|
221
|
-
const result = await this.fetchWithRetry(options);
|
|
222
|
-
// Update cache with fresh data (fire-and-forget)
|
|
223
|
-
this.cache.set(cacheKey, JSON.stringify(result)).catch(() => {
|
|
224
|
-
// Cache write failures are non-critical
|
|
225
|
-
});
|
|
226
|
-
return result;
|
|
227
|
-
}
|
|
228
|
-
catch (error) {
|
|
229
|
-
// Try cache fallback
|
|
230
|
-
const cached = await this.cache.get(cacheKey);
|
|
231
|
-
if (cached !== null) {
|
|
232
|
-
try {
|
|
233
|
-
return JSON.parse(cached);
|
|
234
|
-
}
|
|
235
|
-
catch {
|
|
236
|
-
// Invalid cache data, rethrow original error
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
throw error;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* P70.06: fetch with VOLATILE in-memory cache fallback.
|
|
244
|
-
*
|
|
245
|
-
* Identical structure to {@link fetchWithCacheFallback} but the backing
|
|
246
|
-
* store is {@link InMemoryAssetCache} -- entries live only in the running
|
|
247
|
-
* process memory, never written to disk.
|
|
248
|
-
*
|
|
249
|
-
* When the server is unreachable and the in-memory cache is empty (e.g.
|
|
250
|
-
* first invocation of a fresh CLI process), the original error is
|
|
251
|
-
* re-thrown instead of silently degrading to a stale disk cache.
|
|
252
|
-
*/
|
|
253
|
-
async fetchWithInMemoryCache(cacheKey, options) {
|
|
254
|
-
try {
|
|
255
|
-
const result = await this.fetchWithRetry(options);
|
|
256
|
-
// Update volatile cache (fire-and-forget)
|
|
257
|
-
this.assetCache.set(cacheKey, JSON.stringify(result)).catch(() => {
|
|
258
|
-
// Cache write failures are non-critical
|
|
259
|
-
});
|
|
260
|
-
return result;
|
|
261
|
-
}
|
|
262
|
-
catch (error) {
|
|
263
|
-
// Try in-memory fallback -- NEVER fall back to disk cache
|
|
264
|
-
const cached = await this.assetCache.get(cacheKey);
|
|
265
|
-
if (cached !== null) {
|
|
266
|
-
try {
|
|
267
|
-
return JSON.parse(cached);
|
|
268
|
-
}
|
|
269
|
-
catch {
|
|
270
|
-
// Invalid cache data, rethrow original error
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
throw error;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Sleep for the specified duration.
|
|
278
|
-
*/
|
|
279
|
-
sleep(ms) {
|
|
280
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
281
|
-
}
|
|
282
|
-
}
|
|
1
|
+
import{ResolverMode as y,NoOpCache as d}from"../types/index.js";import{InMemoryAssetCache as m}from"../cache/in-memory-asset-cache.js";const C=3e4,f=3,w=1e3,p=1e4;class o extends Error{statusCode;endpoint;constructor(t,e,s){super(t),this.statusCode=e,this.endpoint=s,this.name="RemoteResolverError"}}class ${mode=y.REMOTE;serverUrl;licenseKey;timeout;retryCount;cache;assetCache;licenseClient;constructor(t){this.serverUrl=t.serverUrl.replace(/\/+$/,""),this.licenseKey=t.licenseKey,this.timeout=t.timeout??C,this.retryCount=t.retryCount??f,this.cache=t.cacheProvider??new d,this.licenseClient=t.licenseClient??null,this.assetCache=new m}async resolveStep(t,e){const s=`step:${t}`;return this.fetchWithInMemoryCache(s,{method:"GET",path:`/api/v1/steps/${encodeURIComponent(t)}`})}async resolveSkill(t,e){const s=`skill:${t}`;return this.fetchWithInMemoryCache(s,{method:"GET",path:`/api/v1/skills/${encodeURIComponent(t)}`})}async resolveStandard(t){const e=`standard:${t}`;return this.fetchWithInMemoryCache(e,{method:"GET",path:`/api/v1/standards/${encodeURIComponent(t)}`})}async resolveRegistry(){return this.fetchWithCacheFallback("registry",{method:"GET",path:"/api/v1/registry"})}async assemblePrompt(t,e){return this.fetchWithRetry({method:"POST",path:"/api/v1/prompts/assemble",body:{stepId:t,context:e}})}async isAvailable(){try{const t=new AbortController,e=setTimeout(()=>t.abort(),5e3),s=await fetch(`${this.serverUrl}/api/v1/health`,{method:"GET",headers:await this.buildHeaders(),signal:t.signal});return clearTimeout(e),s.ok}catch{return!1}}async dispose(){await this.cache.clear(),await this.assetCache.clear()}async buildHeaders(){const t={"Content-Type":"application/json","X-Client-Version":"0.1.0"};if(this.licenseClient){const e=await this.licenseClient.getToken();e&&(t.Authorization=`Bearer ${e}`)}return t}async fetchWithRetry(t){let e,s=!1;for(let n=0;n<=this.retryCount;n++)try{const a=new AbortController,c=setTimeout(()=>a.abort(),this.timeout),i=`${this.serverUrl}${t.path}`,h={method:t.method,headers:await this.buildHeaders(),signal:a.signal};t.body&&(h.body=JSON.stringify(t.body));const r=await fetch(i,h);if(clearTimeout(c),!r.ok){const l=await r.text().catch(()=>"Unknown error");if(r.status===401&&!s&&this.licenseClient){if(s=!0,await this.licenseClient.forceRefresh()){n--;continue}throw new o(`API error: ${r.status} ${r.statusText} - ${l}`,r.status,t.path)}throw r.status>=400&&r.status<500&&r.status!==429?new o(`API error: ${r.status} ${r.statusText} - ${l}`,r.status,t.path):new o(`Server error: ${r.status} ${r.statusText}`,r.status,t.path)}return await r.json()}catch(a){if(e=a instanceof Error?a:new Error(String(a)),a instanceof o&&a.statusCode&&a.statusCode>=400&&a.statusCode<500&&a.statusCode!==429)throw a;if(n<this.retryCount){const c=Math.min(w*Math.pow(2,n),p),i=Math.random()*c*.25;await this.sleep(c+i)}}throw e??new o("All retry attempts failed",void 0,t.path)}async fetchWithCacheFallback(t,e){try{const s=await this.fetchWithRetry(e);return this.cache.set(t,JSON.stringify(s)).catch(()=>{}),s}catch(s){const n=await this.cache.get(t);if(n!==null)try{return JSON.parse(n)}catch{}throw s}}async fetchWithInMemoryCache(t,e){try{const s=await this.fetchWithRetry(e);return this.assetCache.set(t,JSON.stringify(s)).catch(()=>{}),s}catch(s){const n=await this.assetCache.get(t);if(n!==null)try{return JSON.parse(n)}catch{}throw s}}sleep(t){return new Promise(e=>setTimeout(e,t))}}export{$ as RemoteResolver,o as RemoteResolverError};
|
|
@@ -1,131 +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 - Offline Telemetry Queue
|
|
15
|
-
*
|
|
16
|
-
* Persistent FIFO queue for telemetry events when the CLI operates offline.
|
|
17
|
-
* Events are stored in a JSON file and flushed when connection is restored.
|
|
18
|
-
* Respects configurable limits on event count (1000) and file size (5MB).
|
|
19
|
-
*
|
|
20
|
-
* Story 42.9
|
|
21
|
-
*/
|
|
22
|
-
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
23
|
-
import { dirname } from 'node:path';
|
|
24
|
-
import { join } from 'node:path';
|
|
25
|
-
import { homedir } from 'node:os';
|
|
26
|
-
// ── Defaults ──────────────────────────────────────────────────────────────
|
|
27
|
-
const DEFAULT_CONFIG = {
|
|
28
|
-
queueFilePath: join(homedir(), '.neocortex', '.telemetry-queue'),
|
|
29
|
-
maxEvents: 1_000,
|
|
30
|
-
maxSizeBytes: 5_242_880, // 5MB
|
|
31
|
-
};
|
|
32
|
-
// ── OfflineTelemetryQueue ────────────────────────────────────────────────
|
|
33
|
-
export class OfflineTelemetryQueue {
|
|
34
|
-
config;
|
|
35
|
-
constructor(config) {
|
|
36
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Add a telemetry event to the queue.
|
|
40
|
-
* Evicts oldest events if limits are reached.
|
|
41
|
-
*/
|
|
42
|
-
async enqueue(event) {
|
|
43
|
-
try {
|
|
44
|
-
const events = await this.loadQueue();
|
|
45
|
-
events.push(event);
|
|
46
|
-
// Evict oldest events if count limit exceeded
|
|
47
|
-
while (events.length > this.config.maxEvents) {
|
|
48
|
-
events.shift();
|
|
49
|
-
}
|
|
50
|
-
// Evict oldest events if size limit exceeded
|
|
51
|
-
let serialized = JSON.stringify(events);
|
|
52
|
-
while (Buffer.byteLength(serialized, 'utf8') > this.config.maxSizeBytes && events.length > 0) {
|
|
53
|
-
events.shift();
|
|
54
|
-
serialized = JSON.stringify(events);
|
|
55
|
-
}
|
|
56
|
-
await this.saveQueue(events);
|
|
57
|
-
}
|
|
58
|
-
catch {
|
|
59
|
-
// Queue write failures are non-critical
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Flush all queued events using the provided send function.
|
|
64
|
-
* Returns count of sent and failed events.
|
|
65
|
-
*/
|
|
66
|
-
async flush(sendFn) {
|
|
67
|
-
try {
|
|
68
|
-
const events = await this.loadQueue();
|
|
69
|
-
if (events.length === 0) {
|
|
70
|
-
return { sent: 0, failed: 0 };
|
|
71
|
-
}
|
|
72
|
-
const success = await sendFn(events);
|
|
73
|
-
if (success) {
|
|
74
|
-
await this.saveQueue([]);
|
|
75
|
-
return { sent: events.length, failed: 0 };
|
|
76
|
-
}
|
|
77
|
-
return { sent: 0, failed: events.length };
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
return { sent: 0, failed: 0 };
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Get queue statistics.
|
|
85
|
-
*/
|
|
86
|
-
async getStats() {
|
|
87
|
-
try {
|
|
88
|
-
const events = await this.loadQueue();
|
|
89
|
-
const serialized = JSON.stringify(events);
|
|
90
|
-
return {
|
|
91
|
-
count: events.length,
|
|
92
|
-
sizeBytes: Buffer.byteLength(serialized, 'utf8'),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
catch {
|
|
96
|
-
return { count: 0, sizeBytes: 0 };
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Clear all queued events.
|
|
101
|
-
*/
|
|
102
|
-
async clear() {
|
|
103
|
-
try {
|
|
104
|
-
await this.saveQueue([]);
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
// Clear failures are non-critical
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// ── Private ─────────────────────────────────────────────────────────
|
|
111
|
-
async loadQueue() {
|
|
112
|
-
try {
|
|
113
|
-
const raw = await readFile(this.config.queueFilePath, 'utf8');
|
|
114
|
-
const data = JSON.parse(raw);
|
|
115
|
-
if (!Array.isArray(data))
|
|
116
|
-
return [];
|
|
117
|
-
return data;
|
|
118
|
-
}
|
|
119
|
-
catch {
|
|
120
|
-
return [];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
async saveQueue(events) {
|
|
124
|
-
const dir = dirname(this.config.queueFilePath);
|
|
125
|
-
await mkdir(dir, { recursive: true });
|
|
126
|
-
const tmpPath = `${this.config.queueFilePath}.tmp`;
|
|
127
|
-
await writeFile(tmpPath, JSON.stringify(events), 'utf8');
|
|
128
|
-
const { rename } = await import('node:fs/promises');
|
|
129
|
-
await rename(tmpPath, this.config.queueFilePath);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
1
|
+
import{readFile as a,writeFile as n,mkdir as r}from"node:fs/promises";import{dirname as u}from"node:path";import{join as c}from"node:path";import{homedir as o}from"node:os";const h={queueFilePath:c(o(),".neocortex",".telemetry-queue"),maxEvents:1e3,maxSizeBytes:5242880};class d{config;constructor(t){this.config={...h,...t}}async enqueue(t){try{const e=await this.loadQueue();for(e.push(t);e.length>this.config.maxEvents;)e.shift();let i=JSON.stringify(e);for(;Buffer.byteLength(i,"utf8")>this.config.maxSizeBytes&&e.length>0;)e.shift(),i=JSON.stringify(e);await this.saveQueue(e)}catch{}}async flush(t){try{const e=await this.loadQueue();return e.length===0?{sent:0,failed:0}:await t(e)?(await this.saveQueue([]),{sent:e.length,failed:0}):{sent:0,failed:e.length}}catch{return{sent:0,failed:0}}}async getStats(){try{const t=await this.loadQueue(),e=JSON.stringify(t);return{count:t.length,sizeBytes:Buffer.byteLength(e,"utf8")}}catch{return{count:0,sizeBytes:0}}}async clear(){try{await this.saveQueue([])}catch{}}async loadQueue(){try{const t=await a(this.config.queueFilePath,"utf8"),e=JSON.parse(t);return Array.isArray(e)?e:[]}catch{return[]}}async saveQueue(t){const e=u(this.config.queueFilePath);await r(e,{recursive:!0});const i=`${this.config.queueFilePath}.tmp`;await n(i,JSON.stringify(t),"utf8");const{rename:s}=await import("node:fs/promises");await s(i,this.config.queueFilePath)}}export{d as OfflineTelemetryQueue};
|