mlai-code 0.3.7 → 0.3.9

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/worker/preview.py CHANGED
@@ -1,6 +1,14 @@
1
1
  """Explicitly approved local preview launcher: installs what the project declares, serves the production build on
2
2
  shared links and the dev server while the agent edits. Never calls an LLM."""
3
- import argparse, fcntl, json, os, pathlib, re, shutil, subprocess, sys, urllib.request, urllib.error
3
+ import argparse, json, os, pathlib, re, shutil, subprocess, sys, urllib.request, urllib.error
4
+
5
+ try: from winproc import lock_file # Windows has no fcntl; the cloud workspace may copy this file alone
6
+ except ImportError:
7
+ def lock_file(f, blocking=True):
8
+ import fcntl
9
+ fcntl.flock(f, fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB))
10
+ if sys.platform == 'win32':
11
+ import winproc; winproc.install() # os.killpg / os.getpgid for the dev server it stops
4
12
 
5
13
 
6
14
  def plan(repo):
@@ -19,13 +27,13 @@ def plan(repo):
19
27
  if not isinstance(script, str): continue
20
28
  # Only standard framework entrypoints; compound scripts need agent review.
21
29
  if 'next' in deps and re.fullmatch(r'next (dev|start)(?: --(?:hostname|port) [a-zA-Z0-9.:-]+)*', script.strip()):
22
- return {'command': ['npm', 'run', name, '--', '--hostname', '0.0.0.0', '--port', '3000'], 'label': 'Next.js', 'cwd': str(repo)}
30
+ return {'command': ['npm', 'run', name, '--', '--hostname', '0.0.0.0', '--port', str(PORT)], 'label': 'Next.js', 'cwd': str(repo)}
23
31
  if 'vite' in deps and re.fullmatch(r'vite(?: --host)?', script.strip()):
24
- return {'command': ['npm', 'run', name, '--', '--host', '0.0.0.0', '--port', '3000', '--strictPort'], 'label': 'Vite', 'cwd': str(repo)}
32
+ return {'command': ['npm', 'run', name, '--', '--host', '0.0.0.0', '--port', str(PORT), '--strictPort'], 'label': 'Vite', 'cwd': str(repo)}
25
33
  return None
26
34
  index = repo / 'index.html'
27
35
  if index.is_file() and not index.is_symlink():
28
- return {'command': [sys.executable, str(pathlib.Path(__file__).resolve()), 'serve', '--root', str(repo)], 'label': 'Static website', 'cwd': str(repo)}
36
+ return {'command': [sys.executable, str(pathlib.Path(__file__).resolve()), 'serve', '--port', str(PORT), '--root', str(repo)], 'label': 'Static website', 'cwd': str(repo)}
29
37
  return None
30
38
 
31
39
 
@@ -98,7 +106,7 @@ def ensure_next_dev_origins(repo):
98
106
  def next_origin_blocked():
99
107
  """Next.js dev answers 403 'Unauthorized' to /_next requests from an origin outside allowedDevOrigins."""
100
108
  try:
101
- request = urllib.request.Request('http://127.0.0.1:3000/_next/mlai-origin-probe', headers={'Origin': 'https://preview.vercel.run', 'Host': 'preview.vercel.run'})
109
+ request = urllib.request.Request(base_url() + '/_next/mlai-origin-probe', headers={'Origin': 'https://preview.vercel.run', 'Host': 'preview.vercel.run'})
102
110
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
103
111
  with opener.open(request, timeout=3): return False
104
112
  except urllib.error.HTTPError as error:
@@ -111,7 +119,7 @@ def next_origin_blocked():
111
119
  def host_blocked():
112
120
  """Vite answers loopback but rejects the public preview host with 403 'Blocked request'."""
113
121
  try:
114
- request = urllib.request.Request('http://127.0.0.1:3000', headers={'Host': 'preview.vercel.run'})
122
+ request = urllib.request.Request(base_url(), headers={'Host': 'preview.vercel.run'})
115
123
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
116
124
  with opener.open(request, timeout=2): return False
117
125
  except urllib.error.HTTPError as error:
@@ -121,25 +129,26 @@ def host_blocked():
121
129
  except (OSError, urllib.error.URLError): return False
122
130
 
123
131
 
