mlai-code 0.3.8 → 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
@@ -27,13 +27,13 @@ def plan(repo):
27
27
  if not isinstance(script, str): continue
28
28
  # Only standard framework entrypoints; compound scripts need agent review.
29
29
  if 'next' in deps and re.fullmatch(r'next (dev|start)(?: --(?:hostname|port) [a-zA-Z0-9.:-]+)*', script.strip()):
30
- 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)}
31
31
  if 'vite' in deps and re.fullmatch(r'vite(?: --host)?', script.strip()):
32
- 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)}
33
33
  return None
34
34
  index = repo / 'index.html'
35
35
  if index.is_file() and not index.is_symlink():
36
- 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)}
37
37
  return None
38
38
 
39
39
 
@@ -106,7 +106,7 @@ def ensure_next_dev_origins(repo):
106
106
  def next_origin_blocked():
107
107
  """Next.js dev answers 403 'Unauthorized' to /_next requests from an origin outside allowedDevOrigins."""
108
108
  try:
109
- 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'})
110
110
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
111
111
  with opener.open(request, timeout=3): return False
112
112
  except urllib.error.HTTPError as error:
@@ -119,7 +119,7 @@ def next_origin_blocked():
119
119
  def host_blocked():
120
120
  """Vite answers loopback but rejects the public preview host with 403 'Blocked request'."""
121
121
  try:
122
- 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'})
123
123
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
124
124
  with opener.open(request, timeout=2): return False
125
125
  except urllib.error.HTTPError as error:
@@ -129,25 +129,26 @@ def host_blocked():
129
129
  except (OSError, urllib.error.URLError): return False
130
130
 
131
131
 
132
- 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 '
133
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.')
134
134
 
135
135
 
136
136
  def loopback6_status(opener):
137
137
  try:
138
- 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
139
139
  except urllib.error.HTTPError as error: return error.code
140
140
  except (OSError, urllib.error.URLError, ValueError): return 0
141
141
 
142
142
 
143
143
  def status(state):
144
+ if PATHS.get('repo') and not CTX: bind(PATHS['repo'], state)
144
145
  code = 0
145
146
  try:
146
147
  # Do not follow redirects off the local app or send loopback through an HTTP proxy.
147
148
  class NoRedirect(urllib.request.HTTPRedirectHandler):
148
149
  def redirect_request(self, *args, **kwargs): return None
149
150
  opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
150
- 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
151
152
  except urllib.error.HTTPError as error: code = error.code
152
153
  except (OSError, urllib.error.URLError): pass
153
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
@@ -163,6 +164,13 @@ def status(state):
163
164
  file.seek(0, 2);file.seek(max(0, file.tell() - 8000));log = file.read().decode(errors='replace')
164
165
  except OSError: pass
165
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}
166
174
  work = job(state); mode = meta.get('mode', 'dev') if running or ready else ''
167
175
  if work.get('alive') and (work.get('step') or '').startswith(('Building', 'Installing', 'Adding')): log = tail(state / 'preview-build.log', 8000) or log
168
176
  extra = {'mode': mode or None, 'notes': [n for n in work.get('notes') or [] if isinstance(n, str)][:4]}
@@ -178,15 +186,306 @@ def status(state):
178
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}
179
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)')
180
188
  elif work.get('alive'): message = (work.get('step') or 'Preparing the preview') + '…'
181
- elif clash: message = PORT_CLASH.format(code=code); extra['portClash'] = True
182
- 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.'))
183
- 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}
184
194
 
185
195
 
186
196
 
187
- def port_owners(port=3000):
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
361
+
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'}
367
+
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):
188
487
  """PIDs holding a listening socket on the port (read from /proc; no lsof/fuser needed)."""
189
- inodes = set()
488
+ port = port or PORT; inodes = set()
190
489
  for table in ('/proc/net/tcp', '/proc/net/tcp6'):
191
490
  try: lines = pathlib.Path(table).read_text().splitlines()[1:]
192
491
  except OSError: continue
@@ -194,6 +493,7 @@ def port_owners(port=3000):
194
493
  parts = line.split()
195
494
  if len(parts) > 9 and parts[3] == '0A' and int(parts[1].rsplit(':', 1)[1], 16) == port: inodes.add(parts[9])
196
495
  owners = set()
496
+ if not inodes: return owners
197
497
  for proc in pathlib.Path('/proc').iterdir():
198
498
  if not proc.name.isdigit(): continue
