vtcode-commons 0.181.2

Shared traits for paths, telemetry, and error reporting reused across VT Code component extractions
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use super::*;

// --- classify_error_message tests ---

#[test]
fn policy_violation_takes_priority_over_permission() {
    assert_eq!(classify_error_message("tool permission denied by policy"), ErrorCategory::PolicyViolation);
}

#[test]
fn rate_limit_classified_correctly() {
    assert_eq!(classify_error_message("provider returned 429 Too Many Requests"), ErrorCategory::RateLimit);
    assert_eq!(classify_error_message("rate limit exceeded"), ErrorCategory::RateLimit);
}

#[test]
fn service_unavailable_is_classified() {
    assert_eq!(classify_error_message("503 service unavailable"), ErrorCategory::ServiceUnavailable);
}

#[test]
fn follow_up_stream_failures_are_retryable() {
    // Post-tool follow-up streaming drops must not fall through to
    // generic `ExecutionError`: both shapes are retryable.
    for msg in [
        "follow-up failed: stream disconnected mid-response",
        "sse stream terminated before completion",
        "incomplete stream while reading follow-up",
        "connection closed before response completed",
        "stream reset by peer during follow-up",
        "h2 stream error received: internal error",
    ] {
        let category = classify_error_message(msg);
        assert!(category.is_retryable(), "{msg} -> {category:?} should be retryable");
    }
    // Asymmetric boundary: unrelated `stream` prose without a failure
    // marker stays non-retryable, and `downstream error` must not match
    // the h2 `stream error received` marker.
    assert_eq!(classify_error_message("upstream stream file ready"), ErrorCategory::ExecutionError);
    assert_eq!(classify_error_message("downstream error: 400 bad request"), ErrorCategory::ExecutionError);
    assert_eq!(classify_error_message("something went wrong"), ErrorCategory::ExecutionError);
}

#[test]
fn authentication_errors() {
    assert_eq!(classify_error_message("invalid api key provided"), ErrorCategory::Authentication);
    assert_eq!(classify_error_message("401 unauthorized"), ErrorCategory::Authentication);
}

#[test]
fn billing_errors_are_resource_exhausted() {
    assert_eq!(classify_error_message("you have reached your weekly usage limit"), ErrorCategory::ResourceExhausted);
    assert_eq!(classify_error_message("quota exceeded for this model"), ErrorCategory::ResourceExhausted);
}

#[test]
fn timeout_errors() {
    assert_eq!(classify_error_message("connection timeout"), ErrorCategory::Timeout);
    assert_eq!(classify_error_message("request timed out after 30s"), ErrorCategory::Timeout);
}

#[test]
fn network_errors() {
    assert_eq!(classify_error_message("connection reset by peer"), ErrorCategory::Network);
    assert_eq!(classify_error_message("dns name resolution failed"), ErrorCategory::Network);
}

#[test]
fn tool_not_found() {
    assert_eq!(classify_error_message("unknown tool: ask_questions"), ErrorCategory::ToolNotFound);
}

#[test]
fn resource_not_found() {
    assert_eq!(classify_error_message("no such file or directory: /tmp/missing"), ErrorCategory::ResourceNotFound);
    assert_eq!(
        classify_error_message("Path 'crates/codegen/vtcode-core/src/agent' does not exist"),
        ErrorCategory::ResourceNotFound
    );
}

#[test]
fn patch_format_errors_are_invalid_parameters() {
    // Patch format errors are LLM argument mistakes, not execution
    // failures. They must classify as InvalidParameters (is_llm_mistake
    // == true, no circuit breaker trip) so the model gets parameter-
    // focused recovery suggestions instead of generic "check tool docs".
    assert_eq!(
        classify_error_message("invalid patch format: missing '*** Begin Patch' marker"),
        ErrorCategory::InvalidParameters
    );
    assert_eq!(
        classify_error_message("invalid patch format: input looks like a standard unified diff (---/+++ format)"),
        ErrorCategory::InvalidParameters
    );
    assert_eq!(
        classify_error_message("invalid patch hunk on line 5: unexpected end of input"),
        ErrorCategory::InvalidParameters
    );
    assert_eq!(classify_error_message("cannot parse empty patch input"), ErrorCategory::InvalidParameters);
    assert_eq!(classify_error_message("patch does not contain any operations"), ErrorCategory::InvalidParameters);
    assert_eq!(
        classify_error_message("semantic patch anchor 'fn main' for 'src/main.rs' could not be resolved"),
        ErrorCategory::InvalidParameters
    );
}