124
- PORT_CLASH = ('Two servers are using port 3000. The preview reaches the one that answers HTTP {code} (usually the API), and the web app '
132
+ PORT_CLASH = ('Two servers are using port {port}. The preview reaches the one that answers HTTP {code} (usually the API), and the web app '
125
133
  'answers on the other address. Ask the agent to move the API to another port, such as 3001, and proxy /api to it from the web server.')
126
134
 
127
135
 
128
136
  def loopback6_status(opener):
129
137
  try:
130
- with opener.open('http://[::1]:3000', timeout=2) as response: return response.status
138
+ with opener.open('http://[::1]:%d' % PORT, timeout=2) as response: return response.status
131
139
  except urllib.error.HTTPError as error: return error.code
132
140
  except (OSError, urllib.error.URLError, ValueError): return 0
133
141
 
134
142
 
135
143
  def status(state):
144
+ if PATHS.get('repo') and not CTX: bind(PATHS['repo'], state)
136
145
  code = 0
137
146
  try:
138
147
  # Do not follow redirects off the local app or send loopback through an HTTP proxy.
139
148
  class NoRedirect(urllib.request.HTTPRedirectHandler):
140
149
  def redirect_request(self, *args, **kwargs): return None
141
150
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
142
- with opener.open('http://127.0.0.1:3000', timeout=2) as response: code = response.status
151
+ with opener.open(base_url(), timeout=2) as response: code = response.status
143
152
  except urllib.error.HTTPError as error: code = error.code
144
153
  except (OSError, urllib.error.URLError): pass
145
154
  # Web Bug-035: a compound dev script can start the API and the web server on the same port 3000, one on the IPv4
@@ -155,6 +164,13 @@ def status(state):
155
164
  file.seek(0, 2);file.seek(max(0, file.tell() - 8000));log = file.read().decode(errors='replace')
156
165
  except OSError: pass
157
166
  ready = 200 <= code < 400
167
+ owner = server_owner() if code else {}
168
+ if owner.get('owner') == 'foreign':
169
+ # Something answers, but it is another app: never this project's preview, whatever its HTTP status.
170
+ return {'ready': False, 'running': running, 'httpStatus': code, 'port': PORT, 'url': base_url(), 'foreignServer': owner,
171
+ 'message': foreign_text(owner) + ' This is not the preview of this workspace, and browser checks against it do not count. '
172
+ + ('Start the preview again and it moves to a free port.' if alternate_ports() else 'Stop that server (it does not belong to this project), then start the preview again.'),
173
+ 'log': log}
158
174
  work = job(state); mode = meta.get('mode', 'dev') if running or ready else ''
159
175
  if work.get('alive') and (work.get('step') or '').startswith(('Building', 'Installing', 'Adding')): log = tail(state / 'preview-build.log', 8000) or log
160
176
  extra = {'mode': mode or None, 'notes': [n for n in work.get('notes') or [] if isinstance(n, str)][:4]}
@@ -170,15 +186,306 @@ def status(state):
170
186
  return {'ready': False, 'running': running, 'httpStatus': 403, 'hostBlocked': True, 'message': 'The dev server is blocking the preview address. Restart it with server.allowedHosts set (or ask the agent to restart the preview).', 'log': log}
171
187
  if ready: message = 'Live preview' if mode == 'prod' else ('Live preview (development). Preparing the production version…' if work.get('alive') else 'Live preview (development)')
172
188
  elif work.get('alive'): message = (work.get('step') or 'Preparing the preview') + '…'
173
- elif clash: message = PORT_CLASH.format(code=code); extra['portClash'] = True
174
- else: message = 'The app returned HTTP '+str(code)+'. Check the startup log.' if code else ('Starting your app…' if running else ('Preview could not start. ' + work['error'] if work.get('state') == 'failed' and work.get('error') else 'No app is running on port 3000.'))
175
- return {'ready': ready, 'running': running or bool(work.get('alive')), 'httpStatus': code or None, 'message': message, 'log': log, **extra}
189
+ elif clash: message = PORT_CLASH.format(code=code, port=PORT); extra['portClash'] = True
190
+ else: message = 'The app returned HTTP '+str(code)+'. Check the startup log.' if code else ('Starting your app…' if running else ('Preview could not start. ' + work['error'] if work.get('state') == 'failed' and work.get('error') else 'No app is running on port %d.' % PORT))
191
+ if ready and owner.get('owner') == 'unknown': extra['notes'].append('Could not confirm that the server on port %d belongs to this project (its process and page gave no clear sign).' % PORT)
192
+ if owner.get('owner') in ('ours', 'workspace', 'unknown'): extra['owner'] = owner['owner']
193
+ return {'ready': ready, 'running': running or bool(work.get('alive')), 'httpStatus': code or None, 'port': PORT, 'url': base_url(), 'message': message, 'log': log, **extra}
194
+
195
+
196
+
197
+ # ---------------------------------------------------------------- which app answers on the preview port
198
+ # CLI 0.3.8 (Notes app): port 3000 was already serving another workspace's app ("Pixel Demo Shop"); preview called it
199
+ # ready and the browser checks passed against the wrong application. A port counts as this project's preview only when
200
+ # the listening socket belongs to the process tree preview started (or a server running from this workspace), with a
201
+ # content check (the page's title and asset paths match files here) when ownership cannot be read.
202
+ DEFAULT_PORT = 3000
203
+ PORT = DEFAULT_PORT
204
+ CTX = {} # repo and state of the preview this command manages (set by bind)
205
+
206
+
207
+ def base_url(port=None):
208
+ return 'http://127.0.0.1:%d' % (port or PORT)
209
+
210
+
211
+ def recorded_port(state):
212
+ try: port = int(json.loads((pathlib.Path(state) / 'preview-port.json').read_text()).get('port'))
213
+ except (OSError, ValueError, TypeError, AttributeError): return DEFAULT_PORT
214
+ return port if 1024 <= port <= 65535 else DEFAULT_PORT
215
+
216
+
217
+ def bind(repo, state):
218
+ """Use this workspace's preview port (3000 unless an earlier start moved it off a port another app holds)."""
219
+ global PORT
220
+ CTX['repo'], CTX['state'] = pathlib.Path(repo).resolve(), pathlib.Path(state)
221
+ PORT = recorded_port(state)
222
+ return PORT
223
+
224
+
225
+ def alternate_ports():
226
+ """Laptop runs can move the preview to another port; the cloud preview address is fixed to port 3000."""
227
+ if os.getenv('MINI_LAPTOP') == '1' or os.getenv('MINI_PREVIEW_ALT_PORTS') == '1': return True
228
+ try: return bool(json.loads((CTX['state'] / 'preview-port.json').read_text()).get('alternates'))
229
+ except (KeyError, OSError, ValueError, AttributeError): return False
230
+
231
+
232
+ def free_port(start, tries=200):
233
+ import socket
234
+ for port in range(start, min(start + tries, 65535)):
235
+ if listening(port): continue
236
+ with socket.socket() as sock:
237
+ try: sock.bind(('0.0.0.0', port))
238
+ except OSError: continue
239
+ return port
240
+ return None
241
+
242
+
243
+ def _run(cmd, timeout=5):
244
+ try:
245
+ flags = getattr(subprocess, 'CREATE_NO_WINDOW', 0)
246
+ return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, stdin=subprocess.DEVNULL, creationflags=flags).stdout or ''
247
+ except (OSError, subprocess.SubprocessError): return ''
248
+
249
+
250
+ def listener_pids(port=None):
251
+ """PIDs listening on the port, or None when this computer does not say (permissions, missing tools)."""
252
+ port = port or PORT
253
+ if sys.platform.startswith('linux'):
254
+ if not pathlib.Path('/proc/net/tcp').exists(): return None
255
+ owners = port_owners(port)
256
+ return owners or None # a socket another user owns has no readable fd: unknown, not "nobody"
257
+ if sys.platform == 'darwin':
258
+ found = {int(line[1:]) for line in _run(['lsof', '-nP', '-iTCP:%d' % port, '-sTCP:LISTEN', '-Fp']).splitlines() if line[1:].isdigit() and line[:1] == 'p'}
259
+ return found or None
260
+ if sys.platform == 'win32':
261
+ found = set()
262
+ for line in _run(['netstat', '-ano', '-p', 'TCP']).splitlines() + _run(['netstat', '-ano', '-p', 'TCPv6']).splitlines():
263
+ parts = line.split()
264
+ # Listening rows have a remote address ending in :0 (the state word is localized, so it is not read).
265
+ if len(parts) >= 5 and parts[0].upper().startswith('TCP') and parts[1].rsplit(':', 1)[-1] == str(port) and parts[2].rsplit(':', 1)[-1] == '0' and parts[-1].isdigit():
266
+ found.add(int(parts[-1]))
267
+ return found or None
268
+ return None
269
+
270
+
271
+ def _win_processes():
272
+ import ctypes
273
+ from ctypes import wintypes
274
+ class Entry(ctypes.Structure):
275
+ _fields_ = [('dwSize', wintypes.DWORD), ('cntUsage', wintypes.DWORD), ('th32ProcessID', wintypes.DWORD), ('th32DefaultHeapID', ctypes.c_size_t),
276
+ ('th32ModuleID', wintypes.DWORD), ('cntThreads', wintypes.DWORD), ('th32ParentProcessID', wintypes.DWORD), ('pcPriClassBase', ctypes.c_long),
277
+ ('dwFlags', wintypes.DWORD), ('szExeFile', ctypes.c_char * 260)]
278
+ k = ctypes.WinDLL('kernel32', use_last_error=True)
279
+ k.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
280
+ snap = k.CreateToolhelp32Snapshot(2, 0)
281
+ out = {}
282
+ if not snap or snap == wintypes.HANDLE(-1).value: return out
283
+ try:
284
+ entry = Entry(); entry.dwSize = ctypes.sizeof(Entry)
285
+ ok = k.Process32First(snap, ctypes.byref(entry))
286
+ while ok:
287
+ out[int(entry.th32ProcessID)] = {'ppid': int(entry.th32ParentProcessID), 'name': entry.szExeFile.decode(errors='replace')}
288
+ ok = k.Process32Next(snap, ctypes.byref(entry))
289
+ finally: k.CloseHandle(snap)
290
+ return out
291
+
292
+
293
+ def processes():
294
+ """pid -> {ppid, name, group, session} for every visible process (best effort on each system)."""
295
+ out = {}
296
+ if sys.platform.startswith('linux'):
297
+ for proc in pathlib.Path('/proc').iterdir():
298
+ if not proc.name.isdigit(): continue
299
+ try:
300
+ raw = (proc / 'stat').read_text(); name = raw[raw.find('(') + 1:raw.rfind(')')]; fields = raw.rsplit(')', 1)[1].split()
301
+ out[int(proc.name)] = {'ppid': int(fields[1]), 'group': int(fields[2]), 'session': int(fields[3]), 'name': name}
302
+ except (OSError, ValueError, IndexError): continue
303
+ elif sys.platform == 'win32':
304
+ try: out = _win_processes()
305
+ except Exception: out = {}
306
+ else:
307
+ for line in _run(['ps', '-axo', 'pid=,ppid=,pgid=,comm=']).splitlines():
308
+ parts = line.split(None, 3)
309
+ if len(parts) >= 3 and all(p.isdigit() for p in parts[:3]):
310
+ out[int(parts[0])] = {'ppid': int(parts[1]), 'group': int(parts[2]), 'name': parts[3] if len(parts) > 3 else ''}
311
+ return out
312
+
313
+
314
+ def process_tree(roots, table):
315
+ """The roots and everything they started (by parent, process group or session)."""
316
+ roots = {r for r in roots if r in table}
317
+ tree = set(roots); changed = True
318
+ while changed:
319
+ changed = False
320
+ for pid, info in table.items():
321
+ if pid not in tree and (info.get('ppid') in tree or info.get('group') in roots or info.get('session') in roots):
322
+ tree.add(pid); changed = True
323
+ return tree
324
+
325
+
326
+ def started_pids(state):
327
+ """The server preview started for this workspace (and its supervisor job), when still running."""
328
+ pids = set()
329
+ for name in ('preview-process.json', 'preview-build.json'):
330
+ try: meta = json.loads((pathlib.Path(state) / name).read_text())
331
+ except (OSError, ValueError): continue
332
+ pid = meta.get('pid')
333
+ if not isinstance(pid, int) or pid <= 1: continue
334
+ if sys.platform.startswith('linux') and meta.get('start') and identity(pid) != meta.get('start'): continue # the PID was reused
335
+ pids.add(pid)
336
+ return pids
337
+
338
+
339
+ def process_cwd(pid):
340
+ if sys.platform.startswith('linux'):
341
+ try: return os.readlink('/proc/%d/cwd' % pid)
342
+ except OSError: return None
343
+ if sys.platform == 'darwin':
344
+ for line in _run(['lsof', '-a', '-p', str(pid), '-d', 'cwd', '-Fn']).splitlines():
345
+ if line.startswith('n/'): return line[1:]
346
+ return None
347
+
348
+
349
+ def process_command(pid):
350
+ if sys.platform.startswith('linux'):
351
+ try: return pathlib.Path('/proc/%d/cmdline' % pid).read_bytes().replace(b'\0', b' ').decode(errors='replace').strip()
352
+ except OSError: return ''
353
+ if sys.platform == 'darwin': return _run(['ps', '-o', 'command=', '-p', str(pid)]).strip()
354
+ return ''
355
+
356
+
357
+ def inside(path, repo):
358
+ try: path = pathlib.Path(path).resolve()
359
+ except (OSError, ValueError): return False
360
+ return path == repo or repo in path.parents
176
361
 
