mlai-code 0.3.0 → 0.3.2
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 +3 -3
- package/package.json +2 -2
- package/src/distribution.mjs +7 -7
- package/src/engine-key.mjs +18 -4
- package/src/engine.mjs +4 -9
- package/src/home.mjs +1 -1
- package/worker/context_manager.py +3 -3
- package/worker/kimi_bridge.py +72 -14
- package/worker/kimi_gates.py +35 -1
- package/worker/kimi_hook.py +49 -1
- package/worker/obs_mask.py +97 -0
- package/worker/runner.py +15 -9
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mlai-code 0.3.2
|
|
2
2
|
|
|
3
3
|
ML AI Code in your terminal. The agent works in your project on this machine; models run in ML AI Code's cloud.
|
|
4
4
|
|
|
5
|
-
npm install -g
|
|
5
|
+
npm install -g mlai-code
|
|
6
6
|
mlai login
|
|
7
7
|
mlai # interactive session in the current folder
|
|
8
8
|
mlai -p "task" # one task without prompts (scripts, CI)
|
|
9
9
|
mlai cloud --help # cloud workspace commands
|
|
10
10
|
|
|
11
|
-
Update with `npm install -g
|
|
11
|
+
Update with `npm install -g mlai-code@latest` (`mlai update` tells you when one is available).
|
|
12
12
|
Shell completion: `mlai completion bash|zsh|fish|powershell`. Requires Node.js 20 or newer.
|
|
13
13
|
Docs: https://mlai-mini-code.vercel.app/docs
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mlai-code",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "ML AI Code in your terminal: an agent that reads, edits and runs code in your project, on your machine.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -18,4 +18,4 @@
|
|
|
18
18
|
],
|
|
19
19
|
"homepage": "https://mlai-mini-code.vercel.app/downloads",
|
|
20
20
|
"license": "UNLICENSED"
|
|
21
|
-
}
|
|
21
|
+
}
|
package/src/distribution.mjs
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* managed install stages the new build so it starts next time; others print a one-line notice next time. It never
|
|
17
17
|
* runs for -p/--print, --json, non-TTY or CI, and is off with settings autoUpdate=false or MLAI_AUTO_UPDATE=0.
|
|
18
18
|
*/
|
|
19
|
-
import {createHash,
|
|
19
|
+
import {createHash,verify as edVerify} from 'node:crypto';
|
|
20
20
|
import {gunzipSync} from 'node:zlib';
|
|
21
21
|
import fs from 'node:fs';
|
|
22
22
|
import fsp from 'node:fs/promises';
|
|
@@ -24,10 +24,10 @@ import path from 'node:path';
|
|
|
24
24
|
import {spawn} from 'node:child_process';
|
|
25
25
|
import {fileURLToPath} from 'node:url';
|
|
26
26
|
import {VERSION,DEFAULT_SERVER,home} from './home.mjs';
|
|
27
|
-
import {
|
|
27
|
+
import {pinnedPublicKey,ed25519PublicKey} from './engine-key.mjs';
|
|
28
28
|
|
|
29
29
|
export const PROTOCOL=1;
|
|
30
|
-
export const NPM_PACKAGE='
|
|
30
|
+
export const NPM_PACKAGE='mlai-code';
|
|
31
31
|
export const BREW_FORMULA='mlai';
|
|
32
32
|
export const CHECK_EVERY_MS=24*3600_000;
|
|
33
33
|
const KEEP_VERSIONS=3;
|
|
@@ -67,13 +67,13 @@ export function canonical(m){return ['mlai-cli',m.protocol,m.version,m.id,m.sha2
|
|
|
67
67
|
export const sha256=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
68
68
|
|
|
69
69
|
/** Throws unless the manifest is well formed and (when this copy pins a key) signed with that key. */
|
|
70
|
-
export function checkManifest(m,{publicKey=
|
|
70
|
+
export function checkManifest(m,{publicKey=pinnedPublicKey()}={}){
|
|
71
71
|
if(!m||typeof m!=='object')throw new Error('The update server sent no manifest.');
|
|
72
72
|
if(m.protocol!==PROTOCOL)throw new Error('This CLI is too old to read the update manifest. Reinstall: '+installCommand());
|
|
73
73
|
if(!ID.test(String(m.id))||!SHA.test(String(m.sha256))||!Number.isInteger(m.size)||m.size<=0||!m.version)throw new Error('The update manifest is malformed.');
|
|
74
74
|
if(publicKey){
|
|
75
75
|
if(!m.signature)throw new Error('The update is not signed. Refusing to install it.');
|
|
76
|
-
let ok=false;try{ok=edVerify(null,Buffer.from(canonical(m)),
|
|
76
|
+
let ok=false;try{ok=edVerify(null,Buffer.from(canonical(m)),ed25519PublicKey(publicKey),Buffer.from(m.signature,'base64'))}catch{}
|
|
77
77
|
if(!ok)throw new Error('The update signature does not match. Refusing to install it.');
|
|
78
78
|
}
|
|
79
79
|
return m;
|
|
@@ -204,7 +204,7 @@ async function withLock(dir,fn){
|
|
|
204
204
|
/**
|
|
205
205
|
* How this copy was installed, from where its files are:
|
|
206
206
|
* managed ~/.mlai/cli/versions/<id> (install.sh / install.ps1) -> mlai update replaces it
|
|
207
|
-
* npm .../node_modules
|
|
207
|
+
* npm .../node_modules/mlai-code -> npm install -g mlai-code@latest
|
|
208
208
|
* brew .../Cellar/mlai/... -> brew upgrade mlai
|
|
209
209
|
* app inside the Mac app bundle -> updates with the app
|
|
210
210
|
* source a repository checkout -> git pull
|
|
@@ -214,7 +214,7 @@ export function installKind(file=fileURLToPath(import.meta.url),dir=cliDir()){
|
|
|
214
214
|
if(f.startsWith(v))return 'managed';
|
|
215
215
|
const n=f.split(path.sep).join('/');
|
|
216
216
|
if(/\/Cellar\/mlai\//.test(n)||/\/homebrew\/.*\/mlai\//i.test(n)&&!/node_modules/.test(n))return 'brew';
|
|
217
|
-
if(/\/node_modules
|
|
217
|
+
if(/\/node_modules\/mlai-code\//.test(n))return 'npm';
|
|
218
218
|
if(/\.app\/Contents\/Resources\//.test(n))return 'app';
|
|
219
219
|
return 'source';
|
|
220
220
|
}
|
package/src/engine-key.mjs
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
|
-
/* The Ed25519 public key that signs harness updates (lib/laptop-engine.ts)
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/* The Ed25519 public key that signs harness updates (lib/laptop-engine.ts) and CLI release manifests (lib/cli-release.ts),
|
|
2
|
+
base64 of the raw 32 bytes (what publicKeyOf() in lib/laptop-engine.ts prints; key id fc37e74e7423f42f). The server
|
|
3
|
+
signs with MLAI_ENGINE_SIGNING_KEY. A copy with this pin refuses unsigned or badly signed manifests: harness updates
|
|
4
|
+
fall back to the bundled harness, `mlai update` installs nothing. MLAI_ENGINE_PUBLIC_KEY overrides it (tests, CI). */
|
|
5
|
+
import {createPublicKey} from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
export const ENGINE_PUBLIC_KEY='xT81sNCk0xWN5qYJ1C1SFZXk9mFw5Vt53JG9zVLawFA=';
|
|
8
|
+
|
|
9
|
+
const SPKI_PREFIX=Buffer.from('302a300506032b6570032100','hex');
|
|
10
|
+
/** The pinned key as base64: MLAI_ENGINE_PUBLIC_KEY, else ENGINE_PUBLIC_KEY ('' when neither is set). */
|
|
11
|
+
export const pinnedPublicKey=(env=process.env)=>String(env.MLAI_ENGINE_PUBLIC_KEY||ENGINE_PUBLIC_KEY||'').trim();
|
|
12
|
+
/** A KeyObject from base64 of either the raw 32-byte Ed25519 key or its full SPKI DER (44 bytes); throws otherwise. */
|
|
13
|
+
export function ed25519PublicKey(b64){
|
|
14
|
+
const k=Buffer.from(String(b64||'').trim(),'base64');
|
|
15
|
+
if(k.length===32)return createPublicKey({key:Buffer.concat([SPKI_PREFIX,k]),format:'der',type:'spki'});
|
|
16
|
+
if(k.length===44&&k.subarray(0,12).equals(SPKI_PREFIX))return createPublicKey({key:k,format:'der',type:'spki'});
|
|
17
|
+
throw Object.assign(new Error('The pinned update key is not a 32-byte Ed25519 public key'),{code:'engine_key'});
|
|
18
|
+
}
|
package/src/engine.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {createHash,
|
|
1
|
+
import {createHash,verify,randomUUID} from 'node:crypto';
|
|
2
2
|
import {readFile,writeFile,mkdir,rename,rm,readdir,copyFile,stat} from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import {sub,workerDir} from './home.mjs';
|
|
5
|
-
import {
|
|
5
|
+
import {pinnedPublicKey,ed25519PublicKey} from './engine-key.mjs';
|
|
6
6
|
|
|
7
7
|
/* Harness auto-update: runs use the newest worker the server publishes (GET /api/laptop/engine, lib/laptop-engine.ts)
|
|
8
8
|
without an app update, and fall back to the copy bundled with the app on any doubt.
|
|
@@ -14,7 +14,7 @@ import {ENGINE_PUBLIC_KEY} from './engine-key.mjs';
|
|
|
14
14
|
export const ENGINE_PROTOCOL=1;
|
|
15
15
|
export const CHECK_EVERY=10*60*1000,WAIT_MS=3000,DOWNLOAD_MS=60000;
|
|
16
16
|
/** Copy of lib/laptop-engine.ts ENGINE_FILES (parity test): the bundled copy's version is hashed over these. */
|
|
17
|
-
export const ENGINE_FILES=['ask_first.py','attachment_reader.py','background_jobs.py','brief_questions.py','browser_local.py','browser_pane.py','browser_tool.py','build_planner.py','checkpoints.py','command_policy.py','command_safety.py','computer_card.sh','computer_setup.sh','computer_use.py','connector_search.py','context_manager.py','conversation.py','deploy_pack.py','design_director.py','dev_jobs.py','edit_guard.py','env_setup.py','evidence.py','fast_search.py','goals.py','handover.py','harness_memory.py','image_generation.py','intelligence.py','intent_model.py','intent_weights.json','ios_simulator.py','kimi_bridge.py','kimi_compact.py','kimi_gates.py','kimi_hook.py','lifecycle.py','lsp.py','mcp_local.py','memory.py','mlai_auto_review.py','mlai_hooks.py','mlai_mcp.py','mlai_permissions.py','model_stream.py','nested_repos.py','offline_guard.py','preview.py','proc_watch.py','project_context.py','project_rules.py','quality.py','reply_language.py','repo_hygiene.py','repo_memory.py','repo_policy.py','roles.json','roles.py','runner.py','runtime_control.py','secret_scan.py','session_hub.py','skill_library.py','skill_pack.py','subagents.py','task_controller.py','task_skills.py','token_count.py','tool_catalog.py','verification.py','vision.py','wakeups.py','web_tools.py','winproc.py','work_ledger.py','worktrees.py'];
|
|
17
|
+
export const ENGINE_FILES=['ask_first.py','attachment_reader.py','background_jobs.py','brief_questions.py','browser_local.py','browser_pane.py','browser_tool.py','build_planner.py','checkpoints.py','command_policy.py','command_safety.py','computer_card.sh','computer_setup.sh','computer_use.py','connector_search.py','context_manager.py','conversation.py','deploy_pack.py','design_director.py','dev_jobs.py','edit_guard.py','env_setup.py','evidence.py','fast_search.py','goals.py','handover.py','harness_memory.py','image_generation.py','intelligence.py','intent_model.py','intent_weights.json','ios_simulator.py','kimi_bridge.py','kimi_compact.py','kimi_gates.py','kimi_hook.py','lifecycle.py','lsp.py','mcp_local.py','memory.py','mlai_auto_review.py','mlai_hooks.py','mlai_mcp.py','mlai_permissions.py','model_stream.py','nested_repos.py','obs_mask.py','offline_guard.py','preview.py','proc_watch.py','project_context.py','project_rules.py','quality.py','reply_language.py','repo_hygiene.py','repo_memory.py','repo_policy.py','roles.json','roles.py','runner.py','runtime_control.py','secret_scan.py','session_hub.py','skill_library.py','skill_pack.py','subagents.py','task_controller.py','task_skills.py','token_count.py','tool_catalog.py','verification.py','vision.py','wakeups.py','web_tools.py','winproc.py','work_ledger.py','worktrees.py'];
|
|
18
18
|
/** The entry scripts; a manifest without them is not a harness. */
|
|
19
19
|
export const REQUIRED=['runner.py','kimi_bridge.py'];
|
|
20
20
|
const NAME=/^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/,SHA=/^[a-f0-9]{64}$/,VERSION=/^[a-f0-9]{32}$/;
|
|
@@ -32,13 +32,8 @@ export function bundleHash(entries){
|
|
|
32
32
|
export function canonicalManifest(m){
|
|
33
33
|
return JSON.stringify({v:1,protocol:m.protocol,version:m.version,files:[...m.files].sort((a,b)=>a.path<b.path?-1:a.path>b.path?1:0).map(f=>[f.path,f.sha256,f.size])});
|
|
34
34
|
}
|
|
35
|
-
const SPKI=Buffer.from('302a300506032b6570032100','hex');
|
|
36
35
|
/** The pinned Ed25519 public key: MLAI_ENGINE_PUBLIC_KEY, else the one shipped in engine-key.mjs, else none. */
|
|
37
|
-
export function pinnedKey(){
|
|
38
|
-
const raw=String(process.env.MLAI_ENGINE_PUBLIC_KEY||ENGINE_PUBLIC_KEY||'').trim();if(!raw)return null;
|
|
39
|
-
const key=Buffer.from(raw,'base64');if(key.length!==32)throw fail('The pinned engine key is not a 32-byte Ed25519 key','engine_key');
|
|
40
|
-
return createPublicKey({key:Buffer.concat([SPKI,key]),format:'der',type:'spki'});
|
|
41
|
-
}
|
|
36
|
+
export function pinnedKey(){const raw=pinnedPublicKey();return raw?ed25519PublicKey(raw):null}
|
|
42
37
|
/** Shape, limits and (with a pinned key) the signature; returns a clean copy. Throws plain errors with a code. */
|
|
43
38
|
export function checkManifest(m,{key=pinnedKey()}={}){
|
|
44
39
|
if(!m||typeof m!=='object')throw fail('The server sent no engine manifest','engine_manifest');
|
package/src/home.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import {mkdirSync,chmodSync,statSync} from 'node:fs';
|
|
4
4
|
import {fileURLToPath} from 'node:url';
|
|
5
5
|
|
|
6
|
-
export const VERSION='0.3.
|
|
6
|
+
export const VERSION='0.3.2';
|
|
7
7
|
export const DEFAULT_SERVER='https://mlai-mini-code.vercel.app';
|
|
8
8
|
/** All local state lives here; nothing is written into the user's repository except what the agent edits. */
|
|
9
9
|
export function home(){
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Selective
|
|
1
|
+
"""Selective working-context compaction (180K by default, MINI_COMPACT_AT; never above the model window) with recoverable original messages."""
|
|
2
2
|
import json,math,time
|
|
3
3
|
from pathlib import Path
|
|
4
4
|
from token_count import TokenCounter
|
|
@@ -26,8 +26,8 @@ class ContextManager:
|
|
|
26
26
|
if before<self.threshold:return current
|
|
27
27
|
# The system, original task and recent messages (at least eight when available) remain byte-for-byte intact.
|
|
28
28
|
end=len(messages)-min(8,max(0,len(messages)-self.end))
|
|
29
|
-
while end>self.end and len(messages)-end<16 and self.count_tokens(messages[end-1:])*self.ratio<48000:end-=1
|
|
30
|
-
if end<=self.end:raise RuntimeError('Context exceeds
|
|
29
|
+
while end>self.end and len(messages)-end<16 and self.count_tokens(messages[end-1:])*self.ratio<min(48000,self.threshold*3//8):end-=1
|
|
30
|
+
if end<=self.end:raise RuntimeError('Context exceeds the compaction threshold but contains no safely compactable older span. Split the task; recent context was not discarded.')
|
|
31
31
|
old=list(messages[self.end:end])
|
|
32
32
|
# Keep human corrections verbatim; tool observations in this harness are JSON user messages.
|
|
33
33
|
pinned=list(self.pinned);compressible=[]
|
package/worker/kimi_bridge.py
CHANGED
|
@@ -11,6 +11,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
11
11
|
import runner as R
|
|
12
12
|
import computer_use
|
|
13
13
|
import kimi_compact
|
|
14
|
+
import obs_mask
|
|
14
15
|
import verification
|
|
15
16
|
import handover
|
|
16
17
|
STALL_SECONDS = int(os.getenv('MINI_KIMI_STALL_SECONDS', '60'))
|
|
@@ -31,7 +32,8 @@ import kimi_gates
|
|
|
31
32
|
KIMI_DEFAULT_VERSION = '2.5.2'
|
|
32
33
|
_requested = os.getenv('MINI_KIMI_VERSION', '')
|
|
33
34
|
KIMI_VERSION = _requested if re.fullmatch(r'\d+\.\d+\.\d+', _requested) else KIMI_DEFAULT_VERSION # canary upgrades are opt-in per account
|
|
34
|
-
|
|
35
|
+
# Repair rounds after the final checks fail (MINI_REPAIR_ROUNDS; quality first, so the default stays at two).
|
|
36
|
+
MAX_REPAIR_ROUNDS = max(0, int(os.getenv('MINI_REPAIR_ROUNDS', '2') or 2))
|
|
35
37
|
MAX_RESTARTS = 1
|
|
36
38
|
def default_runtime():
|
|
37
39
|
"""Where the pinned Kimi Code CLI is installed. Laptop runs share one copy under ~/.mlai (no /vercel/sandbox there)."""
|
|
@@ -101,7 +103,7 @@ class ModelProxy:
|
|
|
101
103
|
self.reasoning = 0
|
|
102
104
|
self.billable = 0
|
|
103
105
|
self.token = pysecrets.token_hex(24)
|
|
104
|
-
self.calls = self.prompt = self.completion = self.cached = self.trimmed = 0
|
|
106
|
+
self.calls = self.prompt = self.completion = self.cached = self.trimmed = self.masked = 0
|
|
105
107
|
self.inference = 0.0
|
|
106
108
|
self.lock = threading.Lock()
|
|
107
109
|
self.last_error = ''
|
|
@@ -138,6 +140,9 @@ class ModelProxy:
|
|
|
138
140
|
if self.provider == 'pixis' and 'grok' in self.model: body.pop('include_reasoning', None) # the gateway rejects it for Grok (HTTP 400)
|
|
139
141
|
kimi_compact.compact_request(body)
|
|
140
142
|
saved = kimi_gates.trim_messages(body)
|
|
143
|
+
masked = obs_mask.mask_body(body) # old tool outputs become pointers (stepwise, so the cached prefix holds)
|
|
144
|
+
if masked:
|
|
145
|
+
with self.lock: self.masked += masked
|
|
141
146
|
effort = None if 'reasoning_effort' in body else pick_effort(self.effort, body, self.provider, self.calls)
|
|
142
147
|
if effort:
|
|
143
148
|
body['reasoning_effort'] = effort
|
|
@@ -281,6 +286,52 @@ def hub_env(env=None):
|
|
|
281
286
|
return out if out.get('MLAI_HUB_URL') else {}
|
|
282
287
|
|
|
283
288
|
|
|
289
|
+
RESERVED_CONTEXT = 40000
|
|
290
|
+
|
|
291
|
+
def context_size(config):
|
|
292
|
+
"""Kimi compacts at min(0.85 x max_context_size, max_context_size - reserved). MINI_COMPACT_AT (default 180K tokens)
|
|
293
|
+
sets where compaction starts. Never above the model's window: a 200K-window model still compacts at 160K so the
|
|
294
|
+
reply always has room."""
|
|
295
|
+
try: at = max(32000, int(os.getenv('MINI_COMPACT_AT', '180000')))
|
|
296
|
+
except ValueError: at = 180000
|
|
297
|
+
size = max(at + RESERVED_CONTEXT, -(-at * 100 // 85))
|
|
298
|
+
try: window = int(config.get('contextWindow') or 0)
|
|
299
|
+
except (TypeError, ValueError): window = 0
|
|
300
|
+
return min(size, window) if window >= 64000 else size
|
|
301
|
+
|
|
302
|
+
def end_reason_for(run_state, result, state):
|
|
303
|
+
"""Why a run ended, stored with its usage so token spend can be split by outcome (e.g. verification_incomplete)."""
|
|
304
|
+
if result.get('exit_status') == 'VerificationIncomplete': return 'verification_incomplete'
|
|
305
|
+
if run_state == 'limited': return 'limited_' + ('tokens' if (state.get('limit_kind') or 'time') == 'tokens' else 'time')
|
|
306
|
+
if (ROOT / 'budget-declined').exists(): return 'budget_declined'
|
|
307
|
+
return run_state if run_state in ('completed', 'stopped', 'error', 'interrupted') else 'completed'
|
|
308
|
+
|
|
309
|
+
def side_model(config, model):
|
|
310
|
+
"""The fast side model for short structured calls when the server sent one (priced gateway picks only)."""
|
|
311
|
+
spec = config.get('sideModel')
|
|
312
|
+
if not isinstance(spec, dict) or not spec.get('endpoint') or not spec.get('model') or not os.environ.get('SIDE_MODEL_API_KEY'): return model
|
|
313
|
+
cfg = {**config, 'endpoint': str(spec['endpoint']), 'model': str(spec['model']), 'provider': str(spec.get('provider') or 'isoquant'),
|
|
314
|
+
'_key_env': 'SIDE_MODEL_API_KEY', 'reasoningEffort': 'low', 'streaming': False, 'images': None}
|
|
315
|
+
cfg.pop('creditRates', None) # billed at the default model's weights
|
|
316
|
+
try: return R.Model(cfg)
|
|
317
|
+
except Exception: return model
|
|
318
|
+
|
|
319
|
+
def run_usage(proxy, model, end_reason='', side=None):
|
|
320
|
+
"""Per-run usage record (mini_runs.usage): billable model tokens, raw prompt/cached/completion counts and the end reason."""
|
|
321
|
+
p = proxy
|
|
322
|
+
helpers = [m for m in (model, side if side is not model else None) if m is not None]
|
|
323
|
+
out = {'model': int((p.tokens if p else 0) + sum(getattr(m, 'tokens', 0) or 0 for m in helpers)), 'tools': int(p.calls if p else 0)}
|
|
324
|
+
if p:
|
|
325
|
+
out.update(prompt=int(p.prompt), cached=int(p.cached), completion=int(p.completion), masked=int(p.masked))
|
|
326
|
+
for m in helpers:
|
|
327
|
+
met = getattr(m, 'metrics', None) or {}
|
|
328
|
+
if met.get('prompt_tokens'):
|
|
329
|
+
out['prompt'] = out.get('prompt', 0) + int(met.get('prompt_tokens') or 0)
|
|
330
|
+
out['cached'] = out.get('cached', 0) + int(met.get('cached_tokens') or 0)
|
|
331
|
+
out['completion'] = out.get('completion', 0) + int(met.get('completion_tokens') or 0)
|
|
332
|
+
if end_reason: out['endReason'] = end_reason
|
|
333
|
+
return out
|
|
334
|
+
|
|
284
335
|
def write_home(config, proxy, mode, readonly, steps):
|
|
285
336
|
HOME.mkdir(parents=True, exist_ok=True)
|
|
286
337
|
hook = sys.executable + ' ' + str(Path(__file__).resolve().parent / 'kimi_hook.py')
|
|
@@ -291,8 +342,8 @@ def write_home(config, proxy, mode, readonly, steps):
|
|
|
291
342
|
'extra_skill_dirs = [' + toml_str(HOME / 'skills') + ']',
|
|
292
343
|
'',
|
|
293
344
|
'[providers.mlai]', 'type = "openai"', 'base_url = ' + toml_str('http://127.0.0.1:%d/v1' % proxy.port), 'api_key_env = "MINI_KIMI_PROXY_TOKEN"', '',
|
|
294
|
-
'[models."mlai/main"]', 'provider = "mlai"', 'model = ' + toml_str(config.get('model') or 'glm'), 'max_context_size =
|
|
295
|
-
'[loop_control]', 'max_steps_per_turn = %d' % max(10, steps), 'max_attempts_per_step = 4', 'reserved_context_size =
|
|
345
|
+
'[models."mlai/main"]', 'provider = "mlai"', 'model = ' + toml_str(config.get('model') or 'glm'), 'max_context_size = %d' % context_size(config), 'capabilities = ["tool_use"]', '',
|
|
346
|
+
'[loop_control]', 'max_steps_per_turn = %d' % max(10, steps), 'max_attempts_per_step = 4', 'reserved_context_size = %d' % RESERVED_CONTEXT, '',
|
|
296
347
|
'[task]', 'bash_auto_background_on_timeout = true', 'print_background_mode = "exit"', '',
|
|
297
348
|
'[mcp]', 'startup_timeout_ms = 20000', 'tool_timeout_ms = 600000', '',
|
|
298
349
|
'[tools]', 'disabled = ["CronCreate", "CronDelete", "CronList", "AskUserQuestion", "ExitPlanMode", "FetchURL", "AgentSwarm", "CreateGoal", "GetGoal", "SetGoalBudget", "UpdateGoal"' + (', "Write", "Edit", "Bash", "Agent"' if readonly else '') + ']', '',
|
|
@@ -473,7 +524,7 @@ def network_note(config):
|
|
|
473
524
|
def workspace_note(config, event):
|
|
474
525
|
"""Jobs (restart the ones that died), project setup and environment facts for the agent's instructions."""
|
|
475
526
|
note = ''
|
|
476
|
-
env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'VERCEL_OIDC_TOKEN', 'EXA_API_KEY', 'FIRECRAWL_API_KEY', 'MINI_VAULT')}
|
|
527
|
+
env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'SIDE_MODEL_API_KEY', 'VERCEL_OIDC_TOKEN', 'EXA_API_KEY', 'FIRECRAWL_API_KEY', 'MINI_VAULT')}
|
|
477
528
|
env.update(VAULT)
|
|
478
529
|
try:
|
|
479
530
|
import dev_jobs
|
|
@@ -712,11 +763,14 @@ def main():
|
|
|
712
763
|
research_proxy = start_research_proxy()
|
|
713
764
|
write('status.json', {'state': 'running', 'started': time.time(), 'boot': boot_id(), 'process': identity(os.getpid()), 'harness': 'kimi'})
|
|
714
765
|
(ROOT / 'kimi-stderr.log').unlink(missing_ok=True)
|
|
715
|
-
for name in ('kimi-state.json', 'kimi-progress.json', 'kimi-hook.json', 'usage.json', 'kimi-stop-count', 'kimi-approve-all', 'design-browser-evidence.json', 'design-review.json', 'acceptance.json'): (ROOT / name).unlink(missing_ok=True)
|
|
766
|
+
for name in ('kimi-state.json', 'kimi-progress.json', 'kimi-hook.json', 'usage.json', 'kimi-stop-count', 'kimi-approve-all', 'design-browser-evidence.json', 'design-review.json', 'acceptance.json', 'budget-declined'): (ROOT / name).unlink(missing_ok=True)
|
|
716
767
|
shutil.rmtree(ROOT / 'acceptance', ignore_errors=True)
|
|
717
768
|
model = R.Model(config)
|
|
769
|
+
side = side_model(config, model) # routing, acceptance tester, review, goal checks
|
|
718
770
|
proxy = None; proc = None; mapper = None; started = time.monotonic()
|
|
719
|
-
def totals():
|
|
771
|
+
def totals():
|
|
772
|
+
extra = side if side is not model else None
|
|
773
|
+
return {'tokens': model.tokens + (extra.tokens if extra else 0) + (proxy.tokens if proxy else 0), 'calls': model.calls + (extra.calls if extra else 0) + (proxy.calls if proxy else 0)}
|
|
720
774
|
try:
|
|
721
775
|
event('harness', name='kimi', version=KIMI_VERSION)
|
|
722
776
|
start_handover(config)
|
|
@@ -729,7 +783,7 @@ def main():
|
|
|
729
783
|
config['conversation_history'] = history
|
|
730
784
|
if selected:
|
|
731
785
|
config['conversation_route'] = 'build'; routing = {'route': 'build', 'mode': config.get('mode', 'approve'), 'fallback': False}
|
|
732
|
-
else: routing = route_conversation(config,
|
|
786
|
+
else: routing = route_conversation(config, side, history, shell=SHELL_ALWAYS)
|
|
733
787
|
write('conversation-route.json', routing); event('intent', intent=routing['route'], mode=routing['mode'], fallback=routing['fallback'], source=routing.get('source', 'model'))
|
|
734
788
|
role = resolve_role(config)
|
|
735
789
|
if role_engine.design_allowed(role) or config.get('selected_design'): prepare_design(config, model, ROOT, event) # never pauses the run
|
|
@@ -784,7 +838,7 @@ def main():
|
|
|
784
838
|
'account_left': account_left(config)}
|
|
785
839
|
save_state(state, static=True)
|
|
786
840
|
for stale in ('scope.json', 'kimi-commands.json', 'verification.json', 'kimi-last-text.json'): (ROOT / stale).unlink(missing_ok=True)
|
|
787
|
-
env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'VERCEL_OIDC_TOKEN', 'EXA_API_KEY', 'FIRECRAWL_API_KEY', 'MINI_VAULT')}
|
|
841
|
+
env = {k: v for k, v in os.environ.items() if k not in ('MODEL_API_KEY', 'SIDE_MODEL_API_KEY', 'VERCEL_OIDC_TOKEN', 'EXA_API_KEY', 'FIRECRAWL_API_KEY', 'MINI_VAULT')}
|
|
788
842
|
env.update(VAULT)
|
|
789
843
|
env.update({'KIMI_CODE_HOME': str(HOME), 'KIMI_DISABLE_TELEMETRY': '1', 'MINI_KIMI_PROXY_TOKEN': proxy.token, 'MINI_ROOT': str(ROOT), 'MINI_REPO': REPO, 'NO_COLOR': '1', 'CI': '1'})
|
|
790
844
|
prior = None if config.get('_handover') else resumable_session(readonly, role_id, handover.model_key(config))
|
|
@@ -864,7 +918,7 @@ def main():
|
|
|
864
918
|
continue
|
|
865
919
|
if not mapper.last_text: break
|
|
866
920
|
if os.getenv('MINI_KIMI_FINAL_GATES', '1') == '0': break
|
|
867
|
-
try: issues = kimi_gates.final_issues(config, state, mapper.last_text, gates_done,
|
|
921
|
+
try: issues = kimi_gates.final_issues(config, state, mapper.last_text, gates_done, side, ROOT, REPO, event, allow_acceptance=not readonly and not state.get('inspect'))
|
|
868
922
|
except Exception as exc:
|
|
869
923
|
unresolved = ['Final verification could not complete: ' + str(exc)[:200]]
|
|
870
924
|
event('note', content=unresolved[0]); break
|
|
@@ -876,7 +930,7 @@ def main():
|
|
|
876
930
|
if rounds: event('final_gate', state='passed', round=rounds, content='Final checks passed after %d repair round%s' % (rounds, '' if rounds == 1 else 's'))
|
|
877
931
|
if goal:
|
|
878
932
|
# Goal mode: the checks passed; keep working until the verifier says the goal holds.
|
|
879
|
-
_, note = goals.evaluate(ROOT, REPO,
|
|
933
|
+
_, note = goals.evaluate(ROOT, REPO, side, mapper.last_text, totals()['tokens'], event)
|
|
880
934
|
if note and mapper.session and time_left() > 240:
|
|
881
935
|
previous = mapper.last_text; mapper.last_text = ''; rounds = 0
|
|
882
936
|
code, reason = launch(resume(note + '\nThen write your final message again from scratch, complete and self-contained.'))
|
|
@@ -973,11 +1027,15 @@ def main():
|
|
|
973
1027
|
if answer: event('answer', content=answer)
|
|
974
1028
|
open_questions = unshown_questions()
|
|
975
1029
|
if open_questions: event('questions', questions=open_questions)
|
|
976
|
-
|
|
1030
|
+
end_reason = end_reason_for(run_state, result, state)
|
|
1031
|
+
write('status.json', {'state': run_state, 'result': result, **({'error': str(report.get('verification') or 'Verification incomplete')[:1500]} if result.get('exit_status') == 'VerificationIncomplete' else {}), **totals(), 'endReason': end_reason, 'usage': run_usage(proxy, model, end_reason, side)})
|
|
977
1032
|
write('completion.json', report); event('completion', **report); event('finished', state=run_state)
|
|
978
1033
|
except Exception as exc:
|
|
979
1034
|
message = str(exc).replace(os.environ.get('MODEL_API_KEY', '__absent__') or '__absent__', '[redacted]')
|
|
980
|
-
|
|
1035
|
+
final = 'stopped' if stopped() else 'error'
|
|
1036
|
+
try: usage = run_usage(locals().get('proxy'), locals().get('model'), final, locals().get('side'))
|
|
1037
|
+
except Exception: usage = None
|
|
1038
|
+
write('status.json', {'state': final, 'error': message, **totals(), 'endReason': final, **({'usage': usage} if usage else {})})
|
|
981
1039
|
event('error', content=message)
|
|
982
1040
|
finally:
|
|
983
1041
|
proc = proc or (locals().get('holder') or {}).get('proc')
|
|
@@ -988,7 +1046,7 @@ def main():
|
|
|
988
1046
|
wall = time.monotonic() - started
|
|
989
1047
|
p = proxy
|
|
990
1048
|
event('run_metrics', engine='kimi', calls=totals()['calls'], wall_seconds=round(wall, 2), inference_seconds=round(p.inference if p else 0, 2), tool_seconds=round(max(0.0, wall - (p.inference if p else 0)), 2),
|
|
991
|
-
prompt_tokens=p.prompt if p else 0, trimmed_chars=p.trimmed if p else 0, cached_tokens=p.cached if p else 0, completion_tokens=p.completion if p else 0, cache_hit_rate=round(p.cached / p.prompt, 3) if p and p.prompt else 0,
|
|
1049
|
+
prompt_tokens=p.prompt if p else 0, trimmed_chars=p.trimmed if p else 0, masked_chars=p.masked if p else 0, cached_tokens=p.cached if p else 0, completion_tokens=p.completion if p else 0, cache_hit_rate=round(p.cached / p.prompt, 3) if p and p.prompt else 0,
|
|
992
1050
|
reasoning_tokens=p.reasoning if p else 0, effort=config.get('reasoningEffort', 'medium') if p else '', effort_steps=dict(p.effort_counts) if p else {})
|
|
993
1051
|
except Exception: pass
|
|
994
1052
|
if proxy: proxy.close()
|
package/worker/kimi_gates.py
CHANGED
|
@@ -14,7 +14,7 @@ import json, os, re, subprocess, sys, time
|
|
|
14
14
|
from pathlib import Path
|
|
15
15
|
|
|
16
16
|
ACCEPTANCE_DIR = 'acceptance'
|
|
17
|
-
TRIM_LIMIT = int(os.getenv('MINI_KIMI_TRIM_CHARS', '
|
|
17
|
+
TRIM_LIMIT = int(os.getenv('MINI_KIMI_TRIM_CHARS', '12000'))
|
|
18
18
|
ERROR_LINE = re.compile(r'error|fail|exception|traceback|assert|panic|cannot|denied|not found|undefined|expected', re.I)
|
|
19
19
|
|
|
20
20
|
# ---------------------------------------------------------------- output trimming
|
|
@@ -215,6 +215,35 @@ def pre_existing_failure(st, checks, repo, done, event):
|
|
|
215
215
|
return True
|
|
216
216
|
return False
|
|
217
217
|
|
|
218
|
+
# Off by default: every code change gets the independent acceptance check. MINI_SMALL_CHANGE_LINES=N skips it for
|
|
219
|
+
# one-file edits of at most N lines that already have a passing check after the last edit.
|
|
220
|
+
SMALL_CHANGE_LINES = int(os.getenv('MINI_SMALL_CHANGE_LINES', '0') or 0)
|
|
221
|
+
|
|
222
|
+
def changed_lines(repo, base, paths):
|
|
223
|
+
"""Added + removed lines in tracked files plus the length of new untracked files."""
|
|
224
|
+
total = 0
|
|
225
|
+
try:
|
|
226
|
+
out = subprocess.run(['git', 'diff', '--numstat', base or 'HEAD', '--', *paths], cwd=repo, capture_output=True, text=True, timeout=15)
|
|
227
|
+
seen = set()
|
|
228
|
+
for line in out.stdout.splitlines():
|
|
229
|
+
parts = line.split('\t')
|
|
230
|
+
if len(parts) == 3:
|
|
231
|
+
seen.add(parts[2]); total += sum(int(x) if x.isdigit() else 200 for x in parts[:2])
|
|
232
|
+
for p in paths:
|
|
233
|
+
if p not in seen:
|
|
234
|
+
try: total += len((Path(repo) / p).read_text(errors='replace').splitlines())
|
|
235
|
+
except OSError: pass
|
|
236
|
+
except (OSError, subprocess.SubprocessError, ValueError): return 10 ** 6
|
|
237
|
+
return total
|
|
238
|
+
|
|
239
|
+
def small_change(repo, base, code_changed, checks, last_edit, last_check):
|
|
240
|
+
"""One code file, at most SMALL_CHANGE_LINES changed lines, and a passing check that ran after the last edit."""
|
|
241
|
+
if SMALL_CHANGE_LINES <= 0 or len(code_changed) != 1 or not last_edit or last_check < last_edit: return False
|
|
242
|
+
try: from runtime_control import check_status
|
|
243
|
+
except Exception: return False
|
|
244
|
+
if not any(isinstance(c, dict) and check_status(c) == 'passed' for c in checks or []): return False
|
|
245
|
+
return changed_lines(repo, base, code_changed) <= SMALL_CHANGE_LINES
|
|
246
|
+
|
|
218
247
|
def final_issues(config, st, answer, done, model, root, repo, event, allow_acceptance=True):
|
|
219
248
|
"""Blockers persist until repaired; expensive new acceptance tests are written once."""
|
|
220
249
|
import roles as role_engine
|
|
@@ -277,6 +306,11 @@ def final_issues(config, st, answer, done, model, root, repo, event, allow_accep
|
|
|
277
306
|
if research_required(config) or ledger.load()['sources']:
|
|
278
307
|
issue = ledger.completion_issue(answer or '')
|
|
279
308
|
if issue: issues.append(issue.replace('evidence {claims', 'mcp__mlai__evidence {claims'))
|
|
309
|
+
if allow_acceptance and code_changed and not issues and 'acceptance' not in done and small_change(repo, st.get('base'), code_changed, checks, last_edit, last_check):
|
|
310
|
+
# A small edit with a passing check after it: writing and running a hidden test costs a model call and a test run
|
|
311
|
+
# for little extra signal. Larger changes still get the independent check.
|
|
312
|
+
done.add('acceptance'); st['_acceptance_skipped'] = True
|
|
313
|
+
event('acceptance', status='skipped', content='Independent acceptance check skipped: small change with a passing check after the last edit')
|
|
280
314
|
if allow_acceptance and code_changed and not issues:
|
|
281
315
|
previous = st.get('_acceptance')
|
|
282
316
|
if 'acceptance' not in done:
|
package/worker/kimi_hook.py
CHANGED
|
@@ -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')
|
|
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')
|
|
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})
|
|
@@ -214,6 +214,52 @@ def account_gate(st, name):
|
|
|
214
214
|
block('Your account has used all its credits. Do not start new work or call more tools. Write the final summary now in plain text: what changed, '
|
|
215
215
|
'what was verified, what is left. The user can continue after picking a plan or buying a top-up. This call was not run.')
|
|
216
216
|
|
|
217
|
+
# Helper agents re-read the repository from scratch: on small tasks they multiplied tokens (one trivial test started
|
|
218
|
+
# four) without finishing sooner. Unless the user asked for them, small tasks get none and large tasks at most three.
|
|
219
|
+
HELPER_ASK = re.compile(r'\b(sub-?agents?|helpers?|in parallel|parallel agents?|delegat\w*|swarm|agents)\b', re.I)
|
|
220
|
+
HELPER_CAP_LARGE = int(os.getenv('MINI_HELPER_CAP', '3') or 0)
|
|
221
|
+
|
|
222
|
+
def helper_gate(name, args, st):
|
|
223
|
+
if name not in ('Agent', 'AgentSwarm') or os.getenv('MINI_HELPER_GATE', '1') == '0': return
|
|
224
|
+
if args.get('resume') or HELPER_ASK.search(st.get('task') or ''): return
|
|
225
|
+
used = int(st.get('helpers') or 0)
|
|
226
|
+
cap = HELPER_CAP_LARGE if st.get('large') else 0
|
|
227
|
+
if used >= cap:
|
|
228
|
+
event('note', content='Helper agent not started: %s' % ('small task, done directly' if not cap else 'limit of %d helpers reached' % cap))
|
|
229
|
+
block(('This task is small enough to do directly. ' if not cap else 'You already started %d helper agents for this task. ' % used) +
|
|
230
|
+
'Do the work yourself with Read, Grep, Edit and Bash (several reads can go in one response). Helpers start cold and cost far more tokens. This call was not run.')
|
|
231
|
+
st['helpers'] = used + 1; save_state(st)
|
|
232
|
+
|
|
233
|
+
# Optional soft per-run budget, off by default (users are not asked about tokens). With MINI_RUN_SOFT_BUDGET set, past
|
|
234
|
+
# that many raw tokens the user is asked once whether to continue; each approval allows another budget's worth.
|
|
235
|
+
def soft_budget():
|
|
236
|
+
try: return max(0, int(os.getenv('MINI_RUN_SOFT_BUDGET', '0') or 0))
|
|
237
|
+
except ValueError: return 0
|
|
238
|
+
|
|
239
|
+
def budget_gate(st, name):
|
|
240
|
+
budget = soft_budget()
|
|
241
|
+
if not budget or name in ('TodoList', 'WaitFor', 'TaskOutput', 'TaskList'): return
|
|
242
|
+
if st.get('budget_declined'):
|
|
243
|
+
if name in ('Write', 'Edit', 'Bash', 'Agent', 'AgentSwarm'):
|
|
244
|
+
block('The user chose to stop this task at its token budget. Do not start new work. Write the final summary now: what changed, what was verified, what is left and the exact next step.')
|
|
245
|
+
return
|
|
246
|
+
u = load('usage.json', {}); used = u.get('raw') or u.get('tokens') or 0
|
|
247
|
+
threshold = st.get('budget_next') or budget
|
|
248
|
+
if used < threshold: return
|
|
249
|
+
request = {'id': os.urandom(8).hex(), 'kind': 'budget', 'command': 'This task has used %s tokens (budget %s). Continue working?' % (format(int(used), ','), format(int(threshold), ',')),
|
|
250
|
+
'title': 'Token budget reached', 'used': int(used), 'budget': int(threshold)}
|
|
251
|
+
policy_event('soft_budget', 'ask', 'Asked the user whether to continue past %s tokens' % format(int(threshold), ','))
|
|
252
|
+
try:
|
|
253
|
+
wait_decision(request, seconds=540, strict=True)
|
|
254
|
+
except SystemExit:
|
|
255
|
+
st['budget_declined'] = True; save_state(st)
|
|
256
|
+
try: (ROOT / 'budget-declined').write_text('1')
|
|
257
|
+
except OSError: pass
|
|
258
|
+
policy_event('soft_budget', 'stop', 'Stopped at the token budget')
|
|
259
|
+
block('The user chose to stop this task at its token budget (or did not answer). Do not start new work. Write the final summary now: what changed, what was verified, what is left and the exact next step, so the task can be resumed.')
|
|
260
|
+
st['budget_next'] = int(used) + budget; save_state(st)
|
|
261
|
+
policy_event('soft_budget', 'continue', 'User approved another %s tokens' % format(budget, ','))
|
|
262
|
+
|
|
217
263
|
def token_gate(pol, st, name):
|
|
218
264
|
"""Per-task token budget from policy: at 80% wrap up (Plan-style, no new work), at 100% stop and summarize."""
|
|
219
265
|
account_gate(st, name)
|
|
@@ -336,6 +382,8 @@ def pre(payload, st):
|
|
|
336
382
|
auto = mode == 'auto' or (ROOT / 'kimi-approve-all').exists()
|
|
337
383
|
runaway(name, args, st)
|
|
338
384
|
token_gate(pol, st, name)
|
|
385
|
+
budget_gate(st, name)
|
|
386
|
+
helper_gate(name, args, st)
|
|
339
387
|
if name == 'Bash':
|
|
340
388
|
command = str(args.get('command', ''))
|
|
341
389
|
try:
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Observation masking: old tool outputs are replaced by a short pointer before a model request.
|
|
2
|
+
|
|
3
|
+
Agents re-send every earlier tool output on every call, so a long run pays for the same file reads and test logs
|
|
4
|
+
again and again. Research on coding agents (JetBrains, Dec 2025; arXiv 2508.21433) found that hiding observations
|
|
5
|
+
older than the last ~10 turns keeps solve rates while roughly halving cost.
|
|
6
|
+
|
|
7
|
+
Rules, all pure functions of the message list so the provider's prefix cache keeps hitting:
|
|
8
|
+
- Only tool outputs are masked. System, user and assistant messages (the agent's reasoning and calls) stay intact.
|
|
9
|
+
- The latest KEEP outputs are never touched, nor outputs shorter than MIN_CHARS.
|
|
10
|
+
- Nothing happens until the request is large (FROM_CHARS), so short tasks are unchanged.
|
|
11
|
+
- The masked boundary moves in STEP-sized jumps: between jumps the request prefix is byte-identical call to call.
|
|
12
|
+
"""
|
|
13
|
+
import json, os, re
|
|
14
|
+
|
|
15
|
+
ENABLED = os.getenv('MINI_MASK', '1') != '0'
|
|
16
|
+
KEEP = max(1, int(os.getenv('MINI_MASK_KEEP', '10')))
|
|
17
|
+
STEP = max(1, int(os.getenv('MINI_MASK_STEP', '8')))
|
|
18
|
+
MIN_CHARS = int(os.getenv('MINI_MASK_MIN_CHARS', '600'))
|
|
19
|
+
FROM_CHARS = int(os.getenv('MINI_MASK_FROM_CHARS', '60000'))
|
|
20
|
+
HEAD = 240
|
|
21
|
+
ERROR_LINE = re.compile(r'error|fail|exception|traceback|assert|panic|denied|not found', re.I)
|
|
22
|
+
MARK = '[Earlier tool output hidden to save context'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _text(content):
|
|
26
|
+
if isinstance(content, str): return content
|
|
27
|
+
if isinstance(content, list):
|
|
28
|
+
out = []
|
|
29
|
+
for part in content:
|
|
30
|
+
if isinstance(part, dict):
|
|
31
|
+
if isinstance(part.get('text'), str): out.append(part['text'])
|
|
32
|
+
elif part.get('type') in ('image_url', 'input_image', 'image'): out.append('[image]' + ' ' * 2000) # images count as large
|
|
33
|
+
elif isinstance(part, str): out.append(part)
|
|
34
|
+
return '\n'.join(out)
|
|
35
|
+
return '' if content is None else str(content)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _json_observation(text):
|
|
39
|
+
if not text.startswith('{'): return None
|
|
40
|
+
try: data = json.loads(text)
|
|
41
|
+
except (ValueError, TypeError): return None
|
|
42
|
+
return data if isinstance(data, dict) and ('output' in data or 'returncode' in data) else None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def is_observation(message, json_observations=False):
|
|
46
|
+
if not isinstance(message, dict): return False
|
|
47
|
+
if message.get('role') == 'tool': return True
|
|
48
|
+
# The mini harness sends tool results as JSON user messages ({"returncode": .., "output": ..}).
|
|
49
|
+
return bool(json_observations and message.get('role') == 'user' and _json_observation(_text(message.get('content'))))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def placeholder(text):
|
|
53
|
+
"""A deterministic pointer: size, the first line or two and up to three error-like lines."""
|
|
54
|
+
if text.startswith(MARK): return text
|
|
55
|
+
clean = text.replace('\r', '')
|
|
56
|
+
head = clean[:HEAD].strip()
|
|
57
|
+
if len(clean) > HEAD: head = head.rsplit('\n', 1)[0] if '\n' in head else head
|
|
58
|
+
errors = [l.strip()[:160] for l in clean[HEAD:].splitlines() if ERROR_LINE.search(l)][:3]
|
|
59
|
+
note = MARK + ' (%d characters). It began:\n%s' % (len(text), head)
|
|
60
|
+
if errors: note += '\nError-like lines:\n' + '\n'.join(errors)
|
|
61
|
+
return note + '\nRun the tool again if you need the full output.]'
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def mask_messages(messages, keep=None, step=None, min_chars=None, from_chars=None, json_observations=False, enabled=None):
|
|
65
|
+
"""Returns (messages, characters_saved). The input list is not modified; masked entries are shallow copies."""
|
|
66
|
+
keep = KEEP if keep is None else keep
|
|
67
|
+
step = STEP if step is None else step
|
|
68
|
+
min_chars = MIN_CHARS if min_chars is None else min_chars
|
|
69
|
+
from_chars = FROM_CHARS if from_chars is None else from_chars
|
|
70
|
+
if not (ENABLED if enabled is None else enabled) or not isinstance(messages, list): return messages, 0
|
|
71
|
+
obs = [i for i, m in enumerate(messages) if is_observation(m, json_observations)]
|
|
72
|
+
if len(obs) <= keep: return messages, 0
|
|
73
|
+
if sum(len(_text(m.get('content'))) for m in messages if isinstance(m, dict)) < from_chars: return messages, 0
|
|
74
|
+
count = ((len(obs) - keep) // step) * step
|
|
75
|
+
if count <= 0: return messages, 0
|
|
76
|
+
out, saved = list(messages), 0
|
|
77
|
+
for i in obs[:count]:
|
|
78
|
+
m = messages[i]; text = _text(m.get('content'))
|
|
79
|
+
if len(text) < min_chars or text.startswith(MARK): continue
|
|
80
|
+
if json_observations and m.get('role') == 'user':
|
|
81
|
+
data = _json_observation(text) or {}
|
|
82
|
+
short = dict(data); short['output'] = placeholder(str(data.get('output', '')))
|
|
83
|
+
new = json.dumps(short)
|
|
84
|
+
else:
|
|
85
|
+
new = placeholder(text)
|
|
86
|
+
if len(new) >= len(text): continue
|
|
87
|
+
copy = dict(m); copy['content'] = new; out[i] = copy
|
|
88
|
+
saved += len(text) - len(new)
|
|
89
|
+
return out, saved
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def mask_body(body, **kw):
|
|
93
|
+
"""In-place on an OpenAI chat request (the Kimi model proxy). Returns characters saved."""
|
|
94
|
+
msgs = body.get('messages') if isinstance(body, dict) else None
|
|
95
|
+
new, saved = mask_messages(msgs, **kw)
|
|
96
|
+
if saved: body['messages'] = new
|
|
97
|
+
return saved
|
package/worker/runner.py
CHANGED
|
@@ -14,6 +14,8 @@ import computer_use
|
|
|
14
14
|
from conversation import enabled as conversation_enabled, readonly as conversation_readonly, route as route_conversation, answer_text, recent_history, READONLY_SYSTEM, READ_TOOLS, WORKFLOW_GUIDANCE, research_required, honor_no_change, NO_CHANGE_NOTE
|
|
15
15
|
from lifecycle import boot_id, identity, nested_guidance, load
|
|
16
16
|
from context_manager import ContextManager, read_archive
|
|
17
|
+
import obs_mask
|
|
18
|
+
OUTPUT_CAP=int(os.getenv('MINI_OUTPUT_CAP','12000') or 12000) # one command's output in the model context (the full log is kept)
|
|
17
19
|
from web_tools import WebResearch
|
|
18
20
|
from runtime_control import request_json, completion_report, approve_operation, safe_command, clip
|
|
19
21
|
from quality import RepairTracker, review as final_review, fingerprint, verification_scope, check_verdict, verification_gate, junit_verdict, report_stamp
|
|
@@ -328,7 +330,8 @@ class Model:
|
|
|
328
330
|
self.transport_note=''
|
|
329
331
|
self.workspace_tool_names=set()
|
|
330
332
|
self.fenced_successes=0
|
|
331
|
-
|
|
333
|
+
compact_at=int(os.getenv('MINI_COMPACT_AT','180000') or 180000) # where compaction starts (never above the model window)
|
|
334
|
+
threshold=min(compact_at,max(16000,int(config['contextWindow'])-int(config.get('responseReserve',32768)))) if config.get('contextWindow') else compact_at
|
|
332
335
|
self.context = ContextManager(ROOT,event,threshold=threshold)
|
|
333
336
|
self.pending_vision=None
|
|
334
337
|
self.vision_disabled=not config.get('vision') or bool(config.get('subagent'))
|
|
@@ -377,6 +380,8 @@ class Model:
|
|
|
377
380
|
if seen:self.pending_vision=seen
|
|
378
381
|
clean = [{'role': m['role'] if m['role'] in ('system','user','assistant') else 'user', 'content': m.get('content','')} for m in messages]
|
|
379
382
|
clean = self.context.prepare(clean,self.summarize)
|
|
383
|
+
clean,masked = obs_mask.mask_messages(clean,json_observations=True) # old observations become pointers
|
|
384
|
+
if masked:self.metrics['masked_chars']=self.metrics.get('masked_chars',0)+masked
|
|
380
385
|
review_wrapup=self.config.get('subagent') and self.config.get('mode')=='plan' and self.calls>=max(1,limit-2)
|
|
381
386
|
if review_wrapup:
|
|
382
387
|
clean.append({'role':'user','content':'Review time is nearly exhausted. Stop inspecting. Return concise findings from the evidence already read, with file references and concrete counterexamples. State unverified requirements and incomplete inspection explicitly. Do not claim a clean review without evidence. Return prose only, no tools or code fences.'})
|
|
@@ -544,7 +549,7 @@ class Model:
|
|
|
544
549
|
if self.config.get('tools') and (not conversation_readonly(self.config) or read_only_tools(self.config.get('tools'))):tools.append({'type':'function','function':{'name':'connected_tool','description':'Request an explicitly enabled integration tool (in a read-only turn, only its reading tools); requires user approval.','parameters':{'type':'object','properties':{'connection':{'type':'string'},'name':{'type':'string'},'arguments':{'type':'object','additionalProperties':True}},'required':['connection','name','arguments'],'additionalProperties':False}}})
|
|
545
550
|
if allow_tools and self.native_tool_failures<2:payload['tools']=tools
|
|
546
551
|
if allow_tools:self.workspace_tool_names=set(next((t['function']['parameters']['properties']['name'].get('enum',[]) for t in tools if t['function']['name']=='workspace_tool'),[]))-{'edit_file','verify'}
|
|
547
|
-
key = os.environ.get('MODEL_API_KEY','')
|
|
552
|
+
key = os.environ.get(self.config.get('_key_env') or 'MODEL_API_KEY','') # the side model has its own key
|
|
548
553
|
headers={'Authorization': 'Bearer ' + key, 'Content-Type':'application/json'}
|
|
549
554
|
suffix='/chat/completions'
|
|
550
555
|
if self.config.get('provider') == 'openai':payload['max_completion_tokens']=payload.pop('max_tokens')
|
|
@@ -1469,7 +1474,7 @@ class Environment(LocalEnvironment):
|
|
|
1469
1474
|
if not approved: return {'returncode':1,'output':'User rejected the command. Choose another approach.'}
|
|
1470
1475
|
event('command', command=command)
|
|
1471
1476
|
# Never pass model credentials into generated shell commands.
|
|
1472
|
-
env = {k:v for k,v in os.environ.items() if k not in ('MODEL_API_KEY','VERCEL_OIDC_TOKEN','EXA_API_KEY','FIRECRAWL_API_KEY','OPENROUTER_API_KEY','MINI_IMAGE_URL','MINI_IMAGE_TOKEN','MINI_RESEARCH_TOKEN')}
|
|
1477
|
+
env = {k:v for k,v in os.environ.items() if k not in ('MODEL_API_KEY','SIDE_MODEL_API_KEY','VERCEL_OIDC_TOKEN','EXA_API_KEY','FIRECRAWL_API_KEY','OPENROUTER_API_KEY','MINI_IMAGE_URL','MINI_IMAGE_TOKEN','MINI_RESEARCH_TOKEN')}
|
|
1473
1478
|
env.update(VAULT)
|
|
1474
1479
|
# The preview iframe reaches dev servers through the sandbox's public *.vercel.run host; Vite rejects unknown hosts otherwise.
|
|
1475
1480
|
env.setdefault('__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS','.vercel.run')
|
|
@@ -1490,9 +1495,9 @@ class Environment(LocalEnvironment):
|
|
|
1490
1495
|
time.sleep(.1)
|
|
1491
1496
|
proc.wait()
|
|
1492
1497
|
size = os.fstat(log.fileno()).st_size
|
|
1493
|
-
log.seek(max(0, size-
|
|
1494
|
-
output = log.read(
|
|
1495
|
-
if size >
|
|
1498
|
+
log.seek(max(0, size-OUTPUT_CAP))
|
|
1499
|
+
output = log.read(OUTPUT_CAP).decode('utf-8',errors='replace')
|
|
1500
|
+
if size > OUTPUT_CAP: output = '[Earlier output truncated; the full log is saved under command-logs]\n' + output
|
|
1496
1501
|
if interrupted: output += '\nCommand stopped or exceeded time/output limit.'
|
|
1497
1502
|
saved_log=None
|
|
1498
1503
|
if size>12000 or time.time()-start>=30:
|
|
@@ -1569,7 +1574,8 @@ def run_usage(model,environment):
|
|
|
1569
1574
|
"""Usage by category for the Settings breakdown (model and subagent tokens; counts for the rest)."""
|
|
1570
1575
|
counts=getattr(environment,'usage_counts',None) or {}
|
|
1571
1576
|
return {'model':int(getattr(model,'tokens',0) or 0),'subagents':int(getattr(environment,'subagent_tokens',0) or 0),'images':int(counts.get('images',0)),
|
|
1572
|
-
'research':int(getattr(environment,'web_attempts',0) or 0),'connectors':int(counts.get('connectors',0)),'tools':int(counts.get('tools',0))
|
|
1577
|
+
'research':int(getattr(environment,'web_attempts',0) or 0),'connectors':int(counts.get('connectors',0)),'tools':int(counts.get('tools',0)),
|
|
1578
|
+
**{k:int((getattr(model,'metrics',None) or {}).get(src,0) or 0) for k,src in (('prompt','prompt_tokens'),('cached','cached_tokens'),('completion','completion_tokens'),('masked','masked_chars'))}}
|
|
1573
1579
|
|
|
1574
1580
|
def main():
|
|
1575
1581
|
config = json.loads((ROOT / 'request.json').read_text())
|
|
@@ -1749,7 +1755,7 @@ def main():
|
|
|
1749
1755
|
result = agent.run(task, tool_catalog=catalog_text)
|
|
1750
1756
|
if goal:result=goal_loop(agent,model,config,result)
|
|
1751
1757
|
state = 'completed' if result.get('exit_status') == 'Submitted' else 'limited'
|
|
1752
|
-
write('status.json', {'state':state,'result':result,**({'error':result.get('submission','Verification incomplete')} if result.get('exit_status')=='VerificationIncomplete' else {}),'tokens':model.tokens+getattr(environment,'subagent_tokens',0),'calls':model.calls+getattr(environment,'subagent_calls',0),'usage':run_usage(model,environment)})
|
|
1758
|
+
write('status.json', {'state':state,'result':result,**({'error':result.get('submission','Verification incomplete')} if result.get('exit_status')=='VerificationIncomplete' else {}),'tokens':model.tokens+getattr(environment,'subagent_tokens',0),'calls':model.calls+getattr(environment,'subagent_calls',0),'usage':{**run_usage(model,environment),'endReason':'verification_incomplete' if result.get('exit_status')=='VerificationIncomplete' else ('limited_'+str(result.get('limit') or 'time')) if state=='limited' else state}})
|
|
1753
1759
|
if state=='completed' and config.get('mode')!='plan' and not config.get('subagent'):
|
|
1754
1760
|
try:
|
|
1755
1761
|
from build_planner import sync_plan_progress
|
|
@@ -1777,7 +1783,7 @@ def main():
|
|
|
1777
1783
|
if (ROOT/'cancel').exists():
|
|
1778
1784
|
import goals
|
|
1779
1785
|
goals.stop(ROOT)
|
|
1780
|
-
write('status.json', {'state':'stopped' if (ROOT/'cancel').exists() else 'error','error':message,'tokens':model.tokens+getattr(environment,'subagent_tokens',0),'calls':model.calls+getattr(environment,'subagent_calls',0),'usage':run_usage(model,environment)})
|
|
1786
|
+
write('status.json', {'state':'stopped' if (ROOT/'cancel').exists() else 'error','error':message,'tokens':model.tokens+getattr(environment,'subagent_tokens',0),'calls':model.calls+getattr(environment,'subagent_calls',0),'usage':{**run_usage(model,environment),'endReason':'stopped' if (ROOT/'cancel').exists() else 'error'}})
|
|
1781
1787
|
event('error', content=message)
|
|
1782
1788
|
finally:
|
|
1783
1789
|
try:
|