@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.
- package/LICENSE +56 -0
- package/README.md +32 -0
- package/install.js +486 -0
- package/install.ps1 +1790 -0
- package/install.sh +1587 -0
- package/package.json +104 -0
- package/packages/client/dist/adapters/adapter-registry.d.ts +61 -0
- package/packages/client/dist/adapters/adapter-registry.js +106 -0
- package/packages/client/dist/adapters/antigravity-adapter.d.ts +18 -0
- package/packages/client/dist/adapters/antigravity-adapter.js +77 -0
- package/packages/client/dist/adapters/claude-code-adapter.d.ts +19 -0
- package/packages/client/dist/adapters/claude-code-adapter.js +79 -0
- package/packages/client/dist/adapters/codex-adapter.d.ts +19 -0
- package/packages/client/dist/adapters/codex-adapter.js +80 -0
- package/packages/client/dist/adapters/cursor-adapter.d.ts +19 -0
- package/packages/client/dist/adapters/cursor-adapter.js +115 -0
- package/packages/client/dist/adapters/gemini-adapter.d.ts +18 -0
- package/packages/client/dist/adapters/gemini-adapter.js +71 -0
- package/packages/client/dist/adapters/index.d.ts +19 -0
- package/packages/client/dist/adapters/index.js +21 -0
- package/packages/client/dist/adapters/platform-detector.d.ts +46 -0
- package/packages/client/dist/adapters/platform-detector.js +106 -0
- package/packages/client/dist/adapters/target-adapter.d.ts +70 -0
- package/packages/client/dist/adapters/target-adapter.js +12 -0
- package/packages/client/dist/adapters/vscode-adapter.d.ts +19 -0
- package/packages/client/dist/adapters/vscode-adapter.js +72 -0
- package/packages/client/dist/agent/refresh-stubs.d.ts +65 -0
- package/packages/client/dist/agent/refresh-stubs.js +234 -0
- package/packages/client/dist/agent/update-agent-yaml.d.ts +26 -0
- package/packages/client/dist/agent/update-agent-yaml.js +102 -0
- package/packages/client/dist/agent/update-description.d.ts +45 -0
- package/packages/client/dist/agent/update-description.js +251 -0
- package/packages/client/dist/cache/crypto-utils.d.ts +30 -0
- package/packages/client/dist/cache/crypto-utils.js +76 -0
- package/packages/client/dist/cache/encrypted-cache.d.ts +30 -0
- package/packages/client/dist/cache/encrypted-cache.js +94 -0
- package/packages/client/dist/cache/in-memory-asset-cache.d.ts +59 -0
- package/packages/client/dist/cache/in-memory-asset-cache.js +70 -0
- package/packages/client/dist/cache/index.d.ts +13 -0
- package/packages/client/dist/cache/index.js +13 -0
- package/packages/client/dist/cli.d.ts +14 -0
- package/packages/client/dist/cli.js +194 -0
- package/packages/client/dist/commands/activate.d.ts +55 -0
- package/packages/client/dist/commands/activate.js +390 -0
- package/packages/client/dist/commands/cache-status.d.ts +39 -0
- package/packages/client/dist/commands/cache-status.js +112 -0
- package/packages/client/dist/commands/invoke.d.ts +70 -0
- package/packages/client/dist/commands/invoke.js +490 -0
- package/packages/client/dist/config/resolver-selection.d.ts +40 -0
- package/packages/client/dist/config/resolver-selection.js +278 -0
- package/packages/client/dist/config/secure-config.d.ts +78 -0
- package/packages/client/dist/config/secure-config.js +269 -0
- package/packages/client/dist/constants.d.ts +25 -0
- package/packages/client/dist/constants.js +25 -0
- package/packages/client/dist/context/context-collector.d.ts +28 -0
- package/packages/client/dist/context/context-collector.js +222 -0
- package/packages/client/dist/context/context-sanitizer.d.ts +28 -0
- package/packages/client/dist/context/context-sanitizer.js +145 -0
- package/packages/client/dist/index.d.ts +55 -0
- package/packages/client/dist/index.js +38 -0
- package/packages/client/dist/license/index.d.ts +5 -0
- package/packages/client/dist/license/index.js +5 -0
- package/packages/client/dist/license/license-client.d.ts +79 -0
- package/packages/client/dist/license/license-client.js +257 -0
- package/packages/client/dist/machine/fingerprint.d.ts +34 -0
- package/packages/client/dist/machine/fingerprint.js +160 -0
- package/packages/client/dist/machine/index.d.ts +5 -0
- package/packages/client/dist/machine/index.js +5 -0
- package/packages/client/dist/resilience/circuit-breaker.d.ts +70 -0
- package/packages/client/dist/resilience/circuit-breaker.js +170 -0
- package/packages/client/dist/resilience/degradation-manager.d.ts +67 -0
- package/packages/client/dist/resilience/degradation-manager.js +164 -0
- package/packages/client/dist/resilience/freshness-indicator.d.ts +59 -0
- package/packages/client/dist/resilience/freshness-indicator.js +100 -0
- package/packages/client/dist/resilience/index.d.ts +8 -0
- package/packages/client/dist/resilience/index.js +8 -0
- package/packages/client/dist/resilience/recovery-detector.d.ts +59 -0
- package/packages/client/dist/resilience/recovery-detector.js +74 -0
- package/packages/client/dist/resolvers/asset-resolver.d.ts +79 -0
- package/packages/client/dist/resolvers/asset-resolver.js +13 -0
- package/packages/client/dist/resolvers/local-resolver.d.ts +26 -0
- package/packages/client/dist/resolvers/local-resolver.js +218 -0
- package/packages/client/dist/resolvers/remote-resolver.d.ts +91 -0
- package/packages/client/dist/resolvers/remote-resolver.js +282 -0
- package/packages/client/dist/telemetry/index.d.ts +5 -0
- package/packages/client/dist/telemetry/index.js +5 -0
- package/packages/client/dist/telemetry/offline-queue.d.ts +57 -0
- package/packages/client/dist/telemetry/offline-queue.js +131 -0
- package/packages/client/dist/tier/index.d.ts +5 -0
- package/packages/client/dist/tier/index.js +5 -0
- package/packages/client/dist/tier/tier-aware-client.d.ts +97 -0
- package/packages/client/dist/tier/tier-aware-client.js +260 -0
- package/packages/client/dist/types/index.d.ts +140 -0
- package/packages/client/dist/types/index.js +38 -0
- package/postinstall.js +272 -0
- package/targets-stubs/antigravity/README.md +36 -0
- package/targets-stubs/antigravity/gemini.md +22 -0
- package/targets-stubs/antigravity/install-antigravity.sh +44 -0
- package/targets-stubs/antigravity/mcp-config.json +9 -0
- package/targets-stubs/antigravity/skill/SKILL.md +67 -0
- package/targets-stubs/claude-code/README.md +20 -0
- package/targets-stubs/claude-code/neocortex.agent.yaml +24 -0
- package/targets-stubs/claude-code/neocortex.md +125 -0
- package/targets-stubs/codex/README.md +32 -0
- package/targets-stubs/codex/agents.md +61 -0
- package/targets-stubs/codex/config-mcp.toml +6 -0
- package/targets-stubs/codex/install-codex.sh +61 -0
- package/targets-stubs/cursor/README.md +33 -0
- package/targets-stubs/cursor/agent.md +94 -0
- package/targets-stubs/cursor/install-cursor.sh +35 -0
- package/targets-stubs/cursor/mcp.json +11 -0
- package/targets-stubs/gemini-cli/README.md +34 -0
- package/targets-stubs/gemini-cli/agent.md +101 -0
- package/targets-stubs/gemini-cli/gemini.md +16 -0
- package/targets-stubs/gemini-cli/install-gemini.sh +56 -0
- package/targets-stubs/gemini-cli/settings-mcp.json +11 -0
- package/targets-stubs/vscode/README.md +34 -0
- package/targets-stubs/vscode/agent.md +102 -0
- package/targets-stubs/vscode/copilot-instructions.md +16 -0
- package/targets-stubs/vscode/install-vscode.sh +42 -0
- package/targets-stubs/vscode/mcp.json +13 -0
|
@@ -0,0 +1,170 @@
|
|
|
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 - Persistent Circuit Breaker
|
|
15
|
+
*
|
|
16
|
+
* Circuit breaker with file-based state persistence for the CLI client.
|
|
17
|
+
* Tracks server health using window-based failure counting and persists
|
|
18
|
+
* state between CLI invocations at ~/.neocortex/.circuit-state.
|
|
19
|
+
*
|
|
20
|
+
* States:
|
|
21
|
+
* CLOSED - Normal operation, requests pass through
|
|
22
|
+
* OPEN - Circuit tripped, requests fail immediately (use cache)
|
|
23
|
+
* HALF_OPEN - Testing if server recovered (one probe request allowed)
|
|
24
|
+
*
|
|
25
|
+
* Story 42.9
|
|
26
|
+
*/
|
|
27
|
+
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
28
|
+
import { dirname } from 'node:path';
|
|
29
|
+
import { homedir } from 'node:os';
|
|
30
|
+
import { join } from 'node:path';
|
|
31
|
+
// ── Defaults ──────────────────────────────────────────────────────────────
|
|
32
|
+
const DEFAULT_CONFIG = {
|
|
33
|
+
failureThreshold: 3,
|
|
34
|
+
failureWindowMs: 60_000,
|
|
35
|
+
halfOpenAfterMs: 60_000,
|
|
36
|
+
stateFilePath: join(homedir(), '.neocortex', '.circuit-state'),
|
|
37
|
+
};
|
|
38
|
+
const INITIAL_STATE = {
|
|
39
|
+
state: 'CLOSED',
|
|
40
|
+
failures: [],
|
|
41
|
+
openedAt: null,
|
|
42
|
+
lastProbeAt: null,
|
|
43
|
+
};
|
|
44
|
+
// ── ClientCircuitBreaker ──────────────────────────────────────────────────
|
|
45
|
+
export class ClientCircuitBreaker {
|
|
46
|
+
config;
|
|
47
|
+
internalState;
|
|
48
|
+
constructor(config, initialState) {
|
|
49
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
50
|
+
this.internalState = initialState ? { ...initialState } : { ...INITIAL_STATE, failures: [] };
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if a request can be made to the server.
|
|
54
|
+
* Returns false if circuit is OPEN (fail-fast to cache).
|
|
55
|
+
*/
|
|
56
|
+
canCall() {
|
|
57
|
+
this.pruneExpiredFailures();
|
|
58
|
+
switch (this.internalState.state) {
|
|
59
|
+
case 'CLOSED':
|
|
60
|
+
return true;
|
|
61
|
+
case 'OPEN': {
|
|
62
|
+
const now = Date.now();
|
|
63
|
+
const openedAt = this.internalState.openedAt ?? 0;
|
|
64
|
+
if (now - openedAt >= this.config.halfOpenAfterMs) {
|
|
65
|
+
this.internalState.state = 'HALF_OPEN';
|
|
66
|
+
this.internalState.lastProbeAt = now;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
case 'HALF_OPEN':
|
|
72
|
+
return true;
|
|
73
|
+
default:
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Record a successful server response. Closes the circuit.
|
|
79
|
+
*/
|
|
80
|
+
async recordSuccess() {
|
|
81
|
+
this.internalState.state = 'CLOSED';
|
|
82
|
+
this.internalState.failures = [];
|
|
83
|
+
this.internalState.openedAt = null;
|
|
84
|
+
this.internalState.lastProbeAt = null;
|
|
85
|
+
await this.saveToDisk();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Record a failed server response. Opens circuit after threshold failures in window.
|
|
89
|
+
*/
|
|
90
|
+
async recordFailure() {
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
this.internalState.failures.push({ timestamp: now });
|
|
93
|
+
this.pruneExpiredFailures();
|
|
94
|
+
if (this.internalState.failures.length >= this.config.failureThreshold) {
|
|
95
|
+
this.internalState.state = 'OPEN';
|
|
96
|
+
this.internalState.openedAt = now;
|
|
97
|
+
}
|
|
98
|
+
// If in HALF_OPEN and probe fails, go back to OPEN
|
|
99
|
+
if (this.internalState.state === 'HALF_OPEN') {
|
|
100
|
+
this.internalState.state = 'OPEN';
|
|
101
|
+
this.internalState.openedAt = now;
|
|
102
|
+
}
|
|
103
|
+
await this.saveToDisk();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get current circuit breaker state (copy).
|
|
107
|
+
*/
|
|
108
|
+
getState() {
|
|
109
|
+
this.pruneExpiredFailures();
|
|
110
|
+
return {
|
|
111
|
+
state: this.internalState.state,
|
|
112
|
+
failures: [...this.internalState.failures],
|
|
113
|
+
openedAt: this.internalState.openedAt,
|
|
114
|
+
lastProbeAt: this.internalState.lastProbeAt,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Reset circuit breaker to initial CLOSED state.
|
|
119
|
+
*/
|
|
120
|
+
async reset() {
|
|
121
|
+
this.internalState = { ...INITIAL_STATE, failures: [] };
|
|
122
|
+
await this.saveToDisk();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Load circuit breaker state from disk.
|
|
126
|
+
*/
|
|
127
|
+
static async loadFromDisk(path) {
|
|
128
|
+
const filePath = path ?? DEFAULT_CONFIG.stateFilePath;
|
|
129
|
+
try {
|
|
130
|
+
const raw = await readFile(filePath, 'utf8');
|
|
131
|
+
const data = JSON.parse(raw);
|
|
132
|
+
// Validate loaded state
|
|
133
|
+
if (!data.state || !Array.isArray(data.failures)) {
|
|
134
|
+
return new ClientCircuitBreaker({ stateFilePath: filePath });
|
|
135
|
+
}
|
|
136
|
+
return new ClientCircuitBreaker({ stateFilePath: filePath }, data);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// File doesn't exist or is corrupted - start fresh
|
|
140
|
+
return new ClientCircuitBreaker({ stateFilePath: filePath });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// ── Private ─────────────────────────────────────────────────────────
|
|
144
|
+
/**
|
|
145
|
+
* Remove failures outside the counting window.
|
|
146
|
+
*/
|
|
147
|
+
pruneExpiredFailures() {
|
|
148
|
+
const cutoff = Date.now() - this.config.failureWindowMs;
|
|
149
|
+
this.internalState.failures = this.internalState.failures.filter((f) => f.timestamp > cutoff);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Persist state to disk with atomic write.
|
|
153
|
+
*/
|
|
154
|
+
async saveToDisk() {
|
|
155
|
+
try {
|
|
156
|
+
const dir = dirname(this.config.stateFilePath);
|
|
157
|
+
await mkdir(dir, { recursive: true });
|
|
158
|
+
const tmpPath = `${this.config.stateFilePath}.tmp`;
|
|
159
|
+
const data = JSON.stringify(this.internalState, null, 2);
|
|
160
|
+
await writeFile(tmpPath, data, 'utf8');
|
|
161
|
+
// Atomic rename
|
|
162
|
+
const { rename } = await import('node:fs/promises');
|
|
163
|
+
await rename(tmpPath, this.config.stateFilePath);
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
// Persistence failures are non-critical - circuit breaker
|
|
167
|
+
// still works in-memory for current session
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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 - Degradation Manager
|
|
15
|
+
*
|
|
16
|
+
* Evaluates current degradation level (L0-L4) based on circuit breaker state,
|
|
17
|
+
* measured latency, cache availability and freshness. Returns a decision
|
|
18
|
+
* object that tells the resolver how to behave.
|
|
19
|
+
*
|
|
20
|
+
* Story 42.9
|
|
21
|
+
*/
|
|
22
|
+
import type { CircuitState } from './circuit-breaker.js';
|
|
23
|
+
export declare enum DegradationLevel {
|
|
24
|
+
/** Server available, fresh data, optimal performance */
|
|
25
|
+
ONLINE = 0,
|
|
26
|
+
/** Server slow (>2s), use cache when available */
|
|
27
|
+
HIGH_LATENCY = 1,
|
|
28
|
+
/** Server down, use cache exclusively */
|
|
29
|
+
SERVER_DOWN = 2,
|
|
30
|
+
/** Cache expired (>24h), use with warning */
|
|
31
|
+
STALE_CACHE = 3,
|
|
32
|
+
/** No cache and no server - error state */
|
|
33
|
+
NO_CACHE = 4
|
|
34
|
+
}
|
|
35
|
+
export interface DegradationContext {
|
|
36
|
+
circuitState: CircuitState;
|
|
37
|
+
latencyMs?: number;
|
|
38
|
+
cacheAvailable: boolean;
|
|
39
|
+
cacheAgeMs?: number;
|
|
40
|
+
forceOffline: boolean;
|
|
41
|
+
}
|
|
42
|
+
export interface DegradationDecision {
|
|
43
|
+
level: DegradationLevel;
|
|
44
|
+
useCache: boolean;
|
|
45
|
+
useServer: boolean;
|
|
46
|
+
prefix: string;
|
|
47
|
+
warning: string | null;
|
|
48
|
+
}
|
|
49
|
+
export interface DegradationManagerConfig {
|
|
50
|
+
/** Cache age threshold for STALE warning. Default: 86_400_000 (24h) */
|
|
51
|
+
staleThresholdMs: number;
|
|
52
|
+
/** Cache age threshold for initial warning. Default: 43_200_000 (12h) */
|
|
53
|
+
warningThresholdMs: number;
|
|
54
|
+
/** Latency threshold for HIGH_LATENCY level. Default: 2000 (2s) */
|
|
55
|
+
latencyThresholdMs: number;
|
|
56
|
+
}
|
|
57
|
+
export declare class DegradationManager {
|
|
58
|
+
private readonly config;
|
|
59
|
+
constructor(config?: Partial<DegradationManagerConfig>);
|
|
60
|
+
/**
|
|
61
|
+
* Evaluate the current degradation level based on context.
|
|
62
|
+
*/
|
|
63
|
+
evaluate(context: DegradationContext): DegradationDecision;
|
|
64
|
+
private buildServerDownDecision;
|
|
65
|
+
private buildCacheOnlyDecision;
|
|
66
|
+
private formatAge;
|
|
67
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
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
|
+
// ── Types ─────────────────────────────────────────────────────────────────
|
|
14
|
+
export var DegradationLevel;
|
|
15
|
+
(function (DegradationLevel) {
|
|
16
|
+
/** Server available, fresh data, optimal performance */
|
|
17
|
+
DegradationLevel[DegradationLevel["ONLINE"] = 0] = "ONLINE";
|
|
18
|
+
/** Server slow (>2s), use cache when available */
|
|
19
|
+
DegradationLevel[DegradationLevel["HIGH_LATENCY"] = 1] = "HIGH_LATENCY";
|
|
20
|
+
/** Server down, use cache exclusively */
|
|
21
|
+
DegradationLevel[DegradationLevel["SERVER_DOWN"] = 2] = "SERVER_DOWN";
|
|
22
|
+
/** Cache expired (>24h), use with warning */
|
|
23
|
+
DegradationLevel[DegradationLevel["STALE_CACHE"] = 3] = "STALE_CACHE";
|
|
24
|
+
/** No cache and no server - error state */
|
|
25
|
+
DegradationLevel[DegradationLevel["NO_CACHE"] = 4] = "NO_CACHE";
|
|
26
|
+
})(DegradationLevel || (DegradationLevel = {}));
|
|
27
|
+
// ── Defaults ──────────────────────────────────────────────────────────────
|
|
28
|
+
const DEFAULT_CONFIG = {
|
|
29
|
+
staleThresholdMs: 86_400_000, // 24 hours
|
|
30
|
+
warningThresholdMs: 43_200_000, // 12 hours
|
|
31
|
+
latencyThresholdMs: 2_000, // 2 seconds
|
|
32
|
+
};
|
|
33
|
+
// ── DegradationManager ───────────────────────────────────────────────────
|
|
34
|
+
export class DegradationManager {
|
|
35
|
+
config;
|
|
36
|
+
constructor(config) {
|
|
37
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Evaluate the current degradation level based on context.
|
|
41
|
+
*/
|
|
42
|
+
evaluate(context) {
|
|
43
|
+
// Force-offline mode: always use cache
|
|
44
|
+
if (context.forceOffline) {
|
|
45
|
+
return this.buildCacheOnlyDecision(context, '[OFFLINE]');
|
|
46
|
+
}
|
|
47
|
+
// Circuit is OPEN: server known to be down
|
|
48
|
+
if (context.circuitState === 'OPEN') {
|
|
49
|
+
return this.buildServerDownDecision(context);
|
|
50
|
+
}
|
|
51
|
+
// Circuit is HALF_OPEN: probing server
|
|
52
|
+
if (context.circuitState === 'HALF_OPEN') {
|
|
53
|
+
// Allow server request (probe), but prepare cache fallback
|
|
54
|
+
return {
|
|
55
|
+
level: DegradationLevel.SERVER_DOWN,
|
|
56
|
+
useCache: true,
|
|
57
|
+
useServer: true,
|
|
58
|
+
prefix: '[PROBING]',
|
|
59
|
+
warning: null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// Circuit is CLOSED: server available
|
|
63
|
+
if (context.latencyMs != null && context.latencyMs > this.config.latencyThresholdMs) {
|
|
64
|
+
// High latency: prefer cache if available
|
|
65
|
+
if (context.cacheAvailable) {
|
|
66
|
+
return {
|
|
67
|
+
level: DegradationLevel.HIGH_LATENCY,
|
|
68
|
+
useCache: true,
|
|
69
|
+
useServer: false,
|
|
70
|
+
prefix: '[CACHED]',
|
|
71
|
+
warning: null,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// No cache available: still use server (slow is better than nothing)
|
|
75
|
+
return {
|
|
76
|
+
level: DegradationLevel.HIGH_LATENCY,
|
|
77
|
+
useCache: false,
|
|
78
|
+
useServer: true,
|
|
79
|
+
prefix: '',
|
|
80
|
+
warning: 'Server response is slow. Consider running `neocortex cache warmup`.',
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// Normal operation
|
|
84
|
+
return {
|
|
85
|
+
level: DegradationLevel.ONLINE,
|
|
86
|
+
useCache: false,
|
|
87
|
+
useServer: true,
|
|
88
|
+
prefix: '',
|
|
89
|
+
warning: null,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
// ── Private ─────────────────────────────────────────────────────────
|
|
93
|
+
buildServerDownDecision(context) {
|
|
94
|
+
if (!context.cacheAvailable) {
|
|
95
|
+
return {
|
|
96
|
+
level: DegradationLevel.NO_CACHE,
|
|
97
|
+
useCache: false,
|
|
98
|
+
useServer: false,
|
|
99
|
+
prefix: '[ERROR]',
|
|
100
|
+
warning: 'Server unavailable and no cached data. Run `neocortex cache warmup` when online.',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const cacheAgeMs = context.cacheAgeMs ?? 0;
|
|
104
|
+
if (cacheAgeMs > this.config.staleThresholdMs) {
|
|
105
|
+
const ageStr = this.formatAge(cacheAgeMs);
|
|
106
|
+
return {
|
|
107
|
+
level: DegradationLevel.STALE_CACHE,
|
|
108
|
+
useCache: true,
|
|
109
|
+
useServer: false,
|
|
110
|
+
prefix: `[STALE CACHE - ${ageStr}]`,
|
|
111
|
+
warning: `Cache is stale (${ageStr} old). Data may be outdated.`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
level: DegradationLevel.SERVER_DOWN,
|
|
116
|
+
useCache: true,
|
|
117
|
+
useServer: false,
|
|
118
|
+
prefix: '[CACHED]',
|
|
119
|
+
warning: cacheAgeMs > this.config.warningThresholdMs
|
|
120
|
+
? `Cache is ${this.formatAge(cacheAgeMs)} old.`
|
|
121
|
+
: null,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
buildCacheOnlyDecision(context, prefix) {
|
|
125
|
+
if (!context.cacheAvailable) {
|
|
126
|
+
return {
|
|
127
|
+
level: DegradationLevel.NO_CACHE,
|
|
128
|
+
useCache: false,
|
|
129
|
+
useServer: false,
|
|
130
|
+
prefix: '[ERROR]',
|
|
131
|
+
warning: 'Offline mode active but no cached data available. Run `neocortex cache warmup` when online.',
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
const cacheAgeMs = context.cacheAgeMs ?? 0;
|
|
135
|
+
if (cacheAgeMs > this.config.staleThresholdMs) {
|
|
136
|
+
const ageStr = this.formatAge(cacheAgeMs);
|
|
137
|
+
return {
|
|
138
|
+
level: DegradationLevel.STALE_CACHE,
|
|
139
|
+
useCache: true,
|
|
140
|
+
useServer: false,
|
|
141
|
+
prefix: `[STALE CACHE - ${ageStr}]`,
|
|
142
|
+
warning: `Using stale cache (${ageStr} old) in offline mode.`,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
level: DegradationLevel.SERVER_DOWN,
|
|
147
|
+
useCache: true,
|
|
148
|
+
useServer: false,
|
|
149
|
+
prefix,
|
|
150
|
+
warning: null,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
formatAge(ms) {
|
|
154
|
+
const hours = Math.floor(ms / 3_600_000);
|
|
155
|
+
if (hours >= 48)
|
|
156
|
+
return `${Math.floor(hours / 24)}d`;
|
|
157
|
+
if (hours >= 1)
|
|
158
|
+
return `${hours}h`;
|
|
159
|
+
const minutes = Math.floor(ms / 60_000);
|
|
160
|
+
if (minutes >= 1)
|
|
161
|
+
return `${minutes}m`;
|
|
162
|
+
return '<1m';
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 - Freshness Indicator
|
|
15
|
+
*
|
|
16
|
+
* Enriches cache responses with freshness metadata for user-facing display.
|
|
17
|
+
* Formats cache age as human-readable strings and generates appropriate
|
|
18
|
+
* warning prefixes based on degradation level.
|
|
19
|
+
*
|
|
20
|
+
* Story 42.9
|
|
21
|
+
*/
|
|
22
|
+
import { DegradationLevel } from './degradation-manager.js';
|
|
23
|
+
export interface FreshnessInfo {
|
|
24
|
+
/** Whether the response came from cache */
|
|
25
|
+
cached: boolean;
|
|
26
|
+
/** Age of cached entry in milliseconds */
|
|
27
|
+
cacheAgeMs: number;
|
|
28
|
+
/** Whether the cache entry is considered stale (>24h) */
|
|
29
|
+
stale: boolean;
|
|
30
|
+
/** Warning message if applicable, null otherwise */
|
|
31
|
+
warning: string | null;
|
|
32
|
+
/** Display prefix for CLI output */
|
|
33
|
+
prefix: string;
|
|
34
|
+
/** Version of the cached asset (if known) */
|
|
35
|
+
version?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface FreshnessConfig {
|
|
38
|
+
/** Threshold in ms for stale warning. Default: 86_400_000 (24h) */
|
|
39
|
+
staleThresholdMs: number;
|
|
40
|
+
/** Threshold in ms for age warning. Default: 43_200_000 (12h) */
|
|
41
|
+
warningThresholdMs: number;
|
|
42
|
+
}
|
|
43
|
+
export declare class FreshnessIndicator {
|
|
44
|
+
private readonly config;
|
|
45
|
+
constructor(config?: Partial<FreshnessConfig>);
|
|
46
|
+
/**
|
|
47
|
+
* Format a duration in ms as human-readable age string.
|
|
48
|
+
* Examples: "30m ago", "4h ago", "2d ago"
|
|
49
|
+
*/
|
|
50
|
+
formatAge(ageMs: number): string;
|
|
51
|
+
/**
|
|
52
|
+
* Evaluate freshness of a cache entry.
|
|
53
|
+
*/
|
|
54
|
+
evaluate(cacheAgeMs: number, _ttlMs?: number): FreshnessInfo;
|
|
55
|
+
/**
|
|
56
|
+
* Format prefix string based on degradation level and optional age.
|
|
57
|
+
*/
|
|
58
|
+
formatPrefix(level: DegradationLevel, ageMs?: number): string;
|
|
59
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
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 - Freshness Indicator
|
|
15
|
+
*
|
|
16
|
+
* Enriches cache responses with freshness metadata for user-facing display.
|
|
17
|
+
* Formats cache age as human-readable strings and generates appropriate
|
|
18
|
+
* warning prefixes based on degradation level.
|
|
19
|
+
*
|
|
20
|
+
* Story 42.9
|
|
21
|
+
*/
|
|
22
|
+
import { DegradationLevel } from './degradation-manager.js';
|
|
23
|
+
// ── Defaults ──────────────────────────────────────────────────────────────
|
|
24
|
+
const DEFAULT_CONFIG = {
|
|
25
|
+
staleThresholdMs: 86_400_000, // 24 hours
|
|
26
|
+
warningThresholdMs: 43_200_000, // 12 hours
|
|
27
|
+
};
|
|
28
|
+
// ── FreshnessIndicator ──────────────────────────────────────────────────
|
|
29
|
+
export class FreshnessIndicator {
|
|
30
|
+
config;
|
|
31
|
+
constructor(config) {
|
|
32
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Format a duration in ms as human-readable age string.
|
|
36
|
+
* Examples: "30m ago", "4h ago", "2d ago"
|
|
37
|
+
*/
|
|
38
|
+
formatAge(ageMs) {
|
|
39
|
+
if (ageMs < 0)
|
|
40
|
+
return 'just now';
|
|
41
|
+
const seconds = Math.floor(ageMs / 1_000);
|
|
42
|
+
const minutes = Math.floor(ageMs / 60_000);
|
|
43
|
+
const hours = Math.floor(ageMs / 3_600_000);
|
|
44
|
+
const days = Math.floor(ageMs / 86_400_000);
|
|
45
|
+
if (days >= 1)
|
|
46
|
+
return `${days}d ago`;
|
|
47
|
+
if (hours >= 1)
|
|
48
|
+
return `${hours}h ago`;
|
|
49
|
+
if (minutes >= 1)
|
|
50
|
+
return `${minutes}m ago`;
|
|
51
|
+
if (seconds >= 1)
|
|
52
|
+
return `${seconds}s ago`;
|
|
53
|
+
return 'just now';
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Evaluate freshness of a cache entry.
|
|
57
|
+
*/
|
|
58
|
+
evaluate(cacheAgeMs, _ttlMs) {
|
|
59
|
+
const stale = cacheAgeMs > this.config.staleThresholdMs;
|
|
60
|
+
const aged = cacheAgeMs > this.config.warningThresholdMs;
|
|
61
|
+
let warning = null;
|
|
62
|
+
let prefix = '[CACHED]';
|
|
63
|
+
if (stale) {
|
|
64
|
+
const ageStr = this.formatAge(cacheAgeMs);
|
|
65
|
+
prefix = `[STALE CACHE - ${ageStr}]`;
|
|
66
|
+
warning = `WARNING: STALE - cached ${ageStr}`;
|
|
67
|
+
}
|
|
68
|
+
else if (aged) {
|
|
69
|
+
const ageStr = this.formatAge(cacheAgeMs);
|
|
70
|
+
prefix = `[CACHED]`;
|
|
71
|
+
warning = `WARNING - cached ${ageStr}`;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
cached: true,
|
|
75
|
+
cacheAgeMs,
|
|
76
|
+
stale,
|
|
77
|
+
warning,
|
|
78
|
+
prefix,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Format prefix string based on degradation level and optional age.
|
|
83
|
+
*/
|
|
84
|
+
formatPrefix(level, ageMs) {
|
|
85
|
+
switch (level) {
|
|
86
|
+
case DegradationLevel.ONLINE:
|
|
87
|
+
return '';
|
|
88
|
+
case DegradationLevel.HIGH_LATENCY:
|
|
89
|
+
return ageMs != null ? `[CACHED] cached ${this.formatAge(ageMs)}` : '[CACHED]';
|
|
90
|
+
case DegradationLevel.SERVER_DOWN:
|
|
91
|
+
return ageMs != null ? `[CACHED] cached ${this.formatAge(ageMs)}` : '[CACHED]';
|
|
92
|
+
case DegradationLevel.STALE_CACHE:
|
|
93
|
+
return ageMs != null ? `[STALE CACHE - ${this.formatAge(ageMs)}]` : '[STALE CACHE]';
|
|
94
|
+
case DegradationLevel.NO_CACHE:
|
|
95
|
+
return '[ERROR]';
|
|
96
|
+
default:
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license FSL-1.1
|
|
3
|
+
* Copyright (c) 2026 OrNexus AI
|
|
4
|
+
*/
|
|
5
|
+
export { ClientCircuitBreaker, type CircuitState, type CircuitBreakerConfig, type CircuitBreakerState, type FailureRecord } from './circuit-breaker.js';
|
|
6
|
+
export { DegradationManager, DegradationLevel, type DegradationContext, type DegradationDecision, type DegradationManagerConfig } from './degradation-manager.js';
|
|
7
|
+
export { FreshnessIndicator, type FreshnessInfo, type FreshnessConfig } from './freshness-indicator.js';
|
|
8
|
+
export { RecoveryDetector, type RecoveryActions, type RecoveryResult } from './recovery-detector.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license FSL-1.1
|
|
3
|
+
* Copyright (c) 2026 OrNexus AI
|
|
4
|
+
*/
|
|
5
|
+
export { ClientCircuitBreaker } from './circuit-breaker.js';
|
|
6
|
+
export { DegradationManager, DegradationLevel } from './degradation-manager.js';
|
|
7
|
+
export { FreshnessIndicator } from './freshness-indicator.js';
|
|
8
|
+
export { RecoveryDetector } from './recovery-detector.js';
|
|
@@ -0,0 +1,59 @@
|
|
|
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 - Recovery Detector
|
|
15
|
+
*
|
|
16
|
+
* Coordinates recovery actions when server comes back online after
|
|
17
|
+
* a circuit breaker trip. Each step is independent - failures in one
|
|
18
|
+
* don't block the others.
|
|
19
|
+
*
|
|
20
|
+
* Recovery sequence:
|
|
21
|
+
* 1. Close circuit breaker (persist to file)
|
|
22
|
+
* 2. Re-validate JWT token
|
|
23
|
+
* 3. Sync cache with fresh server data (background)
|
|
24
|
+
* 4. Flush offline telemetry queue (background)
|
|
25
|
+
* 5. Notify user with [ONLINE] prefix
|
|
26
|
+
*
|
|
27
|
+
* Story 42.9
|
|
28
|
+
*/
|
|
29
|
+
import type { ClientCircuitBreaker } from './circuit-breaker.js';
|
|
30
|
+
import type { CacheProvider } from '../types/index.js';
|
|
31
|
+
import type { OfflineTelemetryQueue } from '../telemetry/offline-queue.js';
|
|
32
|
+
export interface RecoveryActions {
|
|
33
|
+
circuitBreaker: ClientCircuitBreaker;
|
|
34
|
+
licenseClient: {
|
|
35
|
+
getToken(): Promise<string | null>;
|
|
36
|
+
};
|
|
37
|
+
cache: CacheProvider;
|
|
38
|
+
telemetryQueue: OfflineTelemetryQueue;
|
|
39
|
+
serverUrl: string;
|
|
40
|
+
}
|
|
41
|
+
export interface RecoveryResult {
|
|
42
|
+
circuitClosed: boolean;
|
|
43
|
+
jwtRefreshed: boolean;
|
|
44
|
+
cacheSynced: boolean;
|
|
45
|
+
telemetryFlushed: {
|
|
46
|
+
sent: number;
|
|
47
|
+
failed: number;
|
|
48
|
+
};
|
|
49
|
+
notification: string;
|
|
50
|
+
}
|
|
51
|
+
export declare class RecoveryDetector {
|
|
52
|
+
private readonly actions;
|
|
53
|
+
constructor(actions: RecoveryActions);
|
|
54
|
+
/**
|
|
55
|
+
* Execute full recovery sequence.
|
|
56
|
+
* Each step is independent - failures are captured but don't block others.
|
|
57
|
+
*/
|
|
58
|
+
onRecovery(): Promise<RecoveryResult>;
|
|
59
|
+
}
|