mlai-code 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/checkpoints.mjs +217 -0
  4. package/src/cli.mjs +153 -26
  5. package/src/cloud.mjs +2 -0
  6. package/src/engine.mjs +1 -1
  7. package/src/headless.mjs +39 -11
  8. package/src/home.mjs +3 -2
  9. package/src/host.mjs +141 -13
  10. package/src/ide-quick.mjs +147 -0
  11. package/src/ide.mjs +182 -0
  12. package/src/netbox.mjs +1 -1
  13. package/src/parity.mjs +17 -0
  14. package/src/relay.mjs +1 -1
  15. package/src/render.mjs +16 -5
  16. package/src/runtime.mjs +2 -0
  17. package/src/server.mjs +46 -7
  18. package/src/session.mjs +31 -8
  19. package/src/tui/acp.mjs +121 -19
  20. package/src/tui/app.mjs +2 -1
  21. package/src/tui/chat.mjs +35 -10
  22. package/src/tui/cmd-config.mjs +3 -1
  23. package/src/tui/cmd-core.mjs +9 -5
  24. package/src/tui/cmd-extensibility.mjs +5 -0
  25. package/src/tui/extensions-local.mjs +2 -0
  26. package/src/tui/mcp-config.mjs +22 -4
  27. package/src/tui/review-cli.mjs +15 -4
  28. package/src/tui/snapshots.mjs +57 -16
  29. package/src/tui/transcript.mjs +13 -2
  30. package/src/two-words.mjs +14 -8
  31. package/src/view-sync.mjs +48 -0
  32. package/src/winenv.mjs +1 -1
  33. package/src/workspace-api.mjs +21 -4
  34. package/vendor/client-sdk/index.cjs +2 -1
  35. package/vendor/cloud-cli/mlai.mjs +23 -11
  36. package/worker/architecture_worker.py +33 -4
  37. package/worker/browser_tool.py +19 -1
  38. package/worker/chat_fork.py +4 -1
  39. package/worker/checkpoints.py +71 -8
  40. package/worker/command_safety.py +56 -3
  41. package/worker/connected_turn.py +30 -0
  42. package/worker/conversation.py +125 -13
  43. package/worker/git_sync.py +4 -4
  44. package/worker/image_generation.py +2 -2
  45. package/worker/kimi_bridge.py +251 -31
  46. package/worker/kimi_gates.py +25 -6
  47. package/worker/kimi_hook.py +154 -13
  48. package/worker/media_tools.py +4 -1
  49. package/worker/mlai_hooks.py +222 -3
  50. package/worker/mlai_mcp.py +26 -6
  51. package/worker/mlai_review/README.md +35 -0
  52. package/worker/mlai_review/__init__.py +1 -0
  53. package/worker/mlai_review/__main__.py +261 -0
  54. package/worker/mlai_review/_compat.py +29 -0
  55. package/worker/mlai_review/analysis/__init__.py +3 -0
  56. package/worker/mlai_review/analysis/detect.py +50 -0
  57. package/worker/mlai_review/analysis/main.py +128 -0
  58. package/worker/mlai_review/analysis/messages.py +8 -0
  59. package/worker/mlai_review/analysis/tools.py +194 -0
  60. package/worker/mlai_review/application/__init__.py +1 -0
  61. package/worker/mlai_review/application/asking.py +53 -0
  62. package/worker/mlai_review/application/challengers.py +142 -0
  63. package/worker/mlai_review/application/context_builder.py +194 -0
  64. package/worker/mlai_review/application/errors.py +17 -0
  65. package/worker/mlai_review/application/feedback.py +52 -0
  66. package/worker/mlai_review/application/matching.py +46 -0
  67. package/worker/mlai_review/application/messages.py +41 -0
  68. package/worker/mlai_review/application/ports.py +222 -0
  69. package/worker/mlai_review/application/prompts.py +342 -0
  70. package/worker/mlai_review/application/review_service.py +200 -0
  71. package/worker/mlai_review/application/reviewers.py +438 -0
  72. package/worker/mlai_review/application/streaming.py +91 -0
  73. package/worker/mlai_review/application/tools.py +138 -0
  74. package/worker/mlai_review/application/usage.py +145 -0
  75. package/worker/mlai_review/application/write_check.py +46 -0
  76. package/worker/mlai_review/contracts/__init__.py +5 -0
  77. package/worker/mlai_review/contracts/analysis.py +57 -0
  78. package/worker/mlai_review/contracts/review.py +42 -0
  79. package/worker/mlai_review/domain/__init__.py +4 -0
  80. package/worker/mlai_review/domain/changes.py +77 -0
  81. package/worker/mlai_review/domain/context.py +147 -0
  82. package/worker/mlai_review/domain/diagnostics.py +239 -0
  83. package/worker/mlai_review/domain/feedback.py +216 -0
  84. package/worker/mlai_review/domain/findings.py +505 -0
  85. package/worker/mlai_review/domain/lifecycle.py +85 -0
  86. package/worker/mlai_review/domain/partition.py +42 -0
  87. package/worker/mlai_review/domain/relations.py +166 -0
  88. package/worker/mlai_review/domain/relevance.py +202 -0
  89. package/worker/mlai_review/domain/report.py +99 -0
  90. package/worker/mlai_review/domain/structure.py +56 -0
  91. package/worker/mlai_review/domain/writes.py +86 -0
  92. package/worker/mlai_review/eval.py +221 -0
  93. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  94. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  95. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  96. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  97. package/worker/mlai_review/feedback_report.py +60 -0
  98. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  99. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  100. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  101. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  102. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  103. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  104. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  105. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  106. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  107. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  108. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  109. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  110. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  111. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  112. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  113. package/worker/mlai_review/infrastructure/messages.py +54 -0
  114. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  115. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  116. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  117. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  118. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  119. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  120. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  121. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  122. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  123. package/worker/mlai_review/pipeline.py +89 -0
  124. package/worker/mlai_review/runtime.py +194 -0
  125. package/worker/mlai_review/settings.py +139 -0
  126. package/worker/preview.py +15 -0
  127. package/worker/product_facts.py +3 -1
  128. package/worker/repo_policy.py +5 -0
  129. package/worker/roles.py +29 -2
  130. package/worker/runner.py +164 -23
  131. package/worker/runtime_control.py +95 -9
  132. package/worker/secret_scan.py +3 -2
  133. package/worker/server_memory.py +122 -0
  134. package/worker/subagents.py +6 -2
  135. package/worker/tool_catalog.py +5 -2
  136. package/worker/verification.py +113 -8
@@ -2,11 +2,12 @@
2
2
  * Per-turn file snapshots for /rewind. Before each turn the project folder is recorded in a private git store in the
3
3
  * session folder (<session>/snapshots.git), never in the project's own repository: no commits, branches or stash
4
4
  * entries appear there. .gitignore rules are honored, so node_modules and build output are not copied.
5
- * Restoring a turn writes its files back and removes files created after it.
5
+ * Restoring a turn writes back only the files the agent changed in that turn and later (removing the ones it created);
6
+ * the user's own edits are left alone.
6
7
  */
