tweek 0.3.1__py3-none-any.whl → 0.4.0__py3-none-any.whl
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.
- tweek/__init__.py +2 -2
- tweek/audit.py +2 -2
- tweek/cli.py +78 -6605
- tweek/cli_config.py +643 -0
- tweek/cli_configure.py +413 -0
- tweek/cli_core.py +718 -0
- tweek/cli_dry_run.py +390 -0
- tweek/cli_helpers.py +316 -0
- tweek/cli_install.py +1666 -0
- tweek/cli_logs.py +301 -0
- tweek/cli_mcp.py +148 -0
- tweek/cli_memory.py +343 -0
- tweek/cli_plugins.py +748 -0
- tweek/cli_protect.py +564 -0
- tweek/cli_proxy.py +405 -0
- tweek/cli_security.py +236 -0
- tweek/cli_skills.py +289 -0
- tweek/cli_uninstall.py +551 -0
- tweek/cli_vault.py +313 -0
- tweek/config/allowed_dirs.yaml +16 -17
- tweek/config/families.yaml +4 -1
- tweek/config/manager.py +17 -0
- tweek/config/patterns.yaml +29 -5
- tweek/config/templates/config.yaml.template +212 -0
- tweek/config/templates/env.template +45 -0
- tweek/config/templates/overrides.yaml.template +121 -0
- tweek/config/templates/tweek.yaml.template +20 -0
- tweek/config/templates.py +136 -0
- tweek/config/tiers.yaml +5 -4
- tweek/diagnostics.py +112 -32
- tweek/hooks/overrides.py +4 -0
- tweek/hooks/post_tool_use.py +46 -1
- tweek/hooks/pre_tool_use.py +149 -49
- tweek/integrations/openclaw.py +84 -0
- tweek/licensing.py +1 -1
- tweek/mcp/__init__.py +7 -9
- tweek/mcp/clients/chatgpt.py +2 -2
- tweek/mcp/clients/claude_desktop.py +2 -2
- tweek/mcp/clients/gemini.py +2 -2
- tweek/mcp/proxy.py +165 -1
- tweek/memory/provenance.py +438 -0
- tweek/memory/queries.py +2 -0
- tweek/memory/safety.py +23 -4
- tweek/memory/schemas.py +1 -0
- tweek/memory/store.py +101 -71
- tweek/plugins/screening/heuristic_scorer.py +1 -1
- tweek/security/integrity.py +77 -0
- tweek/security/llm_reviewer.py +162 -68
- tweek/security/local_reviewer.py +44 -2
- tweek/security/model_registry.py +73 -7
- tweek/skill_template/overrides-reference.md +1 -1
- tweek/skills/context.py +221 -0
- tweek/skills/scanner.py +2 -2
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/METADATA +8 -7
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/RECORD +60 -38
- tweek/mcp/server.py +0 -320
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/WHEEL +0 -0
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/entry_points.txt +0 -0
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/licenses/LICENSE +0 -0
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/licenses/NOTICE +0 -0
- {tweek-0.3.1.dist-info → tweek-0.4.0.dist-info}/top_level.txt +0 -0
tweek/security/local_reviewer.py
CHANGED
|
@@ -41,6 +41,16 @@ class LocalModelReviewProvider(ReviewProvider):
|
|
|
41
41
|
self._model_name = model_name
|
|
42
42
|
self._escalation_provider = escalation_provider
|
|
43
43
|
|
|
44
|
+
# Tools where the local prompt-injection classifier is effective.
|
|
45
|
+
# The DeBERTa model was trained on natural-language prompt injection,
|
|
46
|
+
# NOT on shell command evaluation. For Bash/Edit/Write the model
|
|
47
|
+
# produces severe false positives (e.g. classifying "./run.sh 2>&1"
|
|
48
|
+
# as injection at 100% confidence). Those tools should be handled by
|
|
49
|
+
# pattern matching + cloud LLM escalation instead.
|
|
50
|
+
_CONTENT_TOOLS: frozenset = frozenset({
|
|
51
|
+
"Read", "WebFetch", "Grep", "WebSearch",
|
|
52
|
+
})
|
|
53
|
+
|
|
44
54
|
def call(self, system_prompt: str, user_prompt: str, max_tokens: int = 256) -> str:
|
|
45
55
|
"""Run local inference and return JSON result.
|
|
46
56
|
|
|
@@ -48,8 +58,11 @@ class LocalModelReviewProvider(ReviewProvider):
|
|
|
48
58
|
runs local inference, and returns a JSON string in the same format
|
|
49
59
|
that LLMReviewer._parse_response() expects.
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
The local model is only used for content-screening tools (Read,
|
|
62
|
+
WebFetch, Grep, WebSearch) where the input is natural-language text
|
|
63
|
+
that the classifier was trained on. For command-execution tools
|
|
64
|
+
(Bash, Edit, Write, etc.) the request is forwarded to the
|
|
65
|
+
escalation provider or returned as low-confidence safe.
|
|
53
66
|
|
|
54
67
|
Args:
|
|
55
68
|
system_prompt: System-level instructions (used for escalation only).
|
|
@@ -61,6 +74,23 @@ class LocalModelReviewProvider(ReviewProvider):
|
|
|
61
74
|
"""
|
|
62
75
|
from tweek.security.local_model import get_local_model
|
|
63
76
|
|
|
77
|
+
# Detect the tool from the analysis prompt (e.g. "Tool: Bash")
|
|
78
|
+
tool_name = self._extract_tool(user_prompt)
|
|
79
|
+
|
|
80
|
+
# The DeBERTa prompt-injection model only works on natural-language
|
|
81
|
+
# content. For shell commands and code, defer to cloud LLM or
|
|
82
|
+
# pattern matching.
|
|
83
|
+
if tool_name and tool_name not in self._CONTENT_TOOLS:
|
|
84
|
+
if self._escalation_provider:
|
|
85
|
+
return self._escalation_provider.call(
|
|
86
|
+
system_prompt, user_prompt, max_tokens
|
|
87
|
+
)
|
|
88
|
+
return json.dumps({
|
|
89
|
+
"risk_level": "safe",
|
|
90
|
+
"reason": f"Local model not applicable for {tool_name} commands",
|
|
91
|
+
"confidence": 0.0,
|
|
92
|
+
})
|
|
93
|
+
|
|
64
94
|
# Extract command from untrusted_command tags
|
|
65
95
|
command = self._extract_command(user_prompt)
|
|
66
96
|
if not command:
|
|
@@ -124,6 +154,18 @@ class LocalModelReviewProvider(ReviewProvider):
|
|
|
124
154
|
def model_name(self) -> str:
|
|
125
155
|
return self._model_name
|
|
126
156
|
|
|
157
|
+
@staticmethod
|
|
158
|
+
def _extract_tool(user_prompt: str) -> Optional[str]:
|
|
159
|
+
"""Extract the tool name from the analysis prompt.
|
|
160
|
+
|
|
161
|
+
The LLMReviewer ANALYSIS_PROMPT includes a ``Tool: <name>`` line.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
Tool name (e.g. "Bash", "Read"), or None if not found.
|
|
165
|
+
"""
|
|
166
|
+
match = re.search(r"^Tool:\s*(\S+)", user_prompt, re.MULTILINE)
|
|
167
|
+
return match.group(1) if match else None
|
|
168
|
+
|
|
127
169
|
@staticmethod
|
|
128
170
|
def _extract_command(user_prompt: str) -> str:
|
|
129
171
|
"""Extract the command from <untrusted_command> tags.
|
tweek/security/model_registry.py
CHANGED
|
@@ -40,7 +40,9 @@ class ModelDefinition:
|
|
|
40
40
|
license: str = "unknown"
|
|
41
41
|
size_mb: float = 0.0 # approximate download size
|
|
42
42
|
files: List[str] = field(default_factory=list)
|
|
43
|
+
file_hashes: Dict[str, str] = field(default_factory=dict) # filename -> sha256
|
|
43
44
|
hf_subfolder: str = "" # subfolder in the HF repo (e.g., "onnx")
|
|
45
|
+
hf_revision: str = "main" # git revision (commit SHA for pinned downloads)
|
|
44
46
|
requires_auth: bool = False
|
|
45
47
|
default: bool = False
|
|
46
48
|
|
|
@@ -73,7 +75,12 @@ MODEL_CATALOG: Dict[str, ModelDefinition] = {
|
|
|
73
75
|
license="Apache-2.0",
|
|
74
76
|
size_mb=750.0,
|
|
75
77
|
files=["model.onnx", "tokenizer.json"],
|
|
78
|
+
file_hashes={
|
|
79
|
+
"model.onnx": "f0ea7f239f765aedbde7c9e163a7cb38a79c5b8853d3f76db5152172047b228c",
|
|
80
|
+
"tokenizer.json": "752fe5f0d5678ad563e1bd2ecc1ddf7a3ba7e2024d0ac1dba1a72975e26dff2f",
|
|
81
|
+
},
|
|
76
82
|
hf_subfolder="onnx",
|
|
83
|
+
hf_revision="e6535ca4ce3ba852083e75ec585d7c8aeb4be4c5",
|
|
77
84
|
requires_auth=False,
|
|
78
85
|
default=True,
|
|
79
86
|
escalate_min_confidence=0.1,
|
|
@@ -167,11 +174,15 @@ class ModelDownloadError(Exception):
|
|
|
167
174
|
pass
|
|
168
175
|
|
|
169
176
|
|
|
170
|
-
def _build_hf_url(repo: str, filename: str, subfolder: str = "") -> str:
|
|
171
|
-
"""Build a HuggingFace CDN download URL.
|
|
177
|
+
def _build_hf_url(repo: str, filename: str, subfolder: str = "", revision: str = "main") -> str:
|
|
178
|
+
"""Build a HuggingFace CDN download URL.
|
|
179
|
+
|
|
180
|
+
When *revision* is a commit SHA, the URL points to an immutable
|
|
181
|
+
snapshot — the same bytes every time, safe to verify with SHA-256.
|
|
182
|
+
"""
|
|
172
183
|
if subfolder:
|
|
173
|
-
return f"https://huggingface.co/{repo}/resolve/
|
|
174
|
-
return f"https://huggingface.co/{repo}/resolve/
|
|
184
|
+
return f"https://huggingface.co/{repo}/resolve/{revision}/{subfolder}/{filename}"
|
|
185
|
+
return f"https://huggingface.co/{repo}/resolve/{revision}/{filename}"
|
|
175
186
|
|
|
176
187
|
|
|
177
188
|
def _get_hf_headers() -> Dict[str, str]:
|
|
@@ -234,9 +245,12 @@ def download_model(
|
|
|
234
245
|
# Create SSL context
|
|
235
246
|
ssl_context = ssl.create_default_context()
|
|
236
247
|
|
|
237
|
-
# Download each file
|
|
248
|
+
# Download each file, pinned to a specific revision for reproducibility
|
|
238
249
|
for filename in definition.files:
|
|
239
|
-
url = _build_hf_url(
|
|
250
|
+
url = _build_hf_url(
|
|
251
|
+
definition.hf_repo, filename,
|
|
252
|
+
definition.hf_subfolder, definition.hf_revision,
|
|
253
|
+
)
|
|
240
254
|
dest = model_dir / filename
|
|
241
255
|
tmp_dest = model_dir / f".{filename}.tmp"
|
|
242
256
|
|
|
@@ -258,6 +272,20 @@ def download_model(
|
|
|
258
272
|
if progress_callback:
|
|
259
273
|
progress_callback(filename, downloaded, total)
|
|
260
274
|
|
|
275
|
+
# Verify SHA-256 if the catalog provides an expected hash
|
|
276
|
+
expected_hash = definition.file_hashes.get(filename)
|
|
277
|
+
if expected_hash:
|
|
278
|
+
actual_hash = hashlib.sha256(tmp_dest.read_bytes()).hexdigest()
|
|
279
|
+
if actual_hash != expected_hash:
|
|
280
|
+
tmp_dest.unlink(missing_ok=True)
|
|
281
|
+
raise ModelDownloadError(
|
|
282
|
+
f"SHA-256 mismatch for {filename}: "
|
|
283
|
+
f"expected {expected_hash[:16]}..., "
|
|
284
|
+
f"got {actual_hash[:16]}... "
|
|
285
|
+
f"The file may be corrupted or tampered with. "
|
|
286
|
+
f"Try again with --force, or report this issue."
|
|
287
|
+
)
|
|
288
|
+
|
|
261
289
|
# Atomic rename
|
|
262
290
|
tmp_dest.rename(dest)
|
|
263
291
|
|
|
@@ -284,6 +312,8 @@ def download_model(
|
|
|
284
312
|
raise ModelDownloadError(
|
|
285
313
|
f"Network error downloading {filename}: {e.reason}"
|
|
286
314
|
) from e
|
|
315
|
+
except ModelDownloadError:
|
|
316
|
+
raise # Re-raise SHA mismatch without wrapping
|
|
287
317
|
except Exception as e:
|
|
288
318
|
tmp_dest.unlink(missing_ok=True)
|
|
289
319
|
raise ModelDownloadError(
|
|
@@ -327,7 +357,7 @@ def remove_model(name: str) -> bool:
|
|
|
327
357
|
|
|
328
358
|
|
|
329
359
|
def verify_model(name: str) -> Dict[str, bool]:
|
|
330
|
-
"""Verify a model installation.
|
|
360
|
+
"""Verify a model installation (file existence only).
|
|
331
361
|
|
|
332
362
|
Args:
|
|
333
363
|
name: Model name.
|
|
@@ -347,6 +377,42 @@ def verify_model(name: str) -> Dict[str, bool]:
|
|
|
347
377
|
|
|
348
378
|
status["model_meta.yaml"] = (model_dir / "model_meta.yaml").exists()
|
|
349
379
|
|
|
380
|
+
|
|
381
|
+
def verify_model_hashes(name: str) -> Dict[str, Optional[str]]:
|
|
382
|
+
"""Verify SHA-256 integrity of an installed model's files.
|
|
383
|
+
|
|
384
|
+
Args:
|
|
385
|
+
name: Model name from the catalog.
|
|
386
|
+
|
|
387
|
+
Returns:
|
|
388
|
+
Dict mapping filename to verification status:
|
|
389
|
+
- ``"ok"`` — hash matches catalog
|
|
390
|
+
- ``"mismatch"`` — hash does not match (corrupted or tampered)
|
|
391
|
+
- ``"missing"`` — file not found on disk
|
|
392
|
+
- ``"no_hash"`` — catalog has no expected hash for this file
|
|
393
|
+
Returns empty dict if model is not in the catalog.
|
|
394
|
+
"""
|
|
395
|
+
definition = MODEL_CATALOG.get(name)
|
|
396
|
+
if definition is None:
|
|
397
|
+
return {}
|
|
398
|
+
|
|
399
|
+
model_dir = get_model_dir(name)
|
|
400
|
+
results: Dict[str, Optional[str]] = {}
|
|
401
|
+
|
|
402
|
+
for filename in definition.files:
|
|
403
|
+
expected = definition.file_hashes.get(filename)
|
|
404
|
+
path = model_dir / filename
|
|
405
|
+
|
|
406
|
+
if not path.exists():
|
|
407
|
+
results[filename] = "missing"
|
|
408
|
+
elif not expected:
|
|
409
|
+
results[filename] = "no_hash"
|
|
410
|
+
else:
|
|
411
|
+
actual = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
412
|
+
results[filename] = "ok" if actual == expected else "mismatch"
|
|
413
|
+
|
|
414
|
+
return results
|
|
415
|
+
|
|
350
416
|
return status
|
|
351
417
|
|
|
352
418
|
|
tweek/skills/context.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tweek Skill Context Tracking
|
|
3
|
+
|
|
4
|
+
Detects active skill context from Claude Code's Skill tool invocations.
|
|
5
|
+
When PreToolUse sees tool_name=="Skill", the skill name is extracted from
|
|
6
|
+
tool_input and written to a breadcrumb file. Subsequent tool calls within
|
|
7
|
+
the same session read the breadcrumb to get skill context for tier lookup.
|
|
8
|
+
|
|
9
|
+
This bridges the gap where Claude Code's hook protocol doesn't include
|
|
10
|
+
skill_name: the Skill tool IS a regular tool, so PreToolUse sees it.
|
|
11
|
+
|
|
12
|
+
Security properties:
|
|
13
|
+
- Session-isolated: per-session breadcrumb files (no cross-session leakage)
|
|
14
|
+
- Auto-expiring: 60-second staleness timeout
|
|
15
|
+
- Atomic writes: write-to-temp + os.rename (POSIX atomic)
|
|
16
|
+
- Restricted permissions: 0o600 on breadcrumb files
|
|
17
|
+
- Fail-safe: any error falls to no-context = default tier
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import tempfile
|
|
25
|
+
import time
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Optional
|
|
28
|
+
|
|
29
|
+
# Breadcrumb location — per-session files for isolation
|
|
30
|
+
TWEEK_STATE_DIR = Path.home() / ".tweek" / "state"
|
|
31
|
+
|
|
32
|
+
# Breadcrumb expires after 60 seconds of inactivity.
|
|
33
|
+
# Skills typically issue tool calls in rapid succession; 60s is generous
|
|
34
|
+
# while limiting the window for staleness-based attacks.
|
|
35
|
+
STALENESS_TIMEOUT_SECONDS = 60
|
|
36
|
+
|
|
37
|
+
# Maximum age before a per-session breadcrumb file is considered orphaned
|
|
38
|
+
# and eligible for cleanup (1 hour).
|
|
39
|
+
ORPHAN_CLEANUP_SECONDS = 3600
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _breadcrumb_path_for_session(session_id: str, state_dir: Optional[Path] = None) -> Path:
|
|
43
|
+
"""Get the breadcrumb file path for a specific session.
|
|
44
|
+
|
|
45
|
+
Uses first 12 chars of session_id to avoid excessively long filenames
|
|
46
|
+
while maintaining sufficient uniqueness.
|
|
47
|
+
"""
|
|
48
|
+
prefix = session_id[:12] if session_id else "unknown"
|
|
49
|
+
base = state_dir or TWEEK_STATE_DIR
|
|
50
|
+
return base / f"active_skill_{prefix}.json"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def write_skill_breadcrumb(
|
|
54
|
+
skill_name: str,
|
|
55
|
+
session_id: str,
|
|
56
|
+
*,
|
|
57
|
+
breadcrumb_path: Optional[Path] = None,
|
|
58
|
+
) -> None:
|
|
59
|
+
"""Record the active skill for this session.
|
|
60
|
+
|
|
61
|
+
Called when PreToolUse detects a Skill tool invocation.
|
|
62
|
+
Uses atomic write (temp file + rename) and restricts permissions to 0o600.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
skill_name: The skill being invoked (from tool_input["skill"])
|
|
66
|
+
session_id: Current hook session ID for isolation
|
|
67
|
+
breadcrumb_path: Override for testing (bypasses per-session naming)
|
|
68
|
+
"""
|
|
69
|
+
path = breadcrumb_path or _breadcrumb_path_for_session(session_id)
|
|
70
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
71
|
+
|
|
72
|
+
data = {
|
|
73
|
+
"skill": skill_name,
|
|
74
|
+
"session_id": session_id,
|
|
75
|
+
"timestamp": time.time(),
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
# Atomic write: write to temp file, then rename (POSIX atomic)
|
|
79
|
+
fd = None
|
|
80
|
+
tmp_path = None
|
|
81
|
+
try:
|
|
82
|
+
fd, tmp_path = tempfile.mkstemp(
|
|
83
|
+
dir=str(path.parent),
|
|
84
|
+
prefix=".skill_",
|
|
85
|
+
suffix=".tmp",
|
|
86
|
+
)
|
|
87
|
+
os.write(fd, json.dumps(data).encode("utf-8"))
|
|
88
|
+
os.close(fd)
|
|
89
|
+
fd = None # Mark as closed
|
|
90
|
+
|
|
91
|
+
# Restrict permissions before rename (owner read/write only)
|
|
92
|
+
os.chmod(tmp_path, 0o600)
|
|
93
|
+
|
|
94
|
+
# Atomic rename
|
|
95
|
+
os.rename(tmp_path, str(path))
|
|
96
|
+
tmp_path = None # Mark as renamed (don't clean up)
|
|
97
|
+
finally:
|
|
98
|
+
# Clean up on failure
|
|
99
|
+
if fd is not None:
|
|
100
|
+
try:
|
|
101
|
+
os.close(fd)
|
|
102
|
+
except OSError:
|
|
103
|
+
pass
|
|
104
|
+
if tmp_path is not None:
|
|
105
|
+
try:
|
|
106
|
+
os.unlink(tmp_path)
|
|
107
|
+
except OSError:
|
|
108
|
+
pass
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def read_skill_context(
|
|
112
|
+
session_id: str,
|
|
113
|
+
*,
|
|
114
|
+
breadcrumb_path: Optional[Path] = None,
|
|
115
|
+
staleness_seconds: float = STALENESS_TIMEOUT_SECONDS,
|
|
116
|
+
) -> Optional[str]:
|
|
117
|
+
"""Read the active skill for the current session, if any.
|
|
118
|
+
|
|
119
|
+
Returns the skill name if a fresh, session-matching breadcrumb exists.
|
|
120
|
+
Returns None if no breadcrumb, wrong session, or stale.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
session_id: Current hook session ID — must match breadcrumb
|
|
124
|
+
breadcrumb_path: Override for testing (bypasses per-session naming)
|
|
125
|
+
staleness_seconds: Max age in seconds before breadcrumb is stale
|
|
126
|
+
"""
|
|
127
|
+
path = breadcrumb_path or _breadcrumb_path_for_session(session_id)
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
if not path.exists():
|
|
131
|
+
return None
|
|
132
|
+
|
|
133
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
134
|
+
|
|
135
|
+
# Session isolation: only match same session
|
|
136
|
+
if data.get("session_id") != session_id:
|
|
137
|
+
return None
|
|
138
|
+
|
|
139
|
+
# Staleness check
|
|
140
|
+
ts = data.get("timestamp", 0)
|
|
141
|
+
if (time.time() - ts) > staleness_seconds:
|
|
142
|
+
# Expired — clean up
|
|
143
|
+
_clear_breadcrumb(path)
|
|
144
|
+
return None
|
|
145
|
+
|
|
146
|
+
return data.get("skill")
|
|
147
|
+
|
|
148
|
+
except (json.JSONDecodeError, OSError, KeyError):
|
|
149
|
+
return None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def clear_skill_breadcrumb(
|
|
153
|
+
session_id: Optional[str] = None,
|
|
154
|
+
*,
|
|
155
|
+
breadcrumb_path: Optional[Path] = None,
|
|
156
|
+
) -> None:
|
|
157
|
+
"""Clear the active skill breadcrumb.
|
|
158
|
+
|
|
159
|
+
Called on session end or UserPromptSubmit if needed.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
session_id: If provided, clears the per-session breadcrumb.
|
|
163
|
+
breadcrumb_path: Override for testing.
|
|
164
|
+
"""
|
|
165
|
+
if breadcrumb_path:
|
|
166
|
+
_clear_breadcrumb(breadcrumb_path)
|
|
167
|
+
elif session_id:
|
|
168
|
+
_clear_breadcrumb(_breadcrumb_path_for_session(session_id))
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def cleanup_orphaned_breadcrumbs(
|
|
172
|
+
*,
|
|
173
|
+
state_dir: Optional[Path] = None,
|
|
174
|
+
max_age_seconds: float = ORPHAN_CLEANUP_SECONDS,
|
|
175
|
+
) -> int:
|
|
176
|
+
"""Remove breadcrumb files older than max_age_seconds.
|
|
177
|
+
|
|
178
|
+
Called periodically to prevent accumulation of stale session files.
|
|
179
|
+
Returns the number of files cleaned up.
|
|
180
|
+
"""
|
|
181
|
+
base = state_dir or TWEEK_STATE_DIR
|
|
182
|
+
cleaned = 0
|
|
183
|
+
|
|
184
|
+
try:
|
|
185
|
+
if not base.exists():
|
|
186
|
+
return 0
|
|
187
|
+
|
|
188
|
+
now = time.time()
|
|
189
|
+
for f in base.glob("active_skill_*.json"):
|
|
190
|
+
try:
|
|
191
|
+
age = now - f.stat().st_mtime
|
|
192
|
+
if age > max_age_seconds:
|
|
193
|
+
f.unlink(missing_ok=True)
|
|
194
|
+
cleaned += 1
|
|
195
|
+
except OSError:
|
|
196
|
+
continue
|
|
197
|
+
except OSError:
|
|
198
|
+
pass
|
|
199
|
+
|
|
200
|
+
return cleaned
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _clear_breadcrumb(path: Path) -> None:
|
|
204
|
+
"""Remove the breadcrumb file silently."""
|
|
205
|
+
try:
|
|
206
|
+
path.unlink(missing_ok=True)
|
|
207
|
+
except OSError:
|
|
208
|
+
pass
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def extract_skill_from_tool_input(tool_input: dict) -> Optional[str]:
|
|
212
|
+
"""Extract the skill name from a Skill tool's tool_input.
|
|
213
|
+
|
|
214
|
+
The Skill tool sends: {"skill": "commit", "args": "..."}
|
|
215
|
+
|
|
216
|
+
Returns the skill name or None if not a valid Skill invocation.
|
|
217
|
+
"""
|
|
218
|
+
skill = tool_input.get("skill")
|
|
219
|
+
if isinstance(skill, str) and skill.strip():
|
|
220
|
+
return skill.strip()
|
|
221
|
+
return None
|
tweek/skills/scanner.py
CHANGED
|
@@ -6,7 +6,7 @@ installation. Reuses existing Tweek infrastructure where possible.
|
|
|
6
6
|
|
|
7
7
|
Layers:
|
|
8
8
|
1. Structure Validation — file types, size, depth, symlinks
|
|
9
|
-
2. Pattern Matching —
|
|
9
|
+
2. Pattern Matching — 262 regex patterns (reuses audit.py)
|
|
10
10
|
3. Secret Scanning — credential detection (reuses secret_scanner.py)
|
|
11
11
|
4. AST Analysis — forbidden imports/calls (reuses git_security.py)
|
|
12
12
|
5. Prompt Injection Scan — skill-specific instruction injection patterns
|
|
@@ -349,7 +349,7 @@ class SkillScanner:
|
|
|
349
349
|
def _scan_patterns(
|
|
350
350
|
self, skill_dir: Path, text_files: List[Path]
|
|
351
351
|
) -> ScanLayerResult:
|
|
352
|
-
"""Run
|
|
352
|
+
"""Run 262 regex patterns against all text files."""
|
|
353
353
|
result = ScanLayerResult(layer_name="patterns", passed=True)
|
|
354
354
|
|
|
355
355
|
try:
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tweek
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Defense-in-depth security for AI coding assistants - protect credentials, code, and system from prompt injection attacks
|
|
5
5
|
Author: Tommy Mancino
|
|
6
6
|
License-Expression: Apache-2.0
|
|
7
7
|
Project-URL: Homepage, https://gettweek.com
|
|
8
8
|
Project-URL: Repository, https://github.com/gettweek/tweek
|
|
9
9
|
Project-URL: Issues, https://github.com/gettweek/tweek/issues
|
|
10
|
-
Keywords: claude,security,
|
|
10
|
+
Keywords: claude,security,dry-run,ai,llm,tweek,claude-code,prompt-injection,mcp,credential-theft
|
|
11
11
|
Classifier: Development Status :: 4 - Beta
|
|
12
12
|
Classifier: Intended Audience :: Developers
|
|
13
13
|
Classifier: Operating System :: MacOS :: MacOS X
|
|
@@ -45,6 +45,7 @@ Provides-Extra: dev
|
|
|
45
45
|
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
46
46
|
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
47
47
|
Requires-Dist: pytest-xdist>=3.5.0; extra == "dev"
|
|
48
|
+
Requires-Dist: pytest-timeout>=2.2.0; extra == "dev"
|
|
48
49
|
Requires-Dist: hypothesis>=6.98.0; extra == "dev"
|
|
49
50
|
Requires-Dist: black>=23.0; extra == "dev"
|
|
50
51
|
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
@@ -124,7 +125,7 @@ tweek proxy setup # Cursor, Windsurf, Continue.dev (HTTP p
|
|
|
124
125
|
tweek doctor
|
|
125
126
|
```
|
|
126
127
|
|
|
127
|
-
That's it. Tweek auto-detects your tools, applies all
|
|
128
|
+
That's it. Tweek auto-detects your tools, applies all 262 attack patterns across 6 defense layers, and runs 100% locally. Your code never leaves your machine.
|
|
128
129
|
|
|
129
130
|
---
|
|
130
131
|
|
|
@@ -166,7 +167,7 @@ Turn 3: cat ~/.ssh/id_rsa → BLOCKED: path_escalation anomaly
|
|
|
166
167
|
|
|
167
168
|
**Response injection** — Malicious instructions hidden in tool responses are caught at ingestion.
|
|
168
169
|
|
|
169
|
-
See the full [Attack Patterns Reference](docs/ATTACK_PATTERNS.md) for all
|
|
170
|
+
See the full [Attack Patterns Reference](docs/ATTACK_PATTERNS.md) for all 262 patterns across 11 categories.
|
|
170
171
|
|
|
171
172
|
---
|
|
172
173
|
|
|
@@ -217,7 +218,7 @@ Every tool call passes through six independent screening layers. An attacker wou
|
|
|
217
218
|
|
|
218
219
|
| Layer | What It Does |
|
|
219
220
|
|-------|-------------|
|
|
220
|
-
| **1. Pattern Matching** |
|
|
221
|
+
| **1. Pattern Matching** | 262 regex signatures catch known credential theft, exfiltration, and injection attacks instantly |
|
|
221
222
|
| **2. Rate Limiting** | Detects burst attacks, automated probing, and resource theft sequences |
|
|
222
223
|
| **3. Local Prompt Injection AI** | Custom-trained AI models built specifically to classify and detect prompt injection. Run 100% on your machine — no API calls, no cloud, no latency. Small enough to be fast, accurate enough to catch what regex can't. |
|
|
223
224
|
| **4. Session Tracking** | Behavioral analysis across turns detects multi-step attacks that look innocent individually |
|
|
@@ -235,7 +236,7 @@ See [Defense Layers](docs/DEFENSE_LAYERS.md) for the deep dive and [Architecture
|
|
|
235
236
|
| [Full Feature List](docs/FEATURES.md) | Complete feature inventory |
|
|
236
237
|
| [Architecture](docs/ARCHITECTURE.md) | System design and interception layers |
|
|
237
238
|
| [Defense Layers](docs/DEFENSE_LAYERS.md) | Screening pipeline deep dive |
|
|
238
|
-
| [Attack Patterns](docs/ATTACK_PATTERNS.md) | Full
|
|
239
|
+
| [Attack Patterns](docs/ATTACK_PATTERNS.md) | Full 262-pattern library reference |
|
|
239
240
|
| [Configuration](docs/CONFIGURATION.md) | Config files, tiers, and presets |
|
|
240
241
|
| [CLI Reference](docs/CLI_REFERENCE.md) | All commands, flags, and examples |
|
|
241
242
|
| [MCP Integration](docs/MCP_INTEGRATION.md) | MCP proxy and gateway setup |
|
|
@@ -244,7 +245,7 @@ See [Defense Layers](docs/DEFENSE_LAYERS.md) for the deep dive and [Architecture
|
|
|
244
245
|
| [Credential Vault](docs/VAULT.md) | Vault setup and migration |
|
|
245
246
|
| [Plugins](docs/PLUGINS.md) | Plugin development and registry |
|
|
246
247
|
| [Logging](docs/LOGGING.md) | Event logging and audit trail |
|
|
247
|
-
| [
|
|
248
|
+
| [Dry-Run](docs/DRY_RUN.md) | Dry-run preview configuration |
|
|
248
249
|
| [Tweek vs. Claude Code](docs/COMPARISON.md) | Feature comparison with native security |
|
|
249
250
|
| [Troubleshooting](docs/TROUBLESHOOTING.md) | Common issues and fixes |
|
|
250
251
|
|