#[test]
fn permission_denied() {
    assert_eq!(classify_error_message("permission denied: /etc/shadow"), ErrorCategory::PermissionDenied);
}

#[test]
fn cancelled_operations() {
    assert_eq!(classify_error_message("operation cancelled by user"), ErrorCategory::Cancelled);
}

#[test]
fn planning_policy_violation() {
    assert_eq!(classify_error_message("not allowed in planning workflow"), ErrorCategory::PolicyViolation);
}

#[test]
fn sandbox_failure() {
    assert_eq!(classify_error_message("sandbox denied this operation"), ErrorCategory::SandboxFailure);
}

#[test]
fn unknown_error_is_execution_error() {
    assert_eq!(classify_error_message("something went wrong"), ErrorCategory::ExecutionError);
}

#[test]
fn invalid_parameters() {
    assert_eq!(classify_error_message("invalid argument: missing path field"), ErrorCategory::InvalidParameters);
    assert_eq!(
        classify_error_message("Failed to parse arguments for read_file handler: invalid type: boolean `false`"),
        ErrorCategory::InvalidParameters
    );
    assert_eq!(
        classify_error_message("at least one item is required for 'create'"),
        ErrorCategory::InvalidParameters
    );
    assert_eq!(
        classify_error_message("structural pattern preflight failed: pattern is not parseable as Rust syntax"),
        ErrorCategory::InvalidParameters
    );
}

// --- classify_anyhow_error: type-first recovery, context-aware fallback ---

#[test]
fn classify_anyhow_prefers_typed_llm_error_under_context() {
    // A typed `LLMError` must win over an uninformative outer context:
    // the outer message alone would classify as `ExecutionError`, but the
    // error's type is the source of truth for its recovery policy.
    let rate = anyhow::Error::new(crate::llm::LLMError::RateLimit { metadata: None }).context("compaction failed");
    assert_eq!(classify_anyhow_error(&rate), ErrorCategory::RateLimit);

    let auth =
        anyhow::Error::new(crate::llm::LLMError::Authentication { message: "bad token".to_string(), metadata: None })
            .context("request failed");
    assert_eq!(classify_anyhow_error(&auth), ErrorCategory::Authentication);
}

#[test]
fn classify_anyhow_reads_deeper_cause_chain() {
    // When the outer context is uninformative and no typed error survives,
    // the underlying cause must still be classified (`{err:#}`-style).
    let inner = std::io::Error::new(std::io::ErrorKind::ConnectionReset, "connection reset by peer");
    let err = anyhow::Error::new(inner).context("failed to fetch resource");
    assert_eq!(classify_anyhow_error(&err), ErrorCategory::Network);
}

#[test]
fn classify_anyhow_keeps_outermost_context_priority() {
    // A deliberate, specific `.context(...)` still wins over a generic root.
    let inner = std::io::Error::new(std::io::ErrorKind::NotFound, "no such file");
    let err = anyhow::Error::new(inner).context("tool permission denied by policy");
    assert_eq!(classify_anyhow_error(&err), ErrorCategory::PolicyViolation);
}

#[test]
fn classify_anyhow_unknown_stays_execution_error() {
    let err = anyhow::anyhow!("something went wrong");
    assert_eq!(classify_anyhow_error(&err), ErrorCategory::ExecutionError);
}

// --- Retryability tests ---