177
362
 
363
+ TITLE = re.compile(r'<title[^>]*>(.*?)</title>', re.I | re.S)
364
+ ASSET = re.compile(r'''(?:src|href)\s*=\s*["']([^"'#?]+)''', re.I)
365
+ GENERATED = ('/_next/', '/@vite', '/@react-refresh', '/@fs/', '/@id/', '/node_modules/', '/__', '/static/js/', '/assets/')
366
+ TEXT_SUFFIX = {'.html', '.htm', '.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.vue', '.svelte', '.astro', '.json', '.md', '.mdx', '.py', '.erb'}
178
367
 
179
- def port_owners(port=3000):
368
+
369
+ def page(port, path='/'):
370
+ class NoRedirect(urllib.request.HTTPRedirectHandler):
371
+ def redirect_request(self, *args, **kwargs): return None
372
+ try:
373
+ with urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()).open(base_url(port) + path, timeout=3) as response:
374
+ return response.read(300000).decode(errors='replace')
375
+ except urllib.error.HTTPError as error:
376
+ try: return error.read(300000).decode(errors='replace')
377
+ except OSError: return ''
378
+ except (OSError, urllib.error.URLError, ValueError): return ''
379
+
380
+
381
+ def workspace_text(repo, needle, limit=4000):
382
+ """True when the text appears in one of the workspace's source files (bounded scan)."""
383
+ seen = 0
384
+ for base, dirs, files in os.walk(repo):
385
+ dirs[:] = [d for d in dirs if d not in SKIP_DIRS and not d.startswith('.')]
386
+ for name in files:
387
+ if pathlib.Path(name).suffix.lower() not in TEXT_SUFFIX: continue
388
+ seen += 1
389
+ if seen > limit: return False
390
+ try:
391
+ f = pathlib.Path(base, name)
392
+ if f.is_symlink() or f.stat().st_size > 300000: continue
393
+ if needle in f.read_text(errors='replace'): return True
394
+ except OSError: continue
395
+ return False
396
+
397
+
398
+ def content_match(port, repo):
399
+ """Does the served page come from this workspace? True / False, or None when the page gives nothing to compare."""
400
+ import html as htmllib
401
+ body = page(port)
402
+ if not body: return None
403
+ repo = pathlib.Path(repo).resolve()
404
+ title = TITLE.search(body)
405
+ title = ' '.join(htmllib.unescape(title.group(1)).split()) if title else ''
406
+ if title:
407
+ # The title must come from this workspace's files (index.html, a layout's metadata, a component).
408
+ for needle in (title, htmllib.escape(title, quote=False)):
409
+ if workspace_text(repo, needle): return True
410
+ parts = [p for p in re.split(r'\s*[|\u2013\u2014\u00b7:-]\s+', title) if len(p) >= 3]
411
+ if len(parts) > 1 and all(workspace_text(repo, p) for p in parts): return True # templated titles ("Page | Site")
412
+ return False
413
+ assets = []
414
+ for ref in ASSET.findall(body):
415
+ if ref.startswith(('http:', 'https:', '//', 'data:', 'mailto:', 'javascript:')) or ref.startswith(GENERATED): continue
416
+ assets.append(ref.lstrip('./').lstrip('/'))
417
+ assets = [a for a in assets if a][:40]
418
+ if not assets: return None
419
+ return any((repo / a).is_file() or (repo / 'public' / a).is_file() for a in assets)
420
+
421
+
422
+ def describe(pid, table=None):
423
+ info = (table or {}).get(pid) or {}
424
+ name = info.get('name') or (process_command(pid).split(' ')[0].rsplit('/', 1)[-1] if process_command(pid) else '')
425
+ cwd = process_cwd(pid)
426
+ return {'pid': pid, 'name': name or 'unknown process', **({'folder': cwd} if cwd else {})}
427
+
428
+
429
+ def server_owner(port=None, repo=None, state=None):
430
+ """Who serves the port: 'none', 'ours' (the process tree preview started), 'workspace' (a server running from
431
+ this workspace, such as one the agent started), 'foreign' (another app) or 'unknown'. Includes the processes."""
432
+ port = port or PORT
433
+ repo = pathlib.Path(repo).resolve() if repo else CTX.get('repo')
434
+ state = pathlib.Path(state) if state else CTX.get('state')
435
+ if not listening(port): return {'owner': 'none', 'port': port}
436
+ if repo is None: return {'owner': 'unknown', 'port': port, 'how': 'no workspace'}
437
+ pids = listener_pids(port)
438
+ if pids:
439
+ table = processes()
440
+ tree = process_tree(started_pids(state), table) if state else set()
441
+ if pids & tree: return {'owner': 'ours', 'port': port, 'pids': sorted(pids), 'how': 'process'}
442
+ known = 0
443
+ for pid in sorted(pids):
444
+ cwd = process_cwd(pid)
445
+ if (cwd and inside(cwd, repo)) or str(repo) in process_command(pid): return {'owner': 'workspace', 'port': port, 'pids': sorted(pids), 'how': 'folder'}
446
+ known += bool(cwd)
447
+ if known == len(pids):
448
+ return {'owner': 'foreign', 'port': port, 'pids': sorted(pids), 'how': 'folder', 'processes': [describe(p, table) for p in sorted(pids)][:3]}
449
+ processes_seen = [describe(p, table) for p in sorted(pids)][:3]
450
+ else: processes_seen = []
451
+ match = content_match(port, repo)
452
+ if match: return {'owner': 'workspace', 'port': port, 'how': 'content', **({'pids': sorted(pids)} if pids else {})}
453
+ if match is False: return {'owner': 'foreign', 'port': port, 'how': 'content', **({'pids': sorted(pids), 'processes': processes_seen} if pids else {}), 'title': (lambda m: ' '.join(m.group(1).split())[:120] if m else '')(TITLE.search(page(port)))}
454
+ return {'owner': 'unknown', 'port': port, 'how': 'none', **({'pids': sorted(pids)} if pids else {})}
455
+
456
+
457
+ def foreign_text(owner):
458
+ who = []
459
+ for p in owner.get('processes') or []:
460
+ who.append('%s (pid %s%s)' % (p.get('name') or 'process', p.get('pid'), ', folder ' + p['folder'] if p.get('folder') else ''))
461
+ title = owner.get('title')
462
+ return ('Port %d is already in use by %s, which is not this project%s.' % (owner['port'], ' and '.join(who) if who else 'another app', ' (its page is titled "' + title + '")' if title else ''))
463
+
464
+
465
+ def claim_port(state):
466
+ """Before starting a server: when another app holds the preview port, move to a free one (laptop runs) and record it.
467
+ Returns (owner record of the busy port or None, moved)."""
468
+ owner = server_owner()
469
+ if owner.get('owner') != 'foreign': return None, False
470
+ if not alternate_ports(): return owner, False
471
+ port = free_port(PORT + 1)
472
+ if not port: return owner, False
473
+ use_port(state, port, alternates=True)
474
+ return owner, True
475
+
476
+
477
+ def use_port(state, port, alternates=None):
478
+ global PORT
479
+ data = {'port': int(port)}
480
+ if alternates is not None: data['alternates'] = bool(alternates)
481
+ elif alternate_ports(): data['alternates'] = True
482
+ path = pathlib.Path(state) / 'preview-port.json'; tmp = path.with_suffix('.tmp'); tmp.write_text(json.dumps(data)); tmp.replace(path)
483
+ PORT = int(port)
484
+
485
+
486
+ def port_owners(port=None):
180
487
  """PIDs holding a listening socket on the port (read from /proc; no lsof/fuser needed)."""