7
8
  import {execFile} from 'node:child_process';
8
9
  import {existsSync} from 'node:fs';
9
- import {mkdir,readFile,writeFile,rm} from 'node:fs/promises';
10
+ import {mkdir,readFile,writeFile,rm,lstat} from 'node:fs/promises';
10
11
  import path from 'node:path';
11
12
 
12
13
  const LIMIT_MS=20000,MAX_BUFFER=64*1024*1024;
@@ -32,8 +33,24 @@ async function ensure(session){
32
33
  await writeFile(path.join(dir,'info','exclude'),own+'\n.git\n.mlai/sessions/\n');
33
34
  }
34
35
  const ref=n=>'refs/turns/'+n;
35
- /** The current folder as a tree id. */
36
- async function tree(session){await git(session,['add','-A','--','.']);return git(session,['write-tree'])}
36
+ // BUG-0553: snapshots skip large files (videos, data dumps, archives) so a question does not copy them into
37
+ // ~/.mlai/sessions every turn, and a folder too big to record is skipped as a whole.
38
+ export const SNAPSHOT_FILE_LIMIT=5*1024*1024,SNAPSHOT_TOTAL_LIMIT=200*1024*1024;
39
+ /** The current folder as a tree id. Files over SNAPSHOT_FILE_LIMIT are left out. */
40
+ async function tree(session){
41
+ const root=session.meta.root;
42
+ const list=(await git(session,['ls-files','-z','-c','-o','--exclude-standard'])).split('\0').filter(Boolean);
43
+ const keep=[],skip=[];let total=0;
44
+ for(const f of [...new Set(list)]){
45
+ const st=await lstat(path.join(root,f)).catch(()=>null);
46
+ if(st&&st.isFile()&&st.size>SNAPSHOT_FILE_LIMIT){skip.push(f);continue}
47
+ total+=st?.isFile()?st.size:0;keep.push(f);
48
+ }
49
+ if(total>SNAPSHOT_TOTAL_LIMIT)throw Object.assign(new Error('folder too large to snapshot'),{code:'TOO_LARGE'});
50
+ if(skip.length)await git(session,['rm','-q','--cached','--ignore-unmatch','--pathspec-from-file=-','--pathspec-file-nul'],{input:skip.join('\0')});
51
+ if(keep.length)await git(session,['add','-A','--pathspec-from-file=-','--pathspec-file-nul'],{input:keep.join('\0')});
52
+ return git(session,['write-tree']);
53
+ }
37
54
 
38
55
  /** Records the folder as it is before turn `turn` (0-based). Returns the commit id, or null when snapshots are off. */
39
56
  export async function snapshot(session,turn){
@@ -46,7 +63,7 @@ export async function snapshot(session,turn){
46
63
  return c;
47
64
  }catch(e){
48
65
  // A folder too large to record in time: turn snapshots off for this session rather than slowing every turn.
49
- if(e.code==='ETIMEDOUT'||e.killed||/timed out|ENOBUFS/.test(e.message)){session.meta.snapshots=false;await session.save().catch(()=>{})}
66
+ if(e.code==='TOO_LARGE'||e.code==='ETIMEDOUT'||e.killed||/timed out|ENOBUFS/.test(e.message)){session.meta.snapshots=false;await session.save().catch(()=>{})}
50
67
  return null;
51
68
  }
52
69
  }