#[test]
fn retryable_categories() {
    assert!(ErrorCategory::Network.is_retryable());
    assert!(ErrorCategory::Timeout.is_retryable());
    assert!(ErrorCategory::RateLimit.is_retryable());
    assert!(ErrorCategory::ServiceUnavailable.is_retryable());
    assert!(ErrorCategory::CircuitOpen.is_retryable());
}

#[test]
fn non_retryable_categories() {
    assert!(!ErrorCategory::Authentication.is_retryable());
    assert!(!ErrorCategory::InvalidParameters.is_retryable());
    assert!(!ErrorCategory::PolicyViolation.is_retryable());
    assert!(!ErrorCategory::ResourceExhausted.is_retryable());
    assert!(!ErrorCategory::Cancelled.is_retryable());
}

#[test]
fn permanent_error_detection() {
    assert!(ErrorCategory::Authentication.is_permanent());
    assert!(ErrorCategory::PolicyViolation.is_permanent());
    assert!(!ErrorCategory::Network.is_permanent());
    assert!(!ErrorCategory::Timeout.is_permanent());
}

#[test]
fn llm_mistake_detection() {
    assert!(ErrorCategory::InvalidParameters.is_llm_mistake());
    assert!(!ErrorCategory::Network.is_llm_mistake());
    assert!(!ErrorCategory::Timeout.is_llm_mistake());
}

// --- LLM error conversion ---

#[test]
fn llm_error_authentication_converts() {
    let err = crate::llm::LLMError::Authentication { message: "bad key".to_string(), metadata: None };
    assert_eq!(ErrorCategory::from(&err), ErrorCategory::Authentication);
}

#[test]
fn llm_error_rate_limit_converts() {
    let err = crate::llm::LLMError::RateLimit { metadata: None };
    assert_eq!(ErrorCategory::from(&err), ErrorCategory::RateLimit);
}

#[test]
fn llm_error_quota_exhaustion_converts() {
    let err = crate::llm::LLMError::RateLimit {
        metadata: Some(crate::llm::LLMErrorMetadata::new(
            "openai",
            Some(429),
            Some("insufficient_quota".to_string()),
            None,
            None,
            None,
            Some("quota exceeded".to_string()),
        )),
    };

    assert_eq!(ErrorCategory::from(&err), ErrorCategory::ResourceExhausted);
}

#[test]
fn llm_error_network_converts() {
    let err = crate::llm::LLMError::Network {
        message: "connection refused".to_string(),
        metadata: None,
    };
    assert_eq!(ErrorCategory::from(&err), ErrorCategory::Network);
}

#[test]
fn llm_error_provider_with_status_code() {
    use crate::llm::LLMErrorMetadata;
    let err = crate::llm::LLMError::Provider {
        message: "error".to_string(),
        metadata: Some(LLMErrorMetadata::new("openai", Some(503), None, None, None, None, None)),
    };
    assert_eq!(ErrorCategory::from(&err), ErrorCategory::ServiceUnavailable);
}

#[test]
fn minimax_invalid_response_is_service_unavailable() {
    assert_eq!(
        classify_error_message("Invalid response from MiniMax: missing choices"),
        ErrorCategory::ServiceUnavailable
    );
    assert_eq!(
        classify_error_message("Invalid response format: missing message"),
        ErrorCategory::ServiceUnavailable
    );
}

// --- is_retryable_llm_error_message ---

#[test]
fn retryable_llm_messages() {
    assert!(is_retryable_llm_error_message("429 too many requests"));
    assert!(is_retryable_llm_error_message("500 internal server error"));
    assert!(is_retryable_llm_error_message("connection timeout"));
    assert!(is_retryable_llm_error_message("network error"));
}

#[test]
fn non_retryable_llm_messages() {
    assert!(!is_retryable_llm_error_message("invalid api key"));
    assert!(!is_retryable_llm_error_message("weekly usage limit reached"));
    assert!(!is_retryable_llm_error_message("permission denied"));
}