181
- inodes = set()
488
+ port = port or PORT; inodes = set()
182
489
  for table in ('/proc/net/tcp', '/proc/net/tcp6'):
183
490
  try: lines = pathlib.Path(table).read_text().splitlines()[1:]
184
491
  except OSError: continue
@@ -186,6 +493,7 @@ def port_owners(port=3000):
186
493
  parts = line.split()
187
494
  if len(parts) > 9 and parts[3] == '0A' and int(parts[1].rsplit(':', 1)[1], 16) == port: inodes.add(parts[9])
188
495
  owners = set()
496
+ if not inodes: return owners
189
497
  for proc in pathlib.Path('/proc').iterdir():
190
498
  if not proc.name.isdigit(): continue
191
499
  try:
@@ -196,9 +504,13 @@ def port_owners(port=3000):
196
504
  return owners
197
505
 
198
506
 
199
- def stop_port(port=3000):
507
+ def stop_port(port=None):
508
+ """Stop this workspace's server on the port. Another app's server is never stopped (it is not ours to stop)."""
200
509
  import signal, time
201
- for pid in port_owners(port):
510
+ port = port or PORT
511
+ owners = port_owners(port)
512
+ if CTX.get('repo') and owners and server_owner(port).get('owner') not in ('ours', 'workspace'): return False
513
+ for pid in owners:
202
514
  try: os.killpg(os.getpgid(pid), signal.SIGTERM)