199
499
  try:
@@ -204,9 +504,13 @@ def port_owners(port=3000):
204
504
  return owners
205
505
 
206
506
 
207
- 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)."""
208
509
  import signal, time
209
- 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:
210
514
  try: os.killpg(os.getpgid(pid), signal.SIGTERM)
211
515
  except OSError:
212
516
  try: os.kill(pid, signal.SIGTERM)
@@ -260,7 +564,7 @@ NETWORK_ENV = ('HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY', 'http_proxy', 'https_pro
260
564
  def preview_env():
261
565
  # Only what installs and servers need to reach the registry; never model keys, Vault secrets or registry tokens.
262
566
  env = {k: v for k, v in os.environ.items() if k in SAFE_ENV or k in NETWORK_ENV}
263
- 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'})
264
568
  return env
265
569
 
266
570
 
@@ -314,13 +618,13 @@ def prod_plan(repo):
314
618
  if selected['label'] == 'Next.js':
315
619
  direct = [str(bin_dir / 'next'), 'build']
316
620
  build = ['npm', 'run', 'build'] if NEXT_BUILD.fullmatch(script.strip()) else direct
317
- 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)]
318
622
  # Next.js 16+ builds into .next while dev uses .next/dev, so the dev preview keeps serving during the build.
319
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'}
320
624
  direct = [str(bin_dir / 'vite'), 'build']
321
625
  build = ['npm', 'run', 'build'] if VITE_BUILD.fullmatch(script.strip()) else direct
322
626
  server = node_app_server(repo)
323
- 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']
324
628
  return {'label': 'Vite', 'build': build, 'fallback': direct if build != direct else None, 'serve': serve, 'concurrent': True, 'artifact': 'dist/index.html'}
325
629
 
326
630
 
@@ -586,11 +890,11 @@ def wait_ready(seconds, proc=None):
586
890
  end = time.time() + seconds
587
891
  while time.time() < end:
588
892
  if proc is not None and proc.poll() is not None: return False
589
- if listening(3000):
893
+ if listening(PORT) and server_owner().get('owner') != 'foreign':
590
894
  try:
591
895
  class NoRedirect(urllib.request.HTTPRedirectHandler):
592
896
  def redirect_request(self, *args, **kwargs): return None
593
- 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
594
898
  except urllib.error.HTTPError as error: return error.code < 500
595
899
  except (OSError, urllib.error.URLError): pass
596
900
  time.sleep(0.5)
@@ -608,9 +912,11 @@ def supervise(repo, state, mode):
608
912
  """Background preview job: packages, then the dev server, or a production build swapped in once it works."""
609
913
  import time
610
914
  repo, state = pathlib.Path(repo).resolve(), pathlib.Path(state)
915
+ bind(repo, state)
611
916
  with (state / 'preview-build.lock').open('a') as lock:
612
917
  try: lock_file(lock, blocking=False)
613
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
614
920
  env = preview_env(); build_log = state / 'preview-build.log'
615
921
  build_log.write_bytes(b'')
616
922
  data = read_package(repo)
@@ -623,7 +929,10 @@ def supervise(repo, state, mode):
623
929
  if mode == 'dev':
624
930
  selected = plan(repo)
625
931
  if not selected: set_job(state, state='failed', error=unsupported_reason(repo)); return {}
626
- 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'}
627
936
  set_job(state, state='done', step='Development preview', seconds=round(time.time() - started, 1)); return {'mode': 'dev'}
628
937
  pp = prod_plan(repo)
629
938
  if not pp: set_job(state, state='failed', error=unsupported_reason(repo)); return {}
@@ -632,7 +941,7 @@ def supervise(repo, state, mode):
632
941
  dev = plan(repo); interim = None
633
942
  if not cached:
634
943
  if not pp['concurrent']: stop_port()
635
- elif not listening(3000) and dev and dev['label'] != 'Static website':
944
+ elif not listening(PORT) and dev and dev['label'] != 'Static website':
636
945
  # Show the dev preview right away while the production build runs (Next.js 16+ and Vite build into separate folders).
637
946
  if next_dev(repo): ensure_next_dev_origins(repo)
638
947
  interim = start_server(dev['command'], dev['cwd'], state, env, 'dev')
@@ -660,7 +969,7 @@ def supervise(repo, state, mode):
660
969
  set_job(state, state='failed', step='Production build failed', error=error, fixed=fixed, seconds=round(time.time() - started, 1))
661
970
  # An older production server would show stale code; replace it with the dev server so the preview matches the files.
662
971
  if serving_mode(state).get('mode') == 'prod' and not (state / 'run.lock').exists(): stop_port()
663
- 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():
664
973
  if next_dev(repo): ensure_next_dev_origins(repo)
665
974
  start_server(dev['command'], dev['cwd'], state, env, 'dev')
666
975
  return {'error': error}
@@ -670,6 +979,7 @@ def supervise(repo, state, mode):
670
979
  if (state / 'run.lock').exists(): set_job(state, state='cancelled', step='The agent started a new task'); return {}
671
980
  set_job(state, step='Switching to the production version')
672
981
  stop_port()
982
+ if claim_port(state)[1]: pp, dev, env = prod_plan(repo), plan(repo), preview_env()
673
983
  proc = start_server(pp['serve'], repo, state, env, 'prod', fp)
674
984
  if wait_ready(SERVE_WAIT, proc):
675
985
  set_job(state, state='done', step='Production preview', seconds=round(time.time() - started, 1)); return {'mode': 'prod'}
@@ -705,8 +1015,17 @@ def launch(repo, state, confirmed=False, agent=False, mode=None):
705
1015
  mode = mode or ('dev' if agent else 'prod')
706
1016
  with (state / 'preview.lock').open('a') as lock:
707
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)
708
1020
  changed = ensure_next_dev_origins(repo) if next_dev(repo) else ''
709
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']
710
1029
  # A confirmed start replaces a dev server that rejects the preview host (for example one started without allowedHosts / allowedDevOrigins).
711
1030
  if current.get('hostBlocked') and (agent or not (state / 'run.lock').exists()):
712
1031
  stop_port();current = status(state)
@@ -740,7 +1059,9 @@ def launch(repo, state, confirmed=False, agent=False, mode=None):
740
1059
  spawn(state, mode, repo.parent)
741
1060
  message = 'Installing packages and starting your app…' if mode == 'dev' else ('Preparing the production preview…' if not current['ready'] else current['message'])
742
1061
  building = mode == 'prod' and selected['label'] != 'Static website'
743
- 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 {})}
744
1065
 
745
1066
 
746
1067
  def agent_takeover(repo, state):
@@ -811,7 +1132,8 @@ def revive(repo, state):
811
1132
  try: meta = json.loads((state / 'preview-process.json').read_text())
812
1133
  except (OSError, ValueError): return done
813
1134
  if meta.get('pid') and identity(meta['pid']) == meta.get('start'): return done
814
- if listening(3000): return done
1135
+ bind(repo, state)
1136
+ if listening(PORT) and server_owner().get('owner') != 'foreign': return done
815
1137
  try: last = json.loads((state / 'preview-revive.json').read_text())
816
1138
  except (OSError, ValueError): last = {}
817
1139
  recent = [t for t in last.get('times', []) if time.time() - t < 600]
@@ -833,7 +1155,7 @@ def static_path_allowed(repo, request_path):
833
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)
834
1156
 
835
1157
 
836
- def serve(repo):
1158
+ def serve(repo, port=None):
837
1159
  from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
838
1160
  class Handler(SimpleHTTPRequestHandler):
839
1161
  def __init__(self, *args, **kwargs): super().__init__(*args, directory=str(repo), **kwargs)
@@ -843,17 +1165,18 @@ def serve(repo):
843
1165
  return super().send_head()
844
1166
  def list_directory(self, path):
845
1167
  self.send_error(403);return None
846
- 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()
847
1169
 
848
1170
 
849
1171
  PATHS = {} # explicit repo/state (laptop runs keep the session state apart from the user's folder)
850
1172
 
851
1173
  def main():
852
- 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()
853
1175
  root = pathlib.Path(args.root);state = pathlib.Path(args.state) if args.state else root / '.mini'
854
- if args.action == 'serve': return serve(root)
1176
+ if args.action == 'serve': return serve(root, args.port)
855
1177
  repo = pathlib.Path(args.repo) if args.repo else root / 'repo'
856
1178
  if args.repo: PATHS['repo'] = repo
1179
+ bind(repo, state)
857
1180
  if args.action in ('supervise', 'finalize'):
858
1181
  result = supervise(repo, state, args.mode or 'prod') if args.action == 'supervise' else finalize(repo, state)
859
1182
  print(json.dumps(result)); return