mlai-code 0.3.2 → 0.3.3

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.
Files changed (53) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/cli.mjs +27 -7
  4. package/src/engine.mjs +1 -1
  5. package/src/home.mjs +1 -1
  6. package/src/parity.mjs +3 -1
  7. package/src/render.mjs +3 -1
  8. package/src/runtime.mjs +21 -1
  9. package/src/sandbox.mjs +19 -3
  10. package/src/session.mjs +22 -2
  11. package/src/shared-data.mjs +1 -1
  12. package/src/tui/app.mjs +22 -4
  13. package/src/tui/chat.mjs +8 -6
  14. package/src/tui/cmd-core.mjs +2 -2
  15. package/src/tui/cmd-extensibility.mjs +8 -2
  16. package/src/tui/commands.mjs +2 -1
  17. package/src/tui/statusline.mjs +2 -1
  18. package/src/wakeups.mjs +2 -2
  19. package/vendor/client-sdk/index.cjs +3 -3
  20. package/vendor/cloud-cli/mlai.mjs +169 -25
  21. package/worker/build_planner.py +75 -7
  22. package/worker/cloud_pty.py +91 -2
  23. package/worker/command_safety.py +25 -1
  24. package/worker/compaction_brief.py +186 -0
  25. package/worker/context_manager.py +6 -0
  26. package/worker/conversation.py +58 -1
  27. package/worker/deck_render.js +11 -0
  28. package/worker/deck_tools.py +825 -0
  29. package/worker/git_sync.py +38 -13
  30. package/worker/goals.py +4 -0
  31. package/worker/image_generation.py +102 -23
  32. package/worker/kimi_bridge.py +340 -32
  33. package/worker/kimi_gates.py +49 -0
  34. package/worker/kimi_hook.py +65 -12
  35. package/worker/media_tools.py +179 -0
  36. package/worker/memory.py +2 -2
  37. package/worker/mlai_hooks.py +93 -3
  38. package/worker/mlai_mcp.py +23 -5
  39. package/worker/model_stream.py +21 -7
  40. package/worker/offline_guard.py +1 -1
  41. package/worker/preview.py +47 -4
  42. package/worker/product_facts.py +18 -0
  43. package/worker/project_context.py +33 -7
  44. package/worker/reply_language.py +92 -10
  45. package/worker/roles.json +199 -37
  46. package/worker/roles.py +243 -8
  47. package/worker/runner.py +241 -36
  48. package/worker/site_check.py +279 -0
  49. package/worker/skill_library.py +15 -3
  50. package/worker/skill_pack.py +170 -16
  51. package/worker/tool_catalog.py +3 -2
  52. package/worker/verification.py +43 -11
  53. package/worker/wakeups.py +56 -4
@@ -115,6 +115,13 @@ def tester_invalid(language, output):
115
115
  if re.search(r'AssertionError|ERR_ASSERTION', output): return False
116
116
  return bool(re.search(r'ERR_MODULE_NOT_FOUND|SyntaxError|Cannot find module|ReferenceError|does not provide an export', output)) or '# fail 0' in output
117
117
 
118
+ TESTER_RETRY = '''
119
+
120
+ This is a second try. Your previous test could not run; the error is in `error`. Fix the test itself (imports, paths,
121
+ module format, a missing loader) and return the same JSON shape. Test through files and commands that run in plain
122
+ Node or Python here: for a React or other bundled UI, test the server, data and pure modules, or read the built output,
123
+ instead of importing JSX. If no test can run in this repository, return {"applicable": false, "reason": "..."}.'''
124
+
118
125
  REVIEW_SYSTEM = '''You check failing acceptance tests before they are reported to a builder. For each failing test, decide whether the test itself is right: does the task text really require this behaviour, and is the expected value correct? Recompute every expected number step by step from the task. A test is wrong if it asserts something the task does not require, or if its expected value is miscalculated.
119
126
 
120
127
  Reply with JSON only: {"genuine": ["test_name", ...], "wrong": [{"test": "test_name", "why": "..."}]}'''
@@ -172,6 +179,24 @@ def acceptance(config, st, model, root, repo, changed, event):
172
179
  event('acceptance', status='skipped', content='Acceptance check not applicable: ' + str(reason)[:300])
173
180
  return {'status': 'skipped', 'reason': str(reason)[:300]}
174
181
  code, output = run_acceptance(root, repo, spec['language'], spec['code'])