@@ -54,7 +71,7 @@ export async function snapshot(session,turn){
54
71
  export async function snapshots(session){
55
72
  if(!existsSync(path.join(store(session),'HEAD')))return [];
56
73
  const out=await git(session,['for-each-ref','--format=%(refname)','refs/turns/']).catch(()=>'');
57
- return out.split('\n').map(r=>Number(r.split('/').pop())).filter(Number.isInteger).sort((a,b)=>a-b);
74
+ return out.split('\n').filter(r=>/^refs\/turns\/\d+$/.test(r)).map(r=>Number(r.split('/').pop())).sort((a,b)=>a-b);
58
75
  }
59
76
  /** Files that differ between now and the snapshot before `turn`: [{status:'A'|'M'|'D', path}] (A: created since). */
60
77
  export async function changesSince(session,turn){
@@ -62,26 +79,50 @@ export async function changesSince(session,turn){
62
79
  const out=await git(session,['diff','--name-status','--no-renames',ref(turn),now]);
63
80
  return out?out.split('\n').map(l=>{const [s,...p]=l.split('\t');return {status:s[0],path:p.join('\t')}}):[];
64
81
  }
82
+ /** Records the folder as the agent left it after turn `turn` (BUG-0525: so /rewind can tell the agent's changes from the user's). */
83
+ export async function snapshotAfter(session,turn){
84
+ if(session.meta.snapshots===false||!existsSync(path.join(store(session),'HEAD')))return null;
85
+ try{const c=await git(session,['commit-tree',await tree(session),'-m','After turn '+(turn+1)]);await git(session,['update-ref',afterRef(turn),c]);return c}catch{return null}
86
+ }
87
+ const afterRef=n=>'refs/turns/after/'+n;
88
+ const exists=(session,r)=>git(session,['rev-parse','-q','--verify',r+'^{commit}']).then(()=>true,()=>false);
89
+ const names=out=>out?out.split('\n').map(l=>{const [s,...p]=l.split('\t');return {status:s[0],path:p.join('\t')}}):[];
65
90
  /**
66
- * Writes the folder back to how it was before `turn`. The current state is kept as refs/turns/undo-<time> first, so a
67
- * rewind can itself be undone by hand. Returns {restored, removed} file counts.
91
+ * Writes back the files the agent changed in turn `turn` and later, as they were before `turn`. The current state is
92
+ * kept as refs/turns/undo-<time> first, so a rewind can itself be undone by hand.
93
+ * BUG-0525: files the user created or edited themselves (outside the agent's turns) are never touched, and a file the
94
+ * agent changed but the user edited again afterwards is kept (counted in `kept`). Returns {restored, removed, kept}.
68
95
  */
69
96
  export async function restore(session,turn){
70
97
  const list=await snapshots(session);
71
98
  if(!list.includes(turn))throw new Error('No file snapshot for turn '+(turn+1)+'.');
72
99
  const now=await tree(session);
73
100
  await git(session,['update-ref','refs/turns/undo-'+Date.now(),await git(session,['commit-tree',now,'-m','Before rewind'])]);
74
- const changes=await changesSince(session,turn);
75
- const created=changes.filter(c=>c.status==='A').map(c=>c.path);
76
- for(const f of created){
77
- const abs=path.resolve(session.meta.root,f);
78
- if(abs.startsWith(path.resolve(session.meta.root)+path.sep))await rm(abs,{force:true});
101
+ // The agent's changes per turn: before-turn snapshot to after-turn snapshot. Sessions from before after-snapshots
102
+ // existed fall back to the next turn's snapshot (or now).
103
+ const last=new Map();// path -> after ref of the last turn that changed it (null: no after snapshot)
104
+ for(const k of list.filter(k=>k>=turn)){
105
+ const hasAfter=await exists(session,afterRef(k));
106
+ const next=list.find(n=>n>k);
107
+ const end=hasAfter?afterRef(k):next!=null?ref(next):now;
108
+ for(const c of names(await git(session,['diff','--name-status','--no-renames',ref(k),end])))last.set(c.path,hasAfter?afterRef(k):null);
109
+ }
110
+ const base=path.resolve(session.meta.root);
111
+ let restored=0,removed=0,kept=0;
112
+ for(const [f,after] of last){
113
+ // Edited again by the user after the agent's turn: keep the user's version.
114
+ if(after&&await git(session,['diff','--quiet',after,now,'--',f]).then(()=>false,()=>true)){kept++;continue}
115
+ const abs=path.resolve(base,f);if(!abs.startsWith(base+path.sep))continue;
116
+ const before=await git(session,['ls-tree','-z',ref(turn),'--',f]).catch(()=>'');
117
+ if(before){await git(session,['checkout',ref(turn),'--',f]);restored++}
118
+ else if(existsSync(abs)||(await lstat(abs).catch(()=>null))){await rm(abs,{force:true});removed++}
79
119
  }
80
- await git(session,['read-tree',ref(turn)]);
81
- await git(session,['checkout-index','-a','-f']);
82
- return {restored:changes.length-created.length,removed:created.length};
120
+ return {restored,removed,kept};
83
121
  }
84
122
  /** Drops snapshots after `turn` (they belong to turns that no longer exist). */
85
123
  export async function forget(session,fromTurn){
86
124
  for(const n of await snapshots(session))if(n>fromTurn)await git(session,['update-ref','-d',ref(n)]).catch(()=>{});
125
+ if(!existsSync(path.join(store(session),'HEAD')))return;
126
+ const after=await git(session,['for-each-ref','--format=%(refname)','refs/turns/after/']).catch(()=>'');
127
+ for(const r of after.split('\n').filter(Boolean))if(Number(r.split('/').pop())>=fromTurn)await git(session,['update-ref','-d',r]).catch(()=>{});
87
128
  }
@@ -43,12 +43,23 @@ export class Transcript{
43
43
  }
44
44
  assistant(content){
45
45
  const p=prose(finishless(content));if(!p||p===this.lastAssistant)return [];
46
- this.lastAssistant=p;
46
+ this.lastAssistant=p;this.shown=[...(this.shown||[]),p].slice(-8);
47
47
  // Raw scrollback: the reply exactly as written (markdown source, no wrapping or color), for copying.
48
48
  if(this.raw)return ['',...p.split('\n')];
49
49
  const md=markdown(p,{t:this.t,cols:this.cols-2,box:this.box});
50
50
  return ['',...md.map((l,i)=>(i?' ':this.dot(this.t.text))+l)];
51
51
  }
52
+ /** The final answer. When the agent already wrote it as a reply above, it is not shown again; only what the harness
53
+ added to it (a verification note, a source list) is. */
54
+ answer(content){
55
+ const p=prose(finishless(content));if(!p)return [];
56
+ const norm=x=>x.replace(/\s+/g,' ').trim();
57
+ for(const seen of this.shown||[]){
58
+ if(norm(seen)===norm(p))return [];
59
+ if(seen.length>40&&p.includes(seen)){const rest=p.replace(seen,'').trim();return rest?this.assistant(rest):[]}
60
+ }
61
+ return this.assistant(p);
62
+ }
52
63
  tool(name,target){
53
64
  const t=this.t,v=verb(name);
54
65
  const tgt=target?' '+truncate(String(target),Math.max(10,this.cols-width(v)-6)):'';
@@ -89,7 +100,7 @@ export class Transcript{
89
100
  const t=this.t;
90
101
  switch(e?.type){
91
102
  case 'assistant':return this.assistant(e.content);
92
- case 'answer':return e.content?this.assistant(String(e.content)):[];
103
+ case 'answer':return e.content?this.answer(String(e.content)):[];
93
104
  case 'tool':return e.name==='todo'?[]:this.tool(e.name,e.target);
94
105
  case 'command':return this.command(e.command);
95
106
  case 'output':return this.output(e);
package/src/two-words.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // Same as lib/two-words.ts (tests/two-words.test.mjs keeps them identical). Every automatic name is exactly two title-case words. Names a person types are never passed through here.
1
+ // Same as lib/two-words.ts (tests/two-words.test.mjs keeps them identical). Every automatic name is one to five title-case words (BUG-0548). Names a person types are never passed through here.
2
2
  const STOP=new Set(('a an the to for of in on at by with from into onto about as and or but if so then than this that these those it its is are was were be been am do does did done '+
3
3
  'i me my mine we us our you your yours he she they them their please pls plz kindly can could would should will shall may might must just also really maybe like '+
4
4
  'help want wanted need needs needed lets let hey hi hello thanks thank ok okay now here there what how why when where which who whom whose some any all every something anything thing things '+
@@ -18,19 +18,25 @@ function tokens(text){
18
18
  // a file name counts as its base name: notes.txt is Notes, never Txt
19
19
  const t=raw.replace(/^[^\p{L}\p{N}#]+|[^\p{L}\p{N}]+$/gu,'').replace(/^([\p{L}\p{N}_-]+)\.[a-z][a-z0-9]{0,4}$/iu,'$1');if(!t)continue;
20
20
  const parts=/\p{N}|#/u.test(t)&&/\p{L}/u.test(t)||prose&&/^\p{L}{2,}-\p{L}{2,}$/u.test(t)?[t]:t.split(/[^\p{L}\p{N}]+/u); // in a sentence, long-running stays one word; slugs split
21
- for(const p of parts)if(p&&/\p{L}/u.test(p)&&!STOP.has(p.toLowerCase()))out.push(p.slice(0,24));
21
+ for(const p of parts)if(p&&/\p{L}/u.test(p)&&!/^[a-z]$/i.test(p)&&!STOP.has(p.toLowerCase()))out.push(p.slice(0,24));// a lone letter (add(a, b)) says nothing
22
22
  }
23
23
  return out;
24
24
  }
25
- /** Exactly two meaningful title-case words from free text, or the two-word fallback. `second` completes a single word (else it follows the intent). */
26
- export function twoWordTitle(text,fallback='New Chat',second){
27
- const w=tokens(text);if(!w.length)return fallback||'New Chat';
28
- if(w.length>1)return word(w[0])+' '+word(w[1]);
25
+ /** BUG-0548/BUG-0592: one to five meaningful title-case words from free text (filler dropped), or the fallback. `second` completes a single word.
26
+ `max` caps the words (file names keep two). Old stored two-word names are not renamed. */
27
+ export const TITLE_WORDS=5;
28
+ export function twoWordTitle(text,fallback='New Chat',second,max=TITLE_WORDS){
29
+ // BUG-0297: "file" says nothing about the chat; "Create a file calc.js" is Create Calc, not Create File.
30
+ let w=tokens(text);const lean=w.filter(t=>!/^files?$/i.test(t));if(lean.length>=2)w=lean;
31
+ if(!w.length)return fallback||'New Chat';
32
+ if(w.length>1||max<2)return w.slice(0,Math.max(1,max)).map(word).join(' ');
29
33
  const first=word(w[0]),hit=INTENT.find(([re])=>re.test(String(text??'')));let next=second||(hit?hit[1]:/\?\s*$/.test(String(text??''))?'Chat':'Task');
30
34
  if(next.toLowerCase()===first.toLowerCase())next='Task';
31
35
  return first+' '+next;
32
36
  }
37
+ /** The same rule under its current name. */
38
+ export const chatTitle=twoWordTitle;
33
39
  /** The first meaningful word plus a fixed second one: "Login Fork", "Web Team". */
34
- export const nameWith=(text,second)=>(tokens(text).length?twoWordTitle(text).split(' ')[0]:'New')+' '+second;
35
- /** True when a name is exactly two words, the shape every automatic name has. */
40
+ export const nameWith=(text,second)=>(tokens(text).length?twoWordTitle(text,'New',undefined,1):'New')+' '+second;
41
+ /** True when a name is exactly two words, the shape automatic names had before BUG-0548 (old stored names keep it). */
36
42
  export const isTwoWords=(t)=>String(t??'').trim().split(/\s+/).filter(Boolean).length===2;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * BUG-0478: terminal (mlai) chats were listed in the web app but opened empty, because only the Mac app sent the
3
+ * compact conversation view (workspace-api.mjs pushView). The terminal now sends the same view after each turn, so the
4
+ * web shows the chat as a read-only transcript. Questions and answers only, never file contents; best effort: an
5
+ * offline computer, an older server or a signed-out session never affects the run.
6
+ */
7
+ import os from 'node:os';
8
+ import {nameWith} from './two-words.mjs';
9
+
10
+ const FINAL=['completed','error','stopped','limited','interrupted'];
11
+ const clip=(s,n)=>String(s??'').slice(0,n);
12
+ // The engine sees the Ask card, session instructions and extra-folder notes; the transcript shows what the person typed.
13
+ export function shownTask(task){
14
+ let t=String(task||'');
15
+ const m=/^Session instructions from the user[\s\S]*?\n\nTask:\n/.exec(t);if(m)t=t.slice(m[0].length);
16
+ t=t.replace(/^Answer the question from the code[^\n]*\n\nQuestion: /,'').replace(/\n\nYou may also read these folders: [^\n]*$/,'');
17
+ return t;
18
+ }
19
+ function answer(turn){
20
+ const ev=Array.isArray(turn?.events)?turn.events:[];
21
+ const a=[...ev].reverse().find(e=>e?.type==='answer'&&typeof e.content==='string'&&e.content.trim())
22
+ ||[...ev].reverse().find(e=>e?.type==='assistant'&&typeof e.content==='string'&&e.content.trim());
23
+ if(a)return String(a.content);
24
+ const sub=turn?.status?.result?.submission;return typeof sub==='string'&&sub!=='Task complete'?sub:'';
25
+ }
26
+ /** Session.transcript() turns -> the view /api/laptop view_save accepts (lib/laptop-view.ts). */
27
+ export function transcriptView(turns,meta={},device=os.hostname(),now=Date.now()){
28
+ const list=(Array.isArray(turns)?turns:[]).filter(t=>t&&String(t.task||'').trim());
29
+ const last=list.at(-1),state=String(last?.status?.state||'idle');
30
+ const done=list.filter(t=>!t.current||FINAL.includes(String(t.status?.state||'')));
31
+ const conversation=done.slice(-12).map((t,i)=>({id:clip(t.status?.runId||t.runId||('turn-'+(list.length-done.length+i+1)),40),task:clip(shownTask(t.task),2000),answer:clip(answer(t),6000),state:String(t.status?.state||'unknown')}));
32
+ const running=last?.current&&!FINAL.includes(state);
33
+ const c=last?.completion;
34
+ return {title:clip(meta.title||nameWith(meta.folder,'Chat'),160),state:running?state:(FINAL.includes(state)?state:'idle'),task:running?clip(shownTask(last.task),2000):'',conversation,todos:[],
35
+ completion:c&&typeof c==='object'?{summary:clip(c.summary,4000),changedFiles:(Array.isArray(c.changedFiles)?c.changedFiles:[]).filter(f=>typeof f==='string').slice(0,50)}:null,updated:now,device:clip(device,80)};
36
+ }
37
+ /** The server name of a terminal session (lib/laptop-run.ts laptopWorkspace). */
38
+ export const serverName=meta=>meta?.cloudProject||meta?.project?'mini-'+(meta.cloudProject||meta.project):null;
39
+ /** Sends the view; resolves true when the server accepted it. Never throws. */
40
+ export async function pushTranscript(session,cloud,{device=os.hostname()}={}){
41
+ try{
42
+ const name=serverName(session?.meta);
43
+ if(!name||!/^mini-[a-f0-9-]{36}$/.test(name)||typeof cloud?.viewSave!=='function'||process.env.MLAI_NO_VIEW_SYNC==='1')return false;
44
+ const view=transcriptView(await session.transcript(),session.meta,device);
45
+ if(!view.conversation.length&&!view.task)return false;
46
+ await cloud.viewSave(name,view);return true;
47
+ }catch{return false}
48
+ }
package/src/winenv.mjs CHANGED
@@ -32,7 +32,7 @@ export function gitForWindows(env=process.env){
32
32
  */
33
33
  export const BUSYBOX={version:'FRP-6354-g5f09fb446',file:'busybox-w64u-FRP-6354-g5f09fb446.exe',sha256:'36b266923fc5765b42f5f981319f39568498fba60b53ccf26e8fc1e575e2a9a9'};
34
34
  export const BUSYBOX_UPSTREAM='https://frippery.org/files/busybox/';
35
- const PROD='https://mlai-mini-code.vercel.app';
35
+ const PROD='https://code.ml.ai';
36
36
  /** Download URLs for the pinned busybox, most trusted host first, without duplicates. */
37
37
  export function busyboxSources({server,env=process.env}={}){
38
38
  const out=[];const add=u=>{if(typeof u==='string'&&/^https?:\/\//.test(u)&&!out.includes(u))out.push(u)};
@@ -17,7 +17,7 @@ import {ensureRuntime} from './runtime.mjs';
17
17
  import {createReviews} from './review.mjs';
18
18
  import {killTree} from './winenv.mjs';
19
19
  import {createWakeScheduler} from './wakeups.mjs';
20
- import {starterSkills,validateSkill,importSession,POLICY_TEMPLATES,parseCheckpoints,checkpointRef,rewind,resumeTask,mimeType} from './parity.mjs';
20
+ import {starterSkills,validateSkill,importSession,POLICY_TEMPLATES,parseCheckpoints,checkpointRef,rewind,parseRescues,restoreRescue,RESCUE_LIST_ARGS,resumeTask,mimeType} from './parity.mjs';
21
21
  import {twoWordTitle,nameWith} from './two-words.mjs';
22
22
 
23
23
  const UUID=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
@@ -342,7 +342,7 @@ export function createLocalWorkspaces({cloud,home,device=os.hostname(),relay=fal
342
342
  const isGit=s.meta.git&&await isGitRepo(root);
343
343
  if(CLOUD_ONLY[action])throw fail(CLOUD_ONLY[action]+' are not available for sessions on this computer yet. Use a cloud workspace for this.',{code:'local_unsupported'});
344
344
  if(action.startsWith('deploy_')||action.startsWith('signals_')||/^(monitor|alert|schedule|schedules|releases|ingest|mission|missions|source|sources)_/.test(action))throw fail('This is not available for sessions on this computer yet. Use a cloud workspace for this.',{code:'local_unsupported'});
345
- if(['skill_save','session_import','browser_setup'].includes(action)&&running(e))throw new Error('Wait for the current task to finish before changing its tools or context.');
345
+ if(['skill_save','skill_delete','session_import','browser_setup'].includes(action)&&running(e))throw new Error('Wait for the current task to finish before changing its tools or context.');
346
346
  switch(action){
347
347
  case 'status':{const snap=await snapshot(e);const key=snap.status?.state+'|'+snap.history.length;if(key!==e.lastKey){e.lastKey=key;pushView(e)}return snap}
348
348
  case 'view':return {view:null,at:0};
@@ -391,6 +391,14 @@ export function createLocalWorkspaces({cloud,home,device=os.hostname(),relay=fal
391
391
  if(previous){const h=path.join(dir,'skill-history',skill.id);await mkdir(h,{recursive:true});await writeFile(path.join(h,Date.now()+'-'+randomUUID()+'.json'),previous)}
392
392
  await mkdir(path.join(dir,'skills'),{recursive:true});await writeFile(file,JSON.stringify(skill));return {ok:true,skill};
393
393
  }
394
+ case 'skill_delete':{
395
+ // BUG-0265: delete a user-created skill; the last version stays in skill history.
396
+ if(typeof b.id!=='string'||!/^[a-z0-9][-a-z0-9]{0,59}$/.test(b.id))throw new Error('Invalid skill ID');
397
+ const file=path.join(dir,'skills',b.id+'.json'),previous=await readFile(file).catch(()=>null);
398
+ if(!previous)throw new Error('Only skills you created can be deleted');
399
+ const h=path.join(dir,'skill-history',b.id);await mkdir(h,{recursive:true});await writeFile(path.join(h,Date.now()+'-'+randomUUID()+'.json'),previous);
400
+ await rm(file,{force:true});return {ok:true};
401
+ }
394
402
  case 'skill_history':{
395
403
  if(typeof b.id!=='string'||!/^[-a-z0-9]{1,60}$/.test(b.id))throw new Error('Invalid skill ID');
396
404
  const h=path.join(dir,'skill-history',b.id),versions=[];
@@ -455,14 +463,17 @@ export function createLocalWorkspaces({cloud,home,device=os.hostname(),relay=fal
455
463
  case 'terminal_stop':{const id=typeof b.id==='string'&&/^[a-z0-9]{8,32}$/.test(b.id)?b.id:'';if(!id)throw new Error('Nothing to stop');const c=terminals.get(id);if(!c)return {stopped:false};killTree(c.pid);return {stopped:true}}
456
464
  case 'diff':{
457
465
  if(!isGit)return {diff:'',untracked:'',skipped:[]};
458
- let d=await git(root,['diff','HEAD','--no-ext-diff','-M'],{ok:[1]}).catch(()=>'');
466
+ /* BUG-0293: Whole repository = working tree against the merge-base with the default branch. */
467
+ let base='HEAD',label='';
468
+ if(b.base==='branch'){const o=await gitOut(root,['symbolic-ref','--quiet','--short','refs/remotes/origin/HEAD']).catch(()=>'');for(const h of [o,'origin/main','origin/master','main','master'].filter(Boolean)){const m=await gitOut(root,['merge-base','HEAD',h]).catch(()=>'');if(m){base=m;label=h;break}}}
469
+ let d=await git(root,['diff',base,'--no-ext-diff','-M'],{ok:[1]}).catch(()=>'');
459
470
  const extra=(await gitOut(root,['ls-files','--others','--exclude-standard'])).split('\n').filter(Boolean),skipped=[];
460
471
  for(const f of extra.slice(0,200)){
461
472
  const p=path.join(root,f);let size=Infinity;try{const st=await stat(p);if(st.isFile())size=st.size}catch{}
462
473
  if(d.length>400000||size>200000){skipped.push(f);continue}
463
474
  d+=(await exec('git',['diff','--no-index','--no-ext-diff','--','/dev/null',f],{cwd:root})).stdout;
464
475
  }
465
- return {diff:d.slice(0,600000),untracked:extra.join('\n'),skipped:[...skipped,...extra.slice(200)]};
476
+ return {diff:d.slice(0,600000),untracked:extra.join('\n'),skipped:[...skipped,...extra.slice(200)],...(label?{base:label}:{})};
466
477
  }
467
478
  case 'export':{
468
479
  if(!isGit)throw new Error('Exporting a patch needs a git repository.');
@@ -475,6 +486,12 @@ export function createLocalWorkspaces({cloud,home,device=os.hostname(),relay=fal
475
486
  const ref=checkpointRef(b.checkpoint);if(ref==='HEAD')throw new Error('Choose a checkpoint to compare');
476
487
  const out=await py('checkpoints.py',['compare',ref.slice(5)],{cwd:root});if(out.error)throw new Error(out.error);return out;
477
488
  }
489
+ case 'rescues':return {rescues:isGit?parseRescues(await gitOut(root,RESCUE_LIST_ARGS).catch(()=>'')):[]};
490
+ case 'rescue_restore':{
491
+ idle(e,'restoring a rescue stash. Stop the agent first');
492
+ if(!isGit)throw new Error('Rescue stashes need a git repository.');
493
+ const r=await restoreRescue(b.sha,args=>git(root,args));change(e);return r;
494
+ }
478
495
  case 'restore':{
479
496
  idle(e,'restoring a checkpoint. Stop the agent first');
480
497
  if(b.confirm!==true)throw new Error('Confirm discarding changes before restoring');
@@ -78,7 +78,8 @@ class Client{
78
78
  if(signal?.aborted)throw abortError();
79
79
  if(error instanceof ClientError)throw error;
80
80
  if(attempt<retries){await delay(300*2**attempt,undefined,{signal});continue}
81
- throw new ClientError('Connection interrupted. Refresh status before retrying a write.',{code:'connection_interrupted',retryable:readOnly});
81
+ // The server address goes with the error so a client can say which server it could not reach (BUG-0504).
82
+ throw Object.assign(new ClientError('Connection interrupted. Refresh status before retrying a write.',{code:'connection_interrupted',retryable:readOnly}),{server:this.baseUrl});
82
83
  }finally{clearTimeout(timer);signal?.removeEventListener('abort',abort)}
83
84
  }
84
85
  }
@@ -28,7 +28,17 @@ export function colorOn(stream,env=process.env){
28
28
  const palette=on=>{const k=(code,s)=>on?`\x1b[${code}m${s}\x1b[0m`:s;return {dim:s=>k(2,s),bold:s=>k(1,s),teal:s=>k(36,s),green:s=>k(32,s),yellow:s=>k(33,s),red:s=>k(31,s),gray:s=>k(90,s)}};
29
29
  const c=palette(colorOn(process.stdout)),ce=palette(colorOn(process.stderr));
30
30
  /** Exit codes scripts can rely on: 0 done, 1 failed, 2 usage (bad command or flag, nothing linked), 130 stopped or interrupted. */
31
- export const EXIT={ok:0,failed:1,usage:2,stopped:130};
31
+ export const EXIT={ok:0,failed:1,usage:2,stopped:130,auth:4,billing:5};
32
+ /**
33
+ * Run as `mlai cloud ...` from the mlai CLI (MLAI_CLOUD_PARENT is its version): help shows that version and the
34
+ * commands with the `cloud` prefix, and sign-in and plan-limit failures use the same exit codes as mlai (4 and 5).
35
+ * BUG-0470, BUG-0472.
36
+ */
37
+ export const PARENT=process.env.MLAI_CLOUD_PARENT||'';
38
+ export function cloudPrefix(text,parent=PARENT,{keepLogin=false}={}){
39
+ if(!parent)return String(text);
40
+ return String(text).replace(/(^|[\s`'"(])mlai (?!cloud\b)(?=[a-z<])/gm,(m,pre,off,all)=>keepLogin&&/^(login|logout|whoami)\b/.test(all.slice(off+m.length))?m:pre+'mlai cloud ');
41
+ }
32
42
  const MODES={plan:'plan',brief:'brief',guided:'approve',approve:'approve',auto:'auto'};
33
43
  let JSON_OUT=false;
34
44
  // `mlai completion bash | head` closes the pipe early; that is not an error.
@@ -67,7 +77,7 @@ const ws=(token,action,extra={})=>api('/api/workspace',{action,token,...extra});
67
77
  // ---------- output helpers ----------
68
78
  function out(s=''){process.stdout.write(s+'\n')}
69
79
  function emit(obj){if(JSON_OUT)out(JSON.stringify(obj))}
70
- function fail(msg,code=1){closeInput();process.stderr.write(ce.red('error: ')+msg+'\n');process.exit(code)}
80
+ function fail(msg,code=1){closeInput();process.stderr.write(ce.red('error: ')+cloudPrefix(msg,PARENT,{keepLogin:true})+'\n');process.exit(code)}
71
81
  function wrap(text,indent=' ',width=Math.min(process.stdout.columns||100,110)){const lines=[];for(const para of String(text).split('\n')){let line='';for(const word of para.split(/\s+/)){if((line+' '+word).trim().length>width-indent.length){lines.push(indent+line.trim());line=word}else line+=' '+word}lines.push(indent+line.trim())}return lines.join('\n')}
72
82
  // One readline for the whole process, so the up arrow brings back earlier tasks and answers in the interactive session.
73
83
  let rl=null;
@@ -130,7 +140,7 @@ async function login(opts){
130
140
 
131
141
  // ---------- commands ----------
132
142
  const commands={};
133
- const help=`${c.bold('mlai')} ${VERSION} — ML AI Code from your terminal
143
+ const help=(PARENT?`${c.bold('mlai cloud')} — cloud workspaces (ML AI Code ${PARENT})`:`${c.bold('mlai')} ${VERSION} — ML AI Code from your terminal`)+`
134
144
 
135
145
  ${c.bold('Account')}
136
146
  mlai login [--manual] [--url URL] approve this computer in your browser
@@ -144,7 +154,7 @@ ${c.bold('Projects')} (a project is a cloud workspace; link one to a folder)
144
154
  mlai open open the linked project in the web app
145
155
 
146
156
  ${c.bold('Tasks')}
147
- mlai run "<task>" [-m plan|brief|guided|auto] [-r role] [-e low|medium|high] [-s steps] [--no-watch] [--build-brief answers.json]
157
+ mlai run "<task>" [--mode plan|brief|guided|auto] [--role role] [--effort low|medium|high] [--steps n] [--no-watch] [--build-brief answers.json]
148
158
  mlai interactive session: type tasks, answer approvals inline
149
159
  mlai watch | status | stop | resume follow the current run / snapshot / stop / continue
150
160
  mlai steer "<message>" add an instruction while the agent is working
@@ -173,10 +183,12 @@ ${c.bold('More')}
173
183
  mlai completion bash|zsh|fish print a shell completion script
174
184
  mlai help <command> help for one command (or mlai <command> --help)
175
185
 
176
- Global: --json (machine output), -p <project-token>, MLAI_URL, MLAI_PROJECT, NO_COLOR, FORCE_COLOR
177
- Exit codes: 0 done, 1 failed, 2 usage error, 130 stopped or interrupted`;
178
- commands.help=async opts=>{const name=opts?._?.[0];if(!name)return out(help);const h=commandHelp(name);if(!h)fail(unknownCommand(name),EXIT.usage);out(h)};
179
- commands.version=async()=>out(VERSION);
186
+ Global: --json (machine output), --project <token>, MLAI_URL, MLAI_PROJECT, NO_COLOR, FORCE_COLOR
187
+ Exit codes: ${PARENT?'0 done, 1 failed, 2 usage, 4 sign-in needed, 5 plan limit reached, 130 stopped':'0 done, 1 failed, 2 usage error, 130 stopped or interrupted'}`;
188
+ // Prefixed help keeps its columns: the padding after a command shrinks by the length of "cloud ".
189
+ const alignedHelp=()=>PARENT?help.split('\n').map(l=>{const m=/^( \S.*?)( {2,})(\S.*)$/.exec(l);if(!m)return cloudPrefix(l.replace(/^ mlai$/,' mlai cloud'));const a=cloudPrefix(m[1].replace(/^ mlai$/,' mlai cloud'));return a+' '.repeat(Math.max(2,m[1].length+m[2].length-a.length))+cloudPrefix(m[3])}).join('\n'):help;
190
+ commands.help=async opts=>{const name=opts?._?.[0];if(!name)return out(alignedHelp());const h=commandHelp(name);if(!h)fail(unknownCommand(name),EXIT.usage);out(h)};
191
+ commands.version=async()=>out(PARENT||VERSION);
180
192
  commands.login=login;
181
193
  // The server has no token revocation endpoint (CLI tokens are signed and expire on their own), so logout removes it here.
182
194
  commands.logout=async()=>{const cfg=config();const had=Boolean(cfg.token);delete cfg.token;delete cfg.owner;delete cfg.label;saveConfig(cfg);out(had?'Signed out. The token was removed from this computer. It cannot be used from here again and expires on its own.':'Already signed out on this computer.')};
@@ -442,7 +454,7 @@ export const COMMANDS={
442
454
  new:{usage:'mlai new [github-url] [-n name] [--profile small|medium|large] [-y]',about:'Create a project, empty or from a GitHub repository, and link this folder to it.',flags:['name','backend','profile','yes']},
443
455
  link:{usage:'mlai link <name|#n>',about:'Link this folder to an existing project (see mlai projects).'},
444
456
  open:{usage:'mlai open',about:'Open the linked chat in the web app.'},
445
- run:{usage:'mlai run "<task>" [-m plan|brief|guided|auto] [-r role] [-e low|medium|high] [-s steps] [--no-watch] [--build-brief answers.json]',about:'Start a task in the linked project and follow it. Ctrl+C stops following; the task keeps running. Exits 0 when it completes, 1 when it fails, 130 when it was stopped.',flags:['mode','role','effort','steps','watch','build-brief']},
457
+ run:{usage:'mlai run "<task>" [--mode plan|brief|guided|auto] [--role role] [--effort low|medium|high] [--steps n] [--no-watch] [--build-brief answers.json]',about:'Start a task in the linked project and follow it. Ctrl+C stops following; the task keeps running. Exits 0 when it completes, 1 when it fails, 130 when it was stopped.',flags:['mode','role','effort','steps','watch','build-brief']},
446
458
  watch:{usage:'mlai watch',about:'Follow the current run and answer approvals inline.'},
447
459
  status:{usage:'mlai status',about:'Show the current state, task list, approval and recent runs.'},
448
460
  stop:{usage:'mlai stop',about:'Stop the current run.'},
@@ -473,7 +485,7 @@ export const COMMANDS={
473
485
  };
474
486
  const GLOBAL_FLAGS=['json','project','help','version'];
475
487
  const SESSION_FLAGS=['mode','role'];
476
- export function commandHelp(name){const h=COMMANDS[name];if(!h)return '';const f=(h.flags||[]).map(x=>'--'+x);return `${c.bold('Usage')} ${h.usage}\n\n${wrap(h.about,' ')}${f.length?'\n\n'+c.bold('Options')+' '+f.join(' '):''}\n${c.dim('Global: --json, -p <project>, --help')}`}
488
+ export function commandHelp(name){const h=COMMANDS[name];if(!h)return '';const f=(h.flags||[]).map(x=>'--'+x);return cloudPrefix(`${c.bold('Usage')} ${h.usage}\n\n${wrap(h.about,' ')}${f.length?'\n\n'+c.bold('Options')+' '+f.join(' '):''}\n${c.dim('Global: --json, --project <token>, --help')}`)}
477
489
  /** Edit distance, for "did you mean". */
478
490
  function distance(a,b){const d=Array.from({length:a.length+1},(_,i)=>[i]);for(let j=1;j<=b.length;j++)d[0][j]=j;for(let i=1;i<=a.length;i++)for(let j=1;j<=b.length;j++)d[i][j]=Math.min(d[i-1][j]+1,d[i][j-1]+1,d[i-1][j-1]+(a[i-1]===b[j-1]?0:1),i>1&&j>1&&a[i-1]===b[j-2]&&a[i-2]===b[j-1]?d[i-2][j-2]+1:Infinity);return d[a.length][b.length]}
479
491
  export function didYouMean(word,options){
@@ -530,7 +542,7 @@ export async function main(argv=process.argv.slice(2)){
530
542
  if(!name)return await session(opts);
531
543
  await commands[name](opts);
532
544
  }catch(e){
533
- if(e instanceof ApiError){if(e.code==='workspace_missing')fail('This project no longer exists. `mlai projects` to pick another.');fail(e.message)}
545
+ if(e instanceof ApiError){if(e.code==='workspace_missing')fail('This project no longer exists. `mlai projects` to pick another.');fail(e.message,PARENT&&e.status===401?EXIT.auth:PARENT&&(e.status===402||e.code==='billing_limit')?EXIT.billing:EXIT.failed)}
534
546
  const net=networkMessage(e,baseUrl());if(net)fail(net);
535
547
  fail(e?.message||String(e));
536
548
  }finally{closeInput()}
@@ -21,12 +21,14 @@ import uuid
21
21
  VERSION = 'architecture-v1.1-ts6.0.2'
22
22
  MAX_FILES, MAX_BYTES, MAX_FILE = 10000, 64 * 1024 * 1024, 512 * 1024
23
23
  MAX_SNAPSHOTS, MAX_DECISIONS = 120, 2000
24
- EXT = {'.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts', '.cts', '.py'}
24
+ # BUG-0240: HTML pages and CSS files are on the map too (script, stylesheet and @import links).
25
+ MARKUP = {'.html', '.htm', '.css'}
26
+ EXT = {'.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs', '.mts', '.cts', '.py'} | MARKUP
25
27
  SKIP = {'.git', 'node_modules', '.next', 'dist', 'build', '.venv', 'venv', '__pycache__', 'vendor', 'coverage', '.turbo'}
26
28
  LIMITATIONS = [
27
29
  'Syntax imports only; not a call graph or runtime evidence.',
28
30
  'Dynamic imports, require(), reflection and generated dependencies are not resolved.',
29
- 'Only local TS/JS and Python modules; no dependency installation or type checking.',
31
+ 'Only local TS/JS and Python modules, plus HTML script/stylesheet links and CSS @import; no dependency installation or type checking.',
30
32
  'Root tsconfig/jsconfig paths only; inherited and nested configs are not resolved.',
31
33
  'Renames appear as added and removed files. No inferred identity.',
32
34
  'Workspace-local history is retained with the computer; deletion removes it.',
@@ -120,6 +122,31 @@ def parse_python(path, text):
120
122
  imports.append({'spec':n.module or '', 'line':n.lineno, 'level':n.level})
121
123
  return {'imports':imports, 'symbols':symbols, 'error':False}
122
124
 
125
+ HTML_LINK = re.compile(r'<script\b[^>]*?\bsrc\s*=\s*["\']([^"\']+)["\']|<link\b(?=[^>]*\brel\s*=\s*["\']?(?:stylesheet|modulepreload)\b)[^>]*?\bhref\s*=\s*["\']([^"\']+)["\']', re.I)
126
+ CSS_IMPORT = re.compile(r'@import\s+(?:url\(\s*)?["\']?([^"\')\s;]+)', re.I)
127
+
128
+ def parse_markup(path, text):
129
+ """Local files an HTML page loads (scripts, stylesheets) or a CSS file @imports. Syntax only."""
130
+ imports, symbols = [], []
131
+ pattern = CSS_IMPORT if path.lower().endswith('.css') else HTML_LINK
132
+ for m in pattern.finditer(text):
133
+ spec = next(g for g in m.groups() if g)
134
+ if len(imports) < 500: imports.append({'spec':spec, 'line':text.count('\n', 0, m.start()) + 1, 'markup':True})
135
+ if not path.lower().endswith('.css'):
136
+ title = re.search(r'<title[^>]*>([^<]{1,120})</title>', text, re.I)
137
+ if title and title.group(1).strip(): symbols.append({'name':title.group(1).strip(), 'line':text.count('\n', 0, title.start()) + 1})
138
+ return {'imports':imports, 'symbols':symbols, 'error':False}
139
+
140
+ def resolve_markup(path, spec, paths):
141
+ """A page or stylesheet link to a file in this repository; URLs and CDNs are not files here."""
142
+ spec = spec.split('#', 1)[0].split('?', 1)[0].strip()
143
+ if not spec or re.match(r'^(?:[a-z][a-z0-9+.-]*:|//)', spec, re.I): return None, 'unresolved'
144
+ parent = str(PurePosixPath(path).parent)
145
+ options = [normalize(spec.lstrip('/')), normalize('public/' + spec.lstrip('/'))] if spec.startswith('/') else [normalize(parent + '/' + spec)]
146
+ for option in options:
147
+ if option and option in paths: return option, 'relative'
148
+ return None, 'unresolved'
149
+
123
150
  def parse_batch(items, parser, compiler):
124
151
  if not items: return []
125
152
  r = subprocess.run(['node', '--max-old-space-size=256', str(parser), str(compiler)],
@@ -246,6 +273,7 @@ def capture(repo, home, ref='working', parser=None, compiler=None):
246
273
  row = db.execute('SELECT data FROM facts WHERE id=?', (key,)).fetchone()
247
274
  if row: facts[path] = json.loads(row[0]); cov['cached'] += 1
248
275
  elif path.endswith('.py'): facts[path] = parse_python(path,content[path]); cov['parsed'] += 1
276
+ elif PurePosixPath(path).suffix.lower() in MARKUP: facts[path] = parse_markup(path,content[path]); cov['parsed'] += 1
249
277
  else: missing.append({'path':path,'text':content[path]})
250
278
  for i in range(0,len(missing),100):
251
279
  batch = missing[i:i+100]
@@ -260,12 +288,13 @@ def capture(repo, home, ref='working', parser=None, compiler=None):
260
288
  f = facts[path]
261
289
  cov['errors'] += int(bool(f.get('error')))
262
290
  if path.endswith('.json'): continue
263
- kind = 'test' if re.search(r'(^|/)(tests?|__tests__)/|\.(test|spec)\.', path) else 'route' if re.search(r'(^|/)(src/)?app/(.*/)?(page|route)\.[tj]sx?$',path) else 'component' if path.endswith(('.tsx','.jsx')) else 'module'
291
+ markup = PurePosixPath(path).suffix.lower() in MARKUP
292
+ kind = 'route' if markup and not path.lower().endswith('.css') else 'module' if markup else 'test' if re.search(r'(^|/)(tests?|__tests__)/|\.(test|spec)\.', path) else 'route' if re.search(r'(^|/)(src/)?app/(.*/)?(page|route)\.[tj]sx?$',path) else 'component' if path.endswith(('.tsx','.jsx')) else 'module'
264
293
  nodes.append(dict(id=path,path=path,kind=kind,hash=manifest[path],symbols=f.get('symbols',[])[:200]))
265
294
  # Syntax errors never produce authoritative edges from recovered parser nodes.
266
295
  if f.get('error'): continue
267
296
  for imp in f.get('imports',[])[:500]:
268
- target,basis = resolve_python(path,imp['spec'],imp.get('level',0),source_paths) if path.endswith('.py') else resolve_js(path,imp['spec'],source_paths,config)
297
+ target,basis = resolve_markup(path,imp['spec'],source_paths) if markup else resolve_python(path,imp['spec'],imp.get('level',0),source_paths) if path.endswith('.py') else resolve_js(path,imp['spec'],source_paths,config)
269
298
  if not target: cov['unresolved'] += 1; continue
270
299
  if target.startswith('pkg:'): external.add(target)
271
300
  evidence = dict(path=path,line=imp['line'],basis='syntax',resolution=basis)
@@ -11,6 +11,24 @@ PREVIEW_HOST='preview-check.vercel.run'
11
11
  # Laptop runs ship the whole worker folder; cloud computers may not have browser_local.py (lib/worker-files.ts).
12
12
  try:import browser_local
13
13
  except ImportError:browser_local=None
14
+ CONTROL_ROLES=('button','link','menuitem','tab','checkbox','radio','switch','option','textbox','combobox','searchbox')
15
+ def step_locator(page,step):
16
+ """The element a click/fill/press step means. CLI bug list (0.3.3): a click with only a name ("Start a conversation",
17
+ a link) ran page.locator('') and failed with 'Unexpected token "" while parsing css selector'. A name alone now
18
+ finds the control by its accessible name, then by its label or visible text."""
19
+ role,name,selector=step.get('role'),step.get('name'),str(step.get('selector') or '').strip()
20
+ if role:return page.get_by_role(role,name=name,exact=True) if name else page.get_by_role(role)
21
+ if selector:return page.locator(selector)
22
+ if name:
23
+ name=str(name)
24
+ found=None
25
+ for r in CONTROL_ROLES:
26
+ loc=page.get_by_role(r,name=name,exact=True)
27
+ found=loc if found is None else found.or_(loc)
28
+ found=found.or_(page.get_by_label(name,exact=True)).or_(page.get_by_text(name,exact=True))
29
+ return found.first
30
+ raise ValueError('This step needs a selector, a role or a name to find the element.')
31
+
14
32
  def on_laptop():return bool(browser_local) and browser_local.laptop()
15
33
 
16
34
  def local_url(value):
@@ -80,7 +98,7 @@ def scenario(args,output):
80
98
  page.goto(as_preview(step['url']),wait_until='domcontentloaded');timeline[-1]['state']='passed';continue
81
99
  if action not in ('click','fill','press','assert_text'):raise ValueError('Unknown browser action')
82
100
  if action=='assert_text':page.get_by_text(str(step.get('text','')),exact=False).first.wait_for(state='visible');timeline[-1]['state']='passed';continue
83
- locator=page.get_by_role(step['role'],name=step.get('name'),exact=True) if step.get('role') else page.locator(str(step.get('selector','')))
101
+ locator=step_locator(page,step)
84
102
  if action=='click':locator.click()
85
103
  elif action=='fill':locator.fill(str(step.get('value',''))[:5000])
86
104
  else:locator.press(str(step.get('key','Enter'))[:50])
@@ -270,7 +270,10 @@ if __name__ == '__main__':
270
270
  import sys
271
271
  try:
272
272
  action, root, argument = sys.argv[1:4]
273
- result = export_chat(root, argument, sys.argv[4] == 'true') if action == 'export' else import_chat(root, argument)
273
+ if action == 'turns': # BUG-0465: the whole conversation for Export current conversation
274
+ result = chat_turns(Path(root), argument)
275
+ else:
276
+ result = export_chat(root, argument, sys.argv[4] == 'true') if action == 'export' else import_chat(root, argument)
274
277
  print(json.dumps(result))
275
278
  except Exception as error:
276
279
  print(json.dumps({'code': 'fork_refused', 'error': str(error)[:500]}))