mlai-code 0.0.0-stage → 0.3.0
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 +12 -2
- package/bin/mlai.mjs +3 -0
- package/package.json +18 -3
- package/src/browser-setup.mjs +77 -0
- package/src/cli-ext.mjs +146 -0
- package/src/cli.mjs +534 -0
- package/src/cloud.mjs +29 -0
- package/src/completion.mjs +159 -0
- package/src/credentials.mjs +54 -0
- package/src/deploy.mjs +134 -0
- package/src/distribution.mjs +373 -0
- package/src/engine-key.mjs +4 -0
- package/src/engine.mjs +167 -0
- package/src/headless.mjs +196 -0
- package/src/home.mjs +22 -0
- package/src/host.mjs +123 -0
- package/src/local-models.mjs +64 -0
- package/src/modes.mjs +38 -0
- package/src/netbox.mjs +97 -0
- package/src/netjail.py +59 -0
- package/src/offline.mjs +98 -0
- package/src/parity.mjs +86 -0
- package/src/project.mjs +68 -0
- package/src/relay.mjs +140 -0
- package/src/render.mjs +73 -0
- package/src/restricted.mjs +47 -0
- package/src/review.mjs +70 -0
- package/src/runner.mjs +127 -0
- package/src/runtime.mjs +75 -0
- package/src/sandbox.mjs +135 -0
- package/src/sdk.mjs +11 -0
- package/src/server.mjs +110 -0
- package/src/session.mjs +290 -0
- package/src/settings.mjs +135 -0
- package/src/shared-data.mjs +3 -0
- package/src/tui/acp.mjs +145 -0
- package/src/tui/agents-cli.mjs +114 -0
- package/src/tui/agents-local.mjs +65 -0
- package/src/tui/ansi.mjs +123 -0
- package/src/tui/app.mjs +710 -0
- package/src/tui/approval.mjs +88 -0
- package/src/tui/bg.mjs +172 -0
- package/src/tui/bgterm.mjs +79 -0
- package/src/tui/chat.mjs +230 -0
- package/src/tui/clipboard.mjs +74 -0
- package/src/tui/cloud-cli.mjs +57 -0
- package/src/tui/cloud-run.mjs +123 -0
- package/src/tui/cmd-agents.mjs +130 -0
- package/src/tui/cmd-auto-review.mjs +94 -0
- package/src/tui/cmd-cloud.mjs +17 -0
- package/src/tui/cmd-config.mjs +104 -0
- package/src/tui/cmd-core.mjs +160 -0
- package/src/tui/cmd-distribution.mjs +15 -0
- package/src/tui/cmd-ecosystem.mjs +169 -0
- package/src/tui/cmd-extensibility.mjs +333 -0
- package/src/tui/cmd-remote.mjs +18 -0
- package/src/tui/cmd-review.mjs +58 -0
- package/src/tui/cmd-session-extra.mjs +152 -0
- package/src/tui/cmd-tui.mjs +200 -0
- package/src/tui/commands.mjs +160 -0
- package/src/tui/custom-commands.mjs +98 -0
- package/src/tui/diff.mjs +60 -0
- package/src/tui/editor.mjs +190 -0
- package/src/tui/ext-state.mjs +16 -0
- package/src/tui/extensions-local.mjs +140 -0
- package/src/tui/files.mjs +72 -0
- package/src/tui/handoff.mjs +110 -0
- package/src/tui/import-setups.mjs +151 -0
- package/src/tui/init.mjs +104 -0
- package/src/tui/keymap.mjs +70 -0
- package/src/tui/keys.mjs +101 -0
- package/src/tui/loop.mjs +75 -0
- package/src/tui/markdown.mjs +118 -0
- package/src/tui/mcp-config.mjs +225 -0
- package/src/tui/mcp-oauth.mjs +162 -0
- package/src/tui/permissions.mjs +189 -0
- package/src/tui/plain.mjs +32 -0
- package/src/tui/plugins-local.mjs +103 -0
- package/src/tui/remote.mjs +125 -0
- package/src/tui/review-cli.mjs +332 -0
- package/src/tui/review-github.mjs +59 -0
- package/src/tui/screen.mjs +60 -0
- package/src/tui/session-ops.mjs +104 -0
- package/src/tui/skills-local.mjs +92 -0
- package/src/tui/snapshots.mjs +87 -0
- package/src/tui/statusline.mjs +98 -0
- package/src/tui/transcript.mjs +116 -0
- package/src/two-words.mjs +36 -0
- package/src/wakeups.mjs +53 -0
- package/src/winenv.mjs +94 -0
- package/src/winsandbox.py +351 -0
- package/src/workspace-api.mjs +621 -0
- package/src/worktree.mjs +128 -0
- package/vendor/client-sdk/controller.cjs +115 -0
- package/vendor/client-sdk/files.cjs +38 -0
- package/vendor/client-sdk/index.cjs +133 -0
- package/vendor/client-sdk/login.cjs +52 -0
- package/vendor/client-sdk/updates.cjs +41 -0
- package/vendor/cloud-cli/mlai.mjs +394 -0
- package/worker/architecture_parse.cjs +23 -0
- package/worker/architecture_worker.py +394 -0
- package/worker/ask_first.py +96 -0
- package/worker/attachment_reader.py +66 -0
- package/worker/background_jobs.py +111 -0
- package/worker/brief_questions.py +56 -0
- package/worker/browser_local.py +49 -0
- package/worker/browser_pane.py +89 -0
- package/worker/browser_tool.py +101 -0
- package/worker/build_planner.py +187 -0
- package/worker/chat_fork.py +277 -0
- package/worker/checkpoints.py +133 -0
- package/worker/cloud_pty.py +351 -0
- package/worker/command_policy.py +39 -0
- package/worker/command_safety.py +717 -0
- package/worker/computer_card.sh +23 -0
- package/worker/computer_setup.sh +86 -0
- package/worker/computer_use.py +221 -0
- package/worker/connector_search.py +43 -0
- package/worker/context_manager.py +60 -0
- package/worker/conversation.py +144 -0
- package/worker/deploy_pack.py +116 -0
- package/worker/design_director.py +240 -0
- package/worker/dev_jobs.py +166 -0
- package/worker/edit_guard.py +99 -0
- package/worker/env_setup.py +129 -0
- package/worker/evidence.py +109 -0
- package/worker/fast_search.py +24 -0
- package/worker/git_sync.py +333 -0
- package/worker/goals.py +156 -0
- package/worker/handover.py +98 -0
- package/worker/harness_memory.py +194 -0
- package/worker/image_generation.py +173 -0
- package/worker/intelligence.py +149 -0
- package/worker/intent_model.py +78 -0
- package/worker/intent_weights.json +1 -0
- package/worker/ios_simulator.py +87 -0
- package/worker/kimi_bridge.py +997 -0
- package/worker/kimi_compact.py +93 -0
- package/worker/kimi_gates.py +304 -0
- package/worker/kimi_hook.py +617 -0
- package/worker/lifecycle.py +95 -0
- package/worker/lsp.py +77 -0
- package/worker/mcp_local.py +293 -0
- package/worker/memory.py +99 -0
- package/worker/mlai_auto_review.py +177 -0
- package/worker/mlai_hooks.py +966 -0
- package/worker/mlai_mcp.py +441 -0
- package/worker/mlai_permissions.py +283 -0
- package/worker/model_stream.py +75 -0
- package/worker/nested_repos.py +76 -0
- package/worker/offline_guard.py +17 -0
- package/worker/preview.py +806 -0
- package/worker/proc_watch.py +124 -0
- package/worker/project_context.py +72 -0
- package/worker/project_rules.py +99 -0
- package/worker/quality.py +180 -0
- package/worker/reply_language.py +56 -0
- package/worker/repo_hygiene.py +132 -0
- package/worker/repo_memory.py +101 -0
- package/worker/repo_policy.py +269 -0
- package/worker/roles.json +3173 -0
- package/worker/roles.py +303 -0
- package/worker/runner.py +1795 -0
- package/worker/runtime_control.py +205 -0
- package/worker/secret_scan.py +168 -0
- package/worker/session_hub.py +80 -0
- package/worker/skill_library.py +129 -0
- package/worker/skill_pack.py +533 -0
- package/worker/subagents.py +91 -0
- package/worker/task_controller.py +139 -0
- package/worker/task_skills.py +159 -0
- package/worker/team_checkpoint.py +33 -0
- package/worker/token_count.py +32 -0
- package/worker/tool_catalog.py +38 -0
- package/worker/upload_storage.py +531 -0
- package/worker/verification.py +725 -0
- package/worker/vision.py +84 -0
- package/worker/wakeups.py +63 -0
- package/worker/web_tools.py +203 -0
- package/worker/winproc.py +135 -0
- package/worker/work_ledger.py +117 -0
- package/worker/worktrees.py +79 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Request compaction for the Kimi model proxy.
|
|
2
|
+
|
|
3
|
+
Every rule here is a pure function of its input, so the same tools and system prompt always produce the same
|
|
4
|
+
bytes: the provider's prefix cache keeps hitting across calls, turns and runs. Nothing rewrites earlier messages.
|
|
5
|
+
"""
|
|
6
|
+
import json, re
|
|
7
|
+
|
|
8
|
+
# Tools the engine ships that ML AI Code never uses. Goal mode and swarms are not reachable from the app.
|
|
9
|
+
PRUNE = frozenset({'AgentSwarm', 'CreateGoal', 'GetGoal', 'SetGoalBudget', 'UpdateGoal'})
|
|
10
|
+
|
|
11
|
+
SHORT = {
|
|
12
|
+
'TodoList': 'Maintain a short TODO list for multi-step work; the user sees it as a live progress panel. Skip it for tasks of one to three steps. '
|
|
13
|
+
'Call with todos:[...] to replace the list (statuses pending / in_progress / done; one in_progress at a time), with no argument to read it, '
|
|
14
|
+
'or todos:[] to clear it. Mark items done as you finish them, never while tests fail or work is partial. Updates can go in the same response as other tool calls.',
|
|
15
|
+
'WaitFor': 'Wait inside this turn for a background task (background Bash or subagent) to finish, or until the timeout. Use only when nothing else can proceed without '
|
|
16
|
+
'that result; finished tasks also notify you automatically. With task_id, waits for that task; without it, for running tasks.',
|
|
17
|
+
'TaskList': 'List background tasks with their IDs and status. Use instead of guessing task IDs.',
|
|
18
|
+
'TaskOutput': 'Non-blocking snapshot of a background task started with run_in_background. Prefer the automatic completion notification over polling.',
|
|
19
|
+
'TaskStop': 'Stop a running background task. Destructive; only when it must be cancelled.',
|
|
20
|
+
'Read': 'Read a text file. Output is "<line-number>\\t<content>" per line, up to max_chars (100000). If a status block reports an incomplete range, continue with its '
|
|
21
|
+
'Next Read arguments. Read known paths directly, several files in parallel in one response. Do not re-read after a successful Edit or Write.',
|
|
22
|
+
'Skill': 'Load a skill from the skill listing when it matches the request. Do not reload a skill whose <skill-loaded> block is already in the conversation with the same args.',
|
|
23
|
+
'Agent': 'Launch a subagent with its own context. Only when the user explicitly asks for helpers, delegation or parallel agents; otherwise do the work yourself. '
|
|
24
|
+
'Brief it fully (goal, known paths, facts); it starts cold. Its result is visible only to you, so summarize it for the user. Pass resume to continue an earlier subagent.',
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
BIG_INT = 9007199254740991
|
|
28
|
+
DROP_PARAMS = {'Glob': {'include_dirs'}}
|
|
29
|
+
|
|
30
|
+
def first_sentence(text, limit=160):
|
|
31
|
+
text = re.sub(r'\s+', ' ', str(text)).strip()
|
|
32
|
+
m = re.match(r'(.+?[.!?])(\s|$)', text)
|
|
33
|
+
out = m.group(1) if m else text
|
|
34
|
+
return out if len(out) <= limit else out[:limit - 1].rstrip() + '…'
|
|
35
|
+
|
|
36
|
+
def compact_schema(node):
|
|
37
|
+
if isinstance(node, dict):
|
|
38
|
+
out = {}
|
|
39
|
+
for k, v in node.items():
|
|
40
|
+
if k == '$schema': continue
|
|
41
|
+
if k in ('maximum', 'minimum') and isinstance(v, int) and abs(v) == BIG_INT: continue
|
|
42
|
+
if k == 'exclusiveMaximum' and v == BIG_INT: continue
|
|
43
|
+
if k == 'description' and isinstance(v, str): out[k] = first_sentence(v, 200); continue
|
|
44
|
+
out[k] = compact_schema(v)
|
|
45
|
+
return out
|
|
46
|
+
if isinstance(node, list): return [compact_schema(v) for v in node]
|
|
47
|
+
return node
|
|
48
|
+
|
|
49
|
+
def compact_tool(tool):
|
|
50
|
+
fn = tool.get('function') if isinstance(tool, dict) else None
|
|
51
|
+
if not isinstance(fn, dict): return tool
|
|
52
|
+
name = fn.get('name', '')
|
|
53
|
+
fn = dict(fn)
|
|
54
|
+
if name in SHORT: fn['description'] = SHORT[name]
|
|
55
|
+
if isinstance(fn.get('parameters'), dict):
|
|
56
|
+
params = compact_schema(fn['parameters'])
|
|
57
|
+
for drop in DROP_PARAMS.get(name, ()):
|
|
58
|
+
(params.get('properties') or {}).pop(drop, None)
|
|
59
|
+
fn['parameters'] = params
|
|
60
|
+
return {**tool, 'function': fn}
|
|
61
|
+
|
|
62
|
+
def compact_tools(tools):
|
|
63
|
+
if not isinstance(tools, list): return tools
|
|
64
|
+
return [compact_tool(t) for t in tools if not (isinstance(t, dict) and (t.get('function') or {}).get('name') in PRUNE)]
|
|
65
|
+
|
|
66
|
+
# Built-in engine skills that are about the engine itself, not the user's work.
|
|
67
|
+
ENGINE_SKILLS = ('check-kimi-code-docs', 'update-config', 'write-goal', 'import-claude', 'mcp-config', 'kimi-')
|
|
68
|
+
|
|
69
|
+
def compact_system(text):
|
|
70
|
+
"""Drop engine-only built-in skills from the skill listing and shorten each listed description to one line."""
|
|
71
|
+
if not isinstance(text, str) or '## Available skills' not in text: return text
|
|
72
|
+
lines = text.split('\n'); out = []; i = 0
|
|
73
|
+
while i < len(lines):
|
|
74
|
+
line = lines[i]
|
|
75
|
+
m = re.match(r'^- ([\w:.-]+): (.*)$', line)
|
|
76
|
+
nxt = lines[i + 1] if i + 1 < len(lines) else ''
|
|
77
|
+
if m and nxt.lstrip().startswith('Path: '):
|
|
78
|
+
name, desc = m.group(1), m.group(2)
|
|
79
|
+
if nxt.strip().startswith('Path: builtin://') and any(name.startswith(p) for p in ENGINE_SKILLS): i += 2; continue
|
|
80
|
+
out.append('- ' + name + ': ' + first_sentence(desc, 140)); out.append(nxt); i += 2; continue
|
|
81
|
+
out.append(line); i += 1
|
|
82
|
+
return '\n'.join(out)
|
|
83
|
+
|
|
84
|
+
def compact_request(body):
|
|
85
|
+
"""Compact tools and the system prompt in place. Returns the number of characters saved."""
|
|
86
|
+
before = len(json.dumps(body.get('tools') or []))
|
|
87
|
+
msgs = body.get('messages') or []
|
|
88
|
+
sys_before = len(msgs[0].get('content') or '') if msgs and msgs[0].get('role') == 'system' and isinstance(msgs[0].get('content'), str) else 0
|
|
89
|
+
if 'tools' in body: body['tools'] = compact_tools(body['tools'])
|
|
90
|
+
if sys_before: msgs[0] = {**msgs[0], 'content': compact_system(msgs[0]['content'])}
|
|
91
|
+
after = len(json.dumps(body.get('tools') or []))
|
|
92
|
+
sys_after = len(msgs[0]['content']) if sys_before else 0
|
|
93
|
+
return (before - after) + (sys_before - sys_after)
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"""Bridge-side completion gates for the Kimi engine.
|
|
2
|
+
|
|
3
|
+
Kimi lets a Stop hook push back only once per run, and hooks fail open. So the checks that decide whether a run is
|
|
4
|
+
really done also run here, after the engine exits, and a failed check resumes the same session for a repair round:
|
|
5
|
+
|
|
6
|
+
- verify: code changed after the last passing check
|
|
7
|
+
- design: the selected design was not checked in a real browser at phone and desktop widths
|
|
8
|
+
- evidence: a research answer's claims are unrecorded or their source links are missing from the answer
|
|
9
|
+
- acceptance: an independent tester writes behavioural tests from the task and the diff, hidden from the builder,
|
|
10
|
+
and the bridge runs them. Only assertion failures count; a broken test file is treated as the tester's fault.
|
|
11
|
+
|
|
12
|
+
Also: deterministic trimming of oversized tool output in model requests (cache-friendly, saves tokens)."""
|
|
13
|
+
import json, os, re, subprocess, sys, time
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
ACCEPTANCE_DIR = 'acceptance'
|
|
17
|
+
TRIM_LIMIT = int(os.getenv('MINI_KIMI_TRIM_CHARS', '16000'))
|
|
18
|
+
ERROR_LINE = re.compile(r'error|fail|exception|traceback|assert|panic|cannot|denied|not found|undefined|expected', re.I)
|
|
19
|
+
|
|
20
|
+
# ---------------------------------------------------------------- output trimming
|
|
21
|
+
ANSI = re.compile(r'\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b\][^\x07]*\x07')
|
|
22
|
+
|
|
23
|
+
def compress_text(text):
|
|
24
|
+
"""Lossless-for-the-model cleanup: ANSI colour codes, carriage-return progress redraws, trailing spaces and runs of
|
|
25
|
+
identical lines. Pure function of the text, so a tool result compresses to the same bytes on every request."""
|
|
26
|
+
if '\x1b' in text: text = ANSI.sub('', text)
|
|
27
|
+
if '\r' in text: text = '\n'.join(l.rsplit('\r', 1)[-1] if '\r' in l.rstrip('\r') else l.rstrip('\r') for l in text.split('\n'))
|
|
28
|
+
lines = text.split('\n'); out = []; i = 0
|
|
29
|
+
while i < len(lines):
|
|
30
|
+
line = lines[i].rstrip(); j = i + 1
|
|
31
|
+
while j < len(lines) and lines[j].rstrip() == line: j += 1
|
|
32
|
+
out.append(line)
|
|
33
|
+
if (j - i - 1) * (len(line) + 1) > 48: out.append('[previous line repeated %d more times]' % (j - i - 1))
|
|
34
|
+
else: out.extend([line] * (j - i - 1))
|
|
35
|
+
i = j
|
|
36
|
+
return '\n'.join(out)
|
|
37
|
+
|
|
38
|
+
def trim_text(text, limit=TRIM_LIMIT):
|
|
39
|
+
"""Keep the head, the tail and the error lines of an oversized tool output. Same input, same output."""
|
|
40
|
+
if not isinstance(text, str): return text, 0
|
|
41
|
+
original = len(text)
|
|
42
|
+
text = compress_text(text)
|
|
43
|
+
if len(text) <= limit: return text, max(0, original - len(text))
|
|
44
|
+
head, tail = text[:limit * 3 // 8], text[-limit * 3 // 8:]
|
|
45
|
+
middle = text[len(head):len(text) - len(tail)]
|
|
46
|
+
errors = [l[:300] for l in middle.splitlines() if ERROR_LINE.search(l)][:40]
|
|
47
|
+
note = '\n[... %d characters trimmed by ML AI Code. Rerun with a narrower command (grep, head, tail, a single test) if you need the middle.' % (len(text) - len(head) - len(tail))
|
|
48
|
+
note += (' Error-like lines from the trimmed part:\n' + '\n'.join(errors) if errors else '') + ' ...]\n'
|
|
49
|
+
out = head + note + tail
|
|
50
|
+
return out, max(0, original - len(out))
|
|
51
|
+
|
|
52
|
+
def trim_messages(body, limit=TRIM_LIMIT):
|
|
53
|
+
"""Trim tool-result messages in an OpenAI chat request in place. Returns characters saved."""
|
|
54
|
+
saved = 0
|
|
55
|
+
for m in body.get('messages') or []:
|
|
56
|
+
if not isinstance(m, dict) or m.get('role') != 'tool': continue
|
|
57
|
+
c = m.get('content')
|
|
58
|
+
if isinstance(c, str):
|
|
59
|
+
m['content'], n = trim_text(c, limit); saved += n
|
|
60
|
+
elif isinstance(c, list):
|
|
61
|
+
for part in c:
|
|
62
|
+
if isinstance(part, dict) and isinstance(part.get('text'), str):
|
|
63
|
+
part['text'], n = trim_text(part['text'], limit); saved += n
|
|
64
|
+
return saved
|
|
65
|
+
|
|
66
|
+
# ---------------------------------------------------------------- acceptance tester
|
|
67
|
+
TESTER_SYSTEM = '''You are an independent acceptance tester. A builder agent changed a repository to satisfy a user's task. You cannot see its reasoning, and it will not see your test. Write ONE small test file that checks the behaviour the task asks for, through the public names visible in the diff.
|
|
68
|
+
|
|
69
|
+
Rules:
|
|
70
|
+
- Test only what the task explicitly requires. Do not test style, internals, logging, or anything the task does not state. Expected values must follow from the task text; if the task does not pin a value, do not assert it.
|
|
71
|
+
- 3 to 8 focused test cases. No network, no sleeping, no servers, no new dependencies, no file writes outside a temporary directory.
|
|
72
|
+
- Python: a unittest module. The repository root is on sys.path, so import modules as the repository does (e.g. `from shop.cart import total`).
|
|
73
|
+
- JavaScript: an ES module using node:test and node:assert/strict. Import repository files with absolute file URLs built from REPO_ROOT, e.g. `const m = await import(REPO_ROOT + '/calc.js')` where `const REPO_ROOT = 'file://' + process.env.ACCEPTANCE_REPO;`. If the module is CommonJS, use createRequire.
|
|
74
|
+
- Prefer expected values stated in the task or in the existing tests. When you compute one, write the arithmetic in a comment and check it twice.
|
|
75
|
+
- If the task is not testable this way (UI-only, docs, config, research, or behaviour you cannot pin down from the task), say so.
|
|
76
|
+
|
|
77
|
+
Reply with JSON only:
|
|
78
|
+
{"applicable": true, "language": "python" | "node", "code": "<the full test file>", "covers": ["requirement 1", "requirement 2"]}
|
|
79
|
+
or {"applicable": false, "reason": "<why>"}'''
|
|
80
|
+
|
|
81
|
+
def _languages(changed):
|
|
82
|
+
langs = set()
|
|
83
|
+
for p in changed:
|
|
84
|
+
if p.endswith('.py'): langs.add('python')
|
|
85
|
+
elif re.search(r'\.(?:m?js|cjs)$', p): langs.add('node')
|
|
86
|
+
return langs
|
|
87
|
+
|
|
88
|
+
def _diff(repo, base, limit=40000):
|
|
89
|
+
import shutil, tempfile
|
|
90
|
+
tmp = tempfile.mkdtemp()
|
|
91
|
+
try:
|
|
92
|
+
# A throwaway index, so new files show up in the diff without touching the user's staging area.
|
|
93
|
+
index = subprocess.run(['git', 'rev-parse', '--git-path', 'index'], cwd=repo, capture_output=True, text=True, timeout=30).stdout.strip()
|
|
94
|
+
env = dict(os.environ, GIT_INDEX_FILE=os.path.join(tmp, 'index'))
|
|
95
|
+
src = Path(repo) / index if index and not os.path.isabs(index) else Path(index or '')
|
|
96
|
+
if src.is_file(): shutil.copy(src, env['GIT_INDEX_FILE'])
|
|
97
|
+
subprocess.run(['git', 'add', '-N', '.'], cwd=repo, env=env, capture_output=True, timeout=30)
|
|
98
|
+
out = subprocess.run(['git', 'diff', base or 'HEAD', '--', '.', ':(exclude)*.lock', ':(exclude)package-lock.json'], cwd=repo, env=env, capture_output=True, text=True, timeout=30).stdout
|
|
99
|
+
except (OSError, subprocess.SubprocessError): return ''
|
|
100
|
+
finally: shutil.rmtree(tmp, ignore_errors=True)
|
|
101
|
+
return out[:limit]
|
|
102
|
+
|
|
103
|
+
def _parse(text):
|
|
104
|
+
text = (text or '').strip()
|
|
105
|
+
m = re.search(r'\{.*\}', text, re.S)
|
|
106
|
+
if not m: return None
|
|
107
|
+
try: return json.loads(m.group(0))
|
|
108
|
+
except ValueError: return None
|
|
109
|
+
|
|
110
|
+
def tester_invalid(language, output):
|
|
111
|
+
"""True when the failure is in the test file itself (import, syntax, collection), not an assertion about behaviour."""
|
|
112
|
+
if language == 'python':
|
|
113
|
+
if re.search(r'^FAIL: ', output, re.M): return False
|
|
114
|
+
return bool(re.search(r'(ImportError|ModuleNotFoundError|SyntaxError|IndentationError|NameError)', output)) or not re.search(r'^ERROR: ', output, re.M)
|
|
115
|
+
if re.search(r'AssertionError|ERR_ASSERTION', output): return False
|
|
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
|
+
|
|
118
|
+
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
|
+
|
|
120
|
+
Reply with JSON only: {"genuine": ["test_name", ...], "wrong": [{"test": "test_name", "why": "..."}]}'''
|
|
121
|
+
|
|
122
|
+
def failing_tests(language, output):
|
|
123
|
+
if language == 'python': return sorted(set(re.findall(r'^(?:FAIL|ERROR): (\w+)', output, re.M)))
|
|
124
|
+
return sorted(set(m.strip() for m in re.findall(r'^\s*not ok \d+ - (.+)$', output, re.M)))
|
|
125
|
+
|
|
126
|
+
def adjudicate(config, model, language, code, output):
|
|
127
|
+
"""Second look at failures: keep only those the task really requires with correct expected values."""
|
|
128
|
+
names = failing_tests(language, output)
|
|
129
|
+
if not names: return names, []
|
|
130
|
+
user = json.dumps({'task': str(config.get('task', ''))[:8000], 'test_file': code[:20000], 'failing_tests': names, 'output': output[-4000:]})
|
|
131
|
+
try: text, _ = model.request([{'role': 'system', 'content': REVIEW_SYSTEM}, {'role': 'user', 'content': user}], max_tokens=3000, allow_tools=False)
|
|
132
|
+
except Exception: return names, []
|
|
133
|
+
verdict = _parse(text) or {}
|
|
134
|
+
genuine = [n for n in names if any(str(g).strip() and (str(g) in n or n in str(g)) for g in verdict.get('genuine') or [])]
|
|
135
|
+
wrong = [w for w in verdict.get('wrong') or [] if isinstance(w, dict)]
|
|
136
|
+
if not genuine and not wrong: return names, [] # unparseable review: keep the failures
|
|
137
|
+
return genuine, wrong
|
|
138
|
+
|
|
139
|
+
def run_acceptance(root, repo, language, code, timeout=180):
|
|
140
|
+
d = Path(root) / ACCEPTANCE_DIR; d.mkdir(exist_ok=True)
|
|
141
|
+
name = 'test_acceptance.py' if language == 'python' else 'acceptance.test.mjs'
|
|
142
|
+
path = d / name; path.write_text(code)
|
|
143
|
+
env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'MINI_VAULT', 'EXA_API_KEY', 'FIRECRAWL_API_KEY', 'VERCEL_OIDC_TOKEN')}
|
|
144
|
+
env.update({'PYTHONPATH': str(repo), 'ACCEPTANCE_REPO': str(repo), 'PYTHONDONTWRITEBYTECODE': '1'})
|
|
145
|
+
cmd = [sys.executable, str(path), '-v'] if language == 'python' else ['node', '--test', str(path)]
|
|
146
|
+
if language == 'python': path.write_text(code if 'unittest.main' in code else code + '\n\nif __name__ == "__main__":\n import unittest; unittest.main()\n')
|
|
147
|
+
try:
|
|
148
|
+
r = subprocess.run(cmd, cwd=repo, env=env, capture_output=True, text=True, timeout=timeout)
|
|
149
|
+
return r.returncode, (r.stdout + '\n' + r.stderr)[-6000:]
|
|
150
|
+
except subprocess.TimeoutExpired:
|
|
151
|
+
return 124, 'Acceptance check timed out after %ds' % timeout
|
|
152
|
+
|
|
153
|
+
def acceptance(config, st, model, root, repo, changed, event):
|
|
154
|
+
"""Write and run the hidden acceptance test once. Returns {status, ...}: passed | failed | skipped | invalid."""
|
|
155
|
+
if os.getenv('MINI_ACCEPTANCE') == '0' or config.get('acceptance') is False: return {'status': 'skipped', 'reason': 'disabled'}
|
|
156
|
+
langs = _languages(changed)
|
|
157
|
+
if not langs: return {'status': 'skipped', 'reason': 'no Python or JavaScript changes'}
|
|
158
|
+
diff = _diff(repo, st.get('base'))
|
|
159
|
+
if not diff.strip(): return {'status': 'skipped', 'reason': 'no diff'}
|
|
160
|
+
files = subprocess.run(['git', 'ls-files', '--cached', '--others', '--exclude-standard'], cwd=repo, capture_output=True, text=True).stdout.splitlines()[:300]
|
|
161
|
+
user = json.dumps({'task': str(config.get('task', ''))[:12000], 'languages_changed': sorted(langs), 'repository_files': files, 'diff': diff})
|
|
162
|
+
event('acceptance', status='running', content='Independent acceptance check: writing tests from the task')
|
|
163
|
+
spec, text = None, ''
|
|
164
|
+
for attempt in range(2): # one retry: reasoning models sometimes run out of tokens or wrap the JSON oddly
|
|
165
|
+
try: text, _ = model.request([{'role': 'system', 'content': TESTER_SYSTEM}, {'role': 'user', 'content': user}], max_tokens=12000, allow_tools=False)
|
|
166
|
+
except Exception as exc: return {'status': 'skipped', 'reason': 'tester unavailable: ' + str(exc)[:200]}
|
|
167
|
+
spec = _parse(text)
|
|
168
|
+
if spec: break
|
|
169
|
+
d = Path(root) / ACCEPTANCE_DIR; d.mkdir(exist_ok=True); (d / 'tester-reply.txt').write_text(str(text)[:60000])
|
|
170
|
+
if not spec or not spec.get('applicable') or spec.get('language') not in langs or not isinstance(spec.get('code'), str) or len(spec['code']) > 40000:
|
|
171
|
+
reason = (spec or {}).get('reason') or 'no usable test'
|
|
172
|
+
event('acceptance', status='skipped', content='Acceptance check not applicable: ' + str(reason)[:300])
|
|
173
|
+
return {'status': 'skipped', 'reason': str(reason)[:300]}
|
|
174
|
+
code, output = run_acceptance(root, repo, spec['language'], spec['code'])
|
|
175
|
+
covers = [str(c)[:200] for c in spec.get('covers') or []][:8]
|
|
176
|
+
if code == 0: status = 'passed'
|
|
177
|
+
elif code != 124 and tester_invalid(spec['language'], output): status = 'invalid'
|
|
178
|
+
else: status = 'failed'
|
|
179
|
+
wrong = []
|
|
180
|
+
if status == 'failed' and code != 124:
|
|
181
|
+
genuine, wrong = adjudicate(config, model, spec['language'], spec['code'], output)
|
|
182
|
+
if not genuine and wrong: status = 'invalid' # Discarding an invalid test is not a passing test.
|
|
183
|
+
elif not genuine: status = 'invalid'
|
|
184
|
+
elif wrong: output = 'Failing checks the task requires: ' + ', '.join(genuine) + '\n(Ignore these, the tester got them wrong: ' + ', '.join(str(w.get('test')) for w in wrong) + ')\n' + output
|
|
185
|
+
event('acceptance', status=status, language=spec['language'], covers=covers, output=output[-3000:],
|
|
186
|
+
ignored=[str(w.get('test')) for w in wrong],
|
|
187
|
+
content={'passed': 'Independent acceptance check passed' + (' (%d tester mistake%s ignored)' % (len(wrong), '' if len(wrong) == 1 else 's') if wrong else ''), 'failed': 'Independent acceptance check failed', 'invalid': 'Acceptance test could not run (tester error); ignored'}[status])
|
|
188
|
+
return {'status': status, 'output': output, 'covers': covers, 'language': spec['language'], 'code': spec['code'], 'wrong': [str(w.get('test')) for w in wrong]}
|
|
189
|
+
|
|
190
|
+
# ---------------------------------------------------------------- final gates
|
|
191
|
+
def code_fingerprint(repo, paths):
|
|
192
|
+
import hashlib
|
|
193
|
+
h = hashlib.sha256()
|
|
194
|
+
for rel in sorted(paths):
|
|
195
|
+
h.update(rel.encode() + b'\0')
|
|
196
|
+
try: h.update((Path(repo) / rel).read_bytes())
|
|
197
|
+
except OSError: h.update(b'<missing>')
|
|
198
|
+
return h.hexdigest()
|
|
199
|
+
|
|
200
|
+
def pre_existing_failure(st, checks, repo, done, event):
|
|
201
|
+
"""When the newest failing check also fails on the base commit, the failure pre-dates this task: report it instead of demanding a fix."""
|
|
202
|
+
if 'preexisting' in done or not st.get('base'): return False
|
|
203
|
+
failing = [c for c in checks if c.get('executed') and c.get('returncode') not in (0, None) and c.get('command')]
|
|
204
|
+
if not failing: return False
|
|
205
|
+
done.add('preexisting')
|
|
206
|
+
newest = max(failing, key=lambda c: c.get('time') or 0)
|
|
207
|
+
try:
|
|
208
|
+
import verification
|
|
209
|
+
result = verification.pre_existing(repo, st['base'], newest['command'])
|
|
210
|
+
except Exception: result = None
|
|
211
|
+
if result:
|
|
212
|
+
st['_preexisting'] = newest['command'][:300]
|
|
213
|
+
event('verification', label='Pre-existing failure', command=newest['command'][:2000], returncode=newest.get('returncode'), executed=True, scope='base', output='The same check fails on the base commit, before this task started.',
|
|
214
|
+
content='The failing check also fails on the base commit; not counted against this task')
|
|
215
|
+
return True
|
|
216
|
+
return False
|
|
217
|
+
|
|
218
|
+
def final_issues(config, st, answer, done, model, root, repo, event, allow_acceptance=True):
|
|
219
|
+
"""Blockers persist until repaired; expensive new acceptance tests are written once."""
|
|
220
|
+
import roles as role_engine
|
|
221
|
+
issues = []
|
|
222
|
+
try: changed = role_engine.changed_files(repo, st.get('base'))
|
|
223
|
+
except Exception: changed = []
|
|
224
|
+
code_changed = [p for p in changed if role_engine.CODE_EXT.search(p)]
|
|
225
|
+
progress = {}
|
|
226
|
+
try: progress = json.loads((Path(root) / 'kimi-progress.json').read_text())
|
|
227
|
+
except (OSError, ValueError): pass
|
|
228
|
+
last_edit, last_check = progress.get('last_edit') or 0, progress.get('last_check') or 0
|
|
229
|
+
checks = []
|
|
230
|
+
try: checks = json.loads((Path(root) / 'checks.json').read_text())
|
|
231
|
+
except (OSError, ValueError): pass
|
|
232
|
+
if code_changed and last_edit and last_check < last_edit:
|
|
233
|
+
blamed = pre_existing_failure(st, checks, repo, done, event)
|
|
234
|
+
if not blamed: issues.append('Code changed (' + ', '.join(code_changed[:6]) + ') after the last passing check. Run the relevant tests or build now and report the real result.')
|
|
235
|
+
try:
|
|
236
|
+
import verification
|
|
237
|
+
commands = []
|
|
238
|
+
try: commands = [c for c in json.loads((Path(root) / 'kimi-commands.json').read_text()) if isinstance(c, dict)]
|
|
239
|
+
except (OSError, ValueError): pass
|
|
240
|
+
record, earlier, scope = [], [], None
|
|
241
|
+
try:
|
|
242
|
+
import work_ledger
|
|
243
|
+
record = work_ledger.entries(root); earlier = work_ledger.statements(root)
|
|
244
|
+
except Exception: pass
|
|
245
|
+
try: scope = json.loads((Path(root) / 'scope.json').read_text())
|
|
246
|
+
except (OSError, ValueError): pass
|
|
247
|
+
# This run's own results count as record too: a denial is checked against everything that ran, not only earlier runs.
|
|
248
|
+
try:
|
|
249
|
+
now = work_ledger.entry(config.get('task', ''), '', checks, json.loads((Path(root) / 'design-browser-evidence.json').read_text()))
|
|
250
|
+
record = record + [now]
|
|
251
|
+
except Exception: pass
|
|
252
|
+
report = verification.report(repo, st.get('base'), answer or '', commands, checks, config.get('task', ''), last_edit=last_edit, edits=progress.get('edits') or 0, scope=scope, record=record, earlier=earlier, asked=set(done))
|
|
253
|
+
(Path(root) / 'verification.json').write_text(json.dumps(report))
|
|
254
|
+
event('verification_report', **{k: report.get(k) for k in ('status', 'reasons', 'findings', 'checks', 'passed', 'stale')})
|
|
255
|
+
for f in report['findings']:
|
|
256
|
+
if f.get('severity') == 'block':
|
|
257
|
+
done.add(f['kind']); issues.append(f['message'])
|
|
258
|
+
# A gate being mentioned once is not evidence it was repaired. The outer
|
|
259
|
+
# bridge bounds repair attempts; unresolved results must survive to it.
|
|
260
|
+
if report['status'] == 'failed' and not issues and not st.get('_preexisting') and not st.get('inspect'):
|
|
261
|
+
issues.append('Repository verification still fails: ' + '; '.join(report['reasons']) + '. Fix and rerun the failing checks, or report the unresolved failure.')
|
|
262
|
+
except Exception as exc:
|
|
263
|
+
issue = 'Verification report could not complete: ' + str(exc)[:200]
|
|
264
|
+
event('note', content=issue); issues.append(issue)
|
|
265
|
+
if config.get('selected_design'):
|
|
266
|
+
from design_director import browser_review
|
|
267
|
+
from quality import fingerprint
|
|
268
|
+
try: records = json.loads((Path(root) / 'design-browser-evidence.json').read_text())
|
|
269
|
+
except (OSError, ValueError): records = []
|
|
270
|
+
report = browser_review(records, fingerprint(repo, st.get('base') or 'HEAD'))
|
|
271
|
+
(Path(root) / 'design-review.json').write_text(json.dumps(report)); event('design_review', designReview=report)
|
|
272
|
+
if report['status'] != 'passed':
|
|
273
|
+
issues.append(report['message'] + ' Start the app with mcp__mlai__preview {"action":"start"}, then run mcp__mlai__browser at width 390 and at width 1280, each with at least one click/fill/press and an assert_text. Fix overflow, console or page errors and failed requests, then check again.')
|
|
274
|
+
from conversation import research_required
|
|
275
|
+
from evidence import EvidenceLedger
|
|
276
|
+
ledger = EvidenceLedger(root, st.get('evidence_key') or 'kimi')
|
|
277
|
+
if research_required(config) or ledger.load()['sources']:
|
|
278
|
+
issue = ledger.completion_issue(answer or '')
|
|
279
|
+
if issue: issues.append(issue.replace('evidence {claims', 'mcp__mlai__evidence {claims'))
|
|
280
|
+
if allow_acceptance and code_changed and not issues:
|
|
281
|
+
previous = st.get('_acceptance')
|
|
282
|
+
if 'acceptance' not in done:
|
|
283
|
+
done.add('acceptance')
|
|
284
|
+
result = acceptance(config, st, model, root, repo, code_changed, event)
|
|
285
|
+
elif previous and previous.get('status') == 'failed' and previous.get('fingerprint') == code_fingerprint(repo, code_changed):
|
|
286
|
+
# The builder kept its code after seeing the failure (it disputes the hidden test). Re-running the same test on the
|
|
287
|
+
# same code cannot change the outcome, so another repair round would only burn time: record the dispute and stop.
|
|
288
|
+
result = {**previous, 'status': 'disputed'}
|
|
289
|
+
event('acceptance', status='disputed', language=previous['language'], covers=previous.get('covers', []), output=previous.get('output', '')[-3000:],
|
|
290
|
+
content='Independent acceptance check disputed: the builder kept its code and explained why; not repeated')
|
|
291
|
+
elif previous and previous.get('status') == 'failed':
|
|
292
|
+
# Rerun the same hidden test after a repair round; do not write a new one.
|
|
293
|
+
code, output = run_acceptance(root, repo, previous['language'], previous['code'])
|
|
294
|
+
status = 'passed' if code == 0 else 'invalid' if code != 124 and tester_invalid(previous['language'], output) else 'failed'
|
|
295
|
+
if status == 'failed' and previous.get('wrong') and set(failing_tests(previous['language'], output)) <= set(previous['wrong']): status = 'invalid'
|
|
296
|
+
result = {**previous, 'status': status, 'output': output}
|
|
297
|
+
event('acceptance', status=status, language=previous['language'], covers=previous.get('covers', []), output=output[-3000:],
|
|
298
|
+
content='Independent acceptance check ' + {'passed': 'passed after the fix', 'failed': 'still failing', 'invalid': 'could not run; ignored'}[status])
|
|
299
|
+
else: result = None
|
|
300
|
+
if result: st['_acceptance'] = {**result, 'fingerprint': code_fingerprint(repo, code_changed)}
|
|
301
|
+
if result and result['status'] == 'failed':
|
|
302
|
+
issues.append('An independent acceptance check, written from the task and hidden from you, failed:\n' + result['output'][-2500:] +
|
|
303
|
+
'\nFix the implementation so it meets the task. Do not look for the test file. If you are confident the check misreads the task, explain exactly why in your final summary instead of changing correct behaviour.')
|
|
304
|
+
return issues
|