203
515
  except OSError:
204
516
  try: os.kill(pid, signal.SIGTERM)
@@ -252,7 +564,7 @@ NETWORK_ENV = ('HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY', 'http_proxy', 'https_pro
252
564
  def preview_env():
253
565
  # Only what installs and servers need to reach the registry; never model keys, Vault secrets or registry tokens.
254
566
  env = {k: v for k, v in os.environ.items() if k in SAFE_ENV or k in NETWORK_ENV}
255
- env.update({'PORT': '3000', 'HOST': '0.0.0.0', 'BROWSER': 'none', '__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS': '.vercel.run', 'NEXT_TELEMETRY_DISABLED': '1', 'npm_config_update_notifier': 'false'})
567
+ env.update({'PORT': str(PORT), 'HOST': '0.0.0.0', 'BROWSER': 'none', '__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS': '.vercel.run', 'NEXT_TELEMETRY_DISABLED': '1', 'npm_config_update_notifier': 'false'})
256
568
  return env
257
569
 
258
570
 
@@ -306,13 +618,13 @@ def prod_plan(repo):
306
618
  if selected['label'] == 'Next.js':
307
619
  direct = [str(bin_dir / 'next'), 'build']
308
620
  build = ['npm', 'run', 'build'] if NEXT_BUILD.fullmatch(script.strip()) else direct
309
- serve = me + [str(repo / 'out')] if static_export(repo) else [str(bin_dir / 'next'), 'start', '--hostname', '0.0.0.0', '--port', '3000']
621
+ serve = me[:-1] + ['--port', str(PORT), '--root', str(repo / 'out')] if static_export(repo) else [str(bin_dir / 'next'), 'start', '--hostname', '0.0.0.0', '--port', str(PORT)]
310
622
  # Next.js 16+ builds into .next while dev uses .next/dev, so the dev preview keeps serving during the build.
311
623
  return {'label': 'Next.js', 'build': build, 'fallback': direct if build != direct else None, 'serve': serve, 'concurrent': next_major(repo) >= 16, 'artifact': 'out' if static_export(repo) else '.next/BUILD_ID'}
312
624
  direct = [str(bin_dir / 'vite'), 'build']
313
625
  build = ['npm', 'run', 'build'] if VITE_BUILD.fullmatch(script.strip()) else direct
314
626
  server = node_app_server(repo)
315
- serve = ['node', server] if server else [str(bin_dir / 'vite'), 'preview', '--host', '0.0.0.0', '--port', '3000', '--strictPort']
627
+ serve = ['node', server] if server else [str(bin_dir / 'vite'), 'preview', '--host', '0.0.0.0', '--port', str(PORT), '--strictPort']
316
628
  return {'label': 'Vite', 'build': build, 'fallback': direct if build != direct else None, 'serve': serve, 'concurrent': True, 'artifact': 'dist/index.html'}
317
629
 
318
630
 
@@ -578,11 +890,11 @@ def wait_ready(seconds, proc=None):
578
890
  end = time.time() + seconds
579
891
  while time.time() < end:
580
892
  if proc is not None and proc.poll() is not None: return False
581
- if listening(3000):
893
+ if listening(PORT) and server_owner().get('owner') != 'foreign':
582
894
  try:
583
895
  class NoRedirect(urllib.request.HTTPRedirectHandler):
584
896
  def redirect_request(self, *args, **kwargs): return None
585
- with urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()).open('http://127.0.0.1:3000', timeout=20) as r: return r.status < 500
897
+ with urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()).open(base_url(), timeout=20) as r: return r.status < 500
586
898
  except urllib.error.HTTPError as error: return error.code < 500
