-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_corruption_improvement.diff
More file actions
297 lines (295 loc) · 10.7 KB
/
config_corruption_improvement.diff
File metadata and controls
297 lines (295 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
diff --git a/src/commands/settings/show/index.ts b/src/commands/settings/show/index.ts
index 2a5115d..fe2804c 100644
--- a/src/commands/settings/show/index.ts
+++ b/src/commands/settings/show/index.ts
@@ -15,12 +15,17 @@ export class SettingsShowHandler {
errors: []
};
} catch (error: any) {
+ // Determine if this is a config parsing error
+ const errorType = error instanceof SyntaxError || error.message.includes('JSON') || error.message.includes('parse')
+ ? 'CONFIG_PARSE_ERROR'
+ : 'CONFIG_LOAD_ERROR';
+
return {
status: 'error',
data: null,
message: error.message,
errors: [{
- type: 'CONFIG_LOAD_ERROR',
+ type: errorType,
message: error.message,
timestamp: new Date().toISOString()
}]
diff --git a/src/commands/system/doctor.ts b/src/commands/system/doctor.ts
index 09c9e84..1b3402d 100644
--- a/src/commands/system/doctor.ts
+++ b/src/commands/system/doctor.ts
@@ -12,6 +12,7 @@ interface DoctorCheck {
name: string;
status: 'ok' | 'warning' | 'error';
message?: string;
+ fixHint?: string;
}
export class SystemDoctorHandler implements CommandHandler {
@@ -75,20 +76,31 @@ async function checkConfig(): Promise<DoctorCheck> {
return {
name: 'config',
status: 'error',
- message: 'Missing API base URL in configuration'
+ message: 'Missing API base URL in configuration',
+ fixHint: 'Run "nexus settings set --key apiBaseUrl --value <your-api-url>" to set the API base URL'
};
}
-
+
return {
name: 'config',
status: 'ok',
- message: 'Configuration loaded successfully'
+ message: 'Configuration loaded successfully',
+ fixHint: 'System configuration is valid'
};
} catch (error: any) {
+ // Check if this is a config parsing error
+ const isParseError = error instanceof SyntaxError || error.message.toLowerCase().includes('json') || error.message.toLowerCase().includes('parse');
+ const message = isParseError
+ ? `Configuration parsing error: ${error.message}`
+ : `Configuration error: ${error.message}`;
+
return {
name: 'config',
status: 'error',
- message: `Configuration error: ${error.message}`
+ message,
+ fixHint: isParseError
+ ? 'Configuration file syntax error. Check ~/.nexus/config.json for proper JSON formatting, or run "rm ~/.nexus/config.json" to reset configuration'
+ : 'Check your configuration file at ~/.nexus/config.json for syntax errors'
};
}
}
@@ -111,20 +123,23 @@ async function checkAPIConnectivity(): Promise<DoctorCheck> {
return {
name: 'api',
status: 'ok',
- message: 'API connectivity established'
+ message: 'API connectivity established',
+ fixHint: 'API is accessible and responding correctly'
};
} else {
return {
name: 'api',
status: 'warning',
- message: `API returned status ${response.status}`
+ message: `API returned status ${response.status}`,
+ fixHint: `Check API server status, received HTTP ${response.status} response`
};
}
} catch (error: any) {
return {
name: 'api',
status: 'warning',
- message: `API connectivity failed: ${error.message}`
+ message: `API connectivity failed: ${error.message}`,
+ fixHint: 'Verify API server is running and accessible at the configured URL'
};
}
}
@@ -137,10 +152,11 @@ async function checkAuthToken(): Promise<DoctorCheck> {
return {
name: 'auth',
status: 'warning',
- message: 'No authentication token set'
+ message: 'No authentication token set',
+ fixHint: 'Run "nexus auth login" to authenticate with the API server'
};
}
-
+
// Check if token is valid by attempting to decode it (basic check)
// JWT tokens have 3 parts separated by dots
const parts = config.authToken.split('.');
@@ -148,10 +164,11 @@ async function checkAuthToken(): Promise<DoctorCheck> {
return {
name: 'auth',
status: 'error',
- message: 'Authentication token format is invalid'
+ message: 'Authentication token format is invalid',
+ fixHint: 'Re-authenticate using "nexus auth login" to get a valid token'
};
}
-
+
// Try to use the token with an API request
const client = new APIClient();
// Use one of the existing methods that would require authentication
@@ -161,20 +178,23 @@ async function checkAuthToken(): Promise<DoctorCheck> {
return {
name: 'auth',
status: 'ok',
- message: 'Authentication token is valid'
+ message: 'Authentication token is valid',
+ fixHint: 'Authentication token is valid and working correctly'
};
} else {
return {
name: 'auth',
status: 'error',
- message: 'Authentication token appears to be invalid or expired'
+ message: 'Authentication token appears to be invalid or expired',
+ fixHint: 'Re-authenticate using "nexus auth login" to get a new token'
};
}
} catch (error: any) {
return {
name: 'auth',
status: 'error',
- message: `Authentication check failed: ${error.message}`
+ message: `Authentication check failed: ${error.message}`,
+ fixHint: 'Check authentication setup, run "nexus auth login" to authenticate'
};
}
}
@@ -191,10 +211,11 @@ async function checkConfigFilePermissions(): Promise<DoctorCheck> {
return {
name: 'permissions',
status: 'error',
- message: 'Configuration directory does not exist or is not writable'
+ message: 'Configuration directory does not exist or is not writable',
+ fixHint: 'Create the directory ~/.nexus and ensure it is writable: "mkdir -p ~/.nexus && chmod 755 ~/.nexus"'
};
}
-
+
// Check if config file exists and is readable/writable
try {
await fs.access(configPath, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK);
@@ -206,21 +227,24 @@ async function checkConfigFilePermissions(): Promise<DoctorCheck> {
return {
name: 'permissions',
status: 'error',
- message: 'Configuration file cannot be created in directory'
+ message: 'Configuration file cannot be created in directory',
+ fixHint: 'Check permissions on the ~/.nexus directory'
};
}
}
-
+
return {
name: 'permissions',
status: 'ok',
- message: 'Configuration file permissions are appropriate'
+ message: 'Configuration file permissions are appropriate',
+ fixHint: 'Configuration file permissions are correctly set'
};
} catch (error: any) {
return {
name: 'permissions',
status: 'error',
- message: `Permission check failed: ${error.message}`
+ message: `Permission check failed: ${error.message}`,
+ fixHint: 'Check file permissions for the ~/.nexus directory and configuration file'
};
}
}
@@ -243,20 +267,23 @@ async function checkParity(): Promise<DoctorCheck> {
return {
name: 'parity',
status: 'ok',
- message: 'File system parity check passed'
+ message: 'File system parity check passed',
+ fixHint: 'File system read/write operations are working correctly'
};
} else {
return {
name: 'parity',
status: 'error',
- message: 'File system parity check failed - read/write inconsistency'
+ message: 'File system parity check failed - read/write inconsistency',
+ fixHint: 'Check disk space and file system for errors'
};
}
} catch (error: any) {
return {
name: 'parity',
status: 'error',
- message: `Parity check failed: ${error.message}`
+ message: `Parity check failed: ${error.message}`,
+ fixHint: 'Check disk space and file system for errors'
};
}
}
\ No newline at end of file
diff --git a/src/state/config-store.ts b/src/state/config-store.ts
index 55b71f7..60e2622 100644
--- a/src/state/config-store.ts
+++ b/src/state/config-store.ts
@@ -79,6 +79,20 @@ export async function loadConfig(): Promise<Config> {
const defaultConfig = getDefaultConfig();
await saveConfig(defaultConfig);
return defaultConfig;
+ } else if (error instanceof SyntaxError) {
+ // JSON parsing error - config file is corrupted
+ console.error(`Config file corrupted: ${error.message}`);
+ // Return default config but log the issue
+ const defaultConfig = getDefaultConfig();
+ // Try to backup the corrupted config
+ try {
+ const backupPath = configPath + '.backup';
+ await fs.copyFile(configPath, backupPath);
+ console.error(`Corrupted config backed up to: ${backupPath}`);
+ } catch (backupError) {
+ console.error(`Failed to backup corrupted config: ${backupError.message}`);
+ }
+ return defaultConfig;
} else {
// Other error occurred, log and return defaults
console.error(`Error reading config file: ${error.message}`);
diff --git a/src/utils/formatters.ts b/src/utils/formatters.ts
index 7dce5f1..b7bc5b0 100644
--- a/src/utils/formatters.ts
+++ b/src/utils/formatters.ts
@@ -46,7 +46,28 @@ function loadConfigSync(): any {
if (fs.existsSync(configPath)) {
const configContent = fs.readFileSync(configPath, 'utf-8');
- return JSON.parse(configContent);
+ try {
+ return JSON.parse(configContent);
+ } catch (parseError) {
+ // Log the parsing error for debugging
+ console.error(`Config file corrupted: ${parseError.message}`);
+ console.error(`Config path: ${configPath}`);
+
+ // Try to backup the corrupted config
+ try {
+ const backupPath = configPath + '.backup';
+ fs.copyFileSync(configPath, backupPath);
+ console.error(`Corrupted config backed up to: ${backupPath}`);
+ } catch (backupError) {
+ console.error(`Failed to backup corrupted config: ${backupError.message}`);
+ }
+
+ // Return default config
+ return {
+ outputMode: 'pretty',
+ showBanner: true
+ };
+ }
} else {
// Return default config if file doesn't exist
return {
diff --git a/src/utils/hints.ts b/src/utils/hints.ts
index 865a243..dee2b1f 100644
--- a/src/utils/hints.ts
+++ b/src/utils/hints.ts
@@ -34,6 +34,10 @@ export const hints: Hint[] = [
{
errorType: 'MISSING_AI_SESSION_CONTEXT',
message: "Tip: Use 'nexus ai session create' to start an AI session."
+ },
+ {
+ errorType: 'CONFIG_PARSE_ERROR',
+ message: "Configuration file is corrupted. Run 'rm ~/.nexus/config.json' to reset configuration, or manually fix the JSON syntax in the config file."
}
];