182
+ if code not in (0, 124) and tester_invalid(spec['language'], output):
183
+ # Web bug list Bug-017 and Bug-021: a test that cannot even load (JSX imported into plain Node, a wrong path)
184
+ # says nothing about the build. Give the tester its own error once and run the corrected test.
185
+ retry = json.dumps({'task': str(config.get('task', ''))[:12000], 'languages_changed': sorted(langs), 'repository_files': files,
186
+ 'previous_test': spec['code'][:20000], 'error': output[-3000:]})
187
+ try:
188
+ fixed_text, _ = model.request([{'role': 'system', 'content': TESTER_SYSTEM + TESTER_RETRY},
189
+ {'role': 'user', 'content': retry}], max_tokens=12000, allow_tools=False)
190
+ fixed = _parse(fixed_text)
191
+ except Exception:
192
+ fixed = None
193
+ if fixed and fixed.get('applicable') and fixed.get('language') == spec['language'] and isinstance(fixed.get('code'), str) and len(fixed['code']) <= 40000:
194
+ spec = {**spec, **fixed}
195
+ code, output = run_acceptance(root, repo, spec['language'], spec['code'])
196
+ elif isinstance(fixed, dict) and fixed.get('applicable') is False:
197
+ reason = str(fixed.get('reason') or 'the tester could not write a test that runs here')[:300]
198
+ event('acceptance', status='skipped', content='Acceptance check not applicable: ' + reason)
199
+ return {'status': 'skipped', 'reason': reason}
175
200
  covers = [str(c)[:200] for c in spec.get('covers') or []][:8]
176
201
  if code == 0: status = 'passed'
177
202
  elif code != 124 and tester_invalid(spec['language'], output): status = 'invalid'