587
899
  except (OSError, urllib.error.URLError): pass
588
900
  time.sleep(0.5)
@@ -600,9 +912,11 @@ def supervise(repo, state, mode):
600
912
  """Background preview job: packages, then the dev server, or a production build swapped in once it works."""
601
913
  import time
602
914
  repo, state = pathlib.Path(repo).resolve(), pathlib.Path(state)
915
+ bind(repo, state)
603
916
  with (state / 'preview-build.lock').open('a') as lock:
604
- try: fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
917
+ try: lock_file(lock, blocking=False)
605
918
  except OSError: return {'skipped': 'another preview job is running'}
919
+ claim_port(state) # another app took the preview port: move off it (laptop) before starting anything
606
920
  env = preview_env(); build_log = state / 'preview-build.log'
607
921
  build_log.write_bytes(b'')
608
922
  data = read_package(repo)
@@ -615,7 +929,10 @@ def supervise(repo, state, mode):
615
929
  if mode == 'dev':
616
930
  selected = plan(repo)
617
931
  if not selected: set_job(state, state='failed', error=unsupported_reason(repo)); return {}
618
- if not listening(3000): start_server(selected['command'], selected['cwd'], state, env, 'dev')
932
+ claim_port(state); selected = plan(repo); env = preview_env()
933
+ if not listening(PORT): start_server(selected['command'], selected['cwd'], state, env, 'dev')
934
+ elif server_owner().get('owner') == 'foreign':
935
+ set_job(state, state='failed', step='Port busy', error=foreign_text(server_owner())); return {'error': 'port busy'}
619
936
  set_job(state, state='done', step='Development preview', seconds=round(time.time() - started, 1)); return {'mode': 'dev'}