#[test]
fn context_capacity_markers_are_specific() {
    assert!(is_context_capacity_message("invalid request: maximum context length is 114688 tokens"));
    assert!(is_context_capacity_message("input token count exceeds the maximum number of tokens allowed"));
    assert!(!is_context_capacity_message("invalid request: context field is missing"));
}

#[test]
fn context_capacity_markers_cover_gateway_variants() {
    // Near-budget follow-up failures from merge-gateway / Anthropic /
    // OpenAI routes must enter the compacting recovery path instead of
    // falling through to generic `ExecutionError`.
    for msg in [
        "input tokens exceed context limit (956758 > 1000000)",
        "prompt tokens exceed the model context window",
        "context limit exceeded: too many input tokens",
        "request exceeds context size for anthropic/claude-sonnet-5",
        "token limit exceeded for prompt",
        "prompt too large for context window",
    ] {
        assert!(is_context_capacity_message(msg), "{msg} should be a capacity signal");
    }
    // Negative guard: ordinary prose must stay out of recovery (the
    // field-missing case is covered in `context_capacity_markers_are_specific`).
    assert!(!is_context_capacity_message("upstream stream file ready"));
    // "context limited" contains "context limit" as a substring; it must
    // not enter capacity recovery.
    assert!(!is_context_capacity_message("context limited to 5 tools"));
}

// --- Recovery suggestions ---

#[test]
fn recovery_suggestions_non_empty() {
    for cat in [
        ErrorCategory::Network,
        ErrorCategory::Timeout,
        ErrorCategory::RateLimit,
        ErrorCategory::Authentication,
        ErrorCategory::InvalidParameters,
        ErrorCategory::ToolNotFound,
        ErrorCategory::ResourceNotFound,
        ErrorCategory::PermissionDenied,
        ErrorCategory::PolicyViolation,
        ErrorCategory::ExecutionError,
    ] {
        assert!(!cat.recovery_suggestions().is_empty(), "Missing recovery suggestions for {cat:?}");
    }
}

// --- User label ---

#[test]
fn user_labels_are_non_empty() {
    assert!(!ErrorCategory::Network.user_label().is_empty());
    assert!(!ErrorCategory::ExecutionError.user_label().is_empty());
}

// --- auth_recovery_guidance ---

#[test]
fn auth_recovery_guidance_no_credential_mentions_secret_add() {
    let guidance = ErrorCategory::Authentication.auth_recovery_guidance("StepFun", "stepfun", false, false);
    assert_eq!(guidance.len(), 1);
    assert_eq!(
        guidance[0],
        "Authentication failed for StepFun. Run /secret add stepfun to store your API key in secure storage (OS keyring or encrypted file)."
    );
}

#[test]
fn auth_recovery_guidance_credential_stored_mentions_overwrite() {
    let guidance = ErrorCategory::Authentication.auth_recovery_guidance("StepFun", "stepfun", false, true);
    assert_eq!(guidance.len(), 1);
    assert_eq!(
        guidance[0],
        "Authentication failed for StepFun. The stored API key was rejected — run /secret add stepfun to replace it with a valid key."
    );
}

#[test]
fn auth_recovery_guidance_managed_auth_provider_mentions_login() {
    let guidance = ErrorCategory::Authentication.auth_recovery_guidance("GitHub Copilot", "copilot", true, false);
    assert_eq!(guidance.len(), 1);
    assert_eq!(guidance[0], "Authentication failed for GitHub Copilot. Run /login copilot to re-authenticate.");
}

#[test]
fn auth_recovery_guidance_non_auth_category_returns_empty() {
    assert!(
        ErrorCategory::Network
            .auth_recovery_guidance("OpenAI", "openai", false, false)
            .is_empty()
    );
    assert!(
        ErrorCategory::Timeout
            .auth_recovery_guidance("OpenAI", "openai", false, false)
            .is_empty()
    );
}

// --- Display ---

#[test]
fn display_matches_user_label() {
    assert_eq!(format!("{}", ErrorCategory::RateLimit), ErrorCategory::RateLimit.user_label());
}