@@ -291,6 +316,30 @@ def final_issues(config, st, answer, done, model, root, repo, event, allow_accep
291
316
  except Exception as exc:
292
317
  issue = 'Verification report could not complete: ' + str(exc)[:200]
293
318
  event('note', content=issue); issues.append(issue)
319
+ if allow_acceptance and changed and not st.get('inspect'):
320
+ # The running preview is the user's view of the work: a page that answers 500, or a linked page that 404s, is not
321
+ # done, whatever the final message says. Nothing listening on the preview port means there is nothing to judge.
322
+ try:
323
+ import site_check
324
+ routes = sorted(site_check.internal_links(repo, changed))
325
+ bad = site_check.live_routes(routes)
326
+ found = site_check.live_findings(bad)
327
+ if found:
328
+ event('verification_report', status='failed', reasons=[found[0]['message'].split('.')[0]], findings=found, checks=0, passed=0)
329
+ issues += [f['message'] for f in found if f['message'] not in issues]
330
+ except Exception as exc:
331
+ event('note', content='Could not check the preview routes: ' + str(exc)[:200])
332
+ if allow_acceptance and config.get('conversation_route') in (None, 'build'):
333
+ # A run must not end on "I'll now make the slides", and a presentation or PDF request must leave a file behind.
334
+ # Each note is sent once; if the agent still cannot do it, its own explanation stands.
335
+ try:
336
+ from conversation import deliverable_issue, stalled_promise, STALLED_NOTE
337
+ if 'stalled' not in done and not changed and stalled_promise(answer):
338
+ done.add('stalled'); issues.append(STALLED_NOTE)
339
+ elif 'deliverable' not in done:
340
+ note = deliverable_issue(config.get('task', ''), changed, repo)
341
+ if note: done.add('deliverable'); issues.append(note)
342
+ except Exception: pass # these hints never block a run
294
343
  if config.get('selected_design'):
295
344
  from design_director import browser_review
296
345
  from quality import fingerprint
@@ -30,7 +30,7 @@ def save(name, data):
30
30
  def event(event_type, **data):
31
31
  with (ROOT / 'events.jsonl').open('a') as f: f.write(json.dumps({'type': event_type, 'time': time.time(), **data}) + '\n')
32
32
 
33
- HOOK_KEYS = ('recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at', 'helpers', 'budget_next', 'budget_declined')
33
+ HOOK_KEYS = ('recent', 'landed', 'stuck_warned', 'gates_done', 'planned', 'plan_reviewed', 'plan_dismissed', 'brief_ok', 'brief_rounds', 'brief_nudged', 'mode', 'nudged', 'diagnosed_for', 'scope', 'scope_ok', 'approved_kinds', 'calls', 'agents', 'budget_warned', 'touched', 'policy_noted', 'policy_ok_paths', 'token_stage', 'account_stop', 'account_stop_at', 'helpers', 'budget_next', 'budget_declined', 'plan_items', 'brief_q_nudged', 'plan_nudged')
34
34
  def save_state(st):
35
35
  """The hook owns only these keys; the bridge owns kimi-state.json (static) and kimi-progress.json."""
36
36
  save('kimi-hook.json', {k: st[k] for k in HOOK_KEYS if k in st})
@@ -38,6 +38,22 @@ def save_state(st):
38
38
  def state():
39
39
  return {**load('kimi-state.json', {}), **load('kimi-progress.json', {}), **load('kimi-hook.json', {})}
40
40
 
41
+ # Tools that change the project, run code or reach outside it. Before a Brief task list is approved, and after the user
42
+ # dismissed a plan (Not now), none of them run: the agent reads, answers and waits (BUG-0102).
43
+ WORK_TOOLS = frozenset(('Write', 'Edit', 'Bash', 'Agent', 'AgentSwarm'))
44
+ WORK_MCP = frozenset(('scope', 'preview', 'browser', 'job', 'deploy', 'integration_call', 'network', 'schedule_wakeup', 'send_message', 'ios_simulator', 'generate_image', 'stock_photo', 'brand_lookup', 'deck_check', 'deck_import', 'find_image', 'find_icon', 'image_prep'))
45
+ DISMISSED_BLOCK = ('The user dismissed the plan (Not now). Do not start the work in this task: no edits, commands, images, scope '
46
+ 'requests or helper agents, and do not look for another way to start. In one or two sentences, say the plan is on hold and ask '
47
+ 'what they would like changed, then finish. The user starts the work again from the chat.')
48
+
49
+ def is_work_tool(name):
50
+ name = str(name or '')
51
+ if name in WORK_TOOLS: return True
52
+ m = re.match(r'^mcp__(\w+?)__(\w+)$', name)
53
+ if not m: return False
54
+ if m.group(1) != 'mlai': return True # connected-app and other MCP tools can change outside data
55
+ return m.group(2) in WORK_MCP
56
+
41
57
  def block(reason):
42
58
  sys.stderr.write(reason); sys.stderr.flush(); sys.exit(2)
43
59
 
@@ -45,13 +61,16 @@ def steering():
45
61
  """Consume queued user instructions (oldest first)."""
46
62
  d = ROOT / 'steering'
47
63
  if not d.is_dir(): return []
48
- out = []
64
+ out, notes = [], []
49
65
  for f in sorted(d.glob('*.json')):
50
- try: msg = json.loads(f.read_text()).get('message', '')
51
- except (OSError, ValueError): msg = ''
66
+ try: note = json.loads(f.read_text())
67
+ except (OSError, ValueError): note = {}
68
+ msg = note.get('message', '') if isinstance(note, dict) else ''
52
69
  f.unlink(missing_ok=True)
53
- if isinstance(msg, str) and msg.strip(): out.append(msg.strip()[:5000])
54
- for m in out: event('steering', content=m)
70
+ if isinstance(msg, str) and msg.strip():
71
+ out.append(msg.strip()[:5000]); notes.append({'note': f.stem, **({'source': str(note['source'])} if note.get('source') else {})})
72
+ # The note id lets the app match a delivered instruction to the message it sent.
73
+ for m, n in zip(out, notes): event('steering', content=m, **n)
55
74
  return out
56
75
 
57
76
  def platform_notes():
@@ -132,6 +151,17 @@ def brief_unlock(st):
132
151
  st['brief_ok'] = True; st['mode'] = back; st['scope_ok'] = True; save_state(st)
133
152
  event('mode', mode=back, content='Plan agreed. Building the approved list in %s mode.' % ('Auto' if back == 'auto' else 'Guided'))
134
153
 
154
+ def plan_key(text):
155
+ return ' '.join(str(text).lower().split())[:300]
156
+
157
+ def plan_added(items, st):
158
+ """Items in this task list that the user has not approved yet. Status updates of the same list add nothing."""
159
+ known = set(st.get('plan_items') or [])
160
+ return [i for i in items if plan_key(i) not in known]
161
+
162
+ def remember_plan(items, st):
163
+ st['plan_items'] = sorted(set(st.get('plan_items') or []) | {plan_key(i) for i in items})[:200]
164
+
135
165
  def plan_gate(args, st):
136
166
  """First task list of a run: optional user review (Guided mode by default, or always when the user asked).
137
167
  Brief mode: every task list is reviewed until the user approves; replies come back to the agent as feedback."""
@@ -144,7 +174,18 @@ def plan_gate(args, st):
144
174
  wants = brief or policy == 'always' or (policy == 'guided' and st.get('mode') == 'approve')
145
175
  if brief:
146
176
  if len(items) < 2: block('Brief mode: the task list needs at least two concrete items before the user can review it. Call TodoList again with the full plan.')
147
- elif not first or st.get('readonly') or st.get('plan_reviewed') or len(items) < 2 or not wants or (ROOT / 'kimi-approve-all').exists(): return
177
+ if st.get('mode') == 'brief' and not int(st.get('brief_rounds') or 0) and not st.get('brief_q_nudged') and not load('kimi-brief-questions.json', []):
178
+ # BUG-0124: Brief mode promises to clarify requirements before the task list. Ask once for the open questions.
179
+ st['brief_q_nudged'] = True; st['planned'] = False; save_state(st)
180
+ event('note', content='Brief mode: asked for clarifying questions before the task list')
181
+ block('Brief mode clarifies requirements first. Before this task list goes to the user, record one to three short questions about '
182
+ 'what is still open in the request (for example style, content, images or scope) with mcp__mlai__brief_questions '
183
+ '{"questions": [{"question": "...", "options": [{"label": "...", "detail": "..."}], "recommended": 0}]}, each with your '
184
+ 'recommended answer. Then call TodoList again with the same list. If the request truly leaves nothing open, call TodoList again unchanged.')
185
+ elif st.get('readonly') or len(items) < 2 or not wants or (ROOT / 'kimi-approve-all').exists(): return
186
+ elif not first or st.get('plan_reviewed'):
187
+ # Guided: an approved list is not asked again, but a later list that adds work asks again (BUG-0039).
188
+ if not st.get('plan_reviewed') or not plan_added(items, st): return
148
189
  st['plan_reviewed'] = True
149
190
  rounds = int(st.get('brief_rounds') or 0) + 1
150
191
  if brief: st['brief_rounds'] = rounds
@@ -173,17 +214,19 @@ def plan_gate(args, st):
173
214
  block('The user replied to your plan: "' + reply + '"\nAnswer briefly if they asked something, then call TodoList again with the revised task list (3 to 10 items) and, if anything is still open, record your questions with mcp__mlai__brief_questions {"questions": [...]} before the TodoList. Nothing is built until they approve.')
174
215
  if verdict in ('approve', 'approve_all'):
175
216
  if brief: brief_unlock(st)
217
+ remember_plan(items, st)
176
218
  st['scope_ok'] = True; save_state(st); event('plan_review', content='Plan approved'); return
177
219
  if verdict.startswith('approve:'):
178
220
  keep = {int(x) for x in verdict[8:].split(',') if x.strip().isdigit()}
179
221
  kept = [t for i, t in enumerate(items) if i in keep]; removed = [t for i, t in enumerate(items) if i not in keep]
180
222
  if kept:
181
223
  if brief: brief_unlock(st)
224
+ remember_plan(kept, st); save_state(st)
182
225
  event('plan_review', content='%d of %d tasks approved' % (len(kept), len(items)))
183
226
  block('The user approved only part of the task list.\nApproved: ' + '; '.join(kept) + '\nRemoved (keep these out of scope; do not do them): ' + '; '.join(removed) + '\nCall TodoList again with only the approved items, then build.')
184
227
  event('plan_review', content='Plan dismissed')
185
228
  st['plan_dismissed'] = True; save_state(st)
186
- block('The user dismissed this task list. Do not start the work. Briefly ask what they want changed, then finish.')
229
+ block(DISMISSED_BLOCK)
187
230
 
188
231
  def resolve(path):
189
232
  p = Path(path)
@@ -329,6 +372,9 @@ def pre(payload, st):
329
372
  args = payload.get('tool_input') or {}
330
373
  if not isinstance(args, dict): args = {}
331
374
  if (ROOT / 'cancel').exists(): block('Task stopped by user.')
375
+ if st.get('plan_dismissed') and is_work_tool(name): # before any approval card: Not now means nothing else asks or runs
376
+ event('plan_review', content='Plan on hold: ' + re.sub(r'^mcp__\w+?__', '', name) + ' was not run')
377
+ block(DISMISSED_BLOCK)
332
378
  hooked = user_hooks('kimi_pre', name, args, st) # hard-deny, credential block, before_tool / before_command hooks
333
379
  if hooked: block(hooked)
334
380
  queued = steering()
@@ -351,15 +397,19 @@ def pre(payload, st):
351
397
  save_state(st)
352
398
  if ACCEPTANCE in json.dumps(args) or HIDDEN.search(json.dumps(args)): block('That path holds the independent acceptance check, which is hidden from the builder by design. Work from the task and the failure output instead.')
353
399
  if name == 'TodoList': return plan_gate(args, st)
354
- writes = name in ('Write', 'Edit', 'Bash', 'Agent', 'AgentSwarm')
355
- if writes and st.get('plan_dismissed'): block('The user dismissed the task list, so no work may start. Ask what they want changed and finish.')
356
- if briefing(st) and name in ('Write', 'Edit', 'Agent', 'AgentSwarm'):
400
+ if briefing(st) and (name in ('Write', 'Edit', 'Agent', 'AgentSwarm') or (name.startswith('mcp__') and is_work_tool(name))):
357
401
  block('Brief mode: nothing is built until the user approves your task list. Read the repository, then call TodoList with the proposed items and state your open questions. This call was not run.')
358
402
  if st.get('inspect') and name in ('Write', 'Edit', 'Agent', 'AgentSwarm'):
359
403
  event('safety', effect='write', decision='deny', command=name + ' ' + str(args.get('path', ''))[:300], content='Refused: the user asked for no changes in this turn')
360
404
  block(INSPECT_BLOCK)
361
405
  if name in ('Write', 'Edit') and not st.get('readonly'):
362
406
  nudged = set(st.get('nudged') or [])
407
+ if (st.get('plan_first') and st.get('mode', 'approve') == 'approve' and not st.get('planned') and not st.get('plan_reviewed')
408
+ and 'guided_plan' not in nudged and not (ROOT / 'kimi-approve-all').exists()):
409
+ nudged.add('guided_plan'); st['nudged'] = sorted(nudged); save_state(st)
410
+ event('note', content='Guided mode: asked for the plan before the first edit')
411
+ block('Guided mode: the user approves your plan before any file changes. Call TodoList with 2 to 6 short, concrete items '
412
+ '(what you will change and how you will check it), then wait for the review. This edit was not applied; re-issue it after the plan is approved.')
363
413
  if st.get('large') and not st.get('planned') and 'plan' not in nudged:
364
414
  nudged.add('plan'); st['nudged'] = sorted(nudged); save_state(st)
365
415
  event('note', content='Large task: asked for a plan before editing')
@@ -511,8 +561,10 @@ def command_gate(command, st, mode, auto):
511
561
  if effect not in ('read', 'check'): event('safety', effect=effect, decision='allow', command=command[:300], content=describe(decision))
512
562
  return
513
563
  request = {'id': os.urandom(8).hex(), 'kind': 'command', 'effect': effect, 'reason': decision.get('reason') or '', 'command': command[:20000]}
564
+ # Deletes and sudo ask every time, even after Approve all (BUG-0101). The card says so and offers no Approve all for them.
565
+ if effect in ('destructive', 'privilege'): request['strict'] = True
514
566
  event('safety', effect=effect, decision='ask', command=command[:300], content=describe(decision))
515
- wait_decision(request)
567
+ wait_decision(request, strict=effect in ('destructive', 'privilege')) # "Approve all" never covers deletes or sudo (BUG-0101)
516
568
  if effect in ('install', 'service'):
517
569
  kinds = sorted(set(st.get('approved_kinds') or []) | {effect}); st['approved_kinds'] = kinds; save_state(st)
518
570
  elif effect in ('write', 'exec') and not st.get('readonly'):
@@ -580,6 +632,7 @@ def stop(payload, st):
580
632
  queued = steering()
581
633
  if queued: steer_block(queued, stopping=True)
582
634
  if st.get('readonly') or st.get('inspect'): return readonly_stop(st)
635
+ if st.get('plan_dismissed'): return # Not now: the run ends here; no done gate may send the agent back to work (BUG-0102)
583
636
  if briefing(st) and not st.get('planned') and not st.get('brief_nudged'):
584
637
  st['brief_nudged'] = True; save_state(st)
585
638
  event('done_gate', content='Brief mode: asked for the task list before finishing')
@@ -0,0 +1,179 @@
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
+ def call(name, args):
168
+ """Tool side (runner or the Kimi MCP server): forward to the loopback gateway, which holds the keys."""
169
+ import os
170
+ base, token = os.getenv('MINI_IMAGE_URL', ''), os.getenv('MINI_IMAGE_TOKEN', '')
171
+ if not base or not token: raise ValueError('Stock photos and brand lookup are not configured on this server.')
172
+ url = base.rsplit('/', 1)[0] + ('/stock' if name == 'stock_photo' else '/brand')
173
+ req = urllib.request.Request(url, data=json.dumps(args).encode(), headers={'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'})
174
+ try:
175
+ with urllib.request.build_opener(urllib.request.ProxyHandler({})).open(req, timeout=120) as r: return json.load(r)['result']
176
+ except urllib.error.HTTPError as exc:
177
+ try: message = json.load(exc).get('error', 'Request failed')
178
+ except ValueError: message = 'Request failed'
179
+ 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 ('Project memory: empty. This workspace keeps durable facts across tasks in mcp__mlai__memory. When you learn something that will matter next time '
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 = 'Project memory (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'
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]
@@ -411,6 +411,91 @@ def credential_reason(command, repo=None):
411
411
  return None
412
412
 
413
413
 
414
+ # BUG-0182: the refusal used to say "Use a different approach", and the agent did the blocked action another way
415
+ # (mkdir denied -> Write reports/.keep; touch denied -> printf '' > file). The rule blocks the action, not one spelling.
416
+ 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, '
417
+ 'including Auto. Do not get the same result another way (no other command, script, file write or helper agent). '
418
+ 'Stop and tell the user this rule blocks the step; they can change the rule if they want it done.')
419
+ HARD_DENY_REPEAT = ('Refused: this would do what the hard-deny rule "{pat}" blocked earlier in this task ({target}). Working around a hard-deny '
420
+ 'rule is not allowed. Stop and tell the user the rule blocks this step.')
421
+ DENIED_FILE = 'hard-deny-targets.json'
422
+ FILE_OPS = frozenset(('mkdir', 'touch', 'rm', 'rmdir', 'mv', 'cp', 'ln', 'chmod', 'chown', 'tee', 'truncate', 'install', 'unlink', 'shred', 'dd'))
423
+
424
+
425
+ def _deny_targets(command, pat, repo=None):
426
+ """Paths a denied command was about to act on (mkdir reports -> reports), workspace-relative."""
427
+ repo = os.path.normpath(str(repo or REPO))
428
+ out = []
429
+ for seg in segments(command):
430
+ if not _cmd_match(seg, pat) and not _cmd_match(' '.join(str(command).split()), pat): continue
431
+ try: words = shlex.split(seg)
432
+ except ValueError: words = seg.split()
433
+ prog = words[0].rsplit('/', 1)[-1] if words else ''
434
+ for w in words[1:]:
435
+ if not w or w.startswith('-') or any(ch in w for ch in '*?[$`|<>;&'): continue
436
+ 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
437
+ absolute = os.path.normpath(w if os.path.isabs(w) else os.path.join(repo, w))
438
+ if absolute == repo or not absolute.startswith(repo + os.sep): continue
439
+ rel = os.path.relpath(absolute, repo)
440
+ if rel not in out: out.append(rel)
441
+ return out[:20]
442
+
443
+
444
+ def _run_stamp(root=None):
445
+ """Changes with each new request, so a refusal from an earlier task never blocks a later one."""
446
+ try: return (Path(root or ROOT) / 'request.json').stat().st_mtime
447
+ except OSError: return 0
448
+
449
+
450
+ def remember_denied(command, pat, root=None, repo=None):
451
+ if pat in ('credentials', 'network'): return
452
+ targets = _deny_targets(command, pat, repo)
453
+ if not targets: return
454
+ f = Path(root or ROOT) / DENIED_FILE
455
+ try: data = json.loads(f.read_text())
456
+ except (OSError, ValueError): data = []
457
+ if not isinstance(data, list): data = []
458
+ stamp = _run_stamp(root)
459
+ data = [d for d in data if isinstance(d, dict) and d.get('run') == stamp]
460
+ for t in targets:
461
+ 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})
462
+ try: f.write_text(json.dumps(data[-60:]))
463
+ except OSError: pass
464
+
465
+
466
+ def denied_again(tool, command=None, paths=(), root=None, repo=None):
467
+ """(rule, target) when a later call acts on a path an earlier hard-deny refusal protected, else (None, None)."""
468
+ try: data = json.loads((Path(root or ROOT) / DENIED_FILE).read_text())
469
+ except (OSError, ValueError): return None, None
470
+ if not isinstance(data, list) or not data: return None, None
471
+ repo = os.path.normpath(str(repo or REPO))
472
+ rels = []
473
+ for p in paths or []:
474
+ if not p: continue
475
+ absolute = os.path.normpath(str(p) if os.path.isabs(str(p)) else os.path.join(repo, str(p)))
476
+ if absolute.startswith(repo + os.sep): rels.append(os.path.relpath(absolute, repo))
477
+ readonly = False
478
+ if command:
479
+ try:
480
+ from command_safety import classify
481
+ readonly = classify(command, repo, cwd=repo).get('effect') in ('read', 'check')
482
+ except Exception: readonly = False
483
+ stamp = _run_stamp(root)
484
+ for d in data:
485
+ if not isinstance(d, dict) or not d.get('target') or d.get('run') != stamp: continue
486
+ t = str(d['target'])
487
+ if any(r == t or r.startswith(t + os.sep) for r in rels) and canonical(tool) != 'read': return d.get('rule'), t
488
+ if command and not readonly:
489
+ for tok in _tokens(command):
490
+ tok = tok.strip('\'"()')
491
+ if not tok: continue
492
+ absolute = os.path.normpath(tok if os.path.isabs(tok) else os.path.join(repo, tok))
493
+ rel = os.path.relpath(absolute, repo) if absolute.startswith(repo + os.sep) else None
494
+ if rel and (rel == t or rel.startswith(t + os.sep)): return d.get('rule'), t
495
+ if re.search(r'(?<![\w./-])' + re.escape(t) + r'(?![\w-])', str(command)): return d.get('rule'), t
496
+ return None, None
497
+
498
+
414
499
  def deny_command(command, cfg=None, repo=None):