620
937
  pp = prod_plan(repo)
621
938
  if not pp: set_job(state, state='failed', error=unsupported_reason(repo)); return {}
@@ -624,7 +941,7 @@ def supervise(repo, state, mode):
624
941
  dev = plan(repo); interim = None
625
942
  if not cached:
626
943
  if not pp['concurrent']: stop_port()
627
- elif not listening(3000) and dev and dev['label'] != 'Static website':
944
+ elif not listening(PORT) and dev and dev['label'] != 'Static website':
628
945
  # Show the dev preview right away while the production build runs (Next.js 16+ and Vite build into separate folders).
629
946
  if next_dev(repo): ensure_next_dev_origins(repo)
630
947
  interim = start_server(dev['command'], dev['cwd'], state, env, 'dev')
@@ -652,7 +969,7 @@ def supervise(repo, state, mode):
652
969
  set_job(state, state='failed', step='Production build failed', error=error, fixed=fixed, seconds=round(time.time() - started, 1))
653
970
  # An older production server would show stale code; replace it with the dev server so the preview matches the files.
654
971
  if serving_mode(state).get('mode') == 'prod' and not (state / 'run.lock').exists(): stop_port()
655
- if not listening(3000) and dev and not (state / 'run.lock').exists():
972
+ if not listening(PORT) and dev and not (state / 'run.lock').exists():
656
973
  if next_dev(repo): ensure_next_dev_origins(repo)
657
974
  start_server(dev['command'], dev['cwd'], state, env, 'dev')
658
975
  return {'error': error}
@@ -662,6 +979,7 @@ def supervise(repo, state, mode):
662
979
  if (state / 'run.lock').exists(): set_job(state, state='cancelled', step='The agent started a new task'); return {}
663
980
  set_job(state, step='Switching to the production version')
664
981
  stop_port()
982
+ if claim_port(state)[1]: pp, dev, env = prod_plan(repo), plan(repo), preview_env()
665
983
  proc = start_server(pp['serve'], repo, state, env, 'prod', fp)
666
984
  if wait_ready(SERVE_WAIT, proc):
667
985
  set_job(state, state='done', step='Production preview', seconds=round(time.time() - started, 1)); return {'mode': 'prod'}
@@ -696,9 +1014,18 @@ def launch(repo, state, confirmed=False, agent=False, mode=None):
696
1014
  repo = pathlib.Path(repo)
697
1015
  mode = mode or ('dev' if agent else 'prod')
698
1016
  with (state / 'preview.lock').open('a') as lock:
699
- fcntl.flock(lock, fcntl.LOCK_EX)
1017
+ lock_file(lock)
1018
+ bind(repo, state)
1019
+ if os.getenv('MINI_LAPTOP') == '1' and not (state / 'preview-port.json').exists(): use_port(state, PORT, alternates=True)
700
1020
  changed = ensure_next_dev_origins(repo) if next_dev(repo) else ''
701
1021
  current = status(state)
1022
+ busy_port = None
1023
+ if current.get('foreignServer'):
1024
+ busy_port, moved = claim_port(state)
1025
+ if not moved:
1026
+ return {**current, 'portBusy': True, 'running': False}
1027
+ current = status(state) # the new port is free
1028
+ current['movedFrom'] = busy_port['port']
702
1029
  # A confirmed start replaces a dev server that rejects the preview host (for example one started without allowedHosts / allowedDevOrigins).
