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
package/worker/checkpoints.py
CHANGED
|
@@ -27,16 +27,79 @@ def tree_of_worktree(repo):
|
|
|
27
27
|
_git(repo, 'read-tree', 'HEAD', env=env)
|
|
28
28
|
except RuntimeError:
|
|
29
29
|
pass # no commits yet
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
# BUG-0540/0541/0552: only tracked changes plus small, non-secret untracked files go into the checkpoint, so
|
|
31
|
+
# a never-committed .env, data dumps, node_modules or videos are not copied into the user's .git, and an
|
|
32
|
+
# empty nested repository (a fresh `git init` in backend/) is skipped instead of having its .git moved.
|
|
33
|
+
_git(repo, 'add', '-u', env=env, timeout=60)
|
|
34
|
+
keep = untracked_to_snapshot(repo)
|
|
35
|
+
if keep:
|
|
36
|
+
subprocess.run(['git', 'add', '-A', '--pathspec-from-file=-', '--pathspec-file-nul'], cwd=repo, input='\0'.join(keep),
|
|
37
|
+
capture_output=True, text=True, timeout=60, env={**os.environ, **IDENT, **env, 'GIT_LITERAL_PATHSPECS': '1'}, check=False)
|
|
38
38
|
return _git(repo, 'write-tree', env=env)
|
|
39
39
|
|
|
40
|
+
UNTRACKED_LIMIT = 2 * 1024 * 1024
|
|
41
|
+
HEAVY_DIR = re.compile(r'(^|/)(node_modules|\.venv|venv|__pycache__|\.next|\.turbo|\.cache|target|dist|build)/')
|
|
42
|
+
SECRET_NAME = re.compile(r'(^|/)(\.env(\.(?!example$|sample$|template$|dist$)[^/]*)?|\.netrc|\.npmrc|\.pypirc|id_(rsa|dsa|ecdsa|ed25519)[^/]*|credentials(\.json)?|[^/]*\.(pem|key|p12|pfx|keystore|jks))$', re.I)
|
|
43
|
+
|
|
44
|
+
def _has_commit(path):
|
|
45
|
+
r = subprocess.run(['git', 'rev-parse', '-q', '--verify', 'HEAD'], cwd=path, capture_output=True, text=True, timeout=15,
|
|
46
|
+
env={**os.environ, 'GIT_DIR': os.path.join(path, '.git'), 'GIT_WORK_TREE': path})
|
|
47
|
+
return r.returncode == 0
|
|
48
|
+
|
|
49
|
+
def untracked_to_snapshot(repo):
|
|
50
|
+
"""Untracked (not ignored) paths that are safe to copy into a checkpoint: no secrets, no large files, no
|
|
51
|
+
dependency/build folders, no empty nested repositories."""
|
|
52
|
+
out = subprocess.run(['git', 'ls-files', '-z', '-o', '--exclude-standard', '--directory', '--no-empty-directory'], cwd=repo,
|
|
53
|
+
capture_output=True, text=True, timeout=60, env={**os.environ, **IDENT})
|
|
54
|
+
if out.returncode:
|
|
55
|
+
return []
|
|
56
|
+
try:
|
|
57
|
+
import secret_scan
|
|
58
|
+
except ImportError:
|
|
59
|
+
secret_scan = None
|
|
60
|
+
keep = []
|
|
61
|
+
for rel in [p for p in out.stdout.split('\0') if p]:
|
|
62
|
+
if rel.endswith('/'):
|
|
63
|
+
# A whole untracked folder: a nested repository (kept only when it has commits, as a gitlink) or a plain folder.
|
|
64
|
+
full = os.path.join(repo, rel)
|
|
65
|
+
if os.path.isdir(os.path.join(full, '.git')):
|
|
66
|
+
if _has_commit(full.rstrip('/')):
|
|
67
|
+
keep.append(rel.rstrip('/'))
|
|
68
|
+
continue
|
|
69
|
+
if HEAVY_DIR.search(rel):
|
|
70
|
+
continue
|
|
71
|
+
for dp, dirs, files in os.walk(full):
|
|
72
|
+
dirs[:] = [d for d in dirs if not HEAVY_DIR.search(d + '/') and not os.path.isdir(os.path.join(dp, d, '.git'))]
|
|
73
|
+
for f in files:
|
|
74
|
+
sub = os.path.relpath(os.path.join(dp, f), repo).replace(os.sep, '/')
|
|
75
|
+
if _safe_file(repo, sub, secret_scan):
|
|
76
|
+
keep.append(sub)
|
|
77
|
+
if len(keep) > 5000:
|
|
78
|
+
break
|
|
79
|
+
continue
|
|
80
|
+
if not HEAVY_DIR.search(rel) and _safe_file(repo, rel, secret_scan):
|
|
81
|
+
keep.append(rel)
|
|
82
|
+
return keep
|
|
83
|
+
|
|
84
|
+
def _safe_file(repo, rel, secret_scan):
|
|
85
|
+
if SECRET_NAME.search(rel):
|
|
86
|
+
return False
|
|
87
|
+
full = os.path.join(repo, rel)
|
|
88
|
+
try:
|
|
89
|
+
st = os.lstat(full)
|
|
90
|
+
except OSError:
|
|
91
|
+
return False
|
|
92
|
+
if st.st_size > UNTRACKED_LIMIT:
|
|
93
|
+
return False
|
|
94
|
+
if secret_scan and st.st_size and os.path.isfile(full) and not os.path.islink(full):
|
|
95
|
+
try:
|
|
96
|
+
with open(full, 'r', encoding='utf-8') as fh:
|
|
97
|
+
if secret_scan.scan_text(fh.read()):
|
|
98
|
+
return False
|
|
99
|
+
except (UnicodeDecodeError, OSError):
|
|
100
|
+
pass
|
|
101
|
+
return True
|
|
102
|
+
|
|
40
103
|
def head(repo):
|
|
41
104
|
try:
|
|
42
105
|
return _git(repo, 'rev-parse', '--verify', '-q', 'HEAD')
|
package/worker/cloud_pty.py
CHANGED
|
@@ -10,9 +10,12 @@ when it is closed, or after IDLE seconds with nobody reading it.
|
|
|
10
10
|
cloud_pty.py rename <id> <title>
|
|
11
11
|
cloud_pty.py close <id>
|
|
12
12
|
cloud_pty.py list
|
|
13
|
-
|
|
13
|
+
cloud_pty.py attach <id> <offset>
|
|
14
|
+
Every command prints one JSON object, except attach: it runs on the terminal the browser holds over a WebSocket
|
|
15
|
+
(Sandbox.openInteractive, lib/cloud-pty.ts ptySocketStart) and copies keys into the tab and its output back, byte for
|
|
16
|
+
byte, until the shell ends or the socket closes. The tab itself stays with its daemon, so it survives a reload.
|
|
14
17
|
"""
|
|
15
|
-
import base64, errno, fcntl, json, os, pty, re, select, shutil, signal, struct, sys, termios, time
|
|
18
|
+
import base64, errno, fcntl, json, os, pty, re, select, shutil, signal, struct, sys, termios, time, tty
|
|
16
19
|
|
|
17
20
|
BASE = os.environ.get('MLAI_PTY_DIR') or os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pty')
|
|
18
21
|
ID = re.compile(r'^t[a-z0-9]{6,24}$')
|
|
@@ -289,6 +292,90 @@ def cmd_io(i, offset, wait_ms, data_b64='', cols=None, rows=None):
|
|
|
289
292
|
'exited': caught_up, 'code': int(code) if caught_up and code and code.lstrip('-').isdigit() else None}
|
|
290
293
|
|
|
291
294
|
|
|
295
|
+
def cmd_attach(i, offset):
|
|
296
|
+
"""Stream one tab over this process's own terminal: stdin to the tab's fifo, its output file to stdout.
|
|
297
|
+
|
|
298
|
+
Exit status: 0 when the shell ended and everything it printed was sent, 3 when the tab is gone."""
|
|
299
|
+
d = path(i)
|
|
300
|
+
if not os.path.isdir(d) or not (live(i) or os.path.exists(path(i, 'exit'))):
|
|
301
|
+
return 3
|
|
302
|
+
term = os.isatty(0)
|
|
303
|
+
if term:
|
|
304
|
+
tty.setraw(0, termios.TCSANOW) # keys and output pass through untouched; the tab's own pty does the line editing
|
|
305
|
+
|
|
306
|
+
def push_size(*_):
|
|
307
|
+
try:
|
|
308
|
+
rows, cols = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, b'\0' * 8))[:2]
|
|
309
|
+
if cols and rows:
|
|
310
|
+
write(path(i, 'size'), '%d %d' % (clamp(cols, 10, 500, 100), clamp(rows, 4, 200, 24)))
|
|
311
|
+
except OSError:
|
|
312
|
+
pass
|
|
313
|
+
if term:
|
|
314
|
+
signal.signal(signal.SIGWINCH, push_size)
|
|
315
|
+
push_size()
|
|
316
|
+
out_file, seen_file = path(i, 'out'), path(i, 'seen')
|
|
317
|
+
pos, mark, seen_at, busy_at = max(0, int(offset)), None, 0.0, time.time()
|
|
318
|
+
pending = b''
|
|
319
|
+
while True:
|
|
320
|
+
now = time.time()
|
|
321
|
+
if now - seen_at > 20:
|
|
322
|
+
seen_at = now
|
|
323
|
+
try:
|
|
324
|
+
with open(seen_file, 'a'):
|
|
325
|
+
os.utime(seen_file, None)
|
|
326
|
+
except OSError:
|
|
327
|
+
return 3
|
|
328
|
+
# Short waits while someone is typing or output is flowing, longer ones when the tab is quiet.
|
|
329
|
+
timeout = 0.004 if now - busy_at < 2 else 0.04
|
|
330
|
+
try:
|
|
331
|
+
r = select.select([0], [], [], timeout)[0]
|
|
332
|
+
except InterruptedError:
|
|
333
|
+
r = []
|
|
334
|
+
if r:
|
|
335
|
+
try:
|
|
336
|
+
data = os.read(0, 65536)
|
|
337
|
+
except OSError:
|
|
338
|
+
data = b''
|
|
339
|
+
if not data:
|
|
340
|
+
return 0 # the socket closed; the tab keeps running
|
|
341
|
+
pending += data
|
|
342
|
+
busy_at = now
|
|
343
|
+
if pending and live(i):
|
|
344
|
+
try:
|
|
345
|
+
fd = os.open(path(i, 'in'), os.O_WRONLY | os.O_NONBLOCK)
|
|
346
|
+
try:
|
|
347
|
+
n = os.write(fd, pending)
|
|
348
|
+
pending = pending[n:]
|
|
349
|
+
finally:
|
|
350
|
+
os.close(fd)
|
|
351
|
+
except BlockingIOError:
|
|
352
|
+
pass
|
|
353
|
+
except OSError:
|
|
354
|
+
pending = b''
|
|
355
|
+
try:
|
|
356
|
+
st = os.stat(out_file)
|
|
357
|
+
key = (st.st_size, st.st_mtime_ns)
|
|
358
|
+
except OSError:
|
|
359
|
+
return 3
|
|
360
|
+
exited = os.path.exists(path(i, 'exit'))
|
|
361
|
+
if key != mark or exited:
|
|
362
|
+
start, end, data, _ = snapshot(i, pos)
|
|
363
|
+
if data:
|
|
364
|
+
view = memoryview(data)
|
|
365
|
+
while view:
|
|
366
|
+
try:
|
|
367
|
+
view = view[os.write(1, view):]
|
|
368
|
+
except InterruptedError:
|
|
369
|
+
pass
|
|
370
|
+
pos = end
|
|
371
|
+
busy_at = time.time()
|
|
372
|
+
if len(data) >= READ_MAX:
|
|
373
|
+
continue # more to send; read again before checking the file
|
|
374
|
+
mark = key
|
|
375
|
+
if exited and not snapshot(i, pos)[2]:
|
|
376
|
+
return 0
|
|
377
|
+
|
|
378
|
+
|
|
292
379
|
def cmd_close(i):
|
|
293
380
|
d, c = pids(i)
|
|
294
381
|
for p, sig in ((c, signal.SIGHUP), (d, signal.SIGTERM)):
|
|
@@ -340,6 +427,8 @@ def main(argv):
|
|
|
340
427
|
r = cmd_close(argv[1])
|
|
341
428
|
elif op == 'list':
|
|
342
429
|
r = cmd_list()
|
|
430
|
+
elif op == 'attach':
|
|
431
|
+
sys.exit(cmd_attach(argv[1], int(argv[2]) if len(argv) > 2 else 0))
|
|
343
432
|
else:
|
|
344
433
|
raise ValueError('Unknown terminal operation')
|
|
345
434
|
except (ValueError, IndexError, OSError) as e:
|
package/worker/command_safety.py
CHANGED
|
@@ -257,6 +257,38 @@ def _classify_paths(paths, repo, cwd, home):
|
|
|
257
257
|
REGENERABLE = {'__pycache__', '.pytest_cache', '.mypy_cache', '.ruff_cache', '.tox', '.nox', '.cache', 'node_modules', '.next', '.nuxt', '.svelte-kit', '.turbo', '.parcel-cache', 'dist', 'build', 'out', 'target', 'coverage', '.coverage', 'htmlcov', '.eggs', 'tmp', '.tmp', '.venv', 'venv', '.gradle', 'bin', 'obj', 'storybook-static', '.angular', '.vite'}
|
|
258
258
|
REGENERABLE_SUFFIX = ('.pyc', '.pyo', '.log', '.tsbuildinfo', '.egg-info', '.o', '.class')
|
|
259
259
|
|
|
260
|
+
def _kept_files(targets, repo, cwd):
|
|
261
|
+
"""Existing workspace files the project keeps (tracked by git, or any existing file when there is no git repo).
|
|
262
|
+
Deleting them is a real delete that Auto must ask about (BUG-0101); files the agent just made and never committed are not."""
|
|
263
|
+
import subprocess
|
|
264
|
+
base = cwd or repo
|
|
265
|
+
found = [t for t in targets if os.path.lexists(t if os.path.isabs(t) else os.path.join(base, t))]
|
|
266
|
+
if not found or not repo: return []
|
|
267
|
+
if not os.path.exists(os.path.join(repo, '.git')): return found
|
|
268
|
+
try:
|
|
269
|
+
out = subprocess.run(['git', '-C', base, 'ls-files', '--', *found], capture_output=True, text=True, timeout=5).stdout
|
|
270
|
+
except (OSError, subprocess.SubprocessError): return found
|
|
271
|
+
tracked = set(out.split('\n'))
|
|
272
|
+
rel = lambda t: os.path.relpath(t if os.path.isabs(t) else os.path.join(base, t), base)
|
|
273
|
+
started = _task_started()
|
|
274
|
+
def made_this_task(t):
|
|
275
|
+
# BUG-0596: an uncommitted file that existed before this task (the user's, or an earlier turn's) is a real delete
|
|
276
|
+
# too; only files created or written during this task are scratch. Unknown task start: ask.
|
|
277
|
+
try: return started is not None and os.lstat(t if os.path.isabs(t) else os.path.join(base, t)).st_mtime >= started - 1
|
|
278
|
+
except OSError: return False
|
|
279
|
+
return [t for t in found if rel(t) in tracked or any(x.startswith(rel(t).rstrip('/') + '/') for x in tracked) or not made_this_task(t)]
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _task_started():
|
|
283
|
+
"""Start time of the current task (status.json written by the bridge when the run starts), or None."""
|
|
284
|
+
import json
|
|
285
|
+
try:
|
|
286
|
+
with open(os.path.join(os.getenv('MINI_ROOT', '/vercel/sandbox/.mini'), 'status.json'), encoding='utf-8') as f:
|
|
287
|
+
v = json.load(f).get('started')
|
|
288
|
+
except (OSError, ValueError, AttributeError): return None
|
|
289
|
+
return float(v) if isinstance(v, (int, float)) else None
|
|
290
|
+
|
|
291
|
+
|
|
260
292
|
def _regenerable(target):
|
|
261
293
|
"""A build or cache artefact the toolchain recreates: deleting it is routine, not destructive."""
|
|
262
294
|
t = target.rstrip('/')
|
|
@@ -307,6 +339,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
307
339
|
if all(c == 'tmp' for c in classes) and 'xargs' not in flags: return 'write', 'removes scratch files', targets
|
|
308
340
|
if 'xargs' not in flags and prog == 'rm' and all(_regenerable(t) for t in targets): return 'write', 'removes build or cache output (' + ', '.join(targets[:3]) + ')', targets
|
|
309
341
|
if recursive or 'xargs' in flags or any('*' in t for t in targets) or prog == 'shred': return 'destructive', 'recursive or wildcard delete inside the workspace (' + ', '.join(targets[:3]) + ')', targets
|
|
342
|
+
kept = _kept_files(targets, repo, cwd)
|
|
343
|
+
if kept: return 'destructive', 'deletes project files (' + ', '.join(kept[:3]) + ')', targets
|
|
310
344
|
return 'write', '', targets
|
|
311
345
|
if prog in ('mkfs', 'fdisk', 'parted', 'wipefs', 'shutdown', 'reboot', 'halt', 'poweroff', 'init', 'telinit', 'swapoff', 'mkswap') or prog.startswith('mkfs.'): return 'catastrophic', prog + ' changes disks or the machine state', []
|
|
312
346
|
if prog == 'dd':
|
|
@@ -331,6 +365,9 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
331
365
|
if prog == 'git':
|
|
332
366
|
sub = next((a for a in args if not a.startswith('-')), '')
|
|
333
367
|
rest = args[args.index(sub) + 1:] if sub in args else []
|
|
368
|
+
# BUG-0453: a token written into a git address (https://user:TOKEN@github.com/...) bypasses the GitHub tools'
|
|
369
|
+
# rules and lands in the remote config and logs; every mode asks first.
|
|
370
|
+
if any(re.match(r'(?i)https?://[^/@\s]+@', a) for a in args): return 'privilege', 'git ' + sub + ' with a token written into the address', []
|
|
334
371
|
if sub == 'push':
|
|
335
372
|
if any(o in ('-f', '--force', '--force-with-lease', '--delete', '-d', '--mirror') or o.startswith('--force') for o in rest) or any(r.startswith('+') or r.startswith(':') for r in rest): return 'destructive', 'git push rewrites or deletes remote history', []
|
|
336
373
|
return 'network', 'git push publishes commits', []
|
|
@@ -348,6 +385,9 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
348
385
|
if sub in GIT_READ or sub == 'stash' and rest[:1] == ['list'] or sub == 'remote' and not rest[:1] or (sub == 'remote' and rest[:1] in (['-v'], ['show'], ['get-url'])) or (sub == 'config' and not any(r for r in rest if not r.startswith('-')) or (sub == 'config' and (rest[:1] in (['--get'], ['--list'], ['-l'], ['--get-regexp']) or (len([r for r in rest if not r.startswith('-')]) == 1)))):
|
|
349
386
|
if sub == 'branch' and rest and not rest[0].startswith('-'): return 'write', 'creates a branch', []
|
|
350
387
|
return 'read', '', []
|
|
388
|
+
if sub == 'rm' and '--cached' not in rest:
|
|
389
|
+
files = [r for r in rest if not r.startswith('-')]
|
|
390
|
+
return 'destructive', 'git rm deletes project files (' + ', '.join(files[:3]) + ')', files
|
|
351
391
|
if sub in GIT_WRITE or sub in ('remote', 'config', 'tag', 'branch', 'stash'): return 'write', 'git ' + sub, []
|
|
352
392
|
return 'exec', 'git ' + sub, []
|
|
353
393
|
if prog in NETWORK_PROGRAMS:
|
|
@@ -368,6 +408,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
368
408
|
return 'network', 'gh reads from GitHub', []
|
|
369
409
|
if positional and positional[0] == 'api' and any(a in ('-X', '--method') for a in args) and any(args[i + 1].upper() == 'DELETE' for i, a in enumerate(args) if a in ('-X', '--method') and i + 1 < len(args)): return 'destructive', 'gh api DELETE', []
|
|
370
410
|
if 'delete' in positional or 'archive' in positional or 'transfer' in positional: return 'destructive', 'gh ' + sub + ' deletes or transfers', []
|
|
411
|
+
# BUG-0453: merging a pull request changes the target branch on GitHub and is not undone from here; Auto asks too.
|
|
412
|
+
if positional[:2] == ['pr', 'merge']: return 'destructive', 'gh pr merge changes the target branch on GitHub', []
|
|
371
413
|
return 'network', 'gh ' + sub + ' changes GitHub state', []
|
|
372
414
|
if prog in ('aws', 'gcloud', 'az', 'kubectl', 'helm', 'terraform', 'pulumi', 'doctl', 'fly', 'flyctl', 'vercel', 'netlify', 'wrangler', 'heroku', 'railway', 'supabase', 'firebase'):
|
|
373
415
|
if any(w in positional for w in ('delete', 'destroy', 'rm', 'remove', 'purge', 'terminate', 'drop', 'uninstall', 'prune', 'reset', 'force-delete', 'teardown')): return 'destructive', prog + ' deletes cloud resources', []
|
|
@@ -397,6 +439,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
397
439
|
if sub in ('publish', 'login', 'logout', 'adduser', 'owner', 'deprecate', 'unpublish', 'token', 'access'): return ('destructive' if sub in ('unpublish', 'deprecate') else 'network'), 'npm ' + sub, []
|
|
398
440
|
if sub in ('test', 't', 'tst') or (sub in ('run', 'run-script', 'exec') and len(positional) > 1 and positional[1] in CHECK_SCRIPTS) or (prog in ('pnpm', 'yarn', 'bun') and sub in CHECK_SCRIPTS): return 'check', '', []
|
|
399
441
|
if sub in ('run', 'run-script', 'start', 'exec', 'x', 'dlx', 'create', 'init', 'dev', 'serve', 'preview'): return 'exec', prog + ' ' + ' '.join(positional[:2]), []
|
|
442
|
+
# A version or help check is not an install (BUG-0223: `npm -v` asked "Approve package install · npm install").
|
|
443
|
+
if sub == '' and any(a in ('-v', '-V', '--version', '-h', '--help', 'help') for a in args): return 'read', '', []
|
|
400
444
|
if sub in INSTALLERS[prog] or (sub == '' and prog in ('npm', 'pnpm', 'yarn', 'bun')): return 'install', prog + ' ' + (sub or 'install'), []
|
|
401
445
|
if sub in ('version', 'view', 'ls', 'list', 'outdated', 'why', 'explain', 'audit', 'info', 'show', 'config', 'root', 'bin', 'prefix', 'ping', 'search', 'pack', 'cache', 'doctor', 'help'): return 'read' if sub not in ('search', 'ping', 'view', 'info', 'show', 'audit', 'outdated') else 'network', '', []
|
|
402
446
|
return 'exec', prog + ' ' + sub, []
|
|
@@ -468,6 +512,7 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
468
512
|
if prog in ('python', 'python3') and args[:1] == ['-m'] and len(args) > 1:
|
|
469
513
|
mod = args[1]
|
|
470
514
|
if mod in ('pip', 'pip3', 'ensurepip'): return 'install', 'python -m pip', []
|
|
515
|
+
if mod == 'playwright' and any(a in ('install', 'install-deps') for a in args[2:]): return 'install', 'playwright installs browsers', []
|
|
471
516
|
if mod in ('http.server', 'SimpleHTTPServer', 'venv', 'virtualenv'): return 'service' if mod.endswith('server') else 'write', 'python -m ' + mod, []
|
|
472
517
|
if mod in ('json.tool', 'py_compile', 'compileall', 'pydoc', 'this', 'site', 'sysconfig', 'platform', 'tokenize', 'ast', 'dis'): return 'read', '', []
|
|
473
518
|
scripts = [a for a in positional if re.search(r'\.(py|[cm]?js|ts|tsx|rb|php|pl|jar|R|jl|lua|exs|swift)$', a)]
|
|
@@ -476,7 +521,10 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
476
521
|
if inline or scripts:
|
|
477
522
|
code = joined if inline else ''
|
|
478
523
|
if body_programs: code += '\n' + body_programs
|
|
479
|
-
if re.search(r'(shutil\.rmtree|os\.remove|os\.unlink|rimraf|fs\.rm\w*Sync?\(|fs\.unlink|rm\s+-rf|
|
|
524
|
+
if re.search(r'(shutil\.rmtree|os\.remove|os\.unlink|rimraf|fs\.rm\w*Sync?\(|fs\.unlink|rm\s+-rf|FileUtils\.rm_rf)', code): return 'destructive', 'inline script deletes files', scripts
|
|
525
|
+
# Shelling out alone runs commands like the shell would; it only asks when the script's own text deletes or rewrites history.
|
|
526
|
+
# (Every subprocess-using script used to ask, even in Auto and after Approve all.)
|
|
527
|
+
if re.search(r'(subprocess|child_process|execSync|os\.system)', code) and re.search(r'(\brm\s+-\w*[rf]|\brmdir\b|\bgit\s+(reset\s+--hard|clean\s+-\w*f|push\s+(-f|--force)|rebase|filter-branch|branch\s+-D)|\bdropdb\b|DROP\s+(TABLE|DATABASE))', code, re.I): return 'destructive', 'inline script shells out to delete or rewrite history', scripts
|
|
480
528
|
if re.search(r'(requests\.|urllib|httpx|aiohttp|fetch\(|axios|http\.request|socket\.|net\.connect|smtplib|paramiko)', code): return 'network', 'inline script uses the network', scripts
|
|
481
529
|
if re.search(r'\b(assert|expect\(|unittest|pytest|console\.assert|test\(|describe\()', code) and not re.search(r'(open\([^)]*[\'"]w|writeFile|\.write\(|mkdir|rename|copy)', code): return 'check', '', scripts
|
|
482
530
|
if scripts and re.search(r'(^|/)(test_\w+|\w+_test|\w+\.test|\w+\.spec)\.(py|[cm]?js|ts|tsx)$', scripts[0]): return 'check', '', scripts
|
|
@@ -486,6 +534,8 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
486
534
|
target = next((a for a in positional if not a.startswith('-')), '')
|
|
487
535
|
tname = re.sub(r'@.*$', '', target) if not target.startswith('@') else target
|
|
488
536
|
base = tname.rsplit('/', 1)[-1]
|
|
537
|
+
# BUG-0220: downloading browsers is an install, not a test run.
|
|
538
|
+
if base == 'playwright' and any(a in ('install', 'install-deps') for a in positional[1:]): return 'install', 'playwright installs browsers', []
|
|
489
539
|
if CHECK.match(base) or base in ('tsc', 'vitest', 'jest', 'eslint', 'playwright', 'prettier', 'biome', 'mocha', 'ava', 'cypress', 'stylelint', 'tsx_test'):
|
|
490
540
|
if base in ('prettier', 'eslint', 'biome') and any(a in args for a in ('--write', '--fix', 'format')): return 'write', base + ' rewrites files', []
|
|
491
541
|
return 'check', '', []
|
|
@@ -562,6 +612,7 @@ def evaluate(words, redirects, flags, repo, cwd, home, body_programs=None):
|
|
|
562
612
|
if prog in ('prettier', 'eslint', 'black', 'isort', 'ruff', 'gofmt', 'rustfmt', 'biome', 'stylelint'):
|
|
563
613
|
if any(a in args for a in ('--write', '--fix', 'format', '-w')) or (prog in ('black', 'isort', 'gofmt', 'rustfmt') and not any(a in args for a in ('--check', '--diff', '-l', '-d'))): return 'write', prog + ' rewrites files', positional
|
|
564
614
|
return 'check', '', []
|
|
615
|
+
if prog == 'playwright' and positional[:1] and positional[0] in ('install', 'install-deps'): return 'install', 'playwright installs browsers', []
|
|
565
616
|
if CHECK.match(prog): return 'check', '', []
|
|
566
617
|
if prog in ('wget_out',): return 'network', '', []
|
|
567
618
|
return 'exec', 'unknown program ' + prog, []
|
|
@@ -683,9 +734,12 @@ def relative(path, repo, cwd=None):
|
|
|
683
734
|
|
|
684
735
|
def in_scope(rel, scope):
|
|
685
736
|
"""rel is inside one of the approved scope entries (a file, a directory, or '.' for everything)."""
|
|
686
|
-
|
|
737
|
+
# BUG-0564: "./notes.txt" and "notes.txt" are the same file; an approved area was asked for again under the other spelling.
|
|
738
|
+
import posixpath
|
|
739
|
+
rel = posixpath.normpath(rel.strip('/').replace('__PROCSUB__', '') or '.')
|
|
687
740
|
for entry in scope or []:
|
|
688
741
|
e = str(entry).strip().strip('/')
|
|
742
|
+
if e and e not in ('*', '**') and '*' not in e: e = posixpath.normpath(e)
|
|
689
743
|
if e in ('.', '', '*', '**'): return True
|
|
690
744
|
if rel == e or rel.startswith(e + '/'): return True
|
|
691
745
|
if '*' in e:
|
|
@@ -696,7 +750,9 @@ def in_scope(rel, scope):
|
|
|
696
750
|
|
|
697
751
|
def area(rel):
|
|
698
752
|
"""The approval unit for a first-touch edit: the file's directory (two levels deep), or the file itself at the root."""
|
|
699
|
-
|
|
753
|
+
import posixpath
|
|
754
|
+
parts = PurePosixPath(posixpath.normpath(rel.strip('/') or '.')).parts
|
|
755
|
+
parts = tuple(p for p in parts if p != '.')
|
|
700
756
|
if len(parts) <= 1: return parts[0] if parts else '.'
|
|
701
757
|
return '/'.join(parts[:2]) if len(parts) > 2 else parts[0]
|
|
702
758
|
|
|
@@ -715,3 +771,24 @@ def safe_command(command, repo=None, cwd=None):
|
|
|
715
771
|
"""Read-only or check command that needs no approval in any mode but Plan (compatibility with runtime_control.safe_command)."""
|
|
716
772
|
v = classify(command, repo or os.getenv('MINI_REPO', '/vercel/sandbox/repo'), cwd)
|
|
717
773
|
return v['effect'] in ('read', 'check')
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
# ------------------------------------------------------------------------------------------------- always allow (chat)
|
|
777
|
+
# BUG-0594: "Always allow in this chat" covers one exact command, and only actions that cannot delete, push, pay, reach
|
|
778
|
+
# credentials or write outside the project. Mirrors lib/chat-allow.ts; anything else keeps asking every time.
|
|
779
|
+
CHAT_ALLOW_EFFECTS = ('read', 'check', 'exec', 'write', 'service')
|
|
780
|
+
CHAT_ALLOW_NEVER = re.compile(r"""\b(rm|rmdir|unlink|shred|truncate|dd|mkfs|git\s+(push|reset|clean|rebase|filter-branch|branch\s+-D|checkout\s+--|restore|rm)|gh\s+(pr\s+merge|release|repo\s+delete)|npm\s+(publish|unpublish)|yarn\s+publish|pnpm\s+publish|vercel|netlify|firebase\s+deploy|stripe|paypal|curl|wget|scp|rsync|ssh|sudo|su|chmod|chown|kill|pkill|killall|docker\s+(rm|rmi|system\s+prune)|kubectl|terraform|aws|gcloud|az)\b|(^|[\s"'=/])(\.env\b|\.ssh\b|\.aws\b|\.npmrc\b|\.netrc\b|id_rsa|credentials|secrets?\b|\.git-credentials|keychain)|>\s*/(?!dev/null|tmp/)|(^|\s)\.\./""", re.I)
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
def chat_allow_key(command):
|
|
784
|
+
return ' '.join(str(command or '').split())
|
|
785
|
+
|
|
786
|
+
|
|
787
|
+
def chat_allow_ok(effect, command, repo=None):
|
|
788
|
+
"""True when this command may be remembered for the chat. Re-classifies, so a stored key never widens later."""
|
|
789
|
+
text = str(command or '')
|
|
790
|
+
if effect not in CHAT_ALLOW_EFFECTS or not text.strip() or len(text) > 2000 or CHAT_ALLOW_NEVER.search(text): return False
|
|
791
|
+
if repo:
|
|
792
|
+
v = classify(text, repo)
|
|
793
|
+
if v.effect not in CHAT_ALLOW_EFFECTS: return False
|
|
794
|
+
return True
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""Stronger compaction for the Kimi engine (both steps happen in the model proxy).
|
|
2
|
+
|
|
3
|
+
The engine already writes a handoff summary when context fills up, keeps the real user messages verbatim (up to ~20K
|
|
4
|
+
tokens, head and tail), re-attaches the TODO list and points to the full event log on disk. What it cannot know is the
|
|
5
|
+
state ML AI Code keeps on disk. Two additions close that gap:
|
|
6
|
+
|
|
7
|
+
1. Summary instruction: when the engine asks for the handoff summary, we append harness-specific rules (quote
|
|
8
|
+
requirements and limits verbatim, keep failing checks with their error lines, say which nudges came from the
|
|
9
|
+
harness rather than the user).
|
|
10
|
+
2. Pinned brief: the first request after a compaction gets a short block appended to the engine's summary message,
|
|
11
|
+
read from disk rather than recalled: the current request when it is no longer fully visible, the approved edit
|
|
12
|
+
scope, the latest check results and the files changed so far. Disk wins where it disagrees with the summary.
|
|
13
|
+
|
|
14
|
+
The brief is built once per compaction and cached by the summary's hash, so later requests send the same bytes and
|
|
15
|
+
the provider's prefix cache keeps hitting. Subagents keep their own context: the brief is added only for the main
|
|
16
|
+
agent (its system prompt carries `main_marker`). Opt-in with MINI_COMPACT_BRIEF=1: in a live A/B on long tasks
|
|
17
|
+
(GLM 5.3 Flash, 3 runs per arm) it did not change scores, so it stays off by default.
|
|
18
|
+
|
|
19
|
+
Always on, independent of that flag: fix_summary_budget(), which keeps the summary request itself from failing.
|
|
20
|
+
"""
|
|
21
|
+
import hashlib, json, os, subprocess
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
ENABLED = os.getenv('MINI_COMPACT_BRIEF', '0') == '1'
|
|
25
|
+
REQUEST_MARK = 'Create a handoff summary for the'
|
|
26
|
+
SUMMARY_MARK = 'The conversation so far has been compacted to free up context.'
|
|
27
|
+
BRIEF_MARK = '## Pinned facts from disk'
|
|
28
|
+
INSTRUCTION_MARK = 'ML AI Code handoff rules'
|
|
29
|
+
TASK_CHARS = 12000 # the current request, head + tail when longer
|
|
30
|
+
CHECK_LIMIT = 8
|
|
31
|
+
FILE_LINES = 40
|
|
32
|
+
BRIEF_CHARS = 16000
|
|
33
|
+
SUMMARY_MIN_TOKENS = 16384 # output room for the handoff summary request
|
|
34
|
+
|
|
35
|
+
INSTRUCTION = INSTRUCTION_MARK + ''' (in addition to the above):
|
|
36
|
+
- Quote the user's requirements, acceptance criteria, limits ("do not touch X", "only Y"), chosen design and deploy target word for word in a short list. Do not paraphrase them.
|
|
37
|
+
- For every check or test command run, give the exact command and its latest result. For a failing one, keep the key error lines verbatim.
|
|
38
|
+
- List every file you created or changed, and which ones still need work.
|
|
39
|
+
- Some messages in the conversation come from the ML AI Code harness, not the user (verification reminders, repair rounds, "continue" nudges). Say which is which, so a harness nudge is never mistaken for a new user requirement.
|
|
40
|
+
- A block of pinned facts read from disk (scope, checks, changed files) is attached after this summary automatically; do not spend space copying them.'''
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _text(content):
|
|
44
|
+
if isinstance(content, str): return content
|
|
45
|
+
if isinstance(content, list):
|
|
46
|
+
return '\n'.join(p['text'] for p in content if isinstance(p, dict) and isinstance(p.get('text'), str))
|
|
47
|
+
return ''
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _append(message, extra):
|
|
51
|
+
"""Append text to a message whose content is a string or a list of parts."""
|
|
52
|
+
c = message.get('content')
|
|
53
|
+
if isinstance(c, list):
|
|
54
|
+
for part in reversed(c):
|
|
55
|
+
if isinstance(part, dict) and isinstance(part.get('text'), str):
|
|
56
|
+
part['text'] = part['text'].rstrip() + '\n\n' + extra
|
|
57
|
+
return
|
|
58
|
+
c.append({'type': 'text', 'text': extra})
|
|
59
|
+
else: message['content'] = (c or '').rstrip() + '\n\n' + extra
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _load(path, default):
|
|
63
|
+
try: return json.loads(Path(path).read_text())
|
|
64
|
+
except (OSError, ValueError): return default
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _clip(text, limit):
|
|
68
|
+
text = str(text or '')
|
|
69
|
+
if len(text) <= limit: return text
|
|
70
|
+
half = limit // 2
|
|
71
|
+
return text[:half].rstrip() + '\n[... %d characters omitted; the full request is in .mini/request.json ...]\n' % (len(text) - limit) + text[-half:].lstrip()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _git(repo, *args):
|
|
75
|
+
try: return subprocess.run(['git', *args], cwd=repo, capture_output=True, text=True, timeout=5).stdout
|
|
76
|
+
except Exception: return ''
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def changed_files(repo, base):
|
|
80
|
+
if not repo or not Path(repo).exists(): return []
|
|
81
|
+
lines = [l for l in _git(repo, 'diff', '--stat=120', base or 'HEAD').splitlines() if l.strip()]
|
|
82
|
+
stat = lines[:-1] if lines and ' changed' in lines[-1] else lines
|
|
83
|
+
untracked = [l for l in _git(repo, 'ls-files', '--others', '--exclude-standard').splitlines() if l.strip() and not l.startswith('.mini/')]
|
|
84
|
+
out = [l.strip() for l in stat] + [u + ' (new, untracked)' for u in untracked]
|
|
85
|
+
if len(out) > FILE_LINES: out = out[:FILE_LINES] + ['... and %d more' % (len(out) - FILE_LINES)]
|
|
86
|
+
return out
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _check_line(c):
|
|
90
|
+
rc = c.get('returncode')
|
|
91
|
+
status = 'PASSED' if rc == 0 else ('FAILED (exit %s)' % rc if rc is not None else 'not run')
|
|
92
|
+
if c.get('stale'): status += ', code changed since, so treat as unverified'
|
|
93
|
+
line = '- ' + str(c.get('label') or c.get('command') or '')[:160] + ': ' + status
|
|
94
|
+
if rc not in (0, None):
|
|
95
|
+
import re
|
|
96
|
+
errs = [l.strip() for l in str(c.get('output') or '').splitlines() if re.search(r'error|fail|assert|exception|traceback|expected', l, re.I)]
|
|
97
|
+
if errs: line += '\n ' + '\n '.join(e[:200] for e in errs[-3:])
|
|
98
|
+
return line
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def build(root, repo, visible=''):
|
|
102
|
+
"""The pinned brief, read from disk. `visible` is the text already in the request, used to avoid repeating the task."""
|
|
103
|
+
root = Path(root)
|
|
104
|
+
config = _load(root / 'request.json', {})
|
|
105
|
+
state = _load(root / 'kimi-state.json', {})
|
|
106
|
+
task = str(config.get('task') or state.get('task') or '').strip()
|
|
107
|
+
parts = [BRIEF_MARK + ' (read when the context was compacted; where they differ from the summary, these are right)']
|
|
108
|
+
if task:
|
|
109
|
+
if task in visible: parts.append('Current request: preserved verbatim earlier in this context.')
|
|
110
|
+
else: parts.append('Current request, verbatim (no longer fully visible above):\n' + _clip(task, TASK_CHARS))
|
|
111
|
+
scope = _load(root / 'scope.json', {})
|
|
112
|
+
declared, areas = scope.get('declared') or [], scope.get('areas') or []
|
|
113
|
+
if declared or areas:
|
|
114
|
+
line = 'Approved edit scope: ' + ', '.join(list(dict.fromkeys(list(declared) + list(areas)))[:40])
|
|
115
|
+
if scope.get('reason'): line += ' (why: ' + str(scope['reason'])[:200] + ')'
|
|
116
|
+
parts.append(line + '. Ask before editing outside it.')
|
|
117
|
+
checks = [c for c in _load(root / 'checks.json', []) if isinstance(c, dict)][-CHECK_LIMIT:]
|
|
118
|
+
if checks: parts.append('Latest check results:\n' + '\n'.join(_check_line(c) for c in checks))
|
|
119
|
+
else: parts.append('Latest check results: none recorded yet.')
|
|
120
|
+
files = changed_files(repo, state.get('base'))
|
|
121
|
+
parts.append(('Files changed so far (git diff --stat):\n' + '\n'.join(files)) if files else 'Files changed so far: none.')
|
|
122
|
+
return '\n\n'.join(parts)[:BRIEF_CHARS]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def is_summary_request(body):
|
|
126
|
+
"""True for the engine's handoff-summary request (the compaction call)."""
|
|
127
|
+
messages = body.get('messages') if isinstance(body, dict) else None
|
|
128
|
+
if not isinstance(messages, list) or not messages or not isinstance(messages[-1], dict): return False
|
|
129
|
+
last = messages[-1]
|
|
130
|
+
if last.get('role') != 'user': return False
|
|
131
|
+
text = _text(last.get('content'))
|
|
132
|
+
return REQUEST_MARK in text and 'handoff summary' in text
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def fix_summary_budget(body):
|
|
136
|
+
"""Give the summary request real output room. Always on (a bug fix, not a feature flag).
|
|
137
|
+
|
|
138
|
+
The engine sizes the summary's max_tokens as window minus its own estimate of the context. That estimate counts
|
|
139
|
+
every tool output in full, while the proxy sends masked and trimmed outputs, so after a large read the engine
|
|
140
|
+
can believe the window is already full and send max_tokens=1. The model then spends that single token on
|
|
141
|
+
reasoning, the engine drops the oldest message and retries, and after five tries the run stops with
|
|
142
|
+
compaction.failed (or summarizes only the last message). Returns True when the budget was raised.
|
|
143
|
+
"""
|
|
144
|
+
if not is_summary_request(body): return False
|
|
145
|
+
key = 'max_completion_tokens' if 'max_completion_tokens' in body else 'max_tokens'
|
|
146
|
+
current = body.get(key)
|
|
147
|
+
if isinstance(current, int) and current >= SUMMARY_MIN_TOKENS: return False
|
|
148
|
+
if current is None and key not in body: return False # no cap sent: the provider default applies
|
|
149
|
+
body[key] = SUMMARY_MIN_TOKENS
|
|
150
|
+
return True
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def apply(body, root, repo, main_marker=None):
|
|
154
|
+
"""Strengthen one model request in place. Returns 'instruction', 'brief', 'cached' or ''."""
|
|
155
|
+
if not ENABLED: return ''
|
|
156
|
+
messages = body.get('messages')
|
|
157
|
+
if not isinstance(messages, list) or not messages: return ''
|
|
158
|
+
last = messages[-1] if isinstance(messages[-1], dict) else {}
|
|
159
|
+
if last.get('role') == 'user':
|
|
160
|
+
text = _text(last.get('content'))
|
|
161
|
+
if REQUEST_MARK in text and 'handoff summary' in text and INSTRUCTION_MARK not in text:
|
|
162
|
+
_append(last, INSTRUCTION)
|
|
163
|
+
return 'instruction'
|
|
164
|
+
if main_marker:
|
|
165
|
+
system = next((_text(m.get('content')) for m in messages if isinstance(m, dict) and m.get('role') == 'system'), '')
|
|
166
|
+
if main_marker not in system: return ''
|
|
167
|
+
summary = None
|
|
168
|
+
for m in reversed(messages):
|
|
169
|
+
if isinstance(m, dict) and m.get('role') == 'user' and _text(m.get('content')).lstrip().startswith(SUMMARY_MARK):
|
|
170
|
+
summary = m; break
|
|
171
|
+
if summary is None: return ''
|
|
172
|
+
stext = _text(summary.get('content'))
|
|
173
|
+
if BRIEF_MARK in stext: return ''
|
|
174
|
+
key = hashlib.sha256(stext.encode()).hexdigest()[:20]
|
|
175
|
+
cache_path = Path(root) / 'compaction-brief.json'
|
|
176
|
+
cache = _load(cache_path, {})
|
|
177
|
+
if cache.get('key') == key and isinstance(cache.get('brief'), str):
|
|
178
|
+
_append(summary, cache['brief'])
|
|
179
|
+
return 'cached'
|
|
180
|
+
visible = '\n'.join(_text(m.get('content')) for m in messages if isinstance(m, dict) and m.get('role') == 'user' and m is not summary)
|
|
181
|
+
brief = build(root, repo, visible)
|
|
182
|
+
try:
|
|
183
|
+
tmp = cache_path.with_suffix('.tmp'); tmp.write_text(json.dumps({'key': key, 'brief': brief})); tmp.replace(cache_path)
|
|
184
|
+
except OSError: pass
|
|
185
|
+
_append(summary, brief)
|
|
186
|
+
return 'brief'
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""MCP-SYNC: connected apps can change in the middle of a chat.
|
|
2
|
+
A follow-up turn continues the previous engine session, whose instructions still hold the connected-apps catalog from
|
|
3
|
+
the turn that started it, so the agent kept answering from that old list (a server added mid-chat was "not connected",
|
|
4
|
+
a removed one was still "there"). The catalog's fingerprint is saved with the session; when it differs on resume, the
|
|
5
|
+
turn's prompt gets the current list, which replaces the earlier one. Tool calls were already checked against the
|
|
6
|
+
current request, so only what the agent believes changes."""
|
|
7
|
+
import hashlib, json
|
|
8
|
+
from connector_search import compact_catalog
|
|
9
|
+
|
|
10
|
+
def fingerprint(tools):
|
|
11
|
+
keys = sorted({(str(t.get('connection', '')), str(t.get('name', ''))) for t in tools or [] if isinstance(t, dict)})
|
|
12
|
+
return hashlib.sha256(json.dumps(keys).encode()).hexdigest()[:16]
|
|
13
|
+
|
|
14
|
+
def _names(tools, connected):
|
|
15
|
+
ids = []
|
|
16
|
+
for t in tools or []:
|
|
17
|
+
if isinstance(t, dict) and t.get('connection') not in ids: ids.append(t.get('connection'))
|
|
18
|
+
label = {c.get('connection'): c.get('name') for c in connected or [] if isinstance(c, dict)}
|
|
19
|
+
return [str(label.get(i) or i) + ' (' + str(sum(1 for t in tools if isinstance(t, dict) and t.get('connection') == i)) + ' tools)' for i in ids]
|
|
20
|
+
|
|
21
|
+
def resume_note(prior, tools, connected=None):
|
|
22
|
+
"""Text for the top of a resumed turn's prompt, or '' when the connected apps are the ones the session already knows."""
|
|
23
|
+
tools = [t for t in tools or [] if isinstance(t, dict)]
|
|
24
|
+
if prior and prior == fingerprint(tools): return ''
|
|
25
|
+
lead = 'This replaces any list from earlier in this chat (including your earlier answers)'
|
|
26
|
+
if not tools:
|
|
27
|
+
return 'No connected apps are enabled right now. ' + lead + ': anything listed before was disconnected, so do not call it.'
|
|
28
|
+
catalog, compacted = compact_catalog(tools)
|
|
29
|
+
return ('Connected apps right now: ' + ', '.join(_names(tools, connected)) + '. ' + lead + '; apps not in it were disconnected. '
|
|
30
|
+
'Call them with mcp__mlai__integration_call {connection, name, arguments}' + ('; use mcp__mlai__integration_search for full schemas' if compacted else '') + '. Catalog:\n' + catalog)
|
|
@@ -18,6 +18,12 @@ class ContextManager:
|
|
|
18
18
|
def observe(self,messages,prompt_tokens):
|
|
19
19
|
if isinstance(prompt_tokens,(int,float)) and prompt_tokens>0:
|
|
20
20
|
estimated=self.count_tokens(messages);self.ratio=max(.5,min(3.,prompt_tokens/max(1,estimated)));self.last_actual=int(prompt_tokens);self.last_estimate=estimated
|
|
21
|
+
# Context meter in the app: one event each time use crosses another 5% of the compaction point.
|
|
22
|
+
pct=min(100,int(self.last_actual*100/max(1,self.threshold)))
|
|
23
|
+
if pct//5!=getattr(self,'_meter',-1):
|
|
24
|
+
self._meter=pct//5
|
|
25
|
+
try:self.emit('context_use',used=self.last_actual,limit=int(self.threshold),pct=pct)
|
|
26
|
+
except Exception:pass
|
|
21
27
|
def assembled(self,messages):
|
|
22
28
|
if not self.summary:return list(messages)
|
|
23
29
|
return list(messages[:2])+[{'role':'user','content':self.summary}]+list(messages[self.end:])
|