@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,282 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ */
5
+ export { OfflineTelemetryQueue, type TelemetryEvent, type QueueConfig, type QueueStats, type FlushResult } from './offline-queue.js';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ */
5
+ export { OfflineTelemetryQueue } from './offline-queue.js';
@@ -0,0 +1,57 @@
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
+ export interface TelemetryEvent {
14
+ type: string;
15
+ timestamp: number;
16
+ data: Record<string, unknown>;
17
+ }
18
+ export interface QueueConfig {
19
+ /** Path to queue file. Default: ~/.neocortex/.telemetry-queue */
20
+ queueFilePath: string;
21
+ /** Maximum number of events in queue. Default: 1000 */
22
+ maxEvents: number;
23
+ /** Maximum queue file size in bytes. Default: 5_242_880 (5MB) */
24
+ maxSizeBytes: number;
25
+ }
26
+ export interface QueueStats {
27
+ count: number;
28
+ sizeBytes: number;
29
+ }
30
+ export interface FlushResult {
31
+ sent: number;
32
+ failed: number;
33
+ }
34
+ export declare class OfflineTelemetryQueue {
35
+ private readonly config;
36
+ constructor(config?: Partial<QueueConfig>);
37
+ /**
38
+ * Add a telemetry event to the queue.
39
+ * Evicts oldest events if limits are reached.
40
+ */
41
+ enqueue(event: TelemetryEvent): Promise<void>;
42
+ /**
43
+ * Flush all queued events using the provided send function.
44
+ * Returns count of sent and failed events.
45
+ */
46
+ flush(sendFn: (events: TelemetryEvent[]) => Promise<boolean>): Promise<FlushResult>;
47
+ /**
48
+ * Get queue statistics.
49
+ */
50
+ getStats(): Promise<QueueStats>;
51
+ /**
52
+ * Clear all queued events.
53
+ */
54
+ clear(): Promise<void>;
55
+ private loadQueue;
56
+ private saveQueue;
57
+ }
@@ -0,0 +1,131 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ */
5
+ export { TierAwareClient, type TierAwareClientOptions, type PreFlightResult, type QuotaSnapshot, type SubscriptionTier, } from './tier-aware-client.js';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @license FSL-1.1
3
+ * Copyright (c) 2026 OrNexus AI
4
+ */
5
+ export { TierAwareClient, } from './tier-aware-client.js';
@@ -0,0 +1,97 @@
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 - TierAwareClient
15
+ *
16
+ * Client-side tier awareness: JWT tier extraction, cached tier,
17
+ * pre-flight trigger checks, quota caching from response metadata,
18
+ * and offline enforcement via cached quota data.
19
+ *
20
+ * Follows the same "NEVER throws" pattern as LicenseClient.
21
+ * Fail-open: if anything fails, default to 'free' tier / allow operation.
22
+ * No stdout output (preserve pipe compatibility) -- stderr only.
23
+ *
24
+ * Epic 60
25
+ */
26
+ import type { CacheProvider } from '../types/index.js';
27
+ import type { LicenseClient } from '../license/license-client.js';
28
+ /** Subscription tier (inlined to avoid import path issues) */
29
+ export type SubscriptionTier = 'free' | 'pro' | 'enterprise';
30
+ /** Result of a pre-flight trigger check */
31
+ export interface PreFlightResult {
32
+ readonly allowed: boolean;
33
+ readonly message?: string;
34
+ }
35
+ /** Cached quota data from server response metadata */
36
+ export interface QuotaSnapshot {
37
+ readonly stepsRemaining: number;
38
+ readonly invocationsRemaining: number;
39
+ readonly stepsLimit: number;
40
+ readonly invocationsLimit: number;
41
+ readonly cachedAt: number;
42
+ }
43
+ export interface TierAwareClientOptions {
44
+ readonly cacheProvider: CacheProvider;
45
+ readonly licenseClient: LicenseClient;
46
+ }
47
+ export declare class TierAwareClient {
48
+ private readonly cache;
49
+ private readonly licenseClient;
50
+ private cachedTier;
51
+ constructor(options: TierAwareClientOptions);
52
+ /**
53
+ * Get the current tier. Checks in-memory, then cache, then JWT token.
54
+ * NEVER throws - returns 'free' on failure (fail-open).
55
+ */
56
+ getCachedTier(): Promise<SubscriptionTier>;
57
+ /**
58
+ * Update cached tier (called when server returns tier info).
59
+ */
60
+ updateTier(tier: SubscriptionTier): Promise<void>;
61
+ /**
62
+ * Invalidate all tier and quota caches.
63
+ * Called when a tier change is detected to ensure fresh data.
64
+ * NEVER throws.
65
+ */
66
+ invalidateTierCache(): Promise<void>;
67
+ /**
68
+ * Check if a trigger is allowed for the current tier WITHOUT calling the server.
69
+ * Returns immediately (in-memory lookup).
70
+ * NEVER throws - returns { allowed: true } on failure (fail-open).
71
+ */
72
+ preFlightCheck(trigger: string, tier?: SubscriptionTier): Promise<PreFlightResult>;
73
+ /**
74
+ * Update cached quota from server response metadata.
75
+ * Called after each successful invoke.
76
+ */
77
+ updateQuotaFromResponse(metadata: Record<string, unknown>): Promise<void>;
78
+ /**
79
+ * Get cached quota for today.
80
+ * NEVER throws - returns null if cache empty or expired.
81
+ */
82
+ getCachedQuota(): Promise<QuotaSnapshot | null>;
83
+ /**
84
+ * Check quota in offline mode using cached data.
85
+ * NEVER throws - returns { allowed: true } on failure (fail-open).
86
+ *
87
+ * @param isOffline - Whether the circuit breaker is in L2+ degradation
88
+ */
89
+ offlineQuotaCheck(isOffline?: boolean): Promise<PreFlightResult>;
90
+ /**
91
+ * Extract tier from JWT payload via base64url decode.
92
+ * No cryptographic verification - client-side is UX only.
93
+ */
94
+ private extractTierFromJwt;
95
+ private buildUpgradeMessage;
96
+ private getQuotaCacheKey;
97
+ }