703
1030
  if current.get('hostBlocked') and (agent or not (state / 'run.lock').exists()):
704
1031
  stop_port();current = status(state)
@@ -732,7 +1059,9 @@ def launch(repo, state, confirmed=False, agent=False, mode=None):
732
1059
  spawn(state, mode, repo.parent)
733
1060
  message = 'Installing packages and starting your app…' if mode == 'dev' else ('Preparing the production preview…' if not current['ready'] else current['message'])
734
1061
  building = mode == 'prod' and selected['label'] != 'Static website'
735
- return {'ready': bool(current['ready']) and building, 'running': True, 'building': building, 'message': message, 'log': '', **({'configChanged': changed} if changed else {}), **({'warning': current['warning']} if current.get('warning') else {})}
1062
+ if busy_port: message = foreign_text(busy_port) + ' The preview of this project runs on ' + base_url() + ' instead. ' + message
1063
+ return {'ready': bool(current['ready']) and building, 'running': True, 'building': building, 'message': message, 'log': '', 'port': PORT, 'url': base_url(),
1064
+ **({'foreignServer': busy_port, 'movedFrom': busy_port['port']} if busy_port else {}), **({'configChanged': changed} if changed else {}), **({'warning': current['warning']} if current.get('warning') else {})}
736
1065
 
737
1066
 
738
1067
  def agent_takeover(repo, state):
@@ -803,7 +1132,8 @@ def revive(repo, state):
803
1132
  try: meta = json.loads((state / 'preview-process.json').read_text())
804
1133
  except (OSError, ValueError): return done
805
1134
  if meta.get('pid') and identity(meta['pid']) == meta.get('start'): return done
806
- if listening(3000): return done
1135
+ bind(repo, state)
1136
+ if listening(PORT) and server_owner().get('owner') != 'foreign': return done
807
1137
  try: last = json.loads((state / 'preview-revive.json').read_text())
808
1138
  except (OSError, ValueError): last = {}
809
1139
  recent = [t for t in last.get('times', []) if time.time() - t < 600]
@@ -825,7 +1155,7 @@ def static_path_allowed(repo, request_path):
825
1155
  return candidate.resolve().is_relative_to(pathlib.Path(repo).resolve()) and not any(p.is_symlink() for p in [candidate, *candidate.parents] if p != pathlib.Path(repo).parent)
826
1156
 
827
1157
 
828
- def serve(repo):
1158
+ def serve(repo, port=None):
829
1159
  from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
830
1160
  class Handler(SimpleHTTPRequestHandler):
831
1161
  def __init__(self, *args, **kwargs): super().__init__(*args, directory=str(repo), **kwargs)
@@ -835,17 +1165,18 @@ def serve(repo):
835
1165
  return super().send_head()
836
1166
  def list_directory(self, path):
837
1167
  self.send_error(403);return None
838
- ThreadingHTTPServer(('0.0.0.0', 3000), Handler).serve_forever()
1168
+ ThreadingHTTPServer(('0.0.0.0', int(port or os.getenv('PORT') or PORT)), Handler).serve_forever()
839
1169
 
840
1170
 
841
1171
  PATHS = {} # explicit repo/state (laptop runs keep the session state apart from the user's folder)
842
1172
 
843
1173
  def main():
844
- parser = argparse.ArgumentParser();parser.add_argument('action', choices=['status', 'start', 'serve', 'supervise', 'finalize']);parser.add_argument('--mode', choices=['dev', 'prod']);parser.add_argument('--root', default='/vercel/sandbox');parser.add_argument('--repo');parser.add_argument('--state');parser.add_argument('--confirmed', action='store_true');parser.add_argument('--agent', action='store_true');parser.add_argument('--revive', action='store_true');args = parser.parse_args()
1174
+ parser = argparse.ArgumentParser();parser.add_argument('action', choices=['status', 'start', 'serve', 'supervise', 'finalize']);parser.add_argument('--mode', choices=['dev', 'prod']);parser.add_argument('--root', default='/vercel/sandbox');parser.add_argument('--repo');parser.add_argument('--state');parser.add_argument('--confirmed', action='store_true');parser.add_argument('--agent', action='store_true');parser.add_argument('--revive', action='store_true');parser.add_argument('--port', type=int);args = parser.parse_args()
845
1175
  root = pathlib.Path(args.root);state = pathlib.Path(args.state) if args.state else root / '.mini'
846
- if args.action == 'serve': return serve(root)
1176
+ if args.action == 'serve': return serve(root, args.port)
847
1177
  repo = pathlib.Path(args.repo) if args.repo else root / 'repo'
848
1178
  if args.repo: PATHS['repo'] = repo
1179
+ bind(repo, state)
849
1180
  if args.action in ('supervise', 'finalize'):
850
1181
  result = supervise(repo, state, args.mode or 'prod') if args.action == 'supervise' else finalize(repo, state)
851
1182
  print(json.dumps(result)); return