kubesift 2.0.0__tar.gz → 2.0.1__tar.gz
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.
- {kubesift-2.0.0 → kubesift-2.0.1}/PKG-INFO +1 -1
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/Chart.yaml +2 -2
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/deployment.yaml +4 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/values.yaml +2 -1
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/configuration.md +10 -1
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/__init__.py +1 -1
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/llm.py +104 -6
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/cli.py +46 -13
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/config.py +9 -0
- kubesift-2.0.1/tests/test_llm.py +192 -0
- kubesift-2.0.0/tests/test_llm.py +0 -103
- {kubesift-2.0.0 → kubesift-2.0.1}/.dockerignore +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.gitattributes +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.github/workflows/ci.yml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.github/workflows/pages.yml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.github/workflows/release.yml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.gitignore +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/.pre-commit-hooks.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/CONTRIBUTING.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/Dockerfile +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/LICENSE +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/README.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/SECURITY.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/action.yml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/NOTES.txt +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/_helpers.tpl +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/rbac.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/secret.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/deploy/helm/kubesift/templates/service.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/architecture.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/checks.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/mcp.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/preflight.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/production.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/releasing.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/docs/slack.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/pyproject.toml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/scripts/e2e.sh +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/scripts/e2e_check.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/scripts/gen_check_docs.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/__init__.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/changes.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/correlate.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/describe.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/analysis/engine.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/__init__.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/base.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/config_refs.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/events.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/hygiene.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/lifecycle.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/network.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/nodes.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/operations.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/pods.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/storage.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/checks/workloads.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/cluster/__init__.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/cluster/client.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/cluster/snapshot.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/cost.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/01-crashloop-missing-env.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/02-imagepull-wrong-tag.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/03-oomkilled-low-memory.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/04-service-selector-mismatch.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/05-missing-probe-crashloop.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/06-pending-insufficient-resources.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/07-missing-secret.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/08-pvc-missing-storageclass.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/09-netpol-blocks-dns.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/10a-rollout-good.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/10b-rollout-bad.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/__init__.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drills.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/knowledge.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/leader.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/lint.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/mcp_server.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/models.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/notify.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/postmortem.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/preflight.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/redact.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/remediate.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/report.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/runbooks.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/server/__init__.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/server/app.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/server/static/index.html +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/slack.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/store.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/timetravel.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/upgrade.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/util.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/01-crashloop-missing-env.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/02-imagepull-wrong-tag.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/03-oomkilled-low-memory.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/04-service-selector-mismatch.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/05-missing-probe-crashloop.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/06-pending-insufficient-resources.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/07-missing-secret.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/08-pvc-missing-storageclass.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/09-netpol-blocks-dns.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/10a-rollout-good.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/10b-rollout-bad.yaml +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/apply-scenario.sh +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/cleanup.sh +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/test-scenarios/expected.json +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/conftest.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/fixtures.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_analysis.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_checks.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_client.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_features.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_ha.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_hardening.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_interfaces.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_lint_redact.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_more_checks.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/tests/test_production.py +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/website/README.md +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/website/favicon.svg +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/website/index.html +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/website/main.js +0 -0
- {kubesift-2.0.0 → kubesift-2.0.1}/website/styles.css +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: kubesift
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Kubernetes troubleshooting that explains itself: evidence-based diagnosis, change correlation, cost and upgrade checks. CLI, kubectl plugin, MCP server and in-cluster service.
|
|
5
5
|
Project-URL: Homepage, https://github.com/OpeyemiAdeniji/KubeSift
|
|
6
6
|
Project-URL: Issues, https://github.com/OpeyemiAdeniji/KubeSift/issues
|
|
@@ -2,7 +2,7 @@ apiVersion: v2
|
|
|
2
2
|
name: kubesift
|
|
3
3
|
description: Evidence-based Kubernetes troubleshooting service (dashboard, API, Alertmanager receiver, background scans)
|
|
4
4
|
type: application
|
|
5
|
-
version: 2.0.
|
|
6
|
-
appVersion: "2.0.
|
|
5
|
+
version: 2.0.1
|
|
6
|
+
appVersion: "2.0.1"
|
|
7
7
|
home: https://github.com/OpeyemiAdeniji/KubeSift
|
|
8
8
|
keywords: [kubernetes, troubleshooting, sre, observability]
|
|
@@ -60,6 +60,10 @@ spec:
|
|
|
60
60
|
- name: KUBESIFT_LLM_BASE_URL
|
|
61
61
|
value: {{ . | quote }}
|
|
62
62
|
{{- end }}
|
|
63
|
+
{{- with .Values.llm.timeout }}
|
|
64
|
+
- name: KUBESIFT_LLM_TIMEOUT
|
|
65
|
+
value: {{ . | quote }}
|
|
66
|
+
{{- end }}
|
|
63
67
|
- name: KUBESIFT_SEND_LOGS_TO_LLM
|
|
64
68
|
value: {{ .Values.llm.sendLogs | quote }}
|
|
65
69
|
{{- with .Values.prometheus.url }}
|
|
@@ -23,7 +23,8 @@ existingSecret: ""
|
|
|
23
23
|
llm:
|
|
24
24
|
provider: none # anthropic | openai | openrouter | ollama | none
|
|
25
25
|
model: ""
|
|
26
|
-
baseUrl: "" # e.g. http://ollama.ollama:11434
|
|
26
|
+
baseUrl: "" # e.g. http://ollama.ollama:11434 for Ollama
|
|
27
|
+
timeout: 0 # seconds to wait for one answer; 0 = default (180, Ollama 900)
|
|
27
28
|
anthropicApiKey: ""
|
|
28
29
|
openaiApiKey: ""
|
|
29
30
|
openrouterApiKey: ""
|
|
@@ -35,9 +35,11 @@ KubeSift never needs an LLM: every finding comes from deterministic checks. An L
|
|
|
35
35
|
|---|---|---|
|
|
36
36
|
| `llm_provider` | `auto` | `auto` picks the first of `ANTHROPIC_API_KEY`, `OPENROUTER_API_KEY`, `OPENAI_API_KEY` that is set, else `none` |
|
|
37
37
|
| `llm_model` | per provider | Anthropic: `claude-opus-5-5` |
|
|
38
|
-
| `llm_base_url` | per provider | Any OpenAI-compatible endpoint (vLLM, LiteLLM, Azure gateway) |
|
|
38
|
+
| `llm_base_url` | per provider | Any OpenAI-compatible endpoint (vLLM, LiteLLM, Azure gateway), or your Ollama server |
|
|
39
39
|
| `llm_api_key` | from provider env var | |
|
|
40
40
|
| `llm_effort` | `medium` | Anthropic effort: `low`, `medium`, `high` |
|
|
41
|
+
| `llm_timeout` | `180` (`900` for Ollama) | Seconds to wait for one answer. A slow answer is not retried |
|
|
42
|
+
| `llm_context` | sized to the prompt | Ollama only: context window in tokens (8192–32768 by default) |
|
|
41
43
|
| `send_logs_to_llm` | `true` | `false` keeps log lines out of prompts |
|
|
42
44
|
|
|
43
45
|
The Anthropic provider sends requests with server-side refusal fallback enabled (`fallbacks: "default"`), so a
|
|
@@ -51,6 +53,13 @@ addresses in log lines are redacted before anything is stored or sent. To keep e
|
|
|
51
53
|
export KUBESIFT_LLM_PROVIDER=ollama KUBESIFT_LLM_MODEL=llama3.1
|
|
52
54
|
```
|
|
53
55
|
|
|
56
|
+
KubeSift talks to Ollama's native API: it turns off hidden reasoning ("thinking") so answers come faster, sizes the
|
|
57
|
+
context window to the prompt (Ollama's default window is small and silently drops the start of a long prompt), and
|
|
58
|
+
constrains the output to the expected JSON. A diagnosis prompt is a few thousand tokens. On a machine with a GPU a
|
|
59
|
+
7–8B model answers in seconds; on a laptop CPU it can take many minutes, so raise `llm_timeout`, pick a smaller
|
|
60
|
+
model, use one of Ollama's cloud models, or run with `--no-llm`. If the model does not answer in time KubeSift
|
|
61
|
+
shows the rule-based diagnosis instead.
|
|
62
|
+
|
|
54
63
|
## Usage data and cost
|
|
55
64
|
|
|
56
65
|
| Name | Default | Notes |
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Providers:
|
|
4
4
|
anthropic Claude through the official Anthropic SDK (pip install 'kubesift[anthropic]')
|
|
5
|
-
openai any OpenAI-compatible endpoint: OpenAI, OpenRouter,
|
|
5
|
+
openai any OpenAI-compatible endpoint: OpenAI, OpenRouter, vLLM, LiteLLM, Azure gateways
|
|
6
|
+
openrouter OpenRouter (OpenAI-compatible)
|
|
7
|
+
ollama local models through Ollama's native API
|
|
6
8
|
none rules only
|
|
7
9
|
"""
|
|
8
10
|
|
|
@@ -40,7 +42,8 @@ class AnthropicProvider(Provider):
|
|
|
40
42
|
except ImportError as exc:
|
|
41
43
|
raise LLMError("Install the Anthropic SDK: pip install 'kubesift[anthropic]'") from exc
|
|
42
44
|
self._anthropic = anthropic
|
|
43
|
-
|
|
45
|
+
self.timeout = settings.resolved_llm_timeout()
|
|
46
|
+
kwargs: dict[str, Any] = {"timeout": self.timeout}
|
|
44
47
|
if settings.llm_api_key:
|
|
45
48
|
kwargs["api_key"] = settings.llm_api_key
|
|
46
49
|
if settings.llm_base_url:
|
|
@@ -68,8 +71,12 @@ class AnthropicProvider(Provider):
|
|
|
68
71
|
response = self.client.messages.create(**request)
|
|
69
72
|
except a.APIStatusError as exc:
|
|
70
73
|
raise LLMError(f"Anthropic API error {exc.status_code}: {exc.message}") from exc
|
|
74
|
+
except a.APITimeoutError as exc:
|
|
75
|
+
raise LLMError(_timeout_message(self.model, self.timeout)) from exc
|
|
71
76
|
except a.APIConnectionError as exc:
|
|
72
77
|
raise LLMError(f"Cannot reach the Anthropic API: {exc}") from exc
|
|
78
|
+
except a.APITimeoutError as exc:
|
|
79
|
+
raise LLMError(_timeout_message(self.model, self.timeout)) from exc
|
|
73
80
|
except a.AuthenticationError as exc:
|
|
74
81
|
raise LLMError("Anthropic rejected the API key (set ANTHROPIC_API_KEY).") from exc
|
|
75
82
|
except a.RateLimitError as exc:
|
|
@@ -84,6 +91,15 @@ class AnthropicProvider(Provider):
|
|
|
84
91
|
return parse_json(text)
|
|
85
92
|
|
|
86
93
|
|
|
94
|
+
def _timeout_message(model: str, seconds: float) -> str:
|
|
95
|
+
return (f"{model} did not answer within {seconds:.0f}s. Local models on a CPU can take many minutes: "
|
|
96
|
+
"raise llm_timeout (KUBESIFT_LLM_TIMEOUT), use a smaller or hosted model, or run with --no-llm.")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _with_schema(system: str, schema: dict[str, Any]) -> str:
|
|
100
|
+
return system + "\n\nReply with one JSON object matching this schema:\n" + json.dumps(schema)
|
|
101
|
+
|
|
102
|
+
|
|
87
103
|
class OpenAICompatibleProvider(Provider):
|
|
88
104
|
name = "openai"
|
|
89
105
|
|
|
@@ -92,6 +108,7 @@ class OpenAICompatibleProvider(Provider):
|
|
|
92
108
|
self.model = settings.resolved_model()
|
|
93
109
|
self.api_key = settings.resolved_api_key()
|
|
94
110
|
self.provider = settings.resolved_provider()
|
|
111
|
+
self.timeout = settings.resolved_llm_timeout()
|
|
95
112
|
if self.provider != "ollama" and not self.api_key and "localhost" not in self.base_url:
|
|
96
113
|
raise LLMError(f"No API key for {self.provider}; set {self.provider.upper()}_API_KEY or KUBESIFT_LLM_API_KEY.")
|
|
97
114
|
|
|
@@ -104,16 +121,20 @@ class OpenAICompatibleProvider(Provider):
|
|
|
104
121
|
body: dict[str, Any] = {
|
|
105
122
|
"model": self.model,
|
|
106
123
|
"messages": [
|
|
107
|
-
{"role": "system", "content": system
|
|
108
|
-
+ json.dumps(schema)},
|
|
124
|
+
{"role": "system", "content": _with_schema(system, schema)},
|
|
109
125
|
{"role": "user", "content": user},
|
|
110
126
|
],
|
|
111
127
|
"response_format": {"type": "json_object"},
|
|
112
128
|
}
|
|
113
|
-
with httpx.Client(timeout=
|
|
129
|
+
with httpx.Client(timeout=httpx.Timeout(self.timeout, connect=15.0)) as http:
|
|
114
130
|
for attempt in range(3):
|
|
115
131
|
try:
|
|
116
132
|
r = http.post(f"{self.base_url}/chat/completions", headers=headers, json=body)
|
|
133
|
+
except httpx.TimeoutException as exc:
|
|
134
|
+
if isinstance(exc, httpx.ConnectTimeout) and attempt < 2:
|
|
135
|
+
continue
|
|
136
|
+
# a slow answer is not retried: the next attempt would redo the same slow work
|
|
137
|
+
raise LLMError(_timeout_message(self.model, self.timeout)) from exc
|
|
117
138
|
except httpx.HTTPError as exc:
|
|
118
139
|
if attempt == 2:
|
|
119
140
|
raise LLMError(f"Cannot reach {self.base_url}: {exc}") from exc
|
|
@@ -130,6 +151,81 @@ class OpenAICompatibleProvider(Provider):
|
|
|
130
151
|
raise LLMError("LLM request failed after retries")
|
|
131
152
|
|
|
132
153
|
|
|
154
|
+
class OllamaProvider(Provider):
|
|
155
|
+
"""Ollama's native API: turns off hidden reasoning, sizes the context window to the prompt (Ollama's
|
|
156
|
+
default window is small and silently drops the start of longer prompts) and constrains output to the schema."""
|
|
157
|
+
|
|
158
|
+
name = "ollama"
|
|
159
|
+
MAX_OUTPUT = 4096
|
|
160
|
+
|
|
161
|
+
def __init__(self, settings: Settings):
|
|
162
|
+
base = (settings.resolved_base_url() or "http://localhost:11434").rstrip("/")
|
|
163
|
+
self.base_url = base[:-3] if base.endswith("/v1") else base
|
|
164
|
+
self.model = settings.resolved_model()
|
|
165
|
+
self.timeout = settings.resolved_llm_timeout()
|
|
166
|
+
self.context = settings.llm_context
|
|
167
|
+
self.api_key = settings.resolved_api_key()
|
|
168
|
+
|
|
169
|
+
def status(self) -> tuple[bool, str]:
|
|
170
|
+
"""Is the server up and the model pulled? Used by `kubesift doctor`."""
|
|
171
|
+
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
|
|
172
|
+
try:
|
|
173
|
+
r = httpx.get(f"{self.base_url}/api/tags", headers=headers, timeout=5.0)
|
|
174
|
+
r.raise_for_status()
|
|
175
|
+
names = {m.get("name") for m in r.json().get("models") or []}
|
|
176
|
+
except (httpx.HTTPError, ValueError) as exc:
|
|
177
|
+
return False, f"cannot reach Ollama at {self.base_url} ({exc.__class__.__name__}); is it running? (ollama serve)"
|
|
178
|
+
wanted = self.model if ":" in self.model else f"{self.model}:latest"
|
|
179
|
+
if wanted not in names and self.model not in names:
|
|
180
|
+
return False, f"Ollama is running but has no model {self.model!r}; run: ollama pull {self.model}"
|
|
181
|
+
return True, f"Ollama at {self.base_url} has {self.model}"
|
|
182
|
+
|
|
183
|
+
def context_size(self, text_chars: int) -> int:
|
|
184
|
+
if self.context:
|
|
185
|
+
return self.context
|
|
186
|
+
needed = text_chars // 3 + self.MAX_OUTPUT # ~3 characters per token for JSON-heavy text
|
|
187
|
+
return min(max(8192, -(-needed // 2048) * 2048), 32768)
|
|
188
|
+
|
|
189
|
+
def complete_json(self, system: str, user: str, schema: dict[str, Any]) -> dict[str, Any]:
|
|
190
|
+
system = _with_schema(system, schema)
|
|
191
|
+
body: dict[str, Any] = {
|
|
192
|
+
"model": self.model,
|
|
193
|
+
"stream": False,
|
|
194
|
+
"think": False,
|
|
195
|
+
"format": schema,
|
|
196
|
+
"options": {"num_ctx": self.context_size(len(system) + len(user)), "num_predict": self.MAX_OUTPUT,
|
|
197
|
+
"temperature": 0.2},
|
|
198
|
+
"messages": [{"role": "system", "content": system}, {"role": "user", "content": user}],
|
|
199
|
+
}
|
|
200
|
+
url = f"{self.base_url}/api/chat"
|
|
201
|
+
headers = {"Authorization": f"Bearer {self.api_key}"} if self.api_key else {}
|
|
202
|
+
with httpx.Client(timeout=httpx.Timeout(self.timeout, connect=10.0)) as http:
|
|
203
|
+
for _ in range(3):
|
|
204
|
+
try:
|
|
205
|
+
r = http.post(url, json=body, headers=headers)
|
|
206
|
+
except httpx.ConnectError as exc:
|
|
207
|
+
raise LLMError(f"Cannot reach Ollama at {self.base_url}; is it running? (ollama serve)") from exc
|
|
208
|
+
except httpx.TimeoutException as exc:
|
|
209
|
+
raise LLMError(_timeout_message(self.model, self.timeout)) from exc
|
|
210
|
+
except httpx.HTTPError as exc:
|
|
211
|
+
raise LLMError(f"Ollama request failed: {exc}") from exc
|
|
212
|
+
if r.status_code == 404 and "not found" in r.text.lower():
|
|
213
|
+
raise LLMError(f"Ollama has no model {self.model!r}; run: ollama pull {self.model}")
|
|
214
|
+
if r.status_code == 400 and "think" in body and "think" in r.text.lower():
|
|
215
|
+
body.pop("think") # older Ollama or a model without a thinking switch
|
|
216
|
+
continue
|
|
217
|
+
if r.status_code == 400 and isinstance(body.get("format"), dict):
|
|
218
|
+
body["format"] = "json" # older Ollama without schema-constrained output
|
|
219
|
+
continue
|
|
220
|
+
if r.status_code >= 400:
|
|
221
|
+
raise LLMError(f"Ollama returned HTTP {r.status_code}: {r.text[:300]}")
|
|
222
|
+
data = r.json()
|
|
223
|
+
if data.get("done_reason") == "length":
|
|
224
|
+
raise LLMError("The model ran out of output tokens before finishing its answer.")
|
|
225
|
+
return parse_json((data.get("message") or {}).get("content") or "")
|
|
226
|
+
raise LLMError("Ollama rejected the request.")
|
|
227
|
+
|
|
228
|
+
|
|
133
229
|
def parse_json(text: str) -> dict[str, Any]:
|
|
134
230
|
text = re.sub(r"```(?:json)?", "", text).strip()
|
|
135
231
|
start, end = text.find("{"), text.rfind("}")
|
|
@@ -147,6 +243,8 @@ def get_provider(settings: Settings) -> Provider | None:
|
|
|
147
243
|
return None
|
|
148
244
|
if name == "anthropic":
|
|
149
245
|
return AnthropicProvider(settings)
|
|
150
|
-
if name
|
|
246
|
+
if name == "ollama":
|
|
247
|
+
return OllamaProvider(settings)
|
|
248
|
+
if name in ("openai", "openrouter"):
|
|
151
249
|
return OpenAICompatibleProvider(settings)
|
|
152
250
|
raise LLMError(f"Unknown LLM provider {name!r}; use anthropic, openai, openrouter, ollama or none.")
|
|
@@ -39,6 +39,14 @@ class State:
|
|
|
39
39
|
state = State()
|
|
40
40
|
|
|
41
41
|
|
|
42
|
+
def _print_version(value: bool) -> None:
|
|
43
|
+
if value:
|
|
44
|
+
from kubesift import __version__
|
|
45
|
+
|
|
46
|
+
print(f"kubesift {__version__}")
|
|
47
|
+
raise typer.Exit()
|
|
48
|
+
|
|
49
|
+
|
|
42
50
|
@app.callback()
|
|
43
51
|
def main_options(
|
|
44
52
|
kubeconfig: str | None = typer.Option(None, "--kubeconfig", envvar="KUBECONFIG_PATH", help="Path to kubeconfig."),
|
|
@@ -46,6 +54,8 @@ def main_options(
|
|
|
46
54
|
llm: str | None = typer.Option(None, "--llm-provider", help="anthropic | openai | openrouter | ollama | none"),
|
|
47
55
|
model: str | None = typer.Option(None, "--model", help="LLM model name."),
|
|
48
56
|
prometheus: str | None = typer.Option(None, "--prometheus", help="Prometheus URL for usage history."),
|
|
57
|
+
version: bool = typer.Option(False, "--version", "-V", is_eager=True, callback=_print_version,
|
|
58
|
+
help="Show the version and exit."),
|
|
49
59
|
) -> None:
|
|
50
60
|
state.settings = load_settings(kubeconfig=kubeconfig, context=context, llm_provider=llm, llm_model=model,
|
|
51
61
|
prometheus_url=prometheus)
|
|
@@ -56,12 +66,32 @@ def _fail(msg: str, code: int = 1) -> NoReturn:
|
|
|
56
66
|
raise typer.Exit(code)
|
|
57
67
|
|
|
58
68
|
|
|
59
|
-
def
|
|
69
|
+
def _load_snapshot_file(path: str):
|
|
60
70
|
from kubesift.cluster.snapshot import Snapshot
|
|
61
71
|
|
|
72
|
+
try:
|
|
73
|
+
data = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
74
|
+
except FileNotFoundError:
|
|
75
|
+
_fail(f"snapshot file not found: {path}")
|
|
76
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
77
|
+
_fail(f"cannot read {path}: {exc}")
|
|
78
|
+
except json.JSONDecodeError as exc:
|
|
79
|
+
_fail(f"{path} is not a KubeSift snapshot (invalid JSON: {exc})")
|
|
80
|
+
if not isinstance(data, dict) or "objects" not in data:
|
|
81
|
+
_fail(f"{path} is not a KubeSift snapshot (create one with: kubesift snapshot save)")
|
|
82
|
+
return Snapshot.from_dict(data)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _llm_status(text: str, use_llm: bool) -> str:
|
|
86
|
+
if use_llm and state.settings.resolved_provider() == "ollama":
|
|
87
|
+
return f"{text} (local model: this can take several minutes without a GPU)"
|
|
88
|
+
return text
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _snapshot(namespace: str | None, from_file: str | None = None, with_logs: bool = True):
|
|
92
|
+
|
|
62
93
|
if from_file:
|
|
63
|
-
|
|
64
|
-
snap = Snapshot.from_dict(data)
|
|
94
|
+
snap = _load_snapshot_file(from_file)
|
|
65
95
|
if namespace:
|
|
66
96
|
snap.namespace = namespace
|
|
67
97
|
return snap
|
|
@@ -126,7 +156,7 @@ def diagnose(
|
|
|
126
156
|
|
|
127
157
|
snap = _snapshot(namespace, from_file)
|
|
128
158
|
store = _store()
|
|
129
|
-
with err.status("Analysing…"):
|
|
159
|
+
with err.status(_llm_status("Analysing…", llm)):
|
|
130
160
|
report = investigate(state.settings, snap, use_llm=llm, window=parse_duration(since), store=store)
|
|
131
161
|
if store:
|
|
132
162
|
store.save_report(report, retention_days=state.settings.history_days)
|
|
@@ -175,7 +205,7 @@ def ask(
|
|
|
175
205
|
ns = mentioned_namespace(q, [n.get("metadata", {}).get("name", "") for n in snap.all("namespaces")])
|
|
176
206
|
if ns:
|
|
177
207
|
snap.namespace = ns
|
|
178
|
-
with err.status("Thinking…"):
|
|
208
|
+
with err.status(_llm_status("Thinking…", llm)):
|
|
179
209
|
report, answer = do_ask(state.settings, snap, q, use_llm=llm, store=_store())
|
|
180
210
|
if output == "json":
|
|
181
211
|
print(json.dumps({"answer": answer, "report": report.model_dump(mode="json")}, indent=2, default=str))
|
|
@@ -537,6 +567,11 @@ def doctor() -> None:
|
|
|
537
567
|
if provider == "none":
|
|
538
568
|
line(None, "no LLM configured: rule-based diagnosis only (set ANTHROPIC_API_KEY, OPENAI_API_KEY, "
|
|
539
569
|
"OPENROUTER_API_KEY or KUBESIFT_LLM_PROVIDER=ollama)")
|
|
570
|
+
elif provider == "ollama":
|
|
571
|
+
from kubesift.analysis.llm import OllamaProvider
|
|
572
|
+
|
|
573
|
+
ready, detail = OllamaProvider(s).status()
|
|
574
|
+
line(True if ready else None, f"LLM: {detail}")
|
|
540
575
|
else:
|
|
541
576
|
line(True, f"LLM: {provider} / {s.resolved_model()}")
|
|
542
577
|
line(True if s.prometheus_url else None, f"Prometheus: {s.prometheus_url or 'not configured (optional)'}")
|
|
@@ -665,17 +700,13 @@ def diff(
|
|
|
665
700
|
output: str = OUT,
|
|
666
701
|
) -> None:
|
|
667
702
|
"""Time travel: what is different between two moments (stored history or snapshot files)."""
|
|
668
|
-
from kubesift.cluster.snapshot import Snapshot
|
|
669
703
|
from kubesift.timetravel import diff as do_diff
|
|
670
704
|
from kubesift.timetravel import digest
|
|
671
705
|
from kubesift.util import now, parse_duration
|
|
672
706
|
|
|
673
|
-
if to_file
|
|
674
|
-
later = Snapshot.from_dict(json.loads(Path(to_file).read_text(encoding="utf-8")))
|
|
675
|
-
else:
|
|
676
|
-
later = _snapshot(namespace, with_logs=False)
|
|
707
|
+
later = _load_snapshot_file(to_file) if to_file else _snapshot(namespace, with_logs=False)
|
|
677
708
|
if from_file:
|
|
678
|
-
earlier = digest(
|
|
709
|
+
earlier = digest(_load_snapshot_file(from_file))
|
|
679
710
|
elif since:
|
|
680
711
|
store = _store()
|
|
681
712
|
earlier = store.digest_before(later.context, now() - parse_duration(since)) if store else None
|
|
@@ -853,8 +884,10 @@ def knowledge_import(pack_file: str) -> None:
|
|
|
853
884
|
try:
|
|
854
885
|
pack = yaml.safe_load(Path(pack_file).read_text(encoding="utf-8")) or {}
|
|
855
886
|
result = import_pack(store, pack, state.settings.runbooks_dir)
|
|
856
|
-
except
|
|
857
|
-
_fail(
|
|
887
|
+
except FileNotFoundError:
|
|
888
|
+
_fail(f"knowledge pack not found: {pack_file}")
|
|
889
|
+
except (OSError, ValueError, yaml.YAMLError) as exc:
|
|
890
|
+
_fail(f"cannot import {pack_file}: {exc}")
|
|
858
891
|
console.print(f"Added {result['notes_added']} notes, skipped {result['notes_skipped']}, "
|
|
859
892
|
f"wrote {result['runbooks_written']} runbooks.")
|
|
860
893
|
|
|
@@ -27,6 +27,7 @@ _KEY_ENV = {
|
|
|
27
27
|
"anthropic": "ANTHROPIC_API_KEY",
|
|
28
28
|
"openai": "OPENAI_API_KEY",
|
|
29
29
|
"openrouter": "OPENROUTER_API_KEY",
|
|
30
|
+
"ollama": "OLLAMA_API_KEY", # only for Ollama's hosted API; a local server needs no key
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
|
|
@@ -43,6 +44,8 @@ class Settings(BaseModel):
|
|
|
43
44
|
llm_base_url: str | None = None
|
|
44
45
|
llm_api_key: str | None = Field(default=None, repr=False)
|
|
45
46
|
llm_effort: str = "medium"
|
|
47
|
+
llm_timeout: int | None = None # seconds to wait for an answer; default 180 (hosted) or 900 (ollama)
|
|
48
|
+
llm_context: int | None = None # ollama only: context window in tokens; default sized to the prompt
|
|
46
49
|
send_logs_to_llm: bool = True
|
|
47
50
|
|
|
48
51
|
# Optional data sources
|
|
@@ -99,6 +102,12 @@ class Settings(BaseModel):
|
|
|
99
102
|
def resolved_base_url(self) -> str | None:
|
|
100
103
|
return self.llm_base_url or _DEFAULT_BASE_URLS.get(self.resolved_provider())
|
|
101
104
|
|
|
105
|
+
def resolved_llm_timeout(self) -> float:
|
|
106
|
+
if self.llm_timeout:
|
|
107
|
+
return float(self.llm_timeout)
|
|
108
|
+
# local models on a laptop CPU can need many minutes for one answer
|
|
109
|
+
return 900.0 if self.resolved_provider() == "ollama" else 180.0
|
|
110
|
+
|
|
102
111
|
def resolved_api_key(self) -> str | None:
|
|
103
112
|
if self.llm_api_key:
|
|
104
113
|
return self.llm_api_key
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"""LLM providers against a local stand-in HTTP server: request shape and response parsing."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from kubesift.analysis.engine import DIAGNOSIS_SCHEMA
|
|
11
|
+
from kubesift.analysis.llm import (
|
|
12
|
+
AnthropicProvider,
|
|
13
|
+
LLMError,
|
|
14
|
+
OllamaProvider,
|
|
15
|
+
OpenAICompatibleProvider,
|
|
16
|
+
get_provider,
|
|
17
|
+
parse_json,
|
|
18
|
+
)
|
|
19
|
+
from kubesift.config import Settings, load_settings
|
|
20
|
+
|
|
21
|
+
ANSWER = {"summary": "s", "root_causes": [], "next_checks": []}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@pytest.fixture
|
|
25
|
+
def llm_server():
|
|
26
|
+
requests: list[dict] = []
|
|
27
|
+
responses: list[tuple[int, dict]] = []
|
|
28
|
+
|
|
29
|
+
class Handler(BaseHTTPRequestHandler):
|
|
30
|
+
def log_message(self, *a):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
def do_POST(self):
|
|
34
|
+
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
|
|
35
|
+
requests.append({"path": self.path, "headers": dict(self.headers), "body": body})
|
|
36
|
+
code, payload = responses.pop(0) if responses else (200, {})
|
|
37
|
+
if "_sleep" in payload:
|
|
38
|
+
time.sleep(payload.pop("_sleep")) # a model that is slow to answer
|
|
39
|
+
data = json.dumps(payload).encode()
|
|
40
|
+
self.send_response(code)
|
|
41
|
+
self.send_header("Content-Type", "application/json")
|
|
42
|
+
self.send_header("Content-Length", str(len(data)))
|
|
43
|
+
self.end_headers()
|
|
44
|
+
self.wfile.write(data)
|
|
45
|
+
|
|
46
|
+
server = HTTPServer(("127.0.0.1", 0), Handler)
|
|
47
|
+
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
48
|
+
yield f"http://127.0.0.1:{server.server_port}", requests, responses
|
|
49
|
+
server.shutdown()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def anthropic_message(text, stop="end_turn"):
|
|
53
|
+
return {"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-opus-5-5",
|
|
54
|
+
"content": [{"type": "text", "text": text}], "stop_reason": stop, "stop_sequence": None,
|
|
55
|
+
"usage": {"input_tokens": 10, "output_tokens": 5}}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_anthropic_request_and_parse(llm_server):
|
|
59
|
+
pytest.importorskip("anthropic")
|
|
60
|
+
url, requests, responses = llm_server
|
|
61
|
+
responses.append((200, anthropic_message(json.dumps(ANSWER))))
|
|
62
|
+
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
63
|
+
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
64
|
+
body = requests[0]["body"]
|
|
65
|
+
assert requests[0]["path"].startswith("/v1/messages")
|
|
66
|
+
assert body["model"] == "claude-opus-5-5"
|
|
67
|
+
assert body["output_config"]["format"]["type"] == "json_schema"
|
|
68
|
+
assert body["fallbacks"] == "default"
|
|
69
|
+
assert "server-side-fallback-2026-07-01" in requests[0]["headers"].get("anthropic-beta", "")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_anthropic_retries_without_beta_on_400(llm_server):
|
|
73
|
+
pytest.importorskip("anthropic")
|
|
74
|
+
url, requests, responses = llm_server
|
|
75
|
+
responses += [(400, {"type": "error", "error": {"type": "invalid_request_error", "message": "unsupported"}}),
|
|
76
|
+
(200, anthropic_message(json.dumps(ANSWER)))]
|
|
77
|
+
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
78
|
+
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
79
|
+
assert "fallbacks" not in requests[-1]["body"]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_anthropic_refusal(llm_server):
|
|
83
|
+
pytest.importorskip("anthropic")
|
|
84
|
+
url, _, responses = llm_server
|
|
85
|
+
responses.append((200, anthropic_message("", stop="refusal")))
|
|
86
|
+
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
87
|
+
with pytest.raises(LLMError):
|
|
88
|
+
p.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_openai_compatible(llm_server):
|
|
92
|
+
url, requests, responses = llm_server
|
|
93
|
+
responses += [(400, {"error": "no json mode"}),
|
|
94
|
+
(200, {"choices": [{"message": {"content": "```json\n" + json.dumps(ANSWER) + "\n```"}}]})]
|
|
95
|
+
p = OpenAICompatibleProvider(Settings(llm_provider="openrouter", llm_api_key="k", llm_base_url=url, llm_model="m"))
|
|
96
|
+
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
97
|
+
assert requests[0]["headers"]["Authorization"] == "Bearer k"
|
|
98
|
+
assert "response_format" in requests[0]["body"] and "response_format" not in requests[1]["body"]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_parse_json_errors():
|
|
102
|
+
with pytest.raises(LLMError):
|
|
103
|
+
parse_json("no json here")
|
|
104
|
+
assert parse_json('Sure! {"a": 1} hope that helps') == {"a": 1}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_provider_auto(monkeypatch):
|
|
108
|
+
for k in ("ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY"):
|
|
109
|
+
monkeypatch.delenv(k, raising=False)
|
|
110
|
+
assert Settings().resolved_provider() == "none"
|
|
111
|
+
monkeypatch.setenv("OPENROUTER_API_KEY", "x")
|
|
112
|
+
assert Settings().resolved_provider() == "openrouter"
|
|
113
|
+
assert Settings().resolved_model() == "anthropic/claude-opus-5-5"
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_llm_timeout_defaults_and_override(monkeypatch):
|
|
117
|
+
assert Settings(llm_provider="ollama").resolved_llm_timeout() == 900
|
|
118
|
+
assert Settings(llm_provider="anthropic").resolved_llm_timeout() == 180
|
|
119
|
+
assert Settings(llm_provider="ollama", llm_timeout=30).resolved_llm_timeout() == 30
|
|
120
|
+
monkeypatch.setenv("KUBESIFT_LLM_TIMEOUT", "1200")
|
|
121
|
+
assert load_settings(llm_provider="openai").resolved_llm_timeout() == 1200
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def test_slow_answer_is_not_retried(llm_server):
|
|
125
|
+
url, requests, responses = llm_server
|
|
126
|
+
responses.append((200, {"_sleep": 2.5}))
|
|
127
|
+
p = OpenAICompatibleProvider(Settings(llm_provider="openai", llm_api_key="k", llm_base_url=url, llm_timeout=1))
|
|
128
|
+
with pytest.raises(LLMError, match="did not answer within 1s"):
|
|
129
|
+
p.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
130
|
+
assert len(requests) == 1
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_ollama_uses_native_api(llm_server):
|
|
134
|
+
url, requests, responses = llm_server
|
|
135
|
+
responses.append((200, {"message": {"role": "assistant", "content": json.dumps(ANSWER)}, "done": True,
|
|
136
|
+
"done_reason": "stop"}))
|
|
137
|
+
p = get_provider(Settings(llm_provider="ollama", llm_base_url=url + "/v1", llm_model="qwen3.5:4b"))
|
|
138
|
+
assert isinstance(p, OllamaProvider) and p.base_url == url
|
|
139
|
+
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
140
|
+
req = requests[0]
|
|
141
|
+
assert req["path"] == "/api/chat"
|
|
142
|
+
body = req["body"]
|
|
143
|
+
assert body["think"] is False and body["stream"] is False and body["format"] == DIAGNOSIS_SCHEMA
|
|
144
|
+
assert body["options"]["num_ctx"] >= 8192 and body["options"]["num_predict"] == OllamaProvider.MAX_OUTPUT
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_ollama_context_is_sized_to_the_prompt():
|
|
148
|
+
p = OllamaProvider(Settings(llm_provider="ollama"))
|
|
149
|
+
assert p.context_size(1000) == 8192
|
|
150
|
+
assert p.context_size(60000) == 24576 # 20000 prompt tokens + 4096 output, rounded up
|
|
151
|
+
assert p.context_size(10_000_000) == 32768
|
|
152
|
+
assert OllamaProvider(Settings(llm_provider="ollama", llm_context=4096)).context_size(60000) == 4096
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_ollama_falls_back_for_older_servers(llm_server):
|
|
156
|
+
url, requests, responses = llm_server
|
|
157
|
+
responses += [(400, {"error": 'think value "false" is not supported for this model'}),
|
|
158
|
+
(400, {"error": "invalid format"}),
|
|
159
|
+
(200, {"message": {"content": json.dumps(ANSWER)}, "done": True})]
|
|
160
|
+
p = OllamaProvider(Settings(llm_provider="ollama", llm_base_url=url))
|
|
161
|
+
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
162
|
+
assert "think" not in requests[1]["body"] and requests[2]["body"]["format"] == "json"
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_ollama_explains_common_failures(llm_server):
|
|
166
|
+
url, _, responses = llm_server
|
|
167
|
+
responses.append((404, {"error": 'model "qwen3.5:4b" not found, try pulling it first'}))
|
|
168
|
+
p = OllamaProvider(Settings(llm_provider="ollama", llm_base_url=url, llm_model="qwen3.5:4b"))
|
|
169
|
+
with pytest.raises(LLMError, match="ollama pull qwen3.5:4b"):
|
|
170
|
+
p.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
171
|
+
responses.append((200, {"message": {"content": '{"summary": "cut'}, "done": True, "done_reason": "length"}))
|
|
172
|
+
with pytest.raises(LLMError, match="ran out of output tokens"):
|
|
173
|
+
p.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
174
|
+
down = OllamaProvider(Settings(llm_provider="ollama", llm_base_url="http://127.0.0.1:9"))
|
|
175
|
+
with pytest.raises(LLMError, match="is it running"):
|
|
176
|
+
down.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_ollama_status_and_api_key(llm_server, monkeypatch):
|
|
180
|
+
import httpx
|
|
181
|
+
|
|
182
|
+
url, requests, responses = llm_server
|
|
183
|
+
tags = {"models": [{"name": "qwen3.5:4b"}, {"name": "llama3.1:latest"}]}
|
|
184
|
+
monkeypatch.setattr(httpx, "get", lambda *a, **k: httpx.Response(200, json=tags, request=httpx.Request("GET", a[0])))
|
|
185
|
+
assert OllamaProvider(Settings(llm_provider="ollama", llm_model="qwen3.5:4b")).status()[0]
|
|
186
|
+
assert OllamaProvider(Settings(llm_provider="ollama", llm_model="llama3.1")).status()[0]
|
|
187
|
+
ok, detail = OllamaProvider(Settings(llm_provider="ollama", llm_model="mistral")).status()
|
|
188
|
+
assert not ok and "ollama pull mistral" in detail
|
|
189
|
+
monkeypatch.setenv("OLLAMA_API_KEY", "ok-key")
|
|
190
|
+
responses.append((200, {"message": {"content": json.dumps(ANSWER)}, "done": True}))
|
|
191
|
+
OllamaProvider(Settings(llm_provider="ollama", llm_base_url=url)).complete_json("s", "u", DIAGNOSIS_SCHEMA)
|
|
192
|
+
assert requests[-1]["headers"]["Authorization"] == "Bearer ok-key"
|
kubesift-2.0.0/tests/test_llm.py
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
"""LLM providers against a local stand-in HTTP server: request shape and response parsing."""
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import threading
|
|
5
|
-
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
6
|
-
|
|
7
|
-
import pytest
|
|
8
|
-
|
|
9
|
-
from kubesift.analysis.engine import DIAGNOSIS_SCHEMA
|
|
10
|
-
from kubesift.analysis.llm import AnthropicProvider, LLMError, OpenAICompatibleProvider, parse_json
|
|
11
|
-
from kubesift.config import Settings
|
|
12
|
-
|
|
13
|
-
ANSWER = {"summary": "s", "root_causes": [], "next_checks": []}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@pytest.fixture
|
|
17
|
-
def llm_server():
|
|
18
|
-
requests: list[dict] = []
|
|
19
|
-
responses: list[tuple[int, dict]] = []
|
|
20
|
-
|
|
21
|
-
class Handler(BaseHTTPRequestHandler):
|
|
22
|
-
def log_message(self, *a):
|
|
23
|
-
pass
|
|
24
|
-
|
|
25
|
-
def do_POST(self):
|
|
26
|
-
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
|
|
27
|
-
requests.append({"path": self.path, "headers": dict(self.headers), "body": body})
|
|
28
|
-
code, payload = responses.pop(0) if responses else (200, {})
|
|
29
|
-
data = json.dumps(payload).encode()
|
|
30
|
-
self.send_response(code)
|
|
31
|
-
self.send_header("Content-Type", "application/json")
|
|
32
|
-
self.send_header("Content-Length", str(len(data)))
|
|
33
|
-
self.end_headers()
|
|
34
|
-
self.wfile.write(data)
|
|
35
|
-
|
|
36
|
-
server = HTTPServer(("127.0.0.1", 0), Handler)
|
|
37
|
-
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
38
|
-
yield f"http://127.0.0.1:{server.server_port}", requests, responses
|
|
39
|
-
server.shutdown()
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def anthropic_message(text, stop="end_turn"):
|
|
43
|
-
return {"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-opus-5-5",
|
|
44
|
-
"content": [{"type": "text", "text": text}], "stop_reason": stop, "stop_sequence": None,
|
|
45
|
-
"usage": {"input_tokens": 10, "output_tokens": 5}}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def test_anthropic_request_and_parse(llm_server):
|
|
49
|
-
pytest.importorskip("anthropic")
|
|
50
|
-
url, requests, responses = llm_server
|
|
51
|
-
responses.append((200, anthropic_message(json.dumps(ANSWER))))
|
|
52
|
-
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
53
|
-
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
54
|
-
body = requests[0]["body"]
|
|
55
|
-
assert requests[0]["path"].startswith("/v1/messages")
|
|
56
|
-
assert body["model"] == "claude-opus-5-5"
|
|
57
|
-
assert body["output_config"]["format"]["type"] == "json_schema"
|
|
58
|
-
assert body["fallbacks"] == "default"
|
|
59
|
-
assert "server-side-fallback-2026-07-01" in requests[0]["headers"].get("anthropic-beta", "")
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
def test_anthropic_retries_without_beta_on_400(llm_server):
|
|
63
|
-
pytest.importorskip("anthropic")
|
|
64
|
-
url, requests, responses = llm_server
|
|
65
|
-
responses += [(400, {"type": "error", "error": {"type": "invalid_request_error", "message": "unsupported"}}),
|
|
66
|
-
(200, anthropic_message(json.dumps(ANSWER)))]
|
|
67
|
-
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
68
|
-
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
69
|
-
assert "fallbacks" not in requests[-1]["body"]
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def test_anthropic_refusal(llm_server):
|
|
73
|
-
pytest.importorskip("anthropic")
|
|
74
|
-
url, _, responses = llm_server
|
|
75
|
-
responses.append((200, anthropic_message("", stop="refusal")))
|
|
76
|
-
p = AnthropicProvider(Settings(llm_provider="anthropic", llm_api_key="k", llm_base_url=url))
|
|
77
|
-
with pytest.raises(LLMError):
|
|
78
|
-
p.complete_json("sys", "user", DIAGNOSIS_SCHEMA)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def test_openai_compatible(llm_server):
|
|
82
|
-
url, requests, responses = llm_server
|
|
83
|
-
responses += [(400, {"error": "no json mode"}),
|
|
84
|
-
(200, {"choices": [{"message": {"content": "```json\n" + json.dumps(ANSWER) + "\n```"}}]})]
|
|
85
|
-
p = OpenAICompatibleProvider(Settings(llm_provider="openrouter", llm_api_key="k", llm_base_url=url, llm_model="m"))
|
|
86
|
-
assert p.complete_json("sys", "user", DIAGNOSIS_SCHEMA) == ANSWER
|
|
87
|
-
assert requests[0]["headers"]["Authorization"] == "Bearer k"
|
|
88
|
-
assert "response_format" in requests[0]["body"] and "response_format" not in requests[1]["body"]
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def test_parse_json_errors():
|
|
92
|
-
with pytest.raises(LLMError):
|
|
93
|
-
parse_json("no json here")
|
|
94
|
-
assert parse_json('Sure! {"a": 1} hope that helps') == {"a": 1}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
def test_provider_auto(monkeypatch):
|
|
98
|
-
for k in ("ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY"):
|
|
99
|
-
monkeypatch.delenv(k, raising=False)
|
|
100
|
-
assert Settings().resolved_provider() == "none"
|
|
101
|
-
monkeypatch.setenv("OPENROUTER_API_KEY", "x")
|
|
102
|
-
assert Settings().resolved_provider() == "openrouter"
|
|
103
|
-
assert Settings().resolved_model() == "anthropic/claude-opus-5-5"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/01-crashloop-missing-env.yaml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/04-service-selector-mismatch.yaml
RENAMED
|
File without changes
|
{kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/05-missing-probe-crashloop.yaml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{kubesift-2.0.0 → kubesift-2.0.1}/src/kubesift/drill_scenarios/08-pvc-missing-storageclass.yaml
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|