415
500
  """(rule, reason) when the command may never run, else (None, None)."""
416
501
  cfg = cfg or settings(repo=repo)
@@ -418,7 +503,7 @@ def deny_command(command, cfg=None, repo=None):
418
503
  if reason: return 'credentials', reason
419
504
  d = cfg['deny']
420
505
  pat = match_command(command, d['commands'])
421
- if pat: return pat, 'Refused by a hard-deny rule ("' + pat + '"). This command never runs here, in any mode, including Auto. Use a different approach or ask the user.'
506
+ if pat: return pat, HARD_DENY_COMMAND.format(pat=pat)
422
507
  pat, tok = command_paths(command, d['paths'], repo)
423
508
  if pat: return pat, 'Refused by a hard-deny rule: ' + tok + ' matches the protected path "' + pat + '", which agent commands never touch.'
424
509
  for h in hosts_in(command):
@@ -671,7 +756,12 @@ def before(tool, args, run=None, command=None, path=None, cfg=None, root=None):
671
756
  if blocked: return blocked
672
757
  if command:
673
758
  rule, reason = deny_command(command, cfg)
674
- if rule: deny_event(rule, reason, root, command=command); return reason
759
+ if rule:
760
+ remember_denied(command, rule, root); deny_event(rule, reason, root, command=command); return reason
761
+ rule, target = denied_again(tool, command, [path] if isinstance(path, str) else list(path or []), root)
762
+ if rule:
763
+ reason = HARD_DENY_REPEAT.format(pat=rule, target=target)
764
+ deny_event(rule, reason, root, **({'command': command} if command else {'path': target})); return reason
675
765
  for p in ([path] if isinstance(path, str) else list(path or [])):
676
766
  if not p: continue
677
767
  rule, reason = deny_path(p, cfg)
@@ -932,7 +1022,7 @@ def guidance(cfg=None):
932
1022
  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
1023
  d = cfg['deny']
934
1024
  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] + '.')
1025
+ 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
1026
  try:
937
1027
  import mlai_permissions
938
1028
  text = mlai_permissions.guidance(cfg.get('perms'))