mlai-code 0.3.5 → 0.3.6
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/cli.mjs +6 -6
- package/src/headless.mjs +1 -1
- package/src/home.mjs +1 -1
- package/src/render.mjs +10 -3
- package/src/tui/ansi.mjs +47 -7
- package/src/tui/app.mjs +8 -7
- package/src/tui/markdown.mjs +58 -18
- package/src/tui/transcript.mjs +15 -10
- package/worker/browser_tool.py +12 -2
- package/worker/conversation.py +46 -0
- package/worker/kimi_bridge.py +51 -11
- package/worker/kimi_gates.py +1 -1
- package/worker/kimi_hook.py +19 -1
- package/worker/mlai_hooks.py +1 -1
- package/worker/mlai_mcp.py +11 -0
- package/worker/runner.py +55 -4
- package/worker/runtime_control.py +78 -4
- package/worker/verification.py +24 -7
package/README.md
CHANGED
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -25,7 +25,7 @@ import {SANDBOXES} from './sandbox.mjs';
|
|
|
25
25
|
import {restrictedForced,refusedFlag} from './restricted.mjs';
|
|
26
26
|
import {gitForWindows} from './winenv.mjs';
|
|
27
27
|
import {ensureRuntime,runtimeInfo,PY_VERSION,hideEngineName} from './runtime.mjs';
|
|
28
|
-
import {palette,renderEvent,approvalView,summaryLine,exitCode,EXIT,fmt,credits} from './render.mjs';
|
|
28
|
+
import {palette,colorAllowed,renderEvent,approvalView,summaryLine,exitCode,EXIT,fmt,credits} from './render.mjs';
|
|
29
29
|
import {runHeadless,streamInputs,stdinLines,toolRules,loadSchema,HeadlessError} from './headless.mjs';
|
|
30
30
|
import {terminals,backgroundCommand} from './tui/bgterm.mjs';
|
|
31
31
|
|
|
@@ -261,14 +261,14 @@ export async function main(argv=process.argv.slice(2)){
|
|
|
261
261
|
// `mlai review ...` has its own flags (src/tui/review-cli.mjs).
|
|
262
262
|
// `mlai agents ...` and `mlai attach <id>`: background agents (src/tui/agents-cli.mjs), with their own flags.
|
|
263
263
|
if(argv[0]==='agents'||argv[0]==='attach')return await agentsCli(argv);
|
|
264
|
-
if(argv[0]==='review')return await (await import('./tui/review-cli.mjs')).reviewMain(argv.slice(1),{cloud:o=>cloudFor(o),color:palette(
|
|
264
|
+
if(argv[0]==='review')return await (await import('./tui/review-cli.mjs')).reviewMain(argv.slice(1),{cloud:o=>cloudFor(o),color:palette(!argv.includes('--json')&&colorAllowed(process.stdout))});
|
|
265
265
|
if(EXT_COMMANDS[argv[0]])return await EXT_COMMANDS[argv[0]](argv.slice(1));
|
|
266
266
|
let o;
|
|
267
267
|
try{o=parseArgs(argv)}catch(e){if(jsonArgv(argv))process.stdout.write(JSON.stringify({type:'result',state:'error',subtype:'error_usage',error:e.message,exitCode:EXIT.usage})+'\n');else process.stderr.write(e.message+'\n');return EXIT.usage}
|
|
268
268
|
const cmd=COMMANDS.includes(o._[0])?o._.shift():null;
|
|
269
269
|
if(!cmd){const slip=commandSlip(o);if(slip){process.stderr.write(slip+'\n');return EXIT.usage}}
|
|
270
|
-
const c=palette(process.stdout
|
|
271
|
-
const err=palette(process.stderr
|
|
270
|
+
const c=palette(!o.json&&colorAllowed(process.stdout));
|
|
271
|
+
const err=palette(colorAllowed(process.stderr));
|
|
272
272
|
try{
|
|
273
273
|
if(cmd==='help'){process.stdout.write((SUB_HELP[o._[0]]||HELP)+'\n');return 0}
|
|
274
274
|
if(cmd==='version'){process.stdout.write(VERSION+'\n');return 0}
|
|
@@ -523,7 +523,7 @@ async function headless(o,c,err,task,s){
|
|
|
523
523
|
|
|
524
524
|
async function interactive(o,c,s,first){
|
|
525
525
|
const cloud=await cloudFor(o);
|
|
526
|
-
const err=palette(process.stderr
|
|
526
|
+
const err=palette(colorAllowed(process.stderr));
|
|
527
527
|
const settings=settingsFor(o,s,err),chat=new Chat({session:s,settings,cloud,flags:chatFlags(o,settings)});
|
|
528
528
|
await registry.loadCommands({warn:m=>process.stderr.write(err.yellow(m)+'\n')});
|
|
529
529
|
chat.loadModels().then(()=>{if(o.model)return chat.setModel(o.model)}).catch(e=>process.stderr.write(err.yellow(e.message)+'\n'));
|
|
@@ -645,7 +645,7 @@ async function plainLoop(o,c,chat,first){
|
|
|
645
645
|
const renderDeps=()=>({renderEvent,approvalView,summaryLine,exitCode});
|
|
646
646
|
async function agentsCli(argv){
|
|
647
647
|
const [{agentsMain,attachMain},{findSessions}]=await Promise.all([import('./tui/agents-cli.mjs'),import('./tui/cmd-core.mjs')]);
|
|
648
|
-
const c=palette(
|
|
648
|
+
const c=palette(!argv.includes('--json')&&colorAllowed(process.stdout));
|
|
649
649
|
const deps={Session,sessionsDir:path.join(home(),'sessions'),findSessions,render:renderDeps(),c,out:t=>process.stdout.write(t+'\n'),stdin:process.stdin,stdout:process.stdout};
|
|
650
650
|
try{return argv[0]==='attach'?await attachMain(argv.slice(1),deps):await agentsMain(argv.slice(1),deps)}
|
|
651
651
|
catch(e){process.stderr.write((e.usage?'':'error: ')+friendly(e)+'\n');return e.usage?EXIT.usage:EXIT.error}
|
package/src/headless.mjs
CHANGED
|
@@ -204,7 +204,7 @@ export async function runHeadless(o,{s,chat,err,inputs,out=t=>process.stdout.wri
|
|
|
204
204
|
if(exit!==EXIT.completed)code=exit;
|
|
205
205
|
last={type:'result',subtype,state:r.state,exitCode:exit,sessionId:s.meta.id,name:s.meta.title||null,mode:chat.mode,model:chat.model||chat.info?.id||null,effort:chat.effort,text,
|
|
206
206
|
...(schema?{structuredOutput:structured??null}:{}),error:subtype==='success'?null:(r.error||null),usage:r.usage||null,
|
|
207
|
-
credits:creditsFor(spent),turns,changedFiles:r.completion?.changedFiles||[],checksPassed:r.completion?.checksPassed||0,checksFailed:r.completion?.checksFailed||0,rejectedApprovals:refused,
|
|
207
|
+
credits:creditsFor(spent),turns,changedFiles:r.completion?.changedFiles||[],checksPassed:(r.completion?.checksPassed||0)+(r.completion?.supportingChecksPassed||0),checksFailed:(r.completion?.checksFailed||0)+(r.completion?.supportingChecksFailed||0),rejectedApprovals:refused,
|
|
208
208
|
persisted:!o.noSessionPersistence,
|
|
209
209
|
// BUG-0555: with -w the work is in its own worktree; say where, so scripts and people can find it.
|
|
210
210
|
...(s.meta.worktree&&!s.meta.worktree.removed?{worktree:{path:s.meta.worktree.path,branch:s.meta.worktree.branch}}:{})};
|
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.6';
|
|
7
7
|
// BUG-0469: the production CLI signs in and calls the production domain.
|
|
8
8
|
export const DEFAULT_SERVER='https://code.ml.ai';
|
|
9
9
|
/** All local state lives here; nothing is written into the user's repository except what the agent edits. */
|
package/src/render.mjs
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
// Turns harness events into terminal output. Pure functions so they can be tested without a terminal.
|
|
2
|
+
// Colors for line output (mlai -p, mlai agents, mlai review): on only when colorAllowed says so (a terminal, or
|
|
3
|
+
// FORCE_COLOR; never with NO_COLOR, TERM=dumb or a pipe). Same meaning as the TUI's palette: gray = tool activity and
|
|
4
|
+
// logs, green = success, yellow = needs attention, red = errors.
|
|
5
|
+
export {colorAllowed} from './tui/ansi.mjs';
|
|
2
6
|
const ESC=s=>'\x1b['+s+'m';
|
|
3
7
|
export function palette(on){
|
|
4
8
|
const w=(a,b)=>s=>on?ESC(a)+s+ESC(b):String(s);
|
|
@@ -45,6 +49,7 @@ export function renderEvent(e,{c=palette(false),verbose=false}={}){
|
|
|
45
49
|
case 'output':{
|
|
46
50
|
if(e.returncode===0&&!verbose)return null;
|
|
47
51
|
const tail=String(e.output||'').trim().split('\n').slice(verbose?-20:-4).join('\n');
|
|
52
|
+
if(e.blocked)return c.yellow('not run: '+tail); // refused before it ran: there is no exit code (CLI 0.3.4)
|
|
48
53
|
return e.returncode===0?c.gray(tail):c.yellow('exit '+e.returncode+(tail?'\n'+tail:''));
|
|
49
54
|
}
|
|
50
55
|
case 'verification':{const ok=e.verdict?.status==='passed'||(e.verdict==null&&e.returncode===0);return (ok?c.green('check passed'):c.red('check '+(e.verdict?.status||'failed')))+' '+clip(e.label,120)}
|
|
@@ -58,8 +63,10 @@ export function renderEvent(e,{c=palette(false),verbose=false}={}){
|
|
|
58
63
|
const total=Math.max(files.length,Number(e.changedFilesTotal)||0);
|
|
59
64
|
const parts=[];if(files.length)parts.push(total+' file'+(total===1?'':'s')+' changed: '+clip(files.join(', '),300));
|
|
60
65
|
// A turn that ran no checks has nothing to report: a question shows no "0 checks passed" line, and a change says so plainly.
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
// Evidence checks an inspect-only turn ran count too: they ran and passed or failed (CLI 0.3.4: "0 checks passed").
|
|
67
|
+
const passed=(Number(e.checksPassed)||0)+(Number(e.supportingChecksPassed)||0),failed=(Number(e.checksFailed)||0)+(Number(e.supportingChecksFailed)||0);
|
|
68
|
+
if(passed)parts.push(passed+' check'+(passed===1?'':'s')+' passed'+(failed?', '+failed+' failed':''));
|
|
69
|
+
else if(failed)parts.push(failed+' check'+(failed===1?'':'s')+' failed');
|
|
63
70
|
else if(files.length)parts.push('no checks ran');
|
|
64
71
|
return parts.length?c.dim(parts.join(' · ')):null;
|
|
65
72
|
}
|
|
@@ -82,5 +89,5 @@ export function exitCode(result){
|
|
|
82
89
|
export function summaryLine(result,c=palette(false)){
|
|
83
90
|
const u=result.usage||{};
|
|
84
91
|
const state=result.state==='completed'&&result.cannotComplete?c.yellow('Could not be done'):result.state==='completed'?c.green('Done'):result.state==='limited'?c.yellow('Needs attention'):result.state==='stopped'?c.yellow('Stopped'):c.red('Failed');
|
|
85
|
-
return state+c.dim(' · '+credits(u.tokens)+' · '+fmt(u.calls)+' model calls');
|
|
92
|
+
return state+c.dim(' · '+credits(u.tokens)+' · '+fmt(u.calls)+(Number(u.calls)===1?' model call':' model calls'));
|
|
86
93
|
}
|
package/src/tui/ansi.mjs
CHANGED
|
@@ -33,12 +33,15 @@ export function wrap(text,cols){
|
|
|
33
33
|
if(width(line)<=cols){out.push(line);continue}
|
|
34
34
|
let cur='',w=0,active='',lastSpace=-1,lastSpaceW=0;
|
|
35
35
|
const tokens=line.match(new RegExp(SGR.source+'|[\\s\\S]','gu'))||[];
|
|
36
|
-
for(
|
|
36
|
+
for(let i=0;i<tokens.length;i++){
|
|
37
|
+
const t=tokens[i];
|
|
37
38
|
if(t.startsWith('\x1b')){cur+=t;active=/\x1b\[0?m/.test(t)?'':active+t;continue}
|
|
38
39
|
const cw=charWidth(t);
|
|
39
40
|
if(w+cw>cols){
|
|
40
41
|
if(/\s/.test(t)){out.push(cur+(active?'\x1b[0m':''));cur=active;w=0;lastSpace=-1;continue}
|
|
41
|
-
|
|
42
|
+
// Break at the last space so words stay whole; only a word longer than a whole line (a long URL) is split,
|
|
43
|
+
// and then only when breaking at the space would leave the line mostly empty.
|
|
44
|
+
if(lastSpace>0&&(lastSpaceW>cols/3||wordWidth(tokens,i,w-lastSpaceW-1)<=cols)){out.push(cur.slice(0,lastSpace)+(active?'\x1b[0m':''));cur=active+cur.slice(lastSpace+1);w=width(cur)}
|
|
42
45
|
else{out.push(cur+(active?'\x1b[0m':''));cur=active;w=0}
|
|
43
46
|
lastSpace=-1;
|
|
44
47
|
}
|
|
@@ -49,6 +52,8 @@ export function wrap(text,cols){
|
|
|
49
52
|
}
|
|
50
53
|
return out;
|
|
51
54
|
}
|
|
55
|
+
/** Columns of the word that has `sofar` columns before token i and continues to the next whitespace. */
|
|
56
|
+
function wordWidth(tokens,i,sofar){let w=sofar;for(;i<tokens.length;i++){const t=tokens[i];if(t.startsWith('\x1b'))continue;if(/\s/.test(t))break;w+=charWidth(t)}return w}
|
|
52
57
|
/** Cuts a styled string to `cols` columns, adding an ellipsis when it was longer. */
|
|
53
58
|
export function truncate(s,cols,ellipsis='…'){
|
|
54
59
|
s=String(s??'');if(width(s)<=cols)return s;
|
|
@@ -66,13 +71,38 @@ export const pad=(s,cols)=>s+' '.repeat(Math.max(0,cols-width(s)));
|
|
|
66
71
|
* A theme with color:false returns text unchanged for every role (NO_COLOR, TERM=dumb, pipes).
|
|
67
72
|
*/
|
|
68
73
|
const THEMES={
|
|
69
|
-
dark:{text:'39',dim:'90',faint:'2',bold:'1',italic:'3',under:'4',accent:'36',accent2:'35',ok:'32',warn:'
|
|
74
|
+
dark:{text:'39',dim:'90',faint:'2',bold:'1',italic:'3',under:'4',accent:'36',accent2:'35',ok:'32',warn:'38;5;208',bad:'31',
|
|
70
75
|
add:'32',del:'31',addBg:'48;5;22',delBg:'48;5;52',hunk:'36',border:'90',borderFocus:'36',borderShell:'35',borderPlan:'34',
|
|
71
|
-
code:'38;5;180',keyword:'38;5;176',string:'38;5;114',number:'38;5;209',comment:'38;5;245',fn:'38;5;117',
|
|
76
|
+
code:'38;5;180',keyword:'38;5;176',string:'38;5;114',number:'38;5;209',comment:'38;5;245',fn:'38;5;117',quote:'90',sel:'7',
|
|
77
|
+
// Transcript roles: who said what, and what is happening in the background (see SEMANTIC below).
|
|
78
|
+
user:'1;36',assistant:'37',head:'1;97',head2:'1;37',important:'33',tool:'90',toolHead:'1;90',link:'38;5;75',bullet:'90'},
|
|
72
79
|
light:{text:'39',dim:'38;5;242',faint:'38;5;246',bold:'1',italic:'3',under:'4',accent:'38;5;25',accent2:'38;5;91',ok:'38;5;28',warn:'38;5;130',bad:'38;5;160',
|
|
73
80
|
add:'38;5;28',del:'38;5;160',addBg:'48;5;194',delBg:'48;5;224',hunk:'38;5;25',border:'38;5;248',borderFocus:'38;5;25',borderShell:'38;5;91',borderPlan:'38;5;26',
|
|
74
|
-
code:'38;5;94',keyword:'38;5;90',string:'38;5;28',number:'38;5;166',comment:'38;5;244',fn:'38;5;25',
|
|
81
|
+
code:'38;5;94',keyword:'38;5;90',string:'38;5;28',number:'38;5;166',comment:'38;5;244',fn:'38;5;25',quote:'38;5;242',sel:'7',
|
|
82
|
+
user:'1;38;5;25',assistant:'39',head:'1;38;5;16',head2:'1',important:'38;5;136',tool:'38;5;244',toolHead:'1;38;5;244',link:'38;5;25',bullet:'38;5;244'},
|
|
75
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* The semantic palette the transcript uses, so each kind of text has one look everywhere:
|
|
86
|
+
* user the person's message bold cyan (dark) / bold blue (light)
|
|
87
|
+
* assistant the answer's prose white (dark) / the terminal's text color (light)
|
|
88
|
+
* head headings (h1, h2); head2 = h3+ bold bright white / bold
|
|
89
|
+
* important notes worth seeing yellow
|
|
90
|
+
* ok success (Done, Check passed) green
|
|
91
|
+
* bad errors red
|
|
92
|
+
* warn warnings orange on 256-color terminals, yellow on 16-color ones
|
|
93
|
+
* tool tool rows and logs dim grey; toolHead = the verb on the row (bold grey)
|
|
94
|
+
* link URLs light blue
|
|
95
|
+
*/
|
|
96
|
+
export const SEMANTIC=['user','assistant','head','head2','important','ok','bad','warn','tool','toolHead','link','bullet'];
|
|
97
|
+
/** Roles that use a 256-color code, mapped to the closest basic color for 16-color terminals. */
|
|
98
|
+
const BASIC={dark:{warn:'33',link:'94'},light:{warn:'33',link:'34',important:'33',user:'1;34',tool:'90',toolHead:'1;90',bullet:'90',head:'1'}};
|
|
99
|
+
/** Whether the terminal shows 256 colors. Most do; TERM=xterm/linux/vt100 without COLORTERM are the exceptions. */
|
|
100
|
+
export function colorDepth(env=process.env){
|
|
101
|
+
if(env.MLAI_COLORS==='16')return 16;
|
|
102
|
+
if(/^(truecolor|24bit)$/i.test(env.COLORTERM||'')||/256|direct|kitty|alacritty|wezterm|ghostty/i.test(env.TERM||''))return 256;
|
|
103
|
+
if(env.TERM_PROGRAM||env.WT_SESSION||env.ConEmuANSI)return 256;
|
|
104
|
+
return /^(xterm|linux|vt\d+|screen|ansi|cygwin)$/.test(env.TERM||'')?16:256;
|
|
105
|
+
}
|
|
76
106
|
export const THEME_NAMES=['dark','light'];
|
|
77
107
|
/**
|
|
78
108
|
* Syntax themes recolor fenced code only (code, keyword, string, number, comment, function names). 'default' keeps
|
|
@@ -98,9 +128,9 @@ export function resolveTheme(name,env=process.env){
|
|
|
98
128
|
const bg=Number(String(env.COLORFGBG||'').split(';').pop());
|
|
99
129
|
return Number.isFinite(bg)&&(bg===7||bg===15||(bg>=9&&bg!==8&&bg<16&&bg!==0))?'light':'dark';
|
|
100
130
|
}
|
|
101
|
-
export function theme(name='dark',{color=true,syntax='default'}={}){
|
|
131
|
+
export function theme(name='dark',{color=true,syntax='default',depth=256}={}){
|
|
102
132
|
const base=THEMES[name]?name:'dark';
|
|
103
|
-
const roles={...THEMES[base],...(SYNTAX[syntax]?.[base]||{})};
|
|
133
|
+
const roles={...THEMES[base],...(SYNTAX[syntax]?.[base]||{}),...(depth<256?BASIC[base]:{})};
|
|
104
134
|
const t={name:base,color,syntax:SYNTAX[syntax]?syntax:'default'};
|
|
105
135
|
// Nested styles: an inner reset re-opens the outer style, so bold inside a colored row keeps the row's color.
|
|
106
136
|
for(const [k,v] of Object.entries(roles)){const open=CSI+v+'m';t[k]=color?s=>open+String(s).split(CSI+'0m').join(CSI+'0m'+open)+CSI+'0m':s=>String(s)}
|
|
@@ -108,6 +138,16 @@ export function theme(name='dark',{color=true,syntax='default'}={}){
|
|
|
108
138
|
t.reset=color?CSI+'0m':'';
|
|
109
139
|
return t;
|
|
110
140
|
}
|
|
141
|
+
/** Fills the semantic roles for a theme-like object that predates them (or a hand-made one in a test). */
|
|
142
|
+
const FALLBACK={user:'accent',assistant:'text',head:'bold',head2:'bold',important:'warn',tool:'dim',toolHead:'bold',link:'accent',bullet:'dim'};
|
|
143
|
+
const filled=new WeakMap();
|
|
144
|
+
export function withRoles(t){
|
|
145
|
+
if(!t||SEMANTIC.every(k=>typeof t[k]==='function'))return t;
|
|
146
|
+
let f=filled.get(t);if(f)return f;
|
|
147
|
+
const id=s=>String(s);f={...t};
|
|
148
|
+
for(const k of SEMANTIC)if(typeof f[k]!=='function')f[k]=t[FALLBACK[k]]||t.text||id;
|
|
149
|
+
filled.set(t,f);return f;
|
|
150
|
+
}
|
|
111
151
|
/** Whether colors are allowed for this stream and environment. */
|
|
112
152
|
export function colorAllowed(stream=process.stdout,env=process.env){
|
|
113
153
|
if('NO_COLOR' in env&&env.NO_COLOR!=='')return false;
|
package/src/tui/app.mjs
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
import {readFile} from 'node:fs/promises';
|
|
12
12
|
import {spawn} from 'node:child_process';
|
|
13
13
|
import path from 'node:path';
|
|
14
|
-
import {theme as makeTheme,resolveTheme,colorAllowed,boxChars,width,truncate,wrap,charWidth,strip} from './ansi.mjs';
|
|
14
|
+
import {theme as makeTheme,resolveTheme,colorAllowed,colorDepth,boxChars,width,truncate,wrap,charWidth,strip} from './ansi.mjs';
|
|
15
15
|
import {keyReader} from './keys.mjs';
|
|
16
16
|
import {Editor,loadHistory,saveHistory} from './editor.mjs';
|
|
17
17
|
import {Screen} from './screen.mjs';
|
|
@@ -71,10 +71,10 @@ export function layoutInput(text,cursor,cols){
|
|
|
71
71
|
|
|
72
72
|
export async function runTui({session,settings={},cloud=null,flags={},first=null,images=[],send,makeCtx,onSessionChange,chat=null,
|
|
73
73
|
stdin=process.stdin,stdout=process.stdout,env=process.env,historyFile=path.join(sub('state'),'history.jsonl')}={}){
|
|
74
|
-
const color=colorAllowed(stdout,env);
|
|
74
|
+
const color=colorAllowed(stdout,env),depth=colorDepth(env);
|
|
75
75
|
let themeName=resolveTheme(settings.theme||'auto',env);
|
|
76
76
|
let syntaxName=settings.syntaxTheme||'default';
|
|
77
|
-
let t=makeTheme(themeName,{color,syntax:syntaxName});
|
|
77
|
+
let t=makeTheme(themeName,{color,syntax:syntaxName,depth});
|
|
78
78
|
const box=boxChars(env);
|
|
79
79
|
const screen=new Screen(stdout);
|
|
80
80
|
const editor=new Editor({history:await loadHistory(historyFile),vim:Boolean(settings.vim)});
|
|
@@ -91,7 +91,8 @@ export async function runTui({session,settings={},cloud=null,flags={},first=null
|
|
|
91
91
|
const setModeAll=async m=>{if(chat?.setMode)await chat.setMode(m);else await applyMode(session,m)};
|
|
92
92
|
const curMode=()=>modeName(chat?.mode)||(session?.meta?sessionMode(session.meta):'agent');
|
|
93
93
|
// ---------- output helpers ----------
|
|
94
|
-
|
|
94
|
+
// One blank line between blocks: a block that opens with a blank line right after one that ended with one keeps one.
|
|
95
|
+
const print=lines=>{tr.cols=screen.cols;let l=Array.isArray(lines)?lines:String(lines).split('\n');if(st.blankEnd&&l[0]==='')l=l.slice(1);if(!l.length){draw();return}screen.print(l);st.lastLine=strip(l.at(-1)??'').trim();st.blankEnd=l.at(-1)==='';draw()};
|
|
95
96
|
const flash=(text,ms=2500)=>{st.hint=text;st.hintUntil=Date.now()+ms;draw()};
|
|
96
97
|
const title=s=>{if(stdout.isTTY)stdout.write('\x1b]0;'+s.replace(/[\x00-\x1f]/g,'')+'\x07')};
|
|
97
98
|
const titleMode=()=>{const v=liveSettings?.()?.terminalTitle??settings.terminalTitle;return typeof v==='string'&&v?v:'auto'};
|
|
@@ -120,9 +121,9 @@ export async function runTui({session,settings={},cloud=null,flags={},first=null
|
|
|
120
121
|
attachImage:async p=>attachImage(p),
|
|
121
122
|
toggleTools:()=>toggleTools(),
|
|
122
123
|
setVim:on=>{editor.vim=Boolean(on);editor.vimMode='insert';draw()},
|
|
123
|
-
setTheme:name=>{themeName=resolveTheme(name,env);t=makeTheme(themeName,{color,syntax:syntaxName});tr.t=t;draw()},
|
|
124
|
+
setTheme:name=>{themeName=resolveTheme(name,env);t=makeTheme(themeName,{color,syntax:syntaxName,depth});tr.t=t;draw()},
|
|
124
125
|
get syntax(){return syntaxName},
|
|
125
|
-
setSyntax:name=>{syntaxName=name;t=makeTheme(themeName,{color,syntax:syntaxName});tr.t=t;draw()},
|
|
126
|
+
setSyntax:name=>{syntaxName=name;t=makeTheme(themeName,{color,syntax:syntaxName,depth});tr.t=t;draw()},
|
|
126
127
|
get raw(){return tr.raw},
|
|
127
128
|
setRaw:on=>{tr.raw=Boolean(on);draw()},
|
|
128
129
|
setTitle:text=>{st.title=text==null||text==='auto'||text==='off'?null:String(text).slice(0,200);if(text==='off'&&!st.title)title('');setTitle();draw()},
|
|
@@ -240,7 +241,7 @@ export async function runTui({session,settings={},cloud=null,flags={},first=null
|
|
|
240
241
|
}
|
|
241
242
|
if(result&&!result.intake){
|
|
242
243
|
if(result.state!=='completed'&&result.error&&!st.errorShown)print(t.warn(' '+result.error));
|
|
243
|
-
print(['',' '+summaryLine(result,{dim:t.dim,green:t.ok,yellow:t.
|
|
244
|
+
print(['',' '+summaryLine(result,{dim:t.dim,green:s=>t.ok(t.bold(s)),yellow:s=>t.important(t.bold(s)),red:s=>t.bad(t.bold(s)),bold:t.bold})+t.dim(' · '+elapsed(Date.now()-st.started))]);
|
|
244
245
|
try{const req=JSON.parse(await readFile(path.join(session.dir,'request.json'),'utf8'));if(req.contextWindow){st.window=req.contextWindow;if(st.context)st.context.window=req.contextWindow}}catch{}
|
|
245
246
|
const mode=curMode();
|
|
246
247
|
if(mode==='plan'&&result.state==='completed'&&!st.exiting&&!st.queue.length){
|
package/src/tui/markdown.mjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* and fenced code with syntax highlighting. render(text) returns styled lines wrapped to the width.
|
|
4
4
|
* A Stream renders text that is still arriving: complete blocks are final, the open tail is redrawn.
|
|
5
5
|
*/
|
|
6
|
-
import {wrap,width,pad} from './ansi.mjs';
|
|
6
|
+
import {wrap,width,pad,withRoles} from './ansi.mjs';
|
|
7
7
|
|
|
8
8
|
const KEYWORDS={
|
|
9
9
|
js:'await break case catch class const continue debugger default delete do else export extends finally for from function if import in instanceof let new of return static super switch this throw try typeof var void while with yield async true false null undefined',
|
|
@@ -46,18 +46,37 @@ export function highlightLine(line,lang,t,state={}){
|
|
|
46
46
|
return {text:out,state};
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
// Inline markdown: `code`, **bold**, *em* or _em_, ~~strike~~, [text](url).
|
|
49
|
+
// Inline markdown: `code`, **bold**, *em* or _em_, ~~strike~~, [text](url), bare URLs.
|
|
50
|
+
const URL_RE=/<?\bhttps?:\/\/[^\s<>]+/g;
|
|
51
|
+
const urlEnd=u=>{
|
|
52
|
+
// Trailing punctuation belongs to the sentence, and a closing parenthesis only to a URL that opened one.
|
|
53
|
+
let tail='';u=u.replace(/^</,'');
|
|
54
|
+
while(/[.,;:!?'"*>]$/.test(u)||(u.endsWith(')')&&(u.match(/\(/g)||[]).length<(u.match(/\)/g)||[]).length)){tail=u.slice(-1)+tail;u=u.slice(0,-1)}
|
|
55
|
+
return [u,tail.replace(/^>/,'')];
|
|
56
|
+
};
|
|
50
57
|
export function inline(text,t){
|
|
51
58
|
const parts=[];let s=String(text);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
s=s.replace(
|
|
59
|
+
const hold=v=>{parts.push(v);return '\u0000'+(parts.length-1)+'\u0000'};
|
|
60
|
+
// Pull code spans and URLs out first so their contents are not styled (underscores in a URL are not emphasis).
|
|
61
|
+
s=s.replace(/`([^`]+)`/g,(_,c)=>hold(t.code(c)));
|
|
62
|
+
s=s.replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g,(_,label,url)=>hold(t.under(label)+t.dim(' (')+t.link(url)+t.dim(')')));
|
|
63
|
+
s=s.replace(URL_RE,m=>{const [u,tail]=urlEnd(m);return hold(t.link(u))+tail});
|
|
55
64
|
s=s.replace(/\*\*([^*]+)\*\*|__([^_]+)__/g,(_,a,b)=>t.bold(a||b));
|
|
56
65
|
s=s.replace(/(^|[^\w*])\*([^*\s][^*]*?)\*(?!\w)/g,(_,p,a)=>p+t.italic(a));
|
|
57
66
|
s=s.replace(/(^|[^\w])_([^_\s][^_]*?)_(?!\w)/g,(_,p,a)=>p+t.italic(a));
|
|
58
67
|
s=s.replace(/~~([^~]+)~~/g,(_,a)=>t.faint(a));
|
|
59
68
|
return s.replace(/\u0000(\d+)\u0000/g,(_,n)=>parts[Number(n)]);
|
|
60
69
|
}
|
|
70
|
+
/** "Note:", "Important:" lead-ins read as important (yellow); "Warning:", "Caution:" as warnings. */
|
|
71
|
+
const LEAD=/^(\*\*|__)?(note|notes|important|heads up|tip|nb|warning|caution)(\*\*|__)?:(\*\*|__)?\s*/i;
|
|
72
|
+
function lead(text,t){
|
|
73
|
+
const m=LEAD.exec(text);if(!m)return inline(text,t);
|
|
74
|
+
const label=text.slice(0,m[0].length).replace(/[*_]/g,'').trim();
|
|
75
|
+
const style=/^(warning|caution)/i.test(m[2])?t.warn:t.important;
|
|
76
|
+
return t.bold(style(label))+' '+inline(text.slice(m[0].length),t);
|
|
77
|
+
}
|
|
78
|
+
/** A line that only names a sources block ("Sources:", "**References**"). */
|
|
79
|
+
const SOURCES=/^\s*(\*\*|__)?(sources?|references?|citations?|links|further reading)(\*\*|__)?:?(\*\*|__)?\s*$/i;
|
|
61
80
|
|
|
62
81
|
function table(rows,t,cols){
|
|
63
82
|
const cells=rows.map(r=>r.replace(/^\s*\|/,'').replace(/\|\s*$/,'').split('|').map(c=>c.trim()));
|
|
@@ -72,32 +91,53 @@ function table(rows,t,cols){
|
|
|
72
91
|
}).flatMap(l=>l.split('\n'));
|
|
73
92
|
}
|
|
74
93
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Renders a whole markdown text to lines no wider than cols. Prose (paragraphs, lists, quotes) wraps at maxWidth
|
|
96
|
+
* at most so long answers stay readable on wide terminals; code and tables may use the full width.
|
|
97
|
+
* Blocks are separated by one blank line: before headings and source lists, around lists, code and tables, and
|
|
98
|
+
* between paragraphs. Models often end a paragraph with a single newline, so two consecutive lines are separate
|
|
99
|
+
* paragraphs when either is a full paragraph (longer than a hard-wrapped line); short lines (an address, a poem)
|
|
100
|
+
* stay together.
|
|
101
|
+
*/
|
|
102
|
+
export const PROSE_MAX=100;
|
|
103
|
+
export function render(text,{t,cols=80,box,maxWidth=PROSE_MAX}={}){
|
|
104
|
+
t=withRoles(t);
|
|
77
105
|
const out=[];const lines=String(text??'').replace(/\r\n?/g,'\n').split('\n');
|
|
78
|
-
const
|
|
106
|
+
const pcols=Math.max(4,Math.min(cols,maxWidth||cols));
|
|
107
|
+
const push=(s,indent='',first=indent)=>{const w=wrap(s,pcols-width(first));w.forEach((x,i)=>out.push((i?indent:first)+x))};
|
|
108
|
+
// prev: the kind of the last block ('head', 'label', 'para', 'list', 'code', 'table', 'quote', 'rule'); long: it was a full paragraph.
|
|
109
|
+
let prev=null,long=false;
|
|
110
|
+
const gap=()=>{if(out.length&&out.at(-1)!=='')out.push('')};
|
|
111
|
+
const LONG=100; // source characters: hard-wrapped text stays under it
|
|
79
112
|
for(let i=0;i<lines.length;i++){
|
|
80
113
|
const line=lines[i];let m;
|
|
81
114
|
if((m=/^(\s*)(```+|~~~+)\s*([\w+#.-]*)/.exec(line))){
|
|
82
115
|
const fence=m[2],lang=m[3];const code=[];i++;
|
|
83
116
|
while(i<lines.length&&!lines[i].trimStart().startsWith(fence))code.push(lines[i++]);
|
|
117
|
+
gap();
|
|
84
118
|
const bar=t.dim(box?.bar||'▌')+' ';
|
|
85
119
|
if(lang)out.push(t.dim(box?.bar||'▌')+' '+t.faint(lang));
|
|
86
120
|
let state={};
|
|
87
121
|
for(const c of code){const h=highlightLine(c.replace(/\t/g,' '),lang,t,state);state=h.state;for(const w of wrap(h.text,cols-2))out.push(bar+w)}
|
|
88
|
-
continue;
|
|
122
|
+
prev='code';continue;
|
|
89
123
|
}
|
|
90
|
-
if(!line.trim()){if(
|
|
91
|
-
|
|
92
|
-
if(
|
|
93
|
-
if(
|
|
94
|
-
if((
|
|
95
|
-
if(
|
|
96
|
-
if((m=/^(\s*)
|
|
97
|
-
|
|
98
|
-
push(inline(
|
|
124
|
+
if(!line.trim()){gap();if(prev!=='head'&&prev!=='label')prev='blank';continue}
|
|
125
|
+
const after=k=>{if(prev&&prev!=='blank'&&prev!==k&&prev!=='head'&&prev!=='label')gap()};
|
|
126
|
+
if((m=/^(#{1,6})\s+(.*)$/.exec(line))){const txt=inline(m[2].replace(/\s*#+\s*$/,''),t);gap();push(m[1].length<=2?t.head(txt):t.head2(txt));prev='head';continue}
|
|
127
|
+
if(SOURCES.test(line)){gap();push(t.head2(line.replace(/[*_]/g,'').trim()));prev='label';continue}
|
|
128
|
+
if(/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)){gap();out.push(t.dim((box?.h||'─').repeat(Math.min(pcols,40))));prev='rule';continue}
|
|
129
|
+
if(/^\s*\|.*\|\s*$/.test(line)){const rows=[];while(i<lines.length&&/^\s*\|.*\|\s*$/.test(lines[i]))rows.push(lines[i++]);i--;if(prev!=='head'&&prev!=='label')gap();for(const r of table(rows,t,cols))out.push(r);prev='table';continue}
|
|
130
|
+
if((m=/^(\s*)>\s?(.*)$/.exec(line))){after('quote');push(t.quote(lead(m[2],t)),t.quote('│ '),t.quote('│ '));prev='quote';continue}
|
|
131
|
+
// Lists: indented under the text, with a hanging indent so wrapped lines line up after the marker.
|
|
132
|
+
if((m=/^(\s*)([-*+])\s+\[([ xX])\]\s+(.*)$/.exec(line))){after('list');const ind=' '+' '.repeat(Math.min(8,m[1].length));const done=m[3]!==' ';push(done?t.faint(inline(m[4],t)):inline(m[4],t),ind+' ',ind+(done?t.ok('[x] '):t.dim('[ ] ')));prev='list';continue}
|
|
133
|
+
if((m=/^(\s*)([-*+])\s+(.*)$/.exec(line))){after('list');const ind=' '+' '.repeat(Math.min(8,m[1].length));push(lead(m[3],t),ind+' ',ind+t.bullet((box?.dot||'•')==='*'?'- ':'• '));prev='list';continue}
|
|
134
|
+
if((m=/^(\s*)(\d+)[.)]\s+(.*)$/.exec(line))){after('list');const ind=' '+' '.repeat(Math.min(8,m[1].length)),num=m[2]+'. ';push(lead(m[3],t),ind+' '.repeat(num.length),ind+t.bold(num));prev='list';continue}
|
|
135
|
+
const isLong=width(line)>LONG;
|
|
136
|
+
if(prev==='para'?(long||isLong):prev&&prev!=='blank'&&prev!=='head'&&prev!=='label')gap();
|
|
137
|
+
push(lead(line,t));prev='para';long=isLong;
|
|
99
138
|
}
|
|
100
139
|
while(out.length&&out.at(-1)==='')out.pop();
|
|
140
|
+
while(out.length&&out[0]==='')out.shift();
|
|
101
141
|
return out;
|
|
102
142
|
}
|
|
103
143
|
|
package/src/tui/transcript.mjs
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Harness events -> transcript lines for the TUI. Tool calls are one row each (read, edit, run, search ...);
|
|
3
3
|
* their output is collapsed to a summary unless expanded (Ctrl-O). Pure, so it is tested without a terminal.
|
|
4
4
|
*/
|
|
5
|
-
import {render as markdown} from './markdown.mjs';
|
|
6
|
-
import {wrap,truncate,width} from './ansi.mjs';
|
|
5
|
+
import {render as markdown,PROSE_MAX} from './markdown.mjs';
|
|
6
|
+
import {wrap,truncate,width,withRoles} from './ansi.mjs';
|
|
7
7
|
import {prose,renderEvent} from '../render.mjs';
|
|
8
8
|
|
|
9
9
|
/** Tool names from the harness -> the verb shown on the row. */
|
|
@@ -33,13 +33,17 @@ export const legacy=t=>({dim:t.dim,bold:t.bold,red:t.bad,green:t.ok,yellow:t.war
|
|
|
33
33
|
export const finishless=s=>String(s||'').replace(/```[a-z]*\s*\n\s*echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT\s*\n```/g,'').replace(/^\s*echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT\s*$/gm,'');
|
|
34
34
|
export class Transcript{
|
|
35
35
|
constructor({t,box,cols=80,expanded=false,verbose=false,raw=false}={}){this.t=t;this.box=box;this.cols=cols;this.expanded=expanded;this.verbose=verbose;this.raw=raw;this.collapsed=[];this.lastAssistant=''}
|
|
36
|
+
/** The theme; set by the app when the theme changes. Older theme objects get the semantic roles filled in. */
|
|
37
|
+
get t(){return this._t}
|
|
38
|
+
set t(v){this._t=withRoles(v)}
|
|
36
39
|
dot(style){return style(this.box?.dot||'●')+' '}
|
|
37
40
|
elbow(){return this.t.dim(this.box?.ascii?' L ':' ⎿ ')}
|
|
38
41
|
/** The user's message as it goes to the agent. */
|
|
39
42
|
user(text,{images=0,shell=false}={}){
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
+
// Bold cyan with a marker, then a blank line, so what was asked stands apart from the work and the answer below it.
|
|
44
|
+
const t=this.t,style=shell?t.borderShell:t.user,mark=shell?t.borderShell('!'):t.user(this.box?.arrow||'›');
|
|
45
|
+
const lines=this.raw?String(text).split('\n'):wrap(String(text),Math.min(this.cols,PROSE_MAX+2)-4);
|
|
46
|
+
return ['',...lines.map((l,i)=>(i?' ':mark+' ')+style(l)),...(images?[t.dim(' + '+images+' image'+(images===1?'':'s'))]:[]),''];
|
|
43
47
|
}
|
|
44
48
|
assistant(content){
|
|
45
49
|
const p=prose(finishless(content));if(!p||p===this.lastAssistant)return [];
|
|
@@ -47,7 +51,8 @@ export class Transcript{
|
|
|
47
51
|
// Raw scrollback: the reply exactly as written (markdown source, no wrapping or color), for copying.
|
|
48
52
|
if(this.raw)return ['',...p.split('\n')];
|
|
49
53
|
const md=markdown(p,{t:this.t,cols:this.cols-2,box:this.box});
|
|
50
|
-
|
|
54
|
+
const a=this.t.assistant;
|
|
55
|
+
return ['',...md.map((l,i)=>(i?(l?' ':''):this.dot(a))+(l?a(l):''))];
|
|
51
56
|
}
|
|
52
57
|
/** The final answer. When the agent already wrote it as a reply above, it is not shown again; only what the harness
|
|
53
58
|
added to it (a verification note, a source list) is. */
|
|
@@ -63,12 +68,12 @@ export class Transcript{
|
|
|
63
68
|
tool(name,target){
|
|
64
69
|
const t=this.t,v=verb(name);
|
|
65
70
|
const tgt=target?' '+truncate(String(target),Math.max(10,this.cols-width(v)-6)):'';
|
|
66
|
-
return [this.dot(t.tool)+t.
|
|
71
|
+
return [this.dot(t.tool)+t.toolHead(v)+t.tool(tgt)];
|
|
67
72
|
}
|
|
68
73
|
command(cmd){
|
|
69
74
|
const t=this.t,first=String(cmd||'').split('\n');
|
|
70
75
|
const head=truncate(first[0],this.cols-8)+(first.length>1?t.dim(' (+'+(first.length-1)+' lines)'):'');
|
|
71
|
-
return [this.dot(t.tool)+t.
|
|
76
|
+
return [this.dot(t.tool)+t.toolHead('Run ')+t.tool(head)];
|
|
72
77
|
}
|
|
73
78
|
output(e){
|
|
74
79
|
const t=this.t,text=String(e.output||'').replace(/\s+$/,'');const rows=text?text.split('\n'):[];const ok=e.returncode===0;
|
|
@@ -93,7 +98,7 @@ export class Transcript{
|
|
|
93
98
|
}
|
|
94
99
|
todos(items){
|
|
95
100
|
const t=this.t;if(!Array.isArray(items)||!items.length)return [];
|
|
96
|
-
return [this.dot(t.tool)+t.
|
|
101
|
+
return [this.dot(t.tool)+t.toolHead('Plan'),...items.slice(0,30).map(it=>{const s=it.status;const mark=s==='done'?t.ok('[x]'):s==='in_progress'?t.accent('[>]'):t.dim('[ ]');return ' '+mark+' '+(s==='done'?t.faint(truncate(it.text,this.cols-10)):truncate(String(it.text),this.cols-10))})];
|
|
97
102
|
}
|
|
98
103
|
/** Lines for one event; [] for events that stay quiet. */
|
|
99
104
|
lines(e){
|
|
@@ -105,7 +110,7 @@ export class Transcript{
|
|
|
105
110
|
case 'command':return this.command(e.command);
|
|
106
111
|
case 'output':return this.output(e);
|
|
107
112
|
case 'todos':return this.todos(e.items);
|
|
108
|
-
case 'verification':{const ok=e.verdict?.status==='passed'||(e.verdict==null&&e.returncode===0);return [this.dot(ok?t.ok:t.bad)+t.
|
|
113
|
+
case 'verification':{const ok=e.verdict?.status==='passed'||(e.verdict==null&&e.returncode===0);return [this.dot(ok?t.ok:t.bad)+(ok?t.ok('Check passed'):t.bad(t.bold('Check '+(e.verdict?.status||'failed'))))+t.tool(' '+truncate(String(e.label||''),this.cols-20))]}
|
|
109
114
|
case 'edit_guard':return [this.dot(t.warn)+t.warn('Blocked edit ')+truncate(String(e.path||''),60)+t.dim(': '+truncate(String(e.content||''),this.cols-30))];
|
|
110
115
|
case 'steering':return [this.elbow()+t.accent('Steering received')];
|
|
111
116
|
case 'mode':return e.content?[this.elbow()+t.dim(String(e.content))]:[];
|
package/worker/browser_tool.py
CHANGED
|
@@ -63,6 +63,13 @@ def hydrated(page):
|
|
|
63
63
|
try:return bool(page.evaluate(HYDRATION)['interactive'])
|
|
64
64
|
except Exception:return None
|
|
65
65
|
|
|
66
|
+
def actual(page,checked,local):
|
|
67
|
+
"""The address the browser ended on, so a check that navigated (a goto step) is attributed to the page it checked, not to
|
|
68
|
+
the address it started from (CLI 0.3.4). Cloud checks keep the preview-host description."""
|
|
69
|
+
try:url=page.url if page else ''
|
|
70
|
+
except Exception:url=''
|
|
71
|
+
return url if local and url and url!='about:blank' else checked
|
|
72
|
+
|
|
66
73
|
def scenario(args,output):
|
|
67
74
|
local=on_laptop()
|
|
68
75
|
if not (local and os.getenv('PLAYWRIGHT_BROWSERS_PATH')):os.environ['PLAYWRIGHT_BROWSERS_PATH']=str(output.parent.parent/'pw-browsers')
|
|
@@ -106,7 +113,8 @@ def scenario(args,output):
|
|
|
106
113
|
page.screenshot(path=str(output),full_page=False,timeout=10000)
|
|
107
114
|
interactive=hydrated(page)
|
|
108
115
|
if interactive is False:errors.insert(0,NOT_INTERACTIVE)
|
|
109
|
-
|
|
116
|
+
checked=actual(page,checked,local)
|
|
117
|
+
return {'success':not errors,'interactive':interactive,'checkedAt':checked,**({'requestedUrl':url} if checked!=url and local else {}),'timeline':timeline,'url':page.url,'title':page.title(),'text':page.locator('body').inner_text()[:16000],'pageErrors':errors[:20],'consoleErrors':console[:20],**({'ignoredExtensionNoise':noise[:5]} if noise else {}),'networkFailures':network[:30],'horizontalOverflow':page.evaluate('document.documentElement.scrollWidth>innerWidth'),'screenshot':output.name,'viewport':{'width':width,'height':height}}
|
|
110
118
|
except Exception as exc:
|
|
111
119
|
if timeline and timeline[-1]['state']=='started':timeline[-1]['state']='failed'
|
|
112
120
|
captured=False
|
|
@@ -115,5 +123,7 @@ def scenario(args,output):
|
|
|
115
123
|
except Exception:pass
|
|
116
124
|
interactive=hydrated(page) if page else None
|
|
117
125
|
reason=(NOT_INTERACTIVE+' Step error: ' if interactive is False else '')+str(exc)[:2000]
|
|
118
|
-
|
|
126
|
+
checked=actual(page,checked,local)
|
|
127
|
+
return {'success':False,'error':reason,'interactive':interactive,'checkedAt':checked,**({'requestedUrl':url} if checked!=url and local else {}),
|
|
128
|
+
'timeline':timeline,'pageErrors':errors[:20],'consoleErrors':console[:20],'networkFailures':network[:30],'screenshot':output.name if captured else None,'viewport':{'width':width,'height':height}}
|
|
119
129
|
finally:browser.close()
|
package/worker/conversation.py
CHANGED
|
@@ -226,6 +226,52 @@ SMALLTALK = re.compile(r"^\W*(?:(?:hi|hello|hey|hiya|howdy|yo|hola|salam|assalam
|
|
|
226
226
|
def smalltalk(text):
|
|
227
227
|
return bool(SMALLTALK.match(str(text or '').strip()[:200]))
|
|
228
228
|
|
|
229
|
+
# Tester report on 0.3.4: "Hi" in a project folder read credentials.json and main.py, offered to strip a private key,
|
|
230
|
+
# called an Edit tool that is not offered in a read-only turn, re-sent the greeting on each repair round and ended
|
|
231
|
+
# "Verification incomplete / 0 checks passed" after 11 model calls and 4 minutes. A greeting, thanks or a question about
|
|
232
|
+
# the agent itself is answered with one model call and no tools: nothing is read, scanned, changed or checked.
|
|
233
|
+
DIRECT_SYSTEM = '''You are the ML AI Code agent{model}, a coding agent working in the folder "{folder}" on the user's computer. The user sent a greeting, thanks or a short question about you. Reply in one to three short, friendly sentences of plain text. You have no tools in this reply: do not read, list or scan files, do not mention files, secrets, credentials, checks, tests or changes you have not been asked about, and do not offer to edit anything. Never write a tool call, JSON or code. If asked what you can do, say in one or two sentences that you can read and explain this project, write and fix code, run its tests and commands, and research current facts on the web, and ask what they want to work on. If asked which workspace, folder or project this is, name the folder "{folder}". If asked which model you are, answer with the model name only{label_hint}. Never call yourself Kimi, Kimi Code CLI or any engine or harness name.'''
|
|
234
|
+
_DIRECT_JUNK = re.compile(r'```|<\s*/?\s*(?:tool|function|invoke|antml)|\bfinish_response\b|\bworkspace_tool\b|^\s*[\[{]', re.I | re.M)
|
|
235
|
+
_DIRECT_FALLBACK = {'thanks': "You're welcome. Let me know if there is anything else you'd like to work on.",
|
|
236
|
+
'bye': 'Goodbye. Come back any time you want to work on this project.',
|
|
237
|
+
'what': 'I can read and explain this project, write and fix code, run its tests and commands, and look up current facts on the web. What would you like to work on?',
|
|
238
|
+
'where': 'You are in the folder {folder}. What would you like to work on?',
|
|
239
|
+
'hi': 'Hi! What would you like to work on?'}
|
|
240
|
+
|
|
241
|
+
def direct_reply(config):
|
|
242
|
+
"""A greeting or small-talk turn (route() set config['smalltalk']) is answered directly with one model call."""
|
|
243
|
+
if os.environ.get('MINI_DIRECT_SMALLTALK', '1') == '0': return False
|
|
244
|
+
if not enabled(config) or not config.get('smalltalk') or config.get('mode') != 'plan': return False
|
|
245
|
+
if config.get('wake') or config.get('resuming') or config.get('images') or config.get('selected_design') or config.get('brief'): return False
|
|
246
|
+
return smalltalk(config.get('task'))
|
|
247
|
+
|
|
248
|
+
def direct_folder(repo):
|
|
249
|
+
return os.path.basename(os.path.normpath(str(repo or ''))) or 'this folder'
|
|
250
|
+
|
|
251
|
+
def direct_messages(config, repo, history=()):
|
|
252
|
+
from reply_language import rule as reply_language_rule
|
|
253
|
+
label = str(config.get('modelLabel') or '').strip()[:60]
|
|
254
|
+
system = DIRECT_SYSTEM.format(model=(' running on ' + label) if label else '', folder=direct_folder(repo), label_hint=(' (' + label + ')') if label else '')
|
|
255
|
+
system += '\n' + reply_language_rule(config)
|
|
256
|
+
messages = [{'role': 'system', 'content': system}]
|
|
257
|
+
for message in bounded_history(list(history or []))[-6:]:
|
|
258
|
+
messages.append({'role': message['role'], 'content': message['content'][:1200]})
|
|
259
|
+
messages.append({'role': 'user', 'content': str(config.get('task') or '').strip()[:400]})
|
|
260
|
+
return messages
|
|
261
|
+
|
|
262
|
+
def direct_fallback(task, repo):
|
|
263
|
+
t = str(task or '').strip().lower()
|
|
264
|
+
key = ('thanks' if re.match(r'^\W*(?:thank|thx|ty\b|cheers)', t) else 'bye' if re.match(r'^\W*(?:bye|goodbye|see you)', t)
|
|
265
|
+
else 'where' if re.search(r'\b(?:workspace|folder|directory|project|repo|repository|where am i)\b', t)
|
|
266
|
+
else 'what' if re.search(r'\b(?:what|who) (?:can|could) you do|what (?:are|r) you able', t) else 'hi')
|
|
267
|
+
return _DIRECT_FALLBACK[key].replace('{folder}', direct_folder(repo))
|
|
268
|
+
|
|
269
|
+
def direct_answer(text, task, repo):
|
|
270
|
+
"""The model's reply, or a short canned reply when it is empty, a tool call or a long essay."""
|
|
271
|
+
reply = str(text or '').strip()
|
|
272
|
+
if not reply or _DIRECT_JUNK.search(reply) or len(reply) > 900: return direct_fallback(task, repo)
|
|
273
|
+
return reply
|
|
274
|
+
|
|
229
275
|
def local_route(config,history=()):
|
|
230
276
|
"""Route with the local classifier when it is confident. Short follow-ups that depend on the conversation go to the LLM router."""
|
|
231
277
|
if os.environ.get('MINI_INTENT_LOCAL','1')=='0':return None
|
package/worker/kimi_bridge.py
CHANGED
|
@@ -339,7 +339,18 @@ TALK_RESEARCH = '''Research with mcp__mlai__web_search and mcp__mlai__web_fetch,
|
|
|
339
339
|
INSPECT_RULES = '''The user asked for no changes in this turn (an assessment, review or "ask before fixing"). Do not edit, create or delete project files, install packages, start services or commit; those calls are refused. You may read the code and run existing tests and checks to reproduce defects. Report each finding with its evidence (file and line, failing command and output) and the fix you recommend, then ask whether to apply the fixes. Results of the checks you run are investigation evidence, not proof that the project is broken.'''
|
|
340
340
|
# BUG-0235: a build or deploy request in Plan mode gets a pointer to the Mode switch, never a missing-tools story.
|
|
341
341
|
PLAN_RULES = '''The user picked Plan mode: they review a plan before anything is built. Do not try to create or change files and do not ask the user to run commands themselves. If the message is only a question, just answer it. For work, write the plan as a short numbered list (files to add or change, what each step does and how it will be checked), using project paths such as index.html. End with one sentence saying they can tap Build this plan to start. If they ask you to build, create files, deploy or go ahead while Plan mode is still on, do not try: say in one or two sentences that Plan mode only plans, and that they can tap Build this plan or switch Mode to Approve or Auto, then send the request again. Never describe this mode as a tool restriction, never say a tool is missing or not found, and never list tool names.'''
|
|
342
|
-
READONLY_RULES = '''This request is read-only: answer, research or plan. Do not modify files, run builds or start servers. Inspect the repository with Read, Grep and Glob, research with mcp__mlai__web_search and mcp__mlai__web_fetch when needed (record claims with mcp__mlai__evidence and link sources), then answer directly and concisely. If you propose a plan, make it concrete and numbered.'''
|
|
342
|
+
READONLY_RULES = '''This request is read-only: answer, research or plan. Do not modify files, run builds or start servers. Inspect the repository with Read, Grep and Glob, research with mcp__mlai__web_search and mcp__mlai__web_fetch when needed (record claims with mcp__mlai__evidence and link sources), then answer directly and concisely. If you propose a plan, make it concrete and numbered. Call only the tools in your tool list: files cannot be changed in this turn, so do not try to edit, write or delete anything; describe a change that would help and let the user ask for it. Read only what the request needs: a greeting or a question about you needs no files at all. If you notice a credential or secret in the repository, mention it in at most one sentence and do nothing about it unless the user asks.'''
|
|
343
|
+
# Tester report on 0.3.4: in a read-only turn the engine offers no Write, Edit, Bash or Agent tool, but the working rules
|
|
344
|
+
# still told the model to "change source files with Edit" and run checks with Bash, so it called Edit and got
|
|
345
|
+
# 'Tool "Edit" not found' (the engine rejects unknown tools before any hook runs). The rules now mention only offered tools.
|
|
346
|
+
READONLY_HIDDEN = ('Write', 'Edit', 'Bash', 'Agent', 'run_in_background', 'mcp__mlai__preview', 'mcp__mlai__job', 'mcp__mlai__network', 'scaffolder')
|
|
347
|
+
|
|
348
|
+
def base_rules(hidden=(), text=None):
|
|
349
|
+
"""BASE_RULES (or other bullet rules) without the bullets that tell the model to use a tool it is not offered in this turn."""
|
|
350
|
+
text = BASE_RULES if text is None else text
|
|
351
|
+
if not hidden: return text
|
|
352
|
+
rx = re.compile(r'\b(?:' + '|'.join(re.escape(n) for n in hidden) + r')\b')
|
|
353
|
+
return '\n'.join(line for line in text.split('\n') if not (line.startswith('- ') and rx.search(line)))
|
|
343
354
|
|
|
344
355
|
GLM_RULES = '''Model notes (GLM):
|
|
345
356
|
- Batch independent reads and searches in one step as parallel tool calls; do not re-read a file you already read unless it changed.
|
|
@@ -567,13 +578,14 @@ def identity_rule(config):
|
|
|
567
578
|
|
|
568
579
|
def agent_file(config, readonly, extra_system):
|
|
569
580
|
role = config.get('_role')
|
|
570
|
-
|
|
581
|
+
hidden = READONLY_HIDDEN if readonly else denied_tools(config)
|
|
582
|
+
parts = ['${base_prompt}', '', base_rules(hidden)]
|
|
571
583
|
if readonly: parts += ['', READONLY_RULES]
|
|
572
584
|
if config.get('mode') == 'plan': parts += ['', PLAN_RULES]
|
|
573
585
|
if role: parts += ['', role_engine.prompt(role, readonly=readonly)]
|
|
574
586
|
elif not readonly and role_engine.stack_rule(config.get('task', '')): parts += ['', role_engine.stack_rule(config.get('task', ''))]
|
|
575
587
|
if config.get('_design_lead'): parts += ['', role_engine.design_prompt(config['_design_lead'], role)]
|
|
576
|
-
if 'glm' in str(config.get('model') or '').lower(): parts += ['', GLM_RULES]
|
|
588
|
+
if 'glm' in str(config.get('model') or '').lower(): parts += ['', base_rules(hidden, GLM_RULES)]
|
|
577
589
|
from reply_language import rule as reply_language_rule
|
|
578
590
|
parts += ['', reply_language_rule(config)]
|
|
579
591
|
parts += ['', identity_rule(config)]
|
|
@@ -589,7 +601,26 @@ TOOL_EVENT = {'Read': ('read_file', 'path'), 'Write': ('write_file', 'path'), 'E
|
|
|
589
601
|
# A write the user or a permission rule refused never happened (BUG-0496: it was counted as an edit).
|
|
590
602
|
FAILED = re.compile(r'Command failed with exit code: (\d+)|^(Error|error):|<system>ERROR|Tool execution failed|Tool call was blocked|was not run|The user rejected this action|The user did not approve|Blocked by your permission rule', re.M)
|
|
591
603
|
# A command the hook refused (e.g. a test masked by `| tail` or `; git status`) never ran, so it is not a failed check.
|
|
592
|
-
HOOK_BLOCKED = re.compile(r'Tool call was blocked|This call was not run
|
|
604
|
+
HOOK_BLOCKED = re.compile(r'Tool call was blocked|This call was not run\.|^\s*(?:The user rejected this action|The user did not approve|Blocked by your permission rule)')
|
|
605
|
+
# The engine hands the model only the hook's reason text, so a refusal without these words ("You have made this exact call
|
|
606
|
+
# three times recently") read as a successful run. The hook lists every call it refused here (CLI 0.3.4).
|
|
607
|
+
BLOCKED_CALLS = 'kimi-blocked.jsonl'
|
|
608
|
+
|
|
609
|
+
def was_blocked(call_id, name, args):
|
|
610
|
+
try: lines = (ROOT / BLOCKED_CALLS).read_text().splitlines()[-400:]
|
|
611
|
+
except OSError: return False
|
|
612
|
+
sig = None
|
|
613
|
+
for line in lines:
|
|
614
|
+
try: item = json.loads(line)
|
|
615
|
+
except ValueError: continue
|
|
616
|
+
if not isinstance(item, dict): continue
|
|
617
|
+
if item.get('id'):
|
|
618
|
+
if call_id and item['id'] == call_id: return True
|
|
619
|
+
continue
|
|
620
|
+
if sig is None:
|
|
621
|
+
sig = hashlib.sha1((str(name or '') + json.dumps(args if isinstance(args, dict) else {}, sort_keys=True)).encode()).hexdigest()[:16]
|
|
622
|
+
if item.get('sig') == sig and time.time() - float(item.get('time') or 0) < 600: return True
|
|
623
|
+
return False
|
|
593
624
|
|
|
594
625
|
ENGINE_NAMES = re.compile(r'\bKimi(?:[ -]Code)?[ -]CLI\b|\bKimi Code\b(?= (?:agent|engine|harness))', re.I)
|
|
595
626
|
|
|
@@ -678,6 +709,9 @@ class StreamMapper:
|
|
|
678
709
|
content = str(content or '')
|
|
679
710
|
exit_code = re.search(r'Command failed with exit code: (\d+)', content)
|
|
680
711
|
code = int(exit_code.group(1)) if exit_code else (1 if FAILED.search(content) else 0)
|
|
712
|
+
# A refused call never ran: it is not a result, and never replaces the last real result of that command.
|
|
713
|
+
blocked = bool(not exit_code and HOOK_BLOCKED.search(content)) or was_blocked(msg.get('tool_call_id'), name, args)
|
|
714
|
+
if blocked: code = 1
|
|
681
715
|
if name in ('Agent', 'AgentSwarm'):
|
|
682
716
|
if msg.get('tool_call_id') in getattr(self, 'deferred', set()): self.announce(name, args)
|
|
683
717
|
event('subagent', state='failed' if code else 'completed', content=content[:2000])
|
|
@@ -686,10 +720,10 @@ class StreamMapper:
|
|
|
686
720
|
if name == 'TodoList': return
|
|
687
721
|
if msg.get('tool_call_id') in getattr(self, 'deferred', set()): self.announce(name, args)
|
|
688
722
|
check_code, integrity = code, ''
|
|
689
|
-
if name == 'Bash' and is_check(str(args.get('command', ''))) and not args.get('run_in_background'):
|
|
723
|
+
if name == 'Bash' and is_check(str(args.get('command', ''))) and not args.get('run_in_background') and not blocked:
|
|
690
724
|
# EXEC-001: `npm test | tail` exits with tail's status. Reconcile with the runner's own summary.
|
|
691
725
|
check_code, integrity = verification.check_exit(str(args.get('command', '')), code, content, is_check)
|
|
692
|
-
event('output', output=content[:12000], returncode=code if check_code is None else check_code, **({'integrity': integrity} if integrity else {}))
|
|
726
|
+
event('output', output=content[:12000], returncode=code if check_code is None else check_code, **({'integrity': integrity} if integrity else {}), **({'blocked': True} if blocked else {}))
|
|
693
727
|
if name == 'Bash' and args.get('run_in_background') and not code:
|
|
694
728
|
# BUG-0203: remember the start command so a new computer can offer Restart (never restarted by itself).
|
|
695
729
|
try:
|
|
@@ -708,10 +742,11 @@ class StreamMapper:
|
|
|
708
742
|
command = str(args.get('command', ''))
|
|
709
743
|
try:
|
|
710
744
|
commands = load(ROOT, 'kimi-commands.json', [])
|
|
711
|
-
commands = (commands if isinstance(commands, list) else []) + [{'command': command[:2000], 'returncode': code, 'time': time.time()}]
|
|
745
|
+
commands = (commands if isinstance(commands, list) else []) + [{'command': command[:2000], 'returncode': code, 'time': time.time(), **({'blocked': True} if blocked else {})}]
|
|
712
746
|
write('kimi-commands.json', commands[-300:])
|
|
713
747
|
except Exception: pass
|
|
714
|
-
if
|
|
748
|
+
if blocked: pass
|
|
749
|
+
elif is_check(command) and not args.get('run_in_background'):
|
|
715
750
|
record = {'label': clip(command, 160), 'command': command[:2000], 'returncode': check_code, 'executed': True, 'scope': 'supporting' if st.get('inspect') else 'repository', 'output': content[-4000:], 'time': time.time()}
|
|
716
751
|
if integrity: record.update(shellReturncode=code, integrity=integrity); event('note', content=integrity)
|
|
717
752
|
code = check_code if check_code is not None else code
|
|
@@ -730,7 +765,7 @@ class StreamMapper:
|
|
|
730
765
|
write('checks.json', checks[-20:]); event('verification', **record)
|
|
731
766
|
st['last_check'] = time.time()
|
|
732
767
|
except Exception: pass
|
|
733
|
-
remote = remote_check(command, code, content)
|
|
768
|
+
remote = None if blocked else remote_check(command, code, content)
|
|
734
769
|
if remote:
|
|
735
770
|
checks = [c for c in load(ROOT, 'checks.json', []) if c.get('command') != remote['command']] + [remote]
|
|
736
771
|
write('checks.json', checks[-20:]); event('verification', **remote)
|
|
@@ -1198,7 +1233,7 @@ def main():
|
|
|
1198
1233
|
research_proxy = start_research_proxy()
|
|
1199
1234
|
write('status.json', {'state': 'running', 'started': time.time(), 'boot': boot_id(), 'process': identity(os.getpid()), 'harness': 'kimi'})
|
|
1200
1235
|
(ROOT / 'kimi-stderr.log').unlink(missing_ok=True)
|
|
1201
|
-
for name in ('kimi-state.json', 'kimi-progress.json', 'kimi-hook.json', 'usage.json', 'kimi-stop-count', 'design-browser-evidence.json', 'design-review.json', 'acceptance.json', 'budget-declined', 'hard-deny-targets.json'): (ROOT / name).unlink(missing_ok=True)
|
|
1236
|
+
for name in ('kimi-state.json', 'kimi-progress.json', 'kimi-hook.json', 'usage.json', 'kimi-stop-count', 'design-browser-evidence.json', 'design-review.json', 'acceptance.json', 'budget-declined', 'hard-deny-targets.json', BLOCKED_CALLS): (ROOT / name).unlink(missing_ok=True)
|
|
1202
1237
|
# "Approve all" covers the rest of the request: a Resume of the same request keeps it, a new request asks afresh.
|
|
1203
1238
|
if not config.get('resuming') or config.get('subagent'): (ROOT / 'kimi-approve-all').unlink(missing_ok=True)
|
|
1204
1239
|
shutil.rmtree(ROOT / 'acceptance', ignore_errors=True)
|
|
@@ -1238,6 +1273,11 @@ def main():
|
|
|
1238
1273
|
config['conversation_route'] = 'build'; routing = {'route': 'build', 'mode': config.get('mode', 'approve'), 'fallback': False}
|
|
1239
1274
|
else: routing = route_conversation(config, side, history, shell=SHELL_ALWAYS)
|
|
1240
1275
|
write('conversation-route.json', routing); event('intent', intent=routing['route'], mode=routing['mode'], fallback=routing['fallback'], source=routing.get('source', 'model'))
|
|
1276
|
+
from conversation import direct_reply
|
|
1277
|
+
if direct_reply(config):
|
|
1278
|
+
# A greeting or small talk: one model call, no engine launch, no file reads, no verification or repair rounds.
|
|
1279
|
+
R.direct_turn(config, model, history, status_extra={'harness': 'kimi'}, usage_extra={'side': side.tokens if side is not model else 0}, metrics=False)
|
|
1280
|
+
return
|
|
1241
1281
|
role = resolve_role(config)
|
|
1242
1282
|
if role_engine.design_allowed(role) or config.get('selected_design'): prepare_design(config, model, ROOT, event) # never pauses the run
|
|
1243
1283
|
if config.get('selected_design'):
|
|
@@ -1444,7 +1484,7 @@ def main():
|
|
|
1444
1484
|
event('final_gate', state='repair', round=rounds, issues=issues, content='Not done yet: ' + issues[0][:300])
|
|
1445
1485
|
previous = mapper.last_text; mapper.last_text = ''
|
|
1446
1486
|
asked = re.sub(r'\s+', ' ', str(config.get('task') or '').split('User attachments (untrusted data', 1)[0]).strip()[:600]
|
|
1447
|
-
code, reason = launch(resume('Not done yet. Fix these before finishing:\n' + verification.repair_note(issues) + '\nThen write your final message again from scratch, complete and self-contained, as if it were your first reply to the user: ' + ('the full answer with its source links' if readonly else 'what changed, how you verified it, and anything left open') + '. It must answer the user\'s message: \u00ab' + asked + '\u00bb. Do not mention these checks, this repair step, or what you just re-checked; never open with a correction of yourself.'))
|
|
1487
|
+
code, reason = launch(resume(('Not done yet. Correct your answer for these points; files cannot be changed in this turn, so do not try to edit them:\n' if readonly else 'Not done yet. Fix these before finishing:\n') + verification.repair_note(issues) + '\nThen write your final message again from scratch, complete and self-contained, as if it were your first reply to the user: ' + ('the full answer with its source links' if readonly else 'what changed, how you verified it, and anything left open') + '. It must answer the user\'s message: \u00ab' + asked + '\u00bb. Do not mention these checks, this repair step, or what you just re-checked; never open with a correction of yourself.'))
|
|
1448
1488
|
if not mapper.last_text: mapper.last_text = previous
|
|
1449
1489
|
proc = holder.get('proc')
|
|
1450
1490
|
if checkpoints:
|
package/worker/kimi_gates.py
CHANGED
|
@@ -322,7 +322,7 @@ def final_issues(config, st, answer, done, model, root, repo, event, allow_accep
|
|
|
322
322
|
now = work_ledger.entry(config.get('task', ''), '', checks, json.loads((Path(root) / 'design-browser-evidence.json').read_text()))
|
|
323
323
|
record = record + [now]
|
|
324
324
|
except Exception: pass
|
|
325
|
-
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))
|
|
325
|
+
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), root=root)
|
|
326
326
|
(Path(root) / 'verification.json').write_text(json.dumps(report))
|
|
327
327
|
event('verification_report', **{k: report.get(k) for k in ('status', 'reasons', 'findings', 'checks', 'passed', 'stale')})
|
|
328
328
|
for f in report['findings']:
|
package/worker/kimi_hook.py
CHANGED
|
@@ -54,7 +54,23 @@ def is_work_tool(name):
|
|
|
54
54
|
if m.group(1) != 'mlai': return True # connected-app and other MCP tools can change outside data
|
|
55
55
|
return m.group(2) in WORK_MCP
|
|
56
56
|
|
|
57
|
+
BLOCKED = 'kimi-blocked.jsonl'
|
|
58
|
+
_CALL = None # the PreToolUse payload being decided
|
|
59
|
+
|
|
60
|
+
def call_sig(name, args):
|
|
61
|
+
return hashlib.sha1((str(name or '') + json.dumps(args if isinstance(args, dict) else {}, sort_keys=True)).encode()).hexdigest()[:16]
|
|
62
|
+
|
|
63
|
+
def note_blocked(payload):
|
|
64
|
+
"""Tell the bridge this call did not run. The engine hands the model only the reason text, so without this a refused
|
|
65
|
+
`npm test` ("You have made this exact call three times recently") was saved as a passing check (CLI 0.3.4)."""
|
|
66
|
+
args = payload.get('tool_input') if isinstance(payload.get('tool_input'), dict) else {}
|
|
67
|
+
line = {'id': payload.get('tool_call_id') or payload.get('toolCallId'), 'sig': call_sig(payload.get('tool_name'), args), 'time': time.time()}
|
|
68
|
+
with (ROOT / BLOCKED).open('a') as f: f.write(json.dumps(line) + '\n')
|
|
69
|
+
|
|
57
70
|
def block(reason):
|
|
71
|
+
if _CALL is not None:
|
|
72
|
+
try: note_blocked(_CALL)
|
|
73
|
+
except Exception: pass
|
|
58
74
|
sys.stderr.write(reason); sys.stderr.flush(); sys.exit(2)
|
|
59
75
|
|
|
60
76
|
def steering():
|
|
@@ -473,6 +489,8 @@ def always_plan_gate(command, st):
|
|
|
473
489
|
'(what you will change and how you will check it), then wait for the review. This command was not run; re-issue it after the plan is approved.')
|
|
474
490
|
|
|
475
491
|
def pre(payload, st):
|
|
492
|
+
global _CALL
|
|
493
|
+
_CALL = payload
|
|
476
494
|
name = str(payload.get('tool_name') or '')
|
|
477
495
|
args = payload.get('tool_input') or {}
|
|
478
496
|
if not isinstance(args, dict): args = {}
|
|
@@ -797,7 +815,7 @@ def stop(payload, st):
|
|
|
797
815
|
commands = [c for c in load('kimi-commands.json', []) if isinstance(c, dict)]
|
|
798
816
|
record, earlier = ledger_record()
|
|
799
817
|
report = verification.report(REPO, st.get('base'), answer, commands, load('checks.json', []), st.get('task', ''), last_edit=last_edit, edits=st.get('edits') or 0,
|
|
800
|
-
scope=load('scope.json', None), record=record, earlier=earlier)
|
|
818
|
+
scope=load('scope.json', None), record=record, earlier=earlier, root=ROOT)
|
|
801
819
|
save('verification.json', report); event('verification_report', **{k: report.get(k) for k in ('status', 'reasons', 'findings', 'checks', 'passed', 'stale')})
|
|
802
820
|
blocking = [f['message'] for f in report['findings'] if f.get('severity') == 'block']
|
|
803
821
|
if blocking: done.add('verification'); issues += blocking[:3]
|
package/worker/mlai_hooks.py
CHANGED
|
@@ -1060,7 +1060,7 @@ CANNOT_COMPLETE_TOOL = {'name': 'cannot_complete', 'description': (
|
|
|
1060
1060
|
'Report that the request cannot be done as asked (for example: pushing with no git remote configured, a feature this '
|
|
1061
1061
|
'product does not have, an action the user denied). Call it once, before your final answer, with a short reason the user '
|
|
1062
1062
|
'can act on. Do not call it when the work is done or only partly verified.'),
|
|
1063
|
-
'inputSchema': {'type': 'object', 'properties': {'reason': {'type': 'string', 'minLength': 1, '
|
|
1063
|
+
'inputSchema': {'type': 'object', 'properties': {'reason': {'type': 'string', 'minLength': 1, 'description': 'One or two sentences (longer reasons are shortened to 600 characters).'}}, 'required': ['reason']}}
|
|
1064
1064
|
|
|
1065
1065
|
|
|
1066
1066
|
def record_cannot_complete(args, root=None):
|
package/worker/mlai_mcp.py
CHANGED
|
@@ -257,6 +257,17 @@ def browser(args):
|
|
|
257
257
|
except (OSError, ValueError): evidence = []
|
|
258
258
|
tmp = ROOT / 'design-browser-evidence.json.tmp'; tmp.write_text(json.dumps((evidence + [record])[-12:])); tmp.replace(ROOT / 'design-browser-evidence.json')
|
|
259
259
|
emit('browser', content=json.dumps(outcome)[:4000], screenshot=outcome.get('screenshot'))
|
|
260
|
+
try:
|
|
261
|
+
# A browser scenario is a check like a test run: it counts in the completion record (CLI 0.3.4: two passing browser
|
|
262
|
+
# checks, yet "No verification checks recorded").
|
|
263
|
+
from runtime_control import browser_check
|
|
264
|
+
check = browser_check(outcome, 'supporting' if run_state().get('inspect') else 'repository')
|
|
265
|
+
try: checks = json.loads((ROOT / 'checks.json').read_text())
|
|
266
|
+
except (OSError, ValueError): checks = []
|
|
267
|
+
checks = [c for c in checks if isinstance(c, dict) and c.get('command') != check['command']][-19:] + [check]
|
|
268
|
+
tmp = ROOT / 'checks.json.tmp'; tmp.write_text(json.dumps(checks)); tmp.replace(ROOT / 'checks.json')
|
|
269
|
+
emit('verification', **check)
|
|
270
|
+
except Exception: pass
|
|
260
271
|
shown = {k: v for k, v in outcome.items() if k != 'text'}; shown['text'] = str(outcome.get('text', ''))[:3000]
|
|
261
272
|
return shown
|
|
262
273
|
|
package/worker/runner.py
CHANGED
|
@@ -680,6 +680,17 @@ class Model:
|
|
|
680
680
|
common+='Finish the review with a tool fence containing {"name":"finish_review","arguments":{"report":"Evidence-backed findings and limitations"}}.'
|
|
681
681
|
return common
|
|
682
682
|
return common+'Use a bash fence for one shell command, or a tool fence with connection workspace, name verify and arguments {label,command} to record an actual project check. To submit, use a bash fence containing echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT; the normal completion review and verification gates still apply.'
|
|
683
|
+
def unknown_tool(self,name):
|
|
684
|
+
"""Tester report on 0.3.4: a call to a tool that is not offered (Edit in a read-only turn) only got a generic
|
|
685
|
+
format hint back, and the model kept trying. Say plainly that it does not exist and list what does."""
|
|
686
|
+
name=str(name or '')[:100]
|
|
687
|
+
offered=[n for n in getattr(self,'offered_functions',None) or [] if n!=name]
|
|
688
|
+
names=sorted(getattr(self,'workspace_tool_names',None) or [])
|
|
689
|
+
line='There is no tool named "'+name+'" in this turn; do not call it again.'
|
|
690
|
+
if offered:line+=' Available tools: '+', '.join(offered)+'.'
|
|
691
|
+
if names and 'workspace_tool' in offered:line+=' workspace_tool names: '+', '.join(names)+'.'
|
|
692
|
+
if conversation_readonly(self.config):line+=' This turn is read-only: files cannot be changed, so answer the user with finish_response instead of trying to edit.'
|
|
693
|
+
return line
|
|
683
694
|
def reject_native(self,reason):
|
|
684
695
|
self.native_tool_failures+=1
|
|
685
696
|
event('retry',content=reason+' Nothing was executed.')
|
|
@@ -739,7 +750,7 @@ class Model:
|
|
|
739
750
|
# Computer use (Mac app, local runs, turned on in Settings): offered only when the app passed its broker in the env.
|
|
740
751
|
if computer_use.available() and self.config.get('mode')!='plan' and not self.config.get('subagent') and not conversation_readonly(self.config):tools[0]['function']['parameters']['properties']['name']['enum'].append('computer')
|
|
741
752
|
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}}})
|
|
742
|
-
if allow_tools and self.native_tool_failures<2:payload['tools']=tools
|
|
753
|
+
if allow_tools and self.native_tool_failures<2:payload['tools']=tools;self.offered_functions=[t['function']['name'] for t in tools]
|
|
743
754
|
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'}
|
|
744
755
|
key = os.environ.get(self.config.get('_key_env') or 'MODEL_API_KEY','') # the side model has its own key
|
|
745
756
|
headers={'Authorization': 'Bearer ' + key, 'Content-Type':'application/json'}
|
|
@@ -913,7 +924,7 @@ class Model:
|
|
|
913
924
|
event('retry',content='Recovered a malformed native call into a validated action.')
|
|
914
925
|
text+=('\n```bash\n'+value+'\n```') if kind=='bash' else ('\n```tool\n'+json.dumps(value)+'\n```')
|
|
915
926
|
else:
|
|
916
|
-
return self.reject_native(
|
|
927
|
+
return self.reject_native(self.unknown_tool(function.get('name'))),usage
|
|
917
928
|
if why:remember_step(function.get('name'),arguments,why)
|
|
918
929
|
finish=result.get('choices',[{}])[0].get('finish_reason') if result.get('choices') else result.get('stop_reason')
|
|
919
930
|
if finish in ('length','max_tokens') or not text.strip():
|
|
@@ -1334,8 +1345,9 @@ class Environment(LocalEnvironment):
|
|
|
1334
1345
|
if outcome.get('screenshot'):
|
|
1335
1346
|
vp=outcome.get('viewport') or {}
|
|
1336
1347
|
self.attach_vision({'path':str(directory/outcome['screenshot']),'name':outcome['screenshot'],'note':str(outcome.get('url',''))+' at '+str(vp.get('width','?'))+'x'+str(vp.get('height','?'))+('; scenario passed' if outcome.get('success') else '; scenario reported problems')})
|
|
1337
|
-
|
|
1338
|
-
|
|
1348
|
+
from runtime_control import browser_check
|
|
1349
|
+
record=browser_check(outcome) # names the page actually checked, not the default address (CLI 0.3.4)
|
|
1350
|
+
write('checks.json',[c for c in load(ROOT,'checks.json',[]) if c.get('command')!=record['command']][-19:]+[record])
|
|
1339
1351
|
if not outcome.get('success'):
|
|
1340
1352
|
self.plan_failure('browser','Browser scenario',result)
|
|
1341
1353
|
return {'returncode':1,'output':result}
|
|
@@ -1395,6 +1407,10 @@ class Environment(LocalEnvironment):
|
|
|
1395
1407
|
scope=verification_scope(command,REPO)
|
|
1396
1408
|
before_check=fingerprint(REPO,self.review_base or 'HEAD')
|
|
1397
1409
|
outcome=self.execute({'command':command,'verification':True})
|
|
1410
|
+
if outcome.get('executed') is not True:
|
|
1411
|
+
# Refused, blocked or not run (plan mode, offline wait): not a check result, and never a replacement for
|
|
1412
|
+
# the last real result of this command (CLI 0.3.4).
|
|
1413
|
+
return {**outcome,'output':str(outcome.get('output',''))+'\n[This check did not run, so no result was recorded.]'}
|
|
1398
1414
|
if scope=='inspection-only':outcome={**outcome,'output':outcome.get('output','')+'\n[Inspection only: displaying a log or value does not verify the source. Run an actual test or executable assertion through verify; this command cannot satisfy completion.]'}
|
|
1399
1415
|
elif scope!='repository':outcome={**outcome,'output':outcome.get('output','')+'\n[Auxiliary reproduction only: this check changes outside the repository or has an unresolved working directory. Run passing checks against the actual repository after review to complete.]'}
|
|
1400
1416
|
record={'label':label,'command':command,'scope':scope,'returncode':outcome.get('returncode',1),'executed':outcome.get('executed',False),'output':outcome.get('output','')[-4000:],'time':time.time(),'sourceFingerprint':fingerprint(REPO,self.review_base or 'HEAD')}
|
|
@@ -1892,6 +1908,31 @@ def run_usage(model,environment):
|
|
|
1892
1908
|
'research':int(getattr(environment,'web_attempts',0) or 0),'connectors':int(counts.get('connectors',0)),'tools':int(counts.get('tools',0)),
|
|
1893
1909
|
**{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'))}}
|
|
1894
1910
|
|
|
1911
|
+
def direct_turn(config,model,history=(),status_extra=None,usage_extra=None,metrics=True):
|
|
1912
|
+
"""A greeting, thanks or question about the agent (conversation.direct_reply): one model call with no tools, then the
|
|
1913
|
+
run ends. Nothing is read, scanned, edited or checked, so there is no changed-files or checks line either."""
|
|
1914
|
+
from conversation import direct_messages,direct_answer
|
|
1915
|
+
text=''
|
|
1916
|
+
try:text,_=model.request(direct_messages(config,REPO,history),max_tokens=400,allow_tools=False)
|
|
1917
|
+
except (KeyboardInterrupt,SystemExit):raise
|
|
1918
|
+
except Exception as exc:
|
|
1919
|
+
if (ROOT/'cancel').exists():raise
|
|
1920
|
+
event('retry',content='The model call for this reply failed ('+str(exc).replace(os.environ.get('MODEL_API_KEY','__absent__'),'[redacted]')[:160]+'); sent a short reply instead.')
|
|
1921
|
+
answer=direct_answer(text,config.get('task'),REPO)
|
|
1922
|
+
event('answer',content=answer)
|
|
1923
|
+
report={'changedFiles':[],'changedFilesTotal':0,'checksPassed':0,'checksFailed':0,'checksNotRun':0,'checksInconclusive':0,'checksStale':0,
|
|
1924
|
+
'externalChecksPassed':0,'externalChecksPending':0,'externalChecksFailed':0,'unfinishedTasks':[],'verification':'','verificationStatus':'none','created':time.time()}
|
|
1925
|
+
write('completion.json',report);event('completion',**report)
|
|
1926
|
+
write('status.json',{'state':'completed','result':{'exit_status':'Submitted','submission':answer},'tokens':model.tokens+image_billed(),'calls':model.calls,
|
|
1927
|
+
**(status_extra or {}),'endReason':'completed','usage':{**run_usage(model,None),**(usage_extra or {}),'endReason':'completed','direct':1}})
|
|
1928
|
+
event('finished',state='completed')
|
|
1929
|
+
if not metrics:return answer # the Kimi bridge reports run_metrics in its own finally
|
|
1930
|
+
try:
|
|
1931
|
+
m=model.metrics;wall=time.monotonic()-m['started']
|
|
1932
|
+
event('run_metrics',calls=model.calls,wall_seconds=round(wall,2),inference_seconds=round(m['inference_seconds'],2),tool_seconds=round(max(0.0,wall-m['inference_seconds']),2),prompt_tokens=m['prompt_tokens'],cached_tokens=m['cached_tokens'],completion_tokens=m['completion_tokens'],cache_hit_rate=round(m['cached_tokens']/m['prompt_tokens'],3) if m['prompt_tokens'] else 0)
|
|
1933
|
+
except Exception:pass
|
|
1934
|
+
return answer
|
|
1935
|
+
|
|
1895
1936
|
def main():
|
|
1896
1937
|
config = json.loads((ROOT / 'request.json').read_text())
|
|
1897
1938
|
write('status.json', {'state':'running','started':time.time(),'boot':boot_id(),'process':identity(os.getpid())})
|
|
@@ -1947,6 +1988,16 @@ def main():
|
|
|
1947
1988
|
event('error',content=message)
|
|
1948
1989
|
(ROOT/'pending.json').unlink(missing_ok=True);(ROOT/'run.lock').unlink(missing_ok=True)
|
|
1949
1990
|
return
|
|
1991
|
+
from conversation import direct_reply
|
|
1992
|
+
if direct_reply(config):
|
|
1993
|
+
try:direct_turn(config,model,config.get('conversation_history') or ())
|
|
1994
|
+
except Exception as exc:
|
|
1995
|
+
message=str(exc).replace(os.environ.get('MODEL_API_KEY','__absent__'),'[redacted]')
|
|
1996
|
+
write('status.json',{'state':'stopped' if (ROOT/'cancel').exists() else 'error','error':message,'tokens':model.tokens+image_billed(),'calls':model.calls})
|
|
1997
|
+
event('error',content=message)
|
|
1998
|
+
finally:
|
|
1999
|
+
for name in ('pending.json','run.lock'):(ROOT/name).unlink(missing_ok=True)
|
|
2000
|
+
return
|
|
1950
2001
|
resolve_role(config)
|
|
1951
2002
|
system = SYSTEM
|
|
1952
2003
|
from reply_language import rule as reply_language_rule
|
|
@@ -35,6 +35,11 @@ def latest_checks(checks):
|
|
|
35
35
|
items = list(latest.items())
|
|
36
36
|
out = []
|
|
37
37
|
for n, (key, check) in enumerate(items):
|
|
38
|
+
# A failed browser check followed by a passing one at the same width is history: the agent fixed the page or found
|
|
39
|
+
# the right address (CLI 0.3.4: the first check opened another app on port 3000, the next the notes app on its port).
|
|
40
|
+
if check.get('kind') == 'browser' and check_status(check) == 'failed' and any(
|
|
41
|
+
k[0] == key[0] and c.get('kind') == 'browser' and c.get('width') == check.get('width') and check_status(c) == 'passed'
|
|
42
|
+
for k, c in items[n + 1:]): continue
|
|
38
43
|
broader = [c for k, c in items[n + 1:] if k[0] == key[0] and isinstance(k[1], str) and isinstance(key[1], str)
|
|
39
44
|
and key[1].startswith(k[1] + ' ') and NARROWING.search(key[1][len(k[1]):]) and check_status(c) == 'passed']
|
|
40
45
|
if broader and check_status(check) == 'failed': continue
|
|
@@ -89,7 +94,7 @@ def check_status(check):
|
|
|
89
94
|
return 'passed'
|
|
90
95
|
|
|
91
96
|
def check_summary(checks):
|
|
92
|
-
result = dict(passed=0, failed=0, notRun=0, inconclusive=0, stale=0, externalPending=0, externalFailed=0, externalPassed=0, supporting=0)
|
|
97
|
+
result = dict(passed=0, failed=0, notRun=0, inconclusive=0, stale=0, externalPending=0, externalFailed=0, externalPassed=0, supporting=0, supportingPassed=0, supportingFailed=0)
|
|
93
98
|
for check in latest_checks(checks):
|
|
94
99
|
scope = check.get('scope') or 'repository'
|
|
95
100
|
status = check_status(check)
|
|
@@ -97,6 +102,7 @@ def check_summary(checks):
|
|
|
97
102
|
result[{'passed': 'externalPassed', 'failed': 'externalFailed'}.get(status, 'externalPending')] += 1
|
|
98
103
|
elif scope != 'repository':
|
|
99
104
|
result['supporting'] += 1
|
|
105
|
+
if status in ('passed', 'failed'): result['supporting' + status.title()] += 1
|
|
100
106
|
else:
|
|
101
107
|
result[{'not_run': 'notRun'}.get(status, status)] += 1
|
|
102
108
|
result['needsAttention'] = bool(result['failed'] or result['notRun'] or result['inconclusive'] or result['stale'] or result['externalPending'] or result['externalFailed'])
|
|
@@ -152,7 +158,10 @@ def completion_report(root,repo,base=None,since=None):
|
|
|
152
158
|
except (OSError,ValueError):return fallback
|
|
153
159
|
checks=read('checks.json',[]);todos=read('todos.json',[])
|
|
154
160
|
if since is None:since=read('status.json',{}).get('started') if isinstance(read('status.json',{}),dict) else None
|
|
155
|
-
|
|
161
|
+
files=read(START_FILES,None)
|
|
162
|
+
# A folder without git: what the turn wrote is what differs from the file snapshot taken when it started (CLI 0.3.4:
|
|
163
|
+
# index.html and styles.css were written, yet the record listed no files).
|
|
164
|
+
changes=snapshot_changes(repo,files) if isinstance(files,dict) and not is_git_tree(repo) else completion_paths(repo,base,since)
|
|
156
165
|
# Only what this turn changed: paths already changed or untracked when the turn started (save_start_dirty) are the
|
|
157
166
|
# person's own work. Subtracted before the list is clipped, so 600 old files cannot hide the one this turn made.
|
|
158
167
|
snapshot=read(START_DIRTY,None)
|
|
@@ -163,11 +172,14 @@ def completion_report(root,repo,base=None,since=None):
|
|
|
163
172
|
if summary['stale']: note += ' %d passed before the last code edit and must be rerun.' % summary['stale']
|
|
164
173
|
if summary['externalPending'] or summary['externalFailed'] or summary['externalPassed']:
|
|
165
174
|
note += ' Remote/CI: %(externalPassed)d passed, %(externalFailed)d failed, %(externalPending)d pending or unverified.' % summary
|
|
166
|
-
if summary['supporting']: note += ' %d supporting evidence checks are not repository verification.' % summary['supporting']
|
|
175
|
+
if summary['supporting']: note += ' %d supporting evidence checks are not repository verification (%d passed, %d failed).' % (summary['supporting'], summary['supportingPassed'], summary['supportingFailed'])
|
|
167
176
|
if unfinished: note += ' %d plan items remain unfinished.' % len(unfinished)
|
|
168
177
|
status = 'remote_pending' if summary['remotePendingOnly'] and not unfinished else 'needs_attention' if summary['needsAttention'] or unfinished else 'passed' if summary['passed'] else 'unverified'
|
|
169
178
|
if status == 'remote_pending': note += ' Local checks passed; the remote result has not been observed, so this is not yet confirmed end to end.'
|
|
179
|
+
# checksPassed stays repository verification only (the gates rely on it). The evidence checks an inspect-only turn ran are
|
|
180
|
+
# counted next to it, so the Done line says "1 check passed" instead of "0 checks passed" (CLI 0.3.4).
|
|
170
181
|
return {'changedFiles':changes[:100], 'changedFilesTotal':len(changes), 'checksPassed':summary['passed'], 'checksFailed':summary['failed'],
|
|
182
|
+
'supportingChecksPassed':summary['supportingPassed'], 'supportingChecksFailed':summary['supportingFailed'],
|
|
171
183
|
'checksNotRun':summary['notRun'], 'checksInconclusive':summary['inconclusive'], 'checksStale':summary['stale'], 'externalChecksPassed':summary['externalPassed'],
|
|
172
184
|
'externalChecksPending':summary['externalPending'], 'externalChecksFailed':summary['externalFailed'],
|
|
173
185
|
'unfinishedTasks':unfinished, 'verification':note, 'verificationStatus':status, 'created':time.time()}
|
|
@@ -214,16 +226,66 @@ def dirty_snapshot(repo,base=None,limit=None,hash_limit=2000,hash_bytes=2_000_00
|
|
|
214
226
|
|
|
215
227
|
|
|
216
228
|
START_DIRTY='start-dirty.json'
|
|
229
|
+
START_FILES='start-files.json'
|
|
217
230
|
|
|
218
231
|
def save_start_dirty(root,repo,base=None):
|
|
219
232
|
"""BUG-0495/0497: remember what was already changed or untracked when the turn started, so the checker and the
|
|
220
|
-
"N files changed" summary only count what this turn did.
|
|
233
|
+
"N files changed" summary only count what this turn did. A folder without git gets a file snapshot instead
|
|
234
|
+
(snapshot_changes). Best effort: a failure keeps the old behavior."""
|
|
235
|
+
try:
|
|
236
|
+
files=Path(root)/START_FILES
|
|
237
|
+
if is_git_tree(repo):files.unlink(missing_ok=True)
|
|
238
|
+
else:files.write_text(json.dumps(file_snapshot(repo)))
|
|
239
|
+
except Exception:pass
|
|
221
240
|
try:
|
|
222
241
|
snap=dirty_snapshot(repo,base)
|
|
223
242
|
(Path(root)/START_DIRTY).write_text(json.dumps(snap))
|
|
224
243
|
return snap
|
|
225
244
|
except Exception:return None
|
|
226
245
|
|
|
246
|
+
|
|
247
|
+
def is_git_tree(repo):
|
|
248
|
+
import subprocess
|
|
249
|
+
try:return subprocess.run(['git','rev-parse','--is-inside-work-tree'],cwd=repo,capture_output=True,text=True,timeout=10).stdout.strip()=='true'
|
|
250
|
+
except (OSError,subprocess.SubprocessError):return False
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
# Folders that are never the turn's own work: dependencies, caches, VCS and engine state.
|
|
254
|
+
SNAPSHOT_SKIP=frozenset(('.git','node_modules','.mlai','.mini','.kimi-code','__pycache__','.pytest_cache','.mypy_cache','.ruff_cache','.venv','venv','.next','.turbo','.vercel','.cache','.parcel-cache','.svelte-kit'))
|
|
255
|
+
|
|
256
|
+
def file_snapshot(repo,limit=20000,seconds=3.0):
|
|
257
|
+
"""Size and modification time of every file under repo, sorted, without noise folders. Bounded by file count and time;
|
|
258
|
+
'complete' says whether the whole folder was seen."""
|
|
259
|
+
from repo_hygiene import is_noise
|
|
260
|
+
files,complete,deadline={},True,time.monotonic()+seconds
|
|
261
|
+
root=str(repo)
|
|
262
|
+
for top,dirs,names in os.walk(root):
|
|
263
|
+
dirs[:]=sorted(d for d in dirs if d not in SNAPSHOT_SKIP and not os.path.islink(os.path.join(top,d)))
|
|
264
|
+
rel=os.path.relpath(top,root);rel='' if rel=='.' else rel.replace(os.sep,'/')+'/'
|
|
265
|
+
for name in sorted(names):
|
|
266
|
+
path=rel+name
|
|
267
|
+
if is_noise(path):continue
|
|
268
|
+
try:st=os.lstat(os.path.join(top,name))
|
|
269
|
+
except OSError:continue
|
|
270
|
+
files[path]='%d:%d'%(st.st_size,st.st_mtime_ns)
|
|
271
|
+
if len(files)>=limit:break
|
|
272
|
+
if len(files)>=limit or time.monotonic()>deadline:complete=False;break
|
|
273
|
+
return {'complete':complete,'time':time.time(),'files':files}
|
|
274
|
+
|
|
275
|
+
def snapshot_changes(repo,start):
|
|
276
|
+
"""Paths created, changed or deleted since the file_snapshot start. When the start snapshot was cut short, a path it
|
|
277
|
+
never saw counts only if it was modified after the snapshot."""
|
|
278
|
+
old=start.get('files') if isinstance(start.get('files'),dict) else {}
|
|
279
|
+
now=file_snapshot(repo)
|
|
280
|
+
began=float(start.get('time') or 0)
|
|
281
|
+
out=[]
|
|
282
|
+
for path,fp in now['files'].items():
|
|
283
|
+
if path in old:
|
|
284
|
+
if old[path]!=fp:out.append(path)
|
|
285
|
+
elif start.get('complete',True) or int(fp.split(':')[1])/1e9>=began-1:out.append(path)
|
|
286
|
+
if now['complete']:out+=[p for p in old if p not in now['files']]
|
|
287
|
+
return out
|
|
288
|
+
|
|
227
289
|
def turn_changes(root,repo,changed):
|
|
228
290
|
"""changed minus the paths that were already like this when the turn started (see save_start_dirty)."""
|
|
229
291
|
try:snap=json.loads((Path(root)/START_DIRTY).read_text())
|
|
@@ -285,6 +347,18 @@ def approve_operation(root,mode,description,cancelled,emit):
|
|
|
285
347
|
decision.unlink(missing_ok=True);(root/'pending.json').unlink(missing_ok=True)
|
|
286
348
|
|
|
287
349
|
|
|
350
|
+
def browser_check(outcome, scope='repository'):
|
|
351
|
+
"""A browser scenario as a check record. It names the address the browser actually checked, not the one first asked
|
|
352
|
+
for or the default (CLI 0.3.4: a passing check of the notes app on :8741 was saved as http://127.0.0.1:3000)."""
|
|
353
|
+
outcome = outcome if isinstance(outcome, dict) else {}
|
|
354
|
+
url = str(outcome.get('checkedAt') or outcome.get('url') or outcome.get('requestedUrl') or '')[:1000]
|
|
355
|
+
vp = outcome.get('viewport') if isinstance(outcome.get('viewport'), dict) else {}
|
|
356
|
+
width = ' at %spx' % vp['width'] if vp.get('width') else ''
|
|
357
|
+
return {'label': clip('Browser check ' + url + width, 160), 'command': 'browser ' + url + width, 'kind': 'browser', 'url': url, 'width': vp.get('width'),
|
|
358
|
+
'returncode': 0 if outcome.get('success') else 1, 'executed': True, 'scope': scope,
|
|
359
|
+
'output': json.dumps({k: v for k, v in outcome.items() if k != 'text'})[-4000:], 'time': time.time()}
|
|
360
|
+
|
|
361
|
+
|
|
288
362
|
def clip(text, limit):
|
|
289
363
|
"""Shorten for display with a visible ellipsis, so a shortened command is never read (or rerun) as a complete one."""
|
|
290
364
|
text = ' '.join(str(text or '').split())
|
package/worker/verification.py
CHANGED
|
@@ -132,10 +132,23 @@ def changed_paths(repo, base):
|
|
|
132
132
|
return list(dict.fromkeys(changed)), deleted
|
|
133
133
|
|
|
134
134
|
|
|
135
|
-
def
|
|
135
|
+
def own_paths(repo, base, root=None):
|
|
136
|
+
"""changed_paths, minus what was already changed or untracked when the turn started (runtime_control.save_start_dirty).
|
|
137
|
+
Tester report on 0.3.4: "Hi" in a folder with an untracked service-account key file was told by the done gate to remove
|
|
138
|
+
the "added" private key, so a greeting turned into repair rounds about a file the user had not asked about."""
|
|
139
|
+
changed, deleted = changed_paths(repo, base)
|
|
140
|
+
if root is None: return changed, deleted
|
|
141
|
+
try:
|
|
142
|
+
from runtime_control import turn_changes
|
|
143
|
+
mine = set(turn_changes(root, repo, list(changed) + list(deleted)))
|
|
144
|
+
except Exception: return changed, deleted
|
|
145
|
+
return [p for p in changed if p in mine], [p for p in deleted if p in mine]
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def diff_checks(repo, base, task='', paths=None):
|
|
136
149
|
"""Reward-hacking signals in the working tree versus base: skipped/disabled tests, deleted test files, removed assertions, hard-coded outputs."""
|
|
137
150
|
findings = []
|
|
138
|
-
changed, deleted = changed_paths(repo, base)
|
|
151
|
+
changed, deleted = paths if paths is not None else changed_paths(repo, base)
|
|
139
152
|
if not changed and not deleted: return findings
|
|
140
153
|
tests_changed = [p for p in changed if TEST_PATH.search(p)]
|
|
141
154
|
tests_deleted = [p for p in deleted if TEST_PATH.search(p)]
|
|
@@ -678,9 +691,13 @@ def notice(report):
|
|
|
678
691
|
# in the harness's own words instead of the run being held open. A citation that does not resolve is a fact, so it stays blocking.
|
|
679
692
|
SOFT = frozenset(('uncited_behaviour', 'unacknowledged_change', 'contradicted_denial', 'remote_pending_claim'))
|
|
680
693
|
|
|
681
|
-
def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=()):
|
|
682
|
-
"""Everything the gates need in one dict: findings, verdict, and the texts to show.
|
|
694
|
+
def report(repo, base, final_text, commands, checks, task='', last_edit=None, edits=0, scope=None, record=None, earlier=None, asked=(), root=None):
|
|
695
|
+
"""Everything the gates need in one dict: findings, verdict, and the texts to show. With root (the run folder), the
|
|
696
|
+
diff checks look only at what this turn changed, not at files that were already changed or untracked at its start."""
|
|
683
697
|
findings = []
|
|
698
|
+
paths = None
|
|
699
|
+
try: paths = own_paths(repo, base, root)
|
|
700
|
+
except Exception: pass
|
|
684
701
|
stale = []
|
|
685
702
|
try: stale = stale_checks(checks, last_edit)
|
|
686
703
|
except Exception: pass
|
|
@@ -696,10 +713,10 @@ def report(repo, base, final_text, commands, checks, task='', last_edit=None, ed
|
|
|
696
713
|
except Exception: pass
|
|
697
714
|
try: findings += empty_runs(fresh)
|
|
698
715
|
except Exception: pass
|
|
699
|
-
try: findings += diff_checks(repo, base, task)
|
|
716
|
+
try: findings += diff_checks(repo, base, task, paths)
|
|
700
717
|
except Exception: pass
|
|
701
718
|
files = None
|
|
702
|
-
try: files = file_diffs(repo, base)
|
|
719
|
+
try: files = file_diffs(repo, base, *paths) if paths is not None else file_diffs(repo, base)
|
|
703
720
|
except Exception: pass
|
|
704
721
|
if files is not None:
|
|
705
722
|
for check in (lambda: line_rules(files, task), lambda: check_weakening(files, task), lambda: invalid_json(repo, files), lambda: out_of_scope(files, scope), lambda: empty_change(edits, files, task) if in_git(repo) else [], lambda: forbidden_test_script(files, task)):
|
|
@@ -708,7 +725,7 @@ def report(repo, base, final_text, commands, checks, task='', last_edit=None, ed
|
|
|
708
725
|
# Build integrity: navigation into pages that were never built, and a message that admits an open error yet says done.
|
|
709
726
|
try:
|
|
710
727
|
import site_check
|
|
711
|
-
changed = changed_paths(repo, base)[0] if base else []
|
|
728
|
+
changed = (paths[0] if paths is not None else changed_paths(repo, base)[0]) if base else []
|
|
712
729
|
if changed: findings += site_check.dead_links(repo, changed)
|
|
713
730
|
findings += site_check.open_error_claim(final_text)
|
|
714
731
|
except Exception: pass
|