mlai-code 0.3.2 → 0.3.4
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/checkpoints.mjs +217 -0
- package/src/cli.mjs +178 -31
- package/src/cloud.mjs +2 -0
- package/src/engine.mjs +1 -1
- package/src/headless.mjs +25 -9
- package/src/home.mjs +3 -2
- package/src/host.mjs +141 -13
- package/src/ide-quick.mjs +147 -0
- package/src/ide.mjs +182 -0
- package/src/netbox.mjs +1 -1
- package/src/parity.mjs +20 -1
- package/src/relay.mjs +1 -1
- package/src/render.mjs +12 -3
- package/src/runtime.mjs +23 -1
- package/src/sandbox.mjs +19 -3
- package/src/server.mjs +46 -7
- package/src/session.mjs +53 -10
- package/src/shared-data.mjs +1 -1
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +24 -5
- package/src/tui/chat.mjs +43 -16
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +11 -7
- package/src/tui/cmd-extensibility.mjs +13 -2
- package/src/tui/commands.mjs +2 -1
- package/src/tui/extensions-local.mjs +2 -0
- package/src/tui/mcp-config.mjs +22 -4
- package/src/tui/review-cli.mjs +15 -4
- package/src/tui/snapshots.mjs +57 -16
- package/src/tui/statusline.mjs +2 -1
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/wakeups.mjs +2 -2
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +5 -4
- package/vendor/cloud-cli/mlai.mjs +184 -28
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/build_planner.py +75 -7
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/cloud_pty.py +91 -2
- package/worker/command_safety.py +80 -3
- package/worker/compaction_brief.py +186 -0
- package/worker/connected_turn.py +30 -0
- package/worker/context_manager.py +6 -0
- package/worker/conversation.py +139 -12
- package/worker/deck_render.js +11 -0
- package/worker/deck_tools.py +825 -0
- package/worker/git_sync.py +42 -17
- package/worker/goals.py +4 -0
- package/worker/image_generation.py +102 -23
- package/worker/kimi_bridge.py +580 -52
- package/worker/kimi_gates.py +74 -6
- package/worker/kimi_hook.py +213 -19
- package/worker/media_tools.py +182 -0
- package/worker/memory.py +2 -2
- package/worker/mlai_hooks.py +314 -5
- package/worker/mlai_mcp.py +49 -11
- package/worker/mlai_review/README.md +35 -0
- package/worker/mlai_review/__init__.py +1 -0
- package/worker/mlai_review/__main__.py +261 -0
- package/worker/mlai_review/_compat.py +29 -0
- package/worker/mlai_review/analysis/__init__.py +3 -0
- package/worker/mlai_review/analysis/detect.py +50 -0
- package/worker/mlai_review/analysis/main.py +128 -0
- package/worker/mlai_review/analysis/messages.py +8 -0
- package/worker/mlai_review/analysis/tools.py +194 -0
- package/worker/mlai_review/application/__init__.py +1 -0
- package/worker/mlai_review/application/asking.py +53 -0
- package/worker/mlai_review/application/challengers.py +142 -0
- package/worker/mlai_review/application/context_builder.py +194 -0
- package/worker/mlai_review/application/errors.py +17 -0
- package/worker/mlai_review/application/feedback.py +52 -0
- package/worker/mlai_review/application/matching.py +46 -0
- package/worker/mlai_review/application/messages.py +41 -0
- package/worker/mlai_review/application/ports.py +222 -0
- package/worker/mlai_review/application/prompts.py +342 -0
- package/worker/mlai_review/application/review_service.py +200 -0
- package/worker/mlai_review/application/reviewers.py +438 -0
- package/worker/mlai_review/application/streaming.py +91 -0
- package/worker/mlai_review/application/tools.py +138 -0
- package/worker/mlai_review/application/usage.py +145 -0
- package/worker/mlai_review/application/write_check.py +46 -0
- package/worker/mlai_review/contracts/__init__.py +5 -0
- package/worker/mlai_review/contracts/analysis.py +57 -0
- package/worker/mlai_review/contracts/review.py +42 -0
- package/worker/mlai_review/domain/__init__.py +4 -0
- package/worker/mlai_review/domain/changes.py +77 -0
- package/worker/mlai_review/domain/context.py +147 -0
- package/worker/mlai_review/domain/diagnostics.py +239 -0
- package/worker/mlai_review/domain/feedback.py +216 -0
- package/worker/mlai_review/domain/findings.py +505 -0
- package/worker/mlai_review/domain/lifecycle.py +85 -0
- package/worker/mlai_review/domain/partition.py +42 -0
- package/worker/mlai_review/domain/relations.py +166 -0
- package/worker/mlai_review/domain/relevance.py +202 -0
- package/worker/mlai_review/domain/report.py +99 -0
- package/worker/mlai_review/domain/structure.py +56 -0
- package/worker/mlai_review/domain/writes.py +86 -0
- package/worker/mlai_review/eval.py +221 -0
- package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
- package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
- package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
- package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
- package/worker/mlai_review/feedback_report.py +60 -0
- package/worker/mlai_review/infrastructure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
- package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
- package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/github/client.py +445 -0
- package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
- package/worker/mlai_review/infrastructure/github/http.py +31 -0
- package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
- package/worker/mlai_review/infrastructure/github/token.py +20 -0
- package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
- package/worker/mlai_review/infrastructure/local/events.py +100 -0
- package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
- package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
- package/worker/mlai_review/infrastructure/messages.py +54 -0
- package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
- package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
- package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
- package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
- package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
- package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
- package/worker/mlai_review/pipeline.py +89 -0
- package/worker/mlai_review/runtime.py +194 -0
- package/worker/mlai_review/settings.py +139 -0
- package/worker/model_stream.py +21 -7
- package/worker/offline_guard.py +1 -1
- package/worker/preview.py +62 -4
- package/worker/product_facts.py +20 -0
- package/worker/project_context.py +33 -7
- package/worker/reply_language.py +92 -10
- package/worker/repo_policy.py +5 -0
- package/worker/roles.json +199 -37
- package/worker/roles.py +271 -9
- package/worker/runner.py +366 -53
- package/worker/runtime_control.py +64 -3
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/site_check.py +279 -0
- package/worker/skill_library.py +15 -3
- package/worker/skill_pack.py +170 -16
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +4 -3
- package/worker/verification.py +156 -19
- package/worker/wakeups.py +56 -4
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Stock photos (Pixabay) and brand lookup (Brandfetch) for decks and sites.
|
|
2
|
+
The loopback gateway in image_generation.py owns the provider keys (popped from the environment before any tool
|
|
3
|
+
starts); tools call it with the run-scoped gateway token. Laptop runs have no keys: the gateway forwards to the app
|
|
4
|
+
server's /api/model/v1/media endpoint with the run token, which holds the keys and caps use per run.
|
|
5
|
+
Pixabay terms: search results are cached for 24 hours and chosen images are downloaded into the project (no
|
|
6
|
+
permanent hotlinking); the photographer credit is recorded next to the files.
|
|
7
|
+
"""
|
|
8
|
+
import hashlib, json, re, threading, time, urllib.parse, urllib.request, urllib.error
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
PIXABAY = 'https://pixabay.com/api/'
|
|
12
|
+
BRANDFETCH = 'https://api.brandfetch.io/v2/brands/'
|
|
13
|
+
CACHE_SECONDS = 24 * 3600
|
|
14
|
+
MAX_PHOTO_BYTES = 12_000_000
|
|
15
|
+
MAX_LOGO_BYTES = 3_000_000
|
|
16
|
+
PHOTO_HOSTS = ('pixabay.com', 'cdn.pixabay.com')
|
|
17
|
+
LOGO_HOSTS = ('asset.brandfetch.io', 'cdn.brandfetch.io', 'asset.brandfetch.com')
|
|
18
|
+
CAPS = {'stock': 40, 'brand': 6}
|
|
19
|
+
|
|
20
|
+
STOCK_TOOL = {'name': 'stock_photo', 'description': 'Find a real photograph on Pixabay and save it under public/stock/ (free, no credits). Use for people, places, products, teams and textures; use generate_image for bespoke art. Returns the saved path, size, photographer credit and other matches; call again with pick=<id> to use a different match.',
|
|
21
|
+
'inputSchema': {'type': 'object', 'properties': {'query': {'type': 'string', 'maxLength': 100, 'description': 'A few concrete English words, e.g. "team meeting laptop"'},
|
|
22
|
+
'filename': {'type': 'string', 'description': 'New two-word kebab filename, e.g. team-meeting'},
|
|
23
|
+
'orientation': {'type': 'string', 'enum': ['horizontal', 'vertical', 'all']}, 'pick': {'type': 'integer', 'description': 'Pixabay id from an earlier result'}},
|
|
24
|
+
'required': ['query', 'filename'], 'additionalProperties': False}}
|
|
25
|
+
BRAND_TOOL = {'name': 'brand_lookup', 'description': "Look up a company's brand by domain (e.g. stripe.com): name, colours, fonts and logos saved under public/brand/. Use before designing a deck or site for a named company, so it carries their real logo and palette.",
|
|
26
|
+
'inputSchema': {'type': 'object', 'properties': {'domain': {'type': 'string', 'maxLength': 120}}, 'required': ['domain'], 'additionalProperties': False}}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _stem(name):
|
|
30
|
+
stem = re.sub(r'[^a-z0-9-]+', '-', str(name or '').lower().rsplit('.', 1)[0]).strip('-')[:48]
|
|
31
|
+
if not stem: raise ValueError('filename is required, e.g. team-meeting')
|
|
32
|
+
return stem
|
|
33
|
+
|
|
34
|
+
def clean_domain(value):
|
|
35
|
+
raw = str(value or '').strip().lower()
|
|
36
|
+
if '://' in raw: raw = urllib.parse.urlparse(raw).hostname or ''
|
|
37
|
+
raw = raw.split('/')[0].split('?')[0].removeprefix('www.')
|
|
38
|
+
if not re.fullmatch(r'(?=.{3,120}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,24}', raw): raise ValueError('Give a company domain such as stripe.com')
|
|
39
|
+
return raw
|
|
40
|
+
|
|
41
|
+
def _get(url, headers=None, limit=3_000_000, timeout=30):
|
|
42
|
+
req = urllib.request.Request(url, headers={'User-Agent': 'ml-ai-code/1', **(headers or {})})
|
|
43
|
+
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=timeout) as r:
|
|
44
|
+
data = r.read(limit + 1)
|
|
45
|
+
if len(data) > limit: raise ValueError('Response too large')
|
|
46
|
+
return data
|
|
47
|
+
|
|
48
|
+
def _host_ok(url, hosts):
|
|
49
|
+
p = urllib.parse.urlparse(str(url or ''))
|
|
50
|
+
return p.scheme == 'https' and (p.hostname or '') in hosts
|
|
51
|
+
|
|
52
|
+
def trim_hits(data):
|
|
53
|
+
"""The fields a deck needs from a Pixabay search; nothing else leaves the provider response."""
|
|
54
|
+
out = []
|
|
55
|
+
for h in (data or {}).get('hits', [])[:12]:
|
|
56
|
+
url = h.get('largeImageURL') or h.get('webformatURL')
|
|
57
|
+
if not _host_ok(url, PHOTO_HOSTS): continue
|
|
58
|
+
out.append({'id': int(h.get('id') or 0), 'tags': str(h.get('tags') or '')[:120], 'url': url, 'width': int(h.get('imageWidth') or 0), 'height': int(h.get('imageHeight') or 0),
|
|
59
|
+
'user': str(h.get('user') or '')[:60], 'page': str(h.get('pageURL') or '')[:300]})
|
|
60
|
+
return out
|
|
61
|
+
|
|
62
|
+
def trim_brand(data):
|
|
63
|
+
"""Name, colours, fonts and logo URLs from a Brandfetch brand; logos only from Brandfetch's asset hosts."""
|
|
64
|
+
d = data or {}
|
|
65
|
+
colors = [{'hex': c.get('hex'), 'type': c.get('type'), 'brightness': c.get('brightness')} for c in d.get('colors', [])[:8] if re.fullmatch(r'#[0-9a-fA-F]{6}', str(c.get('hex') or ''))]
|
|
66
|
+
fonts = [{'name': str(f.get('name') or '')[:60], 'type': f.get('type'), 'origin': f.get('origin')} for f in d.get('fonts', [])[:4] if f.get('name')]
|
|
67
|
+
logos = []
|
|
68
|
+
for l in d.get('logos', [])[:8]:
|
|
69
|
+
for f in (l.get('formats') or [])[:6]:
|
|
70
|
+
if _host_ok(f.get('src'), LOGO_HOSTS) and f.get('format') in ('svg', 'png', 'jpeg', 'webp'):
|
|
71
|
+
logos.append({'type': l.get('type'), 'theme': l.get('theme'), 'format': f.get('format'), 'src': f.get('src'), 'width': f.get('width'), 'height': f.get('height')})
|
|
72
|
+
return {'name': str(d.get('name') or '')[:120], 'domain': str(d.get('domain') or '')[:120], 'description': str(d.get('description') or '')[:400], 'colors': colors, 'fonts': fonts, 'logos': logos}
|
|
73
|
+
|
|
74
|
+
def search_pixabay(key, query, orientation='horizontal', pick=None):
|
|
75
|
+
params = {'key': key, 'image_type': 'photo', 'safesearch': 'true', 'per_page': 12, 'order': 'popular'}
|
|
76
|
+
if pick: params['id'] = int(pick)
|
|
77
|
+
else: params.update(q=' '.join(str(query).split())[:100], orientation=orientation if orientation in ('horizontal', 'vertical') else 'all')
|
|
78
|
+
return trim_hits(json.loads(_get(PIXABAY + '?' + urllib.parse.urlencode(params))))
|
|
79
|
+
|
|
80
|
+
def fetch_brand(key, domain):
|
|
81
|
+
return trim_brand(json.loads(_get(BRANDFETCH + urllib.parse.quote(domain), {'Authorization': 'Bearer ' + key})))
|
|
82
|
+
|
|
83
|
+
class Media:
|
|
84
|
+
"""Runs inside the gateway process. `remote` is (url, token) for laptop runs; otherwise the keys are used directly."""
|
|
85
|
+
def __init__(self, root, repo, stock_key='', brand_key='', remote=None, emit=lambda *a, **kw: None):
|
|
86
|
+
self.root, self.repo = Path(root), Path(repo).resolve()
|
|
87
|
+
self.stock_key, self.brand_key, self.remote, self.emit = stock_key, brand_key, remote, emit
|
|
88
|
+
self.used = {'stock': 0, 'brand': 0}; self.lock = threading.Lock()
|
|
89
|
+
@property
|
|
90
|
+
def available(self): return bool(self.remote or self.stock_key or self.brand_key)
|
|
91
|
+
def _cache(self, kind, key, fetch):
|
|
92
|
+
path = self.root / 'media-cache.json'
|
|
93
|
+
try: cache = json.loads(path.read_text())
|
|
94
|
+
except (FileNotFoundError, ValueError): cache = {}
|
|
95
|
+
ident = kind + ':' + hashlib.sha256(json.dumps(key, sort_keys=True).encode()).hexdigest()[:24]
|
|
96
|
+
hit = cache.get(ident)
|
|
97
|
+
if hit and time.time() - hit.get('at', 0) < CACHE_SECONDS: return hit['value']
|
|
98
|
+
with self.lock:
|
|
99
|
+
if self.used[kind] >= CAPS[kind]: raise ValueError('%s limit reached (%d per task). Reuse what you found.' % ('Stock photo search' if kind == 'stock' else 'Brand lookup', CAPS[kind]))
|
|
100
|
+
self.used[kind] += 1
|
|
101
|
+
value = fetch()
|
|
102
|
+
cache = {k: v for k, v in cache.items() if time.time() - v.get('at', 0) < CACHE_SECONDS}
|
|
103
|
+
cache[ident] = {'at': time.time(), 'value': value}
|
|
104
|
+
tmp = path.with_suffix('.tmp'); tmp.write_text(json.dumps(cache)[:4_000_000]); tmp.replace(path)
|
|
105
|
+
return value
|
|
106
|
+
def _call(self, op, body, direct):
|
|
107
|
+
if self.remote:
|
|
108
|
+
url, token = self.remote
|
|
109
|
+
req = urllib.request.Request(url, data=json.dumps({'op': op, **body}).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'})
|
|
110
|
+
try:
|
|
111
|
+
with urllib.request.urlopen(req, timeout=40) as r: return json.load(r)['result']
|
|
112
|
+
except urllib.error.HTTPError as exc:
|
|
113
|
+
try: message = json.load(exc).get('error', {}).get('message')
|
|
114
|
+
except Exception: message = None
|
|
115
|
+
raise ValueError(message or 'The media service had an error') from None
|
|
116
|
+
return direct()
|
|
117
|
+
def _dir(self, name):
|
|
118
|
+
public = self.repo / 'public'; d = public / name
|
|
119
|
+
for p in (public, d):
|
|
120
|
+
if p.is_symlink(): raise ValueError('Asset directory cannot be a symlink')
|
|
121
|
+
d.mkdir(parents=True, exist_ok=True); return d
|
|
122
|
+
def stock(self, args):
|
|
123
|
+
query = ' '.join(str(args.get('query') or '').split())[:100]
|
|
124
|
+
if not query: raise ValueError('query is required')
|
|
125
|
+
stem = _stem(args.get('filename')); orientation = args.get('orientation') or 'horizontal'; pick = args.get('pick')
|
|
126
|
+
if pick is not None and (isinstance(pick, bool) or not isinstance(pick, int) or pick <= 0): raise ValueError('pick is a Pixabay id from an earlier result')
|
|
127
|
+
if not (self.remote or self.stock_key): raise ValueError('Stock photos are not configured on this server (PIXABAY_API_KEY).')
|
|
128
|
+
hits = self._cache('stock', [query, orientation, pick], lambda: self._call('stock', {'query': query, 'orientation': orientation, **({'pick': pick} if pick else {})},
|
|
129
|
+
lambda: search_pixabay(self.stock_key, query, orientation, pick)))
|
|
130
|
+
if not hits: return {'saved': None, 'note': 'No photos matched. Try simpler, concrete words.', 'matches': []}
|
|
131
|
+
chosen = next((h for h in hits if pick and h['id'] == pick), hits[0])
|
|
132
|
+
if not _host_ok(chosen['url'], PHOTO_HOSTS): raise ValueError('Unexpected image host')
|
|
133
|
+
d = self._dir('stock'); ext = '.png' if chosen['url'].lower().endswith('.png') else '.jpg'; path = d / (stem + ext)
|
|
134
|
+
if any(d.glob(stem + '.*')): raise ValueError('Asset filename already exists. Choose a new name.')
|
|
135
|
+
data = _get(chosen['url'], limit=MAX_PHOTO_BYTES, timeout=60)
|
|
136
|
+
if not (data[:3] == b'\xff\xd8\xff' or data[:8] == b'\x89PNG\r\n\x1a\n'): raise ValueError('The photo download was not an image')
|
|
137
|
+
with path.open('xb') as f: f.write(data)
|
|
138
|
+
credit = 'Photo by %s on Pixabay' % (chosen['user'] or 'a Pixabay contributor')
|
|
139
|
+
with (d / 'CREDITS.md').open('a') as f: f.write('- %s: %s (%s)\n' % (path.name, credit, chosen['page']))
|
|
140
|
+
rel = str(path.relative_to(self.repo))
|
|
141
|
+
self.emit('stock_photo', content='Saved ' + rel, path=rel, credit=credit)
|
|
142
|
+
return {'saved': {'path': rel, 'url': '/stock/' + path.name, 'width': chosen['width'], 'height': chosen['height'], 'credit': credit, 'tags': chosen['tags']},
|
|
143
|
+
'matches': [{k: h[k] for k in ('id', 'tags', 'width', 'height')} for h in hits if h is not chosen][:8]}
|
|
144
|
+
def brand(self, args):
|
|
145
|
+
domain = clean_domain(args.get('domain'))
|
|
146
|
+
if not (self.remote or self.brand_key): raise ValueError('Brand lookup is not configured on this server (BRANDFETCH_API_KEY).')
|
|
147
|
+
info = self._cache('brand', [domain], lambda: self._call('brand', {'domain': domain}, lambda: fetch_brand(self.brand_key, domain)))
|
|
148
|
+
d = self._dir('brand'); slug = re.sub(r'[^a-z0-9]+', '-', domain).strip('-'); saved = []
|
|
149
|
+
# One logo per background, SVG first; then an icon as a fallback mark. Brandfetch's theme names the logo's own
|
|
150
|
+
# tone: a "light" logo is white (for dark slides), a "dark" logo is coloured or black (for light slides).
|
|
151
|
+
order = sorted(info.get('logos', []), key=lambda l: (l['type'] != 'logo', l['format'] != 'svg'))
|
|
152
|
+
seen = set()
|
|
153
|
+
for l in order:
|
|
154
|
+
if l['type'] not in ('logo', 'icon', 'symbol'): continue
|
|
155
|
+
tag = l['type'] + {'light': '-for-dark-bg', 'dark': '-for-light-bg'}.get(l.get('theme'), '')
|
|
156
|
+
if tag in seen or len(saved) >= 4: continue
|
|
157
|
+
try: data = _get(l['src'], limit=MAX_LOGO_BYTES)
|
|
158
|
+
except Exception: continue
|
|
159
|
+
ext = {'svg': '.svg', 'png': '.png', 'jpeg': '.jpg', 'webp': '.webp'}[l['format']]
|
|
160
|
+
if ext == '.svg' and b'<svg' not in data[:2000].lower(): continue
|
|
161
|
+
path = d / ('%s-%s%s' % (slug, tag, ext)); path.write_bytes(data); seen.add(tag)
|
|
162
|
+
saved.append({'path': str(path.relative_to(self.repo)), 'type': l['type'], 'use_on': {'light': 'dark backgrounds', 'dark': 'light backgrounds'}.get(l.get('theme'), 'any background'), 'format': l['format']})
|
|
163
|
+
self.emit('brand_lookup', content='Brand for ' + domain + (': ' + info.get('name') if info.get('name') else ''))
|
|
164
|
+
return {'name': info.get('name'), 'domain': domain, 'description': info.get('description'), 'colors': info.get('colors'), 'fonts': info.get('fonts'), 'logos': saved,
|
|
165
|
+
'note': 'Pick the logo whose use_on matches the deck background (theme.logo). Use the brand colours as accent/accent2 only if text stays readable (4.5:1).'}
|
|
166
|
+
|
|
167
|
+
# Said once, plainly, so the agent stops asking (CLI bug list: four parallel calls, then more until the run was stopped).
|
|
168
|
+
NOT_CONFIGURED = 'Stock photos and brand lookup are not available in this run. Do not call stock_photo or brand_lookup again; use the images already in the project or a styled placeholder.'
|
|
169
|
+
|
|
170
|
+
def call(name, args):
|
|
171
|
+
"""Tool side (runner or the Kimi MCP server): forward to the loopback gateway, which holds the keys."""
|
|
172
|
+
import os
|
|
173
|
+
base, token = os.getenv('MINI_IMAGE_URL', ''), os.getenv('MINI_IMAGE_TOKEN', '')
|
|
174
|
+
if not base or not token: raise ValueError(NOT_CONFIGURED)
|
|
175
|
+
url = base.rsplit('/', 1)[0] + ('/stock' if name == 'stock_photo' else '/brand')
|
|
176
|
+
req = urllib.request.Request(url, data=json.dumps(args).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'})
|
|
177
|
+
try:
|
|
178
|
+
with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=120) as r: return json.load(r)['result']
|
|
179
|
+
except urllib.error.HTTPError as exc:
|
|
180
|
+
try: message = json.load(exc).get('error', 'Request failed')
|
|
181
|
+
except ValueError: message = 'Request failed'
|
|
182
|
+
raise ValueError(message) from None
|
package/worker/memory.py
CHANGED
|
@@ -81,10 +81,10 @@ class MemoryStore:
|
|
|
81
81
|
def index_text(self, limit=6000):
|
|
82
82
|
facts = self.list()
|
|
83
83
|
if not facts:
|
|
84
|
-
return ('
|
|
84
|
+
return ('Computer memory (this chat\'s computer only; the project Memory card is separate): empty. This workspace keeps durable facts across tasks in mcp__mlai__memory. When you learn something that will matter next time '
|
|
85
85
|
'(how to run or test this project, a convention, a preference the user stated, a correction they gave, where something lives), save it with '
|
|
86
86
|
'mcp__mlai__memory {"action":"save","name":"short-name","type":"project|user|feedback|reference","description":"one line","content":"the fact"}. Never store secrets.')
|
|
87
|
-
text = '
|
|
87
|
+
text = 'Computer memory (this chat\'s computer only; the project Memory card is separate; facts saved in earlier tasks; read one with mcp__mlai__memory {"action":"read","name":"<name>"}; save new durable facts with action "save", correct wrong ones by saving the same name, drop them with "forget"):\n'
|
|
88
88
|
body = self.index.read_text() if self.index.is_file() else ''
|
|
89
89
|
body = '\n'.join(l for l in body.splitlines() if l.startswith('- ') or l.startswith('## '))
|
|
90
90
|
return (text + body)[:limit]
|
package/worker/mlai_hooks.py
CHANGED
|
@@ -192,6 +192,127 @@ def outside_repo(p, repo=None):
|
|
|
192
192
|
return not (full == base or full.startswith(base.rstrip(os.sep) + os.sep))
|
|
193
193
|
|
|
194
194
|
|
|
195
|
+
# BUG-0499: on This Mac/CLI, reading outside the project asks first unless the path is under a folder the person added
|
|
196
|
+
# with --add-dir (request.json `readDirs`). A yes covers that folder for the rest of the run.
|
|
197
|
+
OUTSIDE_READ_PROGRAMS = frozenset(('cat', 'head', 'tail', 'less', 'more', 'nl', 'tac', 'grep', 'rg', 'egrep', 'fgrep', 'ag', 'ack', 'ls', 'tree', 'wc',
|
|
198
|
+
'stat', 'file', 'xxd', 'od', 'strings', 'diff', 'cmp', 'awk', 'sed', 'sort', 'uniq', 'cut', 'jq', 'yq', 'find', 'fd',
|
|
199
|
+
'md5sum', 'sha256sum', 'sha1sum', 'shasum', 'base64', 'zcat', 'du', 'cp', 'tar', 'zip', 'rsync'))
|
|
200
|
+
_OUTSIDE_OK = set()
|
|
201
|
+
# Installed software and system settings a shell command may inspect (which, ls /usr/local/bin, cat /etc/os-release).
|
|
202
|
+
SYSTEM_READ = ('/usr', '/bin', '/sbin', '/lib', '/opt', '/etc', '/nix', '/snap', '/System', '/Library', '/Applications', '/private/etc')
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _read_roots(cfg, root=None):
|
|
206
|
+
import tempfile
|
|
207
|
+
out = [os.path.realpath(str(root or ROOT)), os.path.realpath(os.path.dirname(os.path.abspath(__file__))), os.path.realpath(tempfile.gettempdir()), '/tmp', '/private/tmp', '/dev']
|
|
208
|
+
out += [os.path.realpath(os.path.expanduser(d)) for d in (cfg.get('readDirs') or []) if isinstance(d, str) and d]
|
|
209
|
+
return out + sorted(_OUTSIDE_OK)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _under(full, roots):
|
|
213
|
+
return any(full == r or full.startswith(r.rstrip(os.sep) + os.sep) for r in roots)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _command_read_paths(command):
|
|
217
|
+
"""Path-looking arguments of read programs in a shell command (absolute, ~, $HOME or ../ paths only)."""
|
|
218
|
+
out = []
|
|
219
|
+
try:
|
|
220
|
+
import command_safety as cs
|
|
221
|
+
for st in cs.split_statements(str(command or ''))[:40]:
|
|
222
|
+
try: words, redirects = cs._words(st); words, _f = cs._strip_wrappers(words)
|
|
223
|
+
except Exception: continue
|
|
224
|
+
targets = [t for kind, t in redirects if kind == 'read']
|
|
225
|
+
if words and words[0].rsplit('/', 1)[-1] in OUTSIDE_READ_PROGRAMS: targets += [w for w in words[1:] if not w.startswith('-')]
|
|
226
|
+
for w in targets:
|
|
227
|
+
w = w.replace('$HOME', '~').replace('${HOME}', '~')
|
|
228
|
+
if w.startswith(('/', '~', '../')) or w == '..': out.append(w)
|
|
229
|
+
except Exception: return []
|
|
230
|
+
return out[:20]
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def outside_read_paths(tool, command, path, cfg, root=None, repo=None):
|
|
234
|
+
"""Paths this call would read outside the project and outside every allowed folder."""
|
|
235
|
+
if command: paths = _command_read_paths(command)
|
|
236
|
+
elif canonical(tool) == 'read': paths = [path] if isinstance(path, str) else list(path or [])
|
|
237
|
+
else: return []
|
|
238
|
+
base = os.path.realpath(str(repo or REPO)); roots = [base] + _read_roots(cfg, root) + (list(SYSTEM_READ) if command else [])
|
|
239
|
+
out = []
|
|
240
|
+
for p in paths:
|
|
241
|
+
if not p: continue
|
|
242
|
+
s = os.path.expanduser(str(p))
|
|
243
|
+
full = os.path.realpath(s if os.path.isabs(s) else os.path.join(base, s))
|
|
244
|
+
if not _under(full, roots): out.append(full)
|
|
245
|
+
return out
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def read_allowed(full, cfg=None, root=None):
|
|
249
|
+
"""True when an absolute path outside the project may be read (an --add-dir folder, or approved this run)."""
|
|
250
|
+
cfg = cfg if cfg is not None else settings(root)
|
|
251
|
+
dirs = [os.path.realpath(os.path.expanduser(d)) for d in (cfg.get('readDirs') or []) if isinstance(d, str) and d]
|
|
252
|
+
return _under(os.path.realpath(str(full)), dirs + sorted(_OUTSIDE_OK))
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def outside_read_gate(tool, args, command, path, cfg, root=None, mode=None):
|
|
256
|
+
if not cfg.get('laptop') or cfg.get('restricted'): return None
|
|
257
|
+
if (cfg.get('perms') or {}).get('bypass') or (Path(root or ROOT) / 'kimi-approve-all').exists(): return None
|
|
258
|
+
outside = outside_read_paths(tool, command, path, cfg, root)
|
|
259
|
+
if not outside: return None
|
|
260
|
+
import mlai_permissions as mp
|
|
261
|
+
shown = ', '.join(clip(p, 200) for p in outside[:3])
|
|
262
|
+
if not mp.ask_user('Read outside the project: ' + shown, 'outside the project', root or ROOT,
|
|
263
|
+
reason='This reads a file outside the project folder. Start mlai with --add-dir <folder> to allow a folder without asking.'):
|
|
264
|
+
emit('policy', root, rule='outside the project', decision='deny', content='Not read: ' + shown, permission=True)
|
|
265
|
+
return ('The user did not approve reading outside the project (' + shown + '). Do not try another way to read it; '
|
|
266
|
+
'tell the user they can allow the folder with --add-dir <folder>.')
|
|
267
|
+
for p in outside: _OUTSIDE_OK.add(p)
|
|
268
|
+
return None
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
# BUG-0531: asked to fix one function, the agent created tests/test_x.py and pip-installed pytest into a new .venv.
|
|
272
|
+
# Running the project's existing tests is fine; adding test files or installing tools needs the user's say-so.
|
|
273
|
+
_INSTALL = re.compile(r'(?:^|[;&|(]\s*|\s)(?:sudo\s+)?(?:(?:python3?|py)\s+-m\s+pip|pip3?|uv\s+pip|pipx|conda|mamba|poetry|brew|apt(?:-get)?|gem|cargo|go)\s+(?:install|add)\b'
|
|
274
|
+
r'|(?:^|[;&|(]\s*|\s)(?:npm|pnpm|yarn|bun)\s+(?:install|i|add)\s+(?:-\S+\s+)*[@\w]', re.I)
|
|
275
|
+
_TEST_FILE = re.compile(r'(?:^|/)(?:tests?|__tests__|spec|e2e)/|(?:^|/)test_[^/]+\.py$|_test\.(?:py|go)$|\.(?:test|spec)\.[cm]?[jt]sx?$', re.I)
|
|
276
|
+
_ASKED_INSTALL = re.compile(r'\b(?:install|installing|dependenc(?:y|ies)|packages?|set ?up|setup|venv|virtual ?env|requirements|pip|npm i|add (?:a |the )?(?:library|lib|package|dependency))\b', re.I)
|
|
277
|
+
_ASKED_TESTS = re.compile(r'\b(?:tests?|testing|unit ?tests?|spec|specs|coverage|pytest|jest|vitest|tdd)\b', re.I)
|
|
278
|
+
_SCOPE_OK = set()
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def install_command(command):
|
|
282
|
+
return bool(command) and bool(_INSTALL.search(str(command)))
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def new_test_file(tool, path, root=None):
|
|
286
|
+
if canonical(tool) != 'edit': return None
|
|
287
|
+
for p in ([path] if isinstance(path, str) else list(path or [])):
|
|
288
|
+
if not p or not _TEST_FILE.search(str(p).replace('\\', '/')): continue
|
|
289
|
+
full = Path(p) if os.path.isabs(str(p)) else Path(REPO) / str(p)
|
|
290
|
+
if not full.exists(): return str(p)
|
|
291
|
+
return None
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def scope_gate(tool, command, path, cfg, run=None, root=None):
|
|
295
|
+
"""Laptop runs: adding a new test file or installing packages the request did not ask for needs the user first.
|
|
296
|
+
Unattended runs (auto, approve all, bypass) cannot ask, so the agent is told to ask in its answer instead."""
|
|
297
|
+
if not cfg.get('laptop') or cfg.get('restricted'): return None
|
|
298
|
+
task = str((run or {}).get('task') or _request(root or ROOT).get('task') or '')
|
|
299
|
+
what = None
|
|
300
|
+
if command and install_command(command) and not _ASKED_INSTALL.search(task): what = ('install', 'Install packages: ' + clip(command, 300))
|
|
301
|
+
elif not command:
|
|
302
|
+
f = new_test_file(tool, path, root)
|
|
303
|
+
if f and not _ASKED_TESTS.search(task): what = ('test file', 'Add a new test file: ' + clip(f, 200))
|
|
304
|
+
if not what or what[1] in _SCOPE_OK: return None
|
|
305
|
+
unattended = (run or {}).get('mode') in ('auto', 'plan') or (cfg.get('perms') or {}).get('bypass') or (Path(root or ROOT) / 'kimi-approve-all').exists()
|
|
306
|
+
stop = ('Not done: ' + what[1] + '. The user did not ask for this. You may run the tests the project already has, but do not add new '
|
|
307
|
+
'test files or install tools on your own. Finish what was asked and, in your answer, ask whether they want this.')
|
|
308
|
+
if unattended:
|
|
309
|
+
emit('policy', root, rule='not requested', decision='deny', content=what[1]); return stop
|
|
310
|
+
import mlai_permissions as mp
|
|
311
|
+
if not mp.ask_user(what[1], 'not requested', root or ROOT, reason='Not part of what you asked for. Allow the agent to do this?'):
|
|
312
|
+
emit('policy', root, rule='not requested', decision='deny', content=what[1], permission=True); return stop
|
|
313
|
+
_SCOPE_OK.add(what[1]); return None
|
|
314
|
+
|
|
315
|
+
|
|
195
316
|
def restricted_block(tool, command, path, cfg, root=None):
|
|
196
317
|
if not cfg.get('restricted'): return None
|
|
197
318
|
if command or canonical(tool) in ('shell', 'agent'):
|
|
@@ -245,7 +366,8 @@ def settings(root=None, repo=None, config=None):
|
|
|
245
366
|
'vault': {}, 'plugins': {p['id']: p for p in (ext.get('plugins') or []) if isinstance(p, dict) and isinstance(p.get('id'), str)},
|
|
246
367
|
'files': ext.get('files') if isinstance(ext.get('files'), dict) else {}, 'perms': _perms_config(ext.get('permissions')),
|
|
247
368
|
'offline': isinstance(ext.get('network'), dict) and ext['network'].get('enabled') is False,
|
|
248
|
-
'restricted': isinstance(ext.get('restricted'), dict) and ext['restricted'].get('enabled') is True
|
|
369
|
+
'restricted': isinstance(ext.get('restricted'), dict) and ext['restricted'].get('enabled') is True,
|
|
370
|
+
'readDirs': [d for d in (config.get('readDirs') or []) if isinstance(d, str)][:20] if isinstance(config.get('readDirs'), list) else []}
|
|
249
371
|
_CACHE.clear(); _CACHE[key] = out
|
|
250
372
|
return out
|
|
251
373
|
|
|
@@ -341,11 +463,33 @@ def _glob_hit(c, q):
|
|
|
341
463
|
|
|
342
464
|
|
|
343
465
|
def path_match(path, patterns, repo=None):
|
|
344
|
-
"""Glob match for repo-relative patterns (src/**, **/*.pem) and absolute or ~ patterns (~/.aws/**).
|
|
466
|
+
"""Glob match for repo-relative patterns (src/**, **/*.pem) and absolute or ~ patterns (~/.aws/**).
|
|
467
|
+
BUG-0526: a symlink alias (docs -> secret, cfg -> ~/.aws) is matched by the real path it points to as well."""
|
|
345
468
|
repo = os.path.normpath(str(repo or REPO))
|
|
346
469
|
raw = _expand(str(path or '').strip().strip('"\''))
|
|
347
470
|
if not raw: return None
|
|
348
471
|
absolute = os.path.normpath(raw if os.path.isabs(raw) else os.path.join(repo, raw))
|
|
472
|
+
hit = _path_match(absolute, patterns, repo)
|
|
473
|
+
if hit or not patterns: return hit
|
|
474
|
+
try:
|
|
475
|
+
real, real_repo = os.path.realpath(absolute), os.path.realpath(repo)
|
|
476
|
+
except (OSError, ValueError):
|
|
477
|
+
return None
|
|
478
|
+
if real == absolute and real_repo == repo: return None
|
|
479
|
+
hit = _path_match(real, patterns, real_repo)
|
|
480
|
+
if hit: return hit
|
|
481
|
+
# Expanded pattern roots can themselves be symlinks (macOS /tmp -> /private/tmp): compare against the real ones too.
|
|
482
|
+
for pat in patterns:
|
|
483
|
+
e = _expand(pat)
|
|
484
|
+
if not os.path.isabs(e): continue
|
|
485
|
+
parts = e.split('/'); i = next((k for k, x in enumerate(parts) if re.search(r'[*?\[]', x)), len(parts))
|
|
486
|
+
fixed = '/'.join(parts[:i]) or '/'
|
|
487
|
+
moved = os.path.realpath(fixed)
|
|
488
|
+
if moved != os.path.normpath(fixed) and _path_match(real, [moved + ('/' + '/'.join(parts[i:]) if i < len(parts) else '')], real_repo): return pat
|
|
489
|
+
return None
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def _path_match(absolute, patterns, repo):
|
|
349
493
|
rel = os.path.relpath(absolute, repo) if absolute == repo or absolute.startswith(repo + os.sep) else None
|
|
350
494
|
for pat in patterns:
|
|
351
495
|
q = os.path.normpath(_expand(pat)) + ('/**' if str(pat).endswith('/**') and not os.path.normpath(_expand(pat)).endswith('**') else '')
|
|
@@ -411,14 +555,124 @@ def credential_reason(command, repo=None):
|
|
|
411
555
|
return None
|
|
412
556
|
|
|
413
557
|
|
|
558
|
+
# BUG-0182: the refusal used to say "Use a different approach", and the agent did the blocked action another way
|
|
559
|
+
# (mkdir denied -> Write reports/.keep; touch denied -> printf '' > file). The rule blocks the action, not one spelling.
|
|
560
|
+
HARD_DENY_COMMAND = ('Refused by a hard-deny rule ("{pat}") that the user set in Settings > Hooks and rules. This action never runs here, in any mode, '
|
|
561
|
+
'including Auto. Do not get the same result another way (no other command, script, file write or helper agent). '
|
|
562
|
+
'Stop and tell the user this rule blocks the step; they can change the rule if they want it done.')
|
|
563
|
+
HARD_DENY_REPEAT = ('Refused: this would do what the hard-deny rule "{pat}" blocked earlier in this task ({target}). Working around a hard-deny '
|
|
564
|
+
'rule is not allowed. Stop and tell the user the rule blocks this step.')
|
|
565
|
+
DENIED_FILE = 'hard-deny-targets.json'
|
|
566
|
+
FILE_OPS = frozenset(('mkdir', 'touch', 'rm', 'rmdir', 'mv', 'cp', 'ln', 'chmod', 'chown', 'tee', 'truncate', 'install', 'unlink', 'shred', 'dd'))
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
def _deny_targets(command, pat, repo=None):
|
|
570
|
+
"""Paths a denied command was about to act on (mkdir reports -> reports), workspace-relative."""
|
|
571
|
+
repo = os.path.normpath(str(repo or REPO))
|
|
572
|
+
out = []
|
|
573
|
+
for seg in segments(command):
|
|
574
|
+
if not _cmd_match(seg, pat) and not _cmd_match(' '.join(str(command).split()), pat): continue
|
|
575
|
+
try: words = shlex.split(seg)
|
|
576
|
+
except ValueError: words = seg.split()
|
|
577
|
+
prog = words[0].rsplit('/', 1)[-1] if words else ''
|
|
578
|
+
for w in words[1:]:
|
|
579
|
+
if not w or w.startswith('-') or any(ch in w for ch in '*?[$`|<>;&'): continue
|
|
580
|
+
if prog not in FILE_OPS and '/' not in w and not re.search(r'\.[A-Za-z0-9]{1,8}$', w): continue # git push origin main: not paths
|
|
581
|
+
absolute = os.path.normpath(w if os.path.isabs(w) else os.path.join(repo, w))
|
|
582
|
+
if absolute == repo or not absolute.startswith(repo + os.sep): continue
|
|
583
|
+
rel = os.path.relpath(absolute, repo)
|
|
584
|
+
if rel not in out: out.append(rel)
|
|
585
|
+
return out[:20]
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def _run_stamp(root=None):
|
|
589
|
+
"""Changes with each new request, so a refusal from an earlier task never blocks a later one."""
|
|
590
|
+
try: return (Path(root or ROOT) / 'request.json').stat().st_mtime
|
|
591
|
+
except OSError: return 0
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def remember_denied(command, pat, root=None, repo=None):
|
|
595
|
+
if pat in ('credentials', 'network', 'git-hooks'): return
|
|
596
|
+
targets = _deny_targets(command, pat, repo)
|
|
597
|
+
if not targets: return
|
|
598
|
+
f = Path(root or ROOT) / DENIED_FILE
|
|
599
|
+
try: data = json.loads(f.read_text())
|
|
600
|
+
except (OSError, ValueError): data = []
|
|
601
|
+
if not isinstance(data, list): data = []
|
|
602
|
+
stamp = _run_stamp(root)
|
|
603
|
+
data = [d for d in data if isinstance(d, dict) and d.get('run') == stamp]
|
|
604
|
+
for t in targets:
|
|
605
|
+
if not any(isinstance(d, dict) and d.get('target') == t for d in data): data.append({'rule': str(pat)[:200], 'target': t, 'run': stamp})
|
|
606
|
+
try: f.write_text(json.dumps(data[-60:]))
|
|
607
|
+
except OSError: pass
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def denied_again(tool, command=None, paths=(), root=None, repo=None):
|
|
611
|
+
"""(rule, target) when a later call acts on a path an earlier hard-deny refusal protected, else (None, None)."""
|
|
612
|
+
try: data = json.loads((Path(root or ROOT) / DENIED_FILE).read_text())
|
|
613
|
+
except (OSError, ValueError): return None, None
|
|
614
|
+
if not isinstance(data, list) or not data: return None, None
|
|
615
|
+
repo = os.path.normpath(str(repo or REPO))
|
|
616
|
+
rels = []
|
|
617
|
+
for p in paths or []:
|
|
618
|
+
if not p: continue
|
|
619
|
+
absolute = os.path.normpath(str(p) if os.path.isabs(str(p)) else os.path.join(repo, str(p)))
|
|
620
|
+
if absolute.startswith(repo + os.sep): rels.append(os.path.relpath(absolute, repo))
|
|
621
|
+
readonly = False
|
|
622
|
+
if command:
|
|
623
|
+
try:
|
|
624
|
+
from command_safety import classify
|
|
625
|
+
readonly = classify(command, repo, cwd=repo).get('effect') in ('read', 'check')
|
|
626
|
+
except Exception: readonly = False
|
|
627
|
+
stamp = _run_stamp(root)
|
|
628
|
+
for d in data:
|
|
629
|
+
if not isinstance(d, dict) or not d.get('target') or d.get('run') != stamp: continue
|
|
630
|
+
t = str(d['target'])
|
|
631
|
+
if any(r == t or r.startswith(t + os.sep) for r in rels) and canonical(tool) != 'read': return d.get('rule'), t
|
|
632
|
+
if command and not readonly:
|
|
633
|
+
for tok in _tokens(command):
|
|
634
|
+
tok = tok.strip('\'"()')
|
|
635
|
+
if not tok: continue
|
|
636
|
+
absolute = os.path.normpath(tok if os.path.isabs(tok) else os.path.join(repo, tok))
|
|
637
|
+
rel = os.path.relpath(absolute, repo) if absolute.startswith(repo + os.sep) else None
|
|
638
|
+
if rel and (rel == t or rel.startswith(t + os.sep)): return d.get('rule'), t
|
|
639
|
+
if re.search(r'(?<![\w./-])' + re.escape(t) + r'(?![\w-])', str(command)): return d.get('rule'), t
|
|
640
|
+
return None, None
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
# BUG-0551: the project's git hooks (pre-commit lint, pre-push tests) are the team's rules. The agent never skips them
|
|
644
|
+
# (--no-verify, -n, core.hooksPath) or rewrites them; when a hook blocks, it reports that and asks the user.
|
|
645
|
+
GIT_HOOK_REASON = ('Refused: this would skip or change the project\'s git hooks (for example a pre-commit lint check). Agents never use '
|
|
646
|
+
'--no-verify or edit .git/hooks. Do not get the same result another way. Tell the user what the hook reported and ask how '
|
|
647
|
+
'they want to proceed (fix the problem, or they can commit themselves).')
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
def git_hook_reason(command):
|
|
651
|
+
for seg in segments(command):
|
|
652
|
+
try: words = shlex.split(seg)
|
|
653
|
+
except ValueError: words = seg.split()
|
|
654
|
+
if not words: continue
|
|
655
|
+
if any(re.search(r'(^|/)\.git/hooks(/|$)', w) for w in words) and (re.search(r'(^|\s)(>|>>|tee|cp|mv|rm|ln|chmod|sed\s+-i|truncate|install)\b', seg) or '>' in seg):
|
|
656
|
+
return GIT_HOOK_REASON
|
|
657
|
+
if os.path.basename(words[0]) != 'git': continue
|
|
658
|
+
if any(re.match(r'core\.hookspath\s*=', w, re.I) for w in words): return GIT_HOOK_REASON
|
|
659
|
+
sub = next((w for w in words[1:] if not w.startswith('-') and '=' not in w), '')
|
|
660
|
+
if sub in ('commit', 'push', 'merge', 'am', 'rebase', 'cherry-pick', 'revert') and '--no-verify' in words: return GIT_HOOK_REASON
|
|
661
|
+
if sub == 'commit' and any(re.fullmatch(r'-[A-Za-z]*n[A-Za-z]*', w) for w in words[words.index('commit') + 1:]): return GIT_HOOK_REASON
|
|
662
|
+
if sub == 'config' and any(w.lower() == 'core.hookspath' for w in words): return GIT_HOOK_REASON
|
|
663
|
+
return None
|
|
664
|
+
|
|
665
|
+
|
|
414
666
|
def deny_command(command, cfg=None, repo=None):
|
|
415
667
|
"""(rule, reason) when the command may never run, else (None, None)."""
|
|
416
668
|
cfg = cfg or settings(repo=repo)
|
|
417
669
|
reason = credential_reason(command, repo)
|
|
418
670
|
if reason: return 'credentials', reason
|
|
671
|
+
reason = git_hook_reason(command)
|
|
672
|
+
if reason: return 'git-hooks', reason
|
|
419
673
|
d = cfg['deny']
|
|
420
674
|
pat = match_command(command, d['commands'])
|
|
421
|
-
if pat: return pat,
|
|
675
|
+
if pat: return pat, HARD_DENY_COMMAND.format(pat=pat)
|
|
422
676
|
pat, tok = command_paths(command, d['paths'], repo)
|
|
423
677
|
if pat: return pat, 'Refused by a hard-deny rule: ' + tok + ' matches the protected path "' + pat + '", which agent commands never touch.'
|
|
424
678
|
for h in hosts_in(command):
|
|
@@ -440,6 +694,26 @@ def deny_path(path, cfg=None, repo=None):
|
|
|
440
694
|
return None, None
|
|
441
695
|
|
|
442
696
|
|
|
697
|
+
_LISTED_PATH = re.compile(r'^(?:\./)?([^\s:][^:\n]*?)(?::\d+)?(?::|$)')
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
def filter_denied_lines(text, cfg=None, repo=None):
|
|
701
|
+
"""BUG-0527: drops search/list output lines that name a hard-denied or credential file (path:line: text, or a bare
|
|
702
|
+
path), so protected text never reaches the model. Returns (text, removed_count)."""
|
|
703
|
+
if not isinstance(text, str) or not text: return text, 0
|
|
704
|
+
cfg = cfg or settings(repo=repo)
|
|
705
|
+
pats = list(cfg['deny']['paths'])
|
|
706
|
+
if not pats: return text, 0
|
|
707
|
+
keep, removed = [], 0
|
|
708
|
+
for line in text.split('\n'):
|
|
709
|
+
m = _LISTED_PATH.match(line)
|
|
710
|
+
if m and ('/' in m.group(1) or '.' in m.group(1)) and path_match(m.group(1), pats, repo):
|
|
711
|
+
removed += 1; continue
|
|
712
|
+
keep.append(line)
|
|
713
|
+
if removed: keep.append('(%d result%s from protected paths left out by a hard-deny rule.)' % (removed, '' if removed == 1 else 's'))
|
|
714
|
+
return '\n'.join(keep), removed
|
|
715
|
+
|
|
716
|
+
|
|
443
717
|
def deny_url(url, cfg=None):
|
|
444
718
|
cfg = cfg or settings()
|
|
445
719
|
try: host = urllib.parse.urlparse(str(url)).hostname or ''
|
|
@@ -671,7 +945,12 @@ def before(tool, args, run=None, command=None, path=None, cfg=None, root=None):
|
|
|
671
945
|
if blocked: return blocked
|
|
672
946
|
if command:
|
|
673
947
|
rule, reason = deny_command(command, cfg)
|
|
674
|
-
if rule:
|
|
948
|
+
if rule:
|
|
949
|
+
remember_denied(command, rule, root); deny_event(rule, reason, root, command=command); return reason
|
|
950
|
+
rule, target = denied_again(tool, command, [path] if isinstance(path, str) else list(path or []), root)
|
|
951
|
+
if rule:
|
|
952
|
+
reason = HARD_DENY_REPEAT.format(pat=rule, target=target)
|
|
953
|
+
deny_event(rule, reason, root, **({'command': command} if command else {'path': target})); return reason
|
|
675
954
|
for p in ([path] if isinstance(path, str) else list(path or [])):
|
|
676
955
|
if not p: continue
|
|
677
956
|
rule, reason = deny_path(p, cfg)
|
|
@@ -691,6 +970,8 @@ def before(tool, args, run=None, command=None, path=None, cfg=None, root=None):
|
|
|
691
970
|
r = dispatch('before_command', payload('before_command', run, tool={'name': tool, 'canonical': name}, command=command), command, (), cfg, root)
|
|
692
971
|
if r['decision'] == 'block': return block_text(r, 'before')
|
|
693
972
|
blocked = permission(tool, args, command, path, cfg, root, mode=(run or {}).get('mode'))
|
|
973
|
+
if not blocked and _PERM.get('grant') not in ('allow', 'asked', 'bypass'): blocked = outside_read_gate(tool, args, command, path, cfg, root)
|
|
974
|
+
if not blocked and _PERM.get('grant') not in ('allow', 'asked', 'bypass'): blocked = scope_gate(tool, command, path, cfg, run, root)
|
|
694
975
|
if blocked or (run or {}).get('mode') != 'auto' or _PERM.get('grant') in ('allow', 'bypass'): return blocked
|
|
695
976
|
try: import mlai_auto_review
|
|
696
977
|
except ImportError: return None
|
|
@@ -771,6 +1052,34 @@ def tool_paths(args):
|
|
|
771
1052
|
return out[:20]
|
|
772
1053
|
|
|
773
1054
|
|
|
1055
|
+
# ------------------------------------------------------------------ structured "cannot complete" outcome (BUG-0523)
|
|
1056
|
+
# The agent reports a request it decided cannot be done (no git remote, an unsupported feature, a refused action) with
|
|
1057
|
+
# the cannot_complete tool. The CLI reads cannot-complete.json after the run and exits with its own code (6).
|
|
1058
|
+
CANNOT_COMPLETE_FILE = 'cannot-complete.json'
|
|
1059
|
+
CANNOT_COMPLETE_TOOL = {'name': 'cannot_complete', 'description': (
|
|
1060
|
+
'Report that the request cannot be done as asked (for example: pushing with no git remote configured, a feature this '
|
|
1061
|
+
'product does not have, an action the user denied). Call it once, before your final answer, with a short reason the user '
|
|
1062
|
+
'can act on. Do not call it when the work is done or only partly verified.'),
|
|
1063
|
+
'inputSchema': {'type': 'object', 'properties': {'reason': {'type': 'string', 'minLength': 1, 'maxLength': 600}}, 'required': ['reason']}}
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
def record_cannot_complete(args, root=None):
|
|
1067
|
+
reason = ' '.join(str((args or {}).get('reason') or '').split())[:600] if isinstance(args, dict) else ''
|
|
1068
|
+
if not reason: raise ValueError('Give a short reason')
|
|
1069
|
+
root = Path(root or ROOT)
|
|
1070
|
+
tmp = root / (CANNOT_COMPLETE_FILE + '.tmp'); tmp.write_text(json.dumps({'reason': reason, 'time': time.time()})); tmp.replace(root / CANNOT_COMPLETE_FILE)
|
|
1071
|
+
emit('cannot_complete', root, content=reason)
|
|
1072
|
+
return {'recorded': True, 'note': 'Recorded. Now finish with a short answer that says what could not be done and why, and what the user can do instead.'}
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
# BUG-0523/BUG-0529/BUG-0531: rules for runs on the person's own computer (CLI and Mac app), both engines.
|
|
1076
|
+
LAPTOP_RULES = (
|
|
1077
|
+
'Outcome rules: (1) If the request cannot be done as asked, call cannot_complete {reason} once and then answer; never report it as done. '
|
|
1078
|
+
'(2) When the user denies an action, stop that line of work: do not retry it another way and do not start other work they did not ask '
|
|
1079
|
+
'for. Say what was not done and ask what they want next. (3) Do only what was asked. You may run the project\'s existing tests, but '
|
|
1080
|
+
'ask the user before adding new test files or installing packages or test tools (pip install, npm install, a new virtualenv) just to verify a change.')
|
|
1081
|
+
|
|
1082
|
+
|
|
774
1083
|
# ------------------------------------------------------------------ built-in runner (worker/runner.py)
|
|
775
1084
|
class _Globals:
|
|
776
1085
|
"""Attribute access over a module's globals() dict."""
|
|
@@ -932,7 +1241,7 @@ def guidance(cfg=None):
|
|
|
932
1241
|
lines.append('Project hooks run on this task: ' + names + '. When a hook blocks a call, its reason is the project\'s rule: follow it instead of working around it.')
|
|
933
1242
|
d = cfg['deny']
|
|
934
1243
|
if any(d.values()):
|
|
935
|
-
lines.append('Hard-deny rules (never allowed, in any mode): ' + '; '.join(k + ': ' + ', '.join(v[:12]) for k, v in d.items() if v)[:800] + '.')
|
|
1244
|
+
lines.append('Hard-deny rules (never allowed, in any mode): ' + '; '.join(k + ': ' + ', '.join(v[:12]) for k, v in d.items() if v)[:800] + '. Never get a blocked result another way (other commands, file writes, scripts or helper agents); stop and tell the user.')
|
|
936
1245
|
try:
|
|
937
1246
|
import mlai_permissions
|
|
938
1247
|
text = mlai_permissions.guidance(cfg.get('perms'))
|