forked from NiuTrans/ToFu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1571 lines (1364 loc) · 64 KB
/
Copy pathserver.py
File metadata and controls
1571 lines (1364 loc) · 64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Tofu Server — Quart + Hypercorn (HTTP/2, ASGI).
App entry point. Uses:
- Quart (async Flask from Pallets) as the application framework
- Hypercorn as the ASGI server with HTTP/2 support
- Auto-generated self-signed TLS for zero-config HTTP/2 in browsers
All existing Flask-style sync route handlers run unchanged in a thread pool.
Usage:
python server.py # HTTPS + HTTP/2 (auto-cert)
python server.py --no-tls # HTTP/1.1 only
python server.py --certfile cert.pem --keyfile key.pem # custom cert
"""
import asyncio
import os
import sys
import json
import logging
import time
import signal
import faulthandler
# ── Capture C-level fatal signals (SIGSEGV / SIGABRT / SIGFPE / SIGILL / SIGBUS) ──
# These fire on heap corruption (e.g. `munmap_chunk(): invalid pointer`) from
# native extensions like urllib3's response decompressor. Without this the
# abort prints to fd 2 only and we lose the Python stack of every thread.
# Writing to a dedicated file (instead of stderr) ensures the trace survives
# even when stderr is the controlling terminal of a process that's about
# to die. all_threads=True captures every Python thread, not just the
# crashing one — essential for diagnosing concurrent-fetch races.
#
# Dual-sink strategy: write to BOTH the FUSE-backed logs/ (durable across
# box restarts, but may be truncated by the very FUSE stall that caused the
# crash) AND a tmpfs mirror in /dev/shm (immune to FUSE stalls, but lost on
# box reboot). On crash, check /dev/shm first for the clean copy.
_fault_log = None
try:
_FAULT_LOG_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'logs', 'faulthandler.log')
os.makedirs(os.path.dirname(_FAULT_LOG_PATH), exist_ok=True)
_fault_log = open(_FAULT_LOG_PATH, 'a', buffering=1) # line-buffered
_fault_log.write('\n=== faulthandler armed pid=%d at %s ===\n'
% (os.getpid(), time.strftime('%Y-%m-%d %H:%M:%S')))
except OSError:
pass
# Prefer tmpfs for the live faulthandler sink (survives FUSE stalls intact);
# fall back to the FUSE log, then stderr.
_fault_shm_log = None
try:
_FAULT_SHM_PATH = '/dev/shm/tofu_faulthandler_%d.log' % os.getpid()
_fault_shm_log = open(_FAULT_SHM_PATH, 'w', buffering=1)
_fault_shm_log.write('=== faulthandler armed pid=%d at %s ===\n'
% (os.getpid(), time.strftime('%Y-%m-%d %H:%M:%S')))
faulthandler.enable(file=_fault_shm_log, all_threads=True)
except OSError:
_fault_shm_log = None
if _fault_shm_log is None:
# tmpfs unavailable — use the FUSE log (better than nothing)
if _fault_log is not None:
faulthandler.enable(file=_fault_log, all_threads=True)
else:
faulthandler.enable(all_threads=True)
# ── Pin mapped pages into RAM (FUSE SIGBUS mitigation) ──
# All .so files (C extensions, libpython, libc) are dlopen'd via mmap with
# demand-paged code segments. When those files live on a FUSE mount, a
# transient stall during a lazy page-in delivers SIGBUS (unrecoverable).
# MCL_CURRENT pins already-mapped pages; MCL_FUTURE pins every future mmap
# at load time, collapsing the dangerous demand-fault window to zero.
try:
import ctypes as _ctypes
_MCL_CURRENT, _MCL_FUTURE = 1, 2
_libc = _ctypes.CDLL('libc.so.6', use_errno=True)
if _libc.mlockall(_MCL_CURRENT | _MCL_FUTURE) != 0:
import errno as _errno
_mlk_err = _ctypes.get_errno()
# ENOMEM (12) = memlock rlimit too low — common in containers
if _mlk_err == _errno.ENOMEM:
os.write(2, b'[boot] mlockall skipped: memlock rlimit too low\n')
else:
os.write(2, (b'[boot] mlockall failed errno=%d\n' % _mlk_err))
else:
os.write(2, b'[boot] mlockall(MCL_CURRENT|MCL_FUTURE) OK '
b'\xe2\x80\x94 pages pinned\n')
except Exception as _mlk_exc:
try:
os.write(2, (b'[boot] mlockall unavailable: %s\n'
% str(_mlk_exc).encode(errors='replace')))
except OSError:
pass
# ── Record process start time (same as server.py) ──
_PROC_T0 = time.time()
try:
os.write(2, b'\033[36m[boot + 0.0s]\033[0m \xf0\x9f\xab\xa7 Tofu '
b'async bootstrap \xe2\x80\x94 importing core libraries\xe2\x80\xa6\n')
except OSError:
pass
# ── Auto-activate conda env (reuse server.py logic) ──
# This must happen before any third-party imports.
_PROJ_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _PROJ_DIR)
# ── Dev fallback: locate a local tofu-search checkout when it isn't
# pip-installed (production installs it via requirements.txt). Set
# TOFU_SEARCH_PATH to the repo root of a sibling tofu-search clone.
_TOFU_SEARCH_PATH = os.environ.get('TOFU_SEARCH_PATH', '')
if _TOFU_SEARCH_PATH and os.path.isdir(_TOFU_SEARCH_PATH):
sys.path.insert(0, _TOFU_SEARCH_PATH)
def _tofu_maybe_reexec_into_env():
"""Re-exec into Tofu's conda env if not already there."""
marker = os.path.join(_PROJ_DIR, '.tofu_env.json')
if not os.path.isfile(marker):
return
try:
with open(marker, 'r', encoding='utf-8') as f:
cfg = json.load(f)
except Exception:
return
target_py = cfg.get('python') or ''
env_prefix = cfg.get('env_prefix') or ''
if not target_py or not os.access(target_py, os.X_OK):
return
try:
same = os.path.realpath(target_py) == os.path.realpath(sys.executable)
except OSError:
same = (target_py == sys.executable)
if same:
return
if os.environ.get('_TOFU_ENV_REEXEC') == '1':
return
if env_prefix and os.path.isdir(env_prefix):
env_lib = os.path.join(env_prefix, 'lib')
if os.path.isdir(env_lib):
os.environ['LD_LIBRARY_PATH'] = (
env_lib + os.pathsep + os.environ.get('LD_LIBRARY_PATH', ''))
env_bin = os.path.join(env_prefix, 'bin')
if os.path.isdir(env_bin):
os.environ['PATH'] = env_bin + os.pathsep + os.environ.get('PATH', '')
os.environ.setdefault('CONDA_PREFIX', env_prefix)
os.environ['_TOFU_ENV_REEXEC'] = '1'
try:
os.execv(target_py, [target_py, *sys.argv])
except OSError:
os.environ.pop('_TOFU_ENV_REEXEC', None)
_tofu_maybe_reexec_into_env()
# ── .env loading ──
def _load_dotenv():
env_path = os.path.join(_PROJ_DIR, '.env')
if not os.path.exists(env_path):
return
with open(env_path) as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
key, _, value = line.partition('=')
key, value = key.strip(), value.strip()
if key not in os.environ:
os.environ[key] = value
_load_dotenv()
# ═══════════════════════════════════════════════════════════════════════
# Framework Compatibility Shim
# ═══════════════════════════════════════════════════════════════════════
# Quart is API-compatible with Flask but lives under `quart.*` imports.
# Our routes and lib/ code import from flask. We install a shim so that
# `from flask import *` resolves to Quart's equivalents at runtime.
# This is the official Quart migration approach.
def _install_flask_shim():
"""Make `from flask import X` resolve to Quart equivalents.
Quart is a superset of Flask's API. This shim allows all existing
route code to work without changing any import statements.
Key difference: Quart makes send_from_directory, send_file, and
make_response async. When sync route handlers (running in Quart's
thread pool) call these, they get coroutine objects. We wrap them
with sync-safe versions that detect this and await appropriately.
"""
try:
import quart
except ImportError:
sys.stderr.write(
'\033[31m[server.py] ERROR: quart is not installed.\n'
' Install with: pip install quart hypercorn cryptography\033[0m\n')
sys.exit(1)
import asyncio
import functools
import inspect
# Recover the GENUINE async helpers. If server.py is imported/exec'd
# more than once in the same process (e.g. a test re-imports it via
# importlib), ``quart.make_response`` etc. are already our sync-safe
# wrappers from the first install. Capturing those as the "originals"
# and wrapping them again would corrupt ``_orig_make_response_async``
# (it would point at a sync-safe wrapper instead of the real async
# ``quart.make_response``), so error handlers that
# ``await _orig_make_response_async(...)`` would route through the
# thread-bridge and deadlock. ``_sync_safe`` stashes the genuine async
# function on ``.__wrapped__``; unwrap through it so a re-install
# always starts from the real async helpers.
def _genuine(fn):
while getattr(fn, '_quart_async_wrapper', False):
fn = getattr(fn, '__wrapped__', fn)
return fn
_orig_send_from_directory = _genuine(quart.send_from_directory)
_orig_send_file = _genuine(quart.send_file)
_orig_make_response = _genuine(quart.make_response)
def _sync_safe(async_fn):
"""Wrap an async function to be callable from sync code in a thread."""
@functools.wraps(async_fn)
def wrapper(*args, **kwargs):
coro = async_fn(*args, **kwargs)
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
# We're in a thread with an event loop running elsewhere.
# Use the Quart-provided mechanism to run coroutines from
# sync code within a request context.
future = asyncio.run_coroutine_threadsafe(coro, loop)
return future.result(timeout=30)
else:
return asyncio.run(coro)
# Also make it awaitable for async callers
wrapper._async = async_fn
wrapper.__wrapped__ = async_fn
# Mark it so Quart's ensure_async can detect the dual nature
wrapper._quart_async_wrapper = True
return wrapper
# Replace in quart module so `from flask import send_from_directory`
# gets the sync-safe version
quart.send_from_directory = _sync_safe(_orig_send_from_directory)
quart.send_file = _sync_safe(_orig_send_file)
quart.make_response = _sync_safe(_orig_make_response)
# Expose originals at module level for async code that needs to await directly
global _orig_make_response_async
_orig_make_response_async = _orig_make_response
# ── Patch Request async methods/properties for sync route handlers ──
# In Quart, get_json(), form, files, data, and json are async. Sync
# route handlers (run in executor threads via run_sync) get coroutine
# objects instead of values. Monkey-patch the Request class to run
# the coroutine on the MAIN event loop — NOT a fresh child loop.
#
# The naive ``asyncio.run(coro)`` here is wrong: it spins up a new
# loop in the worker thread, and the coroutine then awaits hypercorn's
# request body Future, which lives on the main loop. Cross-loop
# awaits never wake up — symptom: large POST bodies hang server-side
# until the client times out, while small bodies (already inlined
# into the ASGI scope before dispatch) work fine. Fix: schedule via
# ``run_coroutine_threadsafe`` on the loop saved by
# ``hub.set_loop`` at startup.
from quart.wrappers import Request as _QuartRequest
_orig_get_json = _QuartRequest.get_json
def _run_coro_sync(coro):
"""Run a coroutine from a sync context (executor thread)."""
if not inspect.iscoroutine(coro):
return coro
try:
from lib.push import hub as _push_hub
main_loop = getattr(_push_hub, '_loop', None)
except Exception:
main_loop = None
if main_loop is not None and main_loop.is_running():
future = asyncio.run_coroutine_threadsafe(coro, main_loop)
return future.result()
return asyncio.run(coro)
def _sync_safe_get_json(self, *args, **kwargs):
return _run_coro_sync(_orig_get_json(self, *args, **kwargs))
# Stash the genuine async original ON the wrapper so async handlers can
# recover it regardless of how many times the shim is (re)installed or
# which module object holds it (test harnesses sometimes exec server.py as
# a second module). Always unwrap to the FIRST genuine coroutine fn.
_genuine_get_json = getattr(_orig_get_json, '_genuine_async_get_json', _orig_get_json)
_sync_safe_get_json._genuine_async_get_json = _genuine_get_json
_QuartRequest.get_json = _sync_safe_get_json
# Patch async properties: form, files, data, json
_orig_form_prop = _QuartRequest.form
_orig_files_prop = _QuartRequest.files
_orig_data_prop = _QuartRequest.data
def _make_sync_safe_property(orig_prop):
_fget = orig_prop.fget
@property
def _prop(self):
return _run_coro_sync(_fget(self))
return _prop
_QuartRequest.form = _make_sync_safe_property(_orig_form_prop)
_QuartRequest.files = _make_sync_safe_property(_orig_files_prop)
_QuartRequest.data = _make_sync_safe_property(_orig_data_prop)
# json property delegates to the already-patched sync get_json
@property
def _json_prop(self):
return self.get_json()
_QuartRequest.json = _json_prop
# Install the shim: make `import flask` resolve to quart
sys.modules['flask'] = quart
# Also shim sub-modules that code might import from
for attr in ('json', 'globals', 'helpers', 'wrappers', 'ctx'):
quart_sub = f'quart.{attr}'
flask_sub = f'flask.{attr}'
if quart_sub in sys.modules:
sys.modules[flask_sub] = sys.modules[quart_sub]
# Werkzeug exceptions are used directly in some places
# Quart re-exports them, but ensure werkzeug is still importable
import importlib.util
if importlib.util.find_spec('werkzeug') is None:
logging.getLogger(__name__).debug('werkzeug not importable; relying on quart re-exports')
_install_flask_shim()
# ── Now safe to import Quart (which the routes will see as 'flask') ──
import quart # noqa: F401 — kept so quart.* monkeypatches in _install_flask_shim resolve
from quart import Quart, request
# ═══════════════════════════════════════════════════════════════════════
# Logging (reuse server.py's architecture)
# ═══════════════════════════════════════════════════════════════════════
import mimetypes
mimetypes.init()
mimetypes.add_type('text/javascript', '.js')
mimetypes.add_type('text/css', '.css')
mimetypes.add_type('application/json', '.json')
mimetypes.add_type('image/svg+xml', '.svg')
mimetypes.add_type('font/woff2', '.woff2')
mimetypes.add_type('font/ttf', '.ttf')
mimetypes.add_type('application/wasm', '.wasm')
BASE_DIR = _PROJ_DIR
# ── Logging setup (identical to server.py) ──
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
LOG_DIR = os.path.join(BASE_DIR, 'logs')
os.makedirs(LOG_DIR, exist_ok=True)
_LOG_FMT = '%(asctime)s [%(levelname)s] %(name)s [%(threadName)s]: %(message)s'
_LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
_formatter = logging.Formatter(_LOG_FMT, datefmt=_LOG_DATEFMT)
_BIZ_PREFIXES = ('lib.', 'routes.', 'server')
class _BizOnly(logging.Filter):
def filter(self, record):
return record.name.startswith(_BIZ_PREFIXES)
class _VendorOnly(logging.Filter):
def filter(self, record):
return (not record.name.startswith(_BIZ_PREFIXES)
and record.name != 'werkzeug'
and record.name != 'hypercorn'
and not record.name.startswith('hypercorn.'))
class _BizAndServerOnly(logging.Filter):
def filter(self, record):
return (record.name.startswith(_BIZ_PREFIXES)
or record.name == 'hypercorn'
or record.name.startswith('hypercorn.'))
class _AccessOnly(logging.Filter):
def filter(self, record):
return (record.name == 'hypercorn.access'
or record.name == 'werkzeug')
class _QuietPollFilter(logging.Filter):
_NOISY_PATHS = ('/api/chat/poll/', '/api/chat/stream/', '/api/browser/commands')
def filter(self, record):
msg = record.getMessage()
if any(p in msg for p in self._NOISY_PATHS) and '200' in msg:
return False
return True
_app_handler = TimedRotatingFileHandler(
os.path.join(LOG_DIR, 'app.log'),
when='midnight', backupCount=30, encoding='utf-8')
_app_handler.setFormatter(_formatter)
_app_handler.setLevel(logging.INFO)
_app_handler.addFilter(_BizOnly())
_access_handler = TimedRotatingFileHandler(
os.path.join(LOG_DIR, 'access.log'),
when='midnight', backupCount=14, encoding='utf-8')
_access_handler.setFormatter(_formatter)
_access_handler.setLevel(logging.INFO)
_access_handler.addFilter(_AccessOnly())
_error_handler = RotatingFileHandler(
os.path.join(LOG_DIR, 'error.log'),
maxBytes=5 * 1024 * 1024, backupCount=10, encoding='utf-8')
_error_handler.setFormatter(_formatter)
_error_handler.setLevel(logging.WARNING)
_error_handler.addFilter(_BizAndServerOnly())
_vendor_handler = RotatingFileHandler(
os.path.join(LOG_DIR, 'vendor.log'),
maxBytes=5 * 1024 * 1024, backupCount=3, encoding='utf-8')
_vendor_handler.setFormatter(_formatter)
_vendor_handler.setLevel(logging.WARNING)
_vendor_handler.addFilter(_VendorOnly())
_console_handler = logging.StreamHandler(sys.stderr)
_console_handler.setFormatter(_formatter)
_console_handler.setLevel(logging.WARNING)
_console_handler.addFilter(_BizAndServerOnly())
logging.basicConfig(
level=logging.INFO,
handlers=[_app_handler, _access_handler, _error_handler,
_vendor_handler, _console_handler],
)
_NOISY_LIBS = (
'courlan', 'htmldate', 'justext',
'urllib3', 'requests', 'charset_normalizer',
'websockets', 'websockets.client',
'PIL', 'pymupdf',
'httpcore', 'httpx',
)
for _lib_name in _NOISY_LIBS:
logging.getLogger(_lib_name).setLevel(logging.WARNING)
logging.getLogger('trafilatura').setLevel(logging.ERROR)
for _sub in ('trafilatura.xml', 'trafilatura.core', 'trafilatura.htmlprocessing',
'trafilatura.metadata'):
logging.getLogger(_sub).setLevel(logging.ERROR)
logging.getLogger('hypercorn.access').addFilter(_QuietPollFilter())
# ── Crash visibility: route uncaught exceptions to the log files ──
# faulthandler (top of file) covers C-level fatal signals, but an uncaught
# *Python* exception in the main thread otherwise reaches only the default
# excepthook → stderr, never app.log / error.log. Install a hook that logs
# it at CRITICAL (with traceback) before delegating to whatever hook was
# already installed (e.g. the bootstrap-delegation hook that re-execs to
# bootstrap.py on ImportError) — so we add visibility without clobbering it.
_prev_excepthook = sys.excepthook
def _crash_excepthook(exc_type, exc_value, exc_tb):
# Ctrl-C is a normal shutdown path, not a crash — don't scream about it.
if not issubclass(exc_type, KeyboardInterrupt):
try:
logging.getLogger('server').critical(
'Uncaught exception — process is terminating',
exc_info=(exc_type, exc_value, exc_tb))
except Exception:
pass # logging must never mask the original crash
(_prev_excepthook or sys.__excepthook__)(exc_type, exc_value, exc_tb)
sys.excepthook = _crash_excepthook
# ── Boot progress ──
_BOOT_T0 = _PROC_T0
_boot_logger = logging.getLogger('server.boot')
def _boot(msg, *args):
try:
line = msg % args if args else msg
except Exception:
line = msg
elapsed = time.time() - _BOOT_T0
sys.stderr.write('\033[36m[boot +%5.1fs]\033[0m %s\n' % (elapsed, line))
sys.stderr.flush()
_boot_logger.info('[boot +%.1fs] %s', elapsed, line)
_boot('🫧 Tofu (async) starting up — loading core modules…')
from lib.database import close_db, init_db, warmup_db
# ═══════════════════════════════════════════════════════════════════════
# Quart App
# ═══════════════════════════════════════════════════════════════════════
# Flask 3.1+ / newer Quart dropped PROVIDE_AUTOMATIC_OPTIONS from default
# config, but add_url_rule (called during __init__ for the static route)
# still reads it → KeyError. Inject it into the class defaults before
# instantiation so it's present from the very first add_url_rule call.
_orig_default_config = Quart.default_config
if 'PROVIDE_AUTOMATIC_OPTIONS' not in _orig_default_config:
Quart.default_config = {**_orig_default_config, 'PROVIDE_AUTOMATIC_OPTIONS': True}
app = Quart(__name__,
static_folder=os.path.join(BASE_DIR, 'static'),
static_url_path='/static')
# ── Flask secret key (reuse server.py logic) ──
def _load_or_create_flask_secret_key():
from lib.config_dir import config_path as _cfg_path
_env_key = os.environ.get('FLASK_SECRET_KEY', '').strip()
if _env_key:
return _env_key
_key_file = _cfg_path('flask_secret_key')
try:
if os.path.isfile(_key_file):
with open(_key_file, 'r', encoding='utf-8') as _kf:
_existing = _kf.read().strip()
if _existing:
return _existing
except Exception:
pass
_new_key = os.urandom(32).hex()
try:
os.makedirs(os.path.dirname(_key_file), exist_ok=True)
_flag = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
try:
_fd = os.open(_key_file, _flag, 0o600)
try:
os.write(_fd, _new_key.encode('utf-8'))
finally:
os.close(_fd)
except (AttributeError, OSError):
with open(_key_file, 'w', encoding='utf-8') as _kf:
_kf.write(_new_key)
except Exception as e:
logging.getLogger('server').warning('[FlaskSecret] Failed to persist: %s', e)
return _new_key
app.secret_key = _load_or_create_flask_secret_key()
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024
# ── Disable response/body timeouts for long-lived SSE streams ──
# Quart's defaults (60s) silently kill /api/chat/stream connections during
# long LLM responses, causing the UI to "stop updating without refresh".
# SSE clients keep their own keepalive (15s comment ping in chat_stream),
# so we set these to None to defer entirely to the SSE layer.
app.config['RESPONSE_TIMEOUT'] = None
app.config['BODY_TIMEOUT'] = None
# ── Compression (Quart-native, no flask-compress dependency) ──
# Quart does not use flask-compress. Instead, we add a simple
# after_request hook for gzip. For heavy production use, Hypercorn
# + a reverse proxy handle this better.
_COMPRESS_MIMETYPES = frozenset([
'text/html', 'text/css', 'text/javascript',
'application/javascript', 'application/json',
])
_COMPRESS_MIN_SIZE = 256
import gzip as _gzip
@app.after_request
async def _compress_response(response):
"""Simple gzip compression for eligible responses."""
# Skip SSE (buffering breaks streaming), small responses, already encoded
if (response.content_type
and 'text/event-stream' in response.content_type):
return response
if response.content_encoding:
return response
accept_enc = request.headers.get('Accept-Encoding', '')
if 'gzip' not in accept_enc:
return response
mime = (response.content_type or '').split(';')[0].strip()
if mime not in _COMPRESS_MIMETYPES:
return response
data = await response.get_data()
if len(data) < _COMPRESS_MIN_SIZE:
return response
# gzip is CPU-bound; running it inline would block the event loop (and
# every other connection / SSE keepalive) for the duration. Offload to
# the sync executor so a multi-MB body doesn't stall the whole server.
loop = asyncio.get_running_loop()
compressed = await loop.run_in_executor(None, _gzip.compress, data, 6)
if len(compressed) >= len(data):
return response
response.set_data(compressed)
response.headers['Content-Encoding'] = 'gzip'
response.headers['Content-Length'] = len(compressed)
response.headers.pop('Vary', None)
response.headers['Vary'] = 'Accept-Encoding'
return response
# ── Auth (legacy compat constants only) ──
# The active auth middleware lives in routes/api_v1/auth.py and is
# registered after blueprints are wired in. ``TUNNEL_TOKEN`` is kept
# only as a deprecated back-compat shim; new deployments mint API
# keys instead (see lib.api_keys.bootstrap_personal_key).
TUNNEL_TOKEN = os.environ.get('TUNNEL_TOKEN', '')
TUNNEL_COOKIE = '_tunnel_auth'
TUNNEL_COOKIE_MAX_AGE = 86400 * 30
if TUNNEL_TOKEN:
logging.getLogger('server.auth').warning(
'[Auth] TUNNEL_TOKEN is deprecated. Migrate to API keys '
'(POST /api/v1/keys with admin scope). The shim remains '
'for now but new code paths target the unified auth gate.')
# ── Method Override + CloudIDE JSON fix ──
@app.before_request
async def method_override():
override = request.args.get('_method')
if override:
request.scope['method'] = override.upper()
# CloudIDE sometimes double-encodes JSON bodies (sends a JSON string
# whose value is itself a JSON object). Detect and unwrap in-place so
# downstream ``request.get_json()`` returns the correct dict.
ct = request.content_type or ''
if request.method in ('POST', 'PUT') and 'json' in ct:
raw = (await request.get_data()).decode('utf-8', errors='replace')
if raw:
try:
data = json.loads(raw)
if isinstance(data, str):
corrected = json.dumps(json.loads(data)).encode('utf-8')
request._body = corrected
except (json.JSONDecodeError, TypeError):
pass
# ── Request lifecycle logging ──
from lib.log import get_logger, set_req_id, req_id as _get_req_id
import uuid as _uuid
_lifecycle_log = get_logger('server.lifecycle')
_QUIET_PREFIXES = ('/api/browser/', '/api/desktop/', '/static/', '/api/task/')
_SLOW_THRESHOLD_S = 2.0
@app.before_request
async def _assign_req_id_and_log():
rid = request.headers.get('X-Request-ID') or _uuid.uuid4().hex[:12]
set_req_id(rid)
request._start_time = time.time()
path = request.path
is_quiet = any(path.startswith(p) for p in _QUIET_PREFIXES)
level = logging.DEBUG if is_quiet else logging.INFO
_lifecycle_log.log(level, '[%s] → %s %s', rid, request.method, path)
@app.after_request
async def _log_response(response):
elapsed = time.time() - getattr(request, '_start_time', time.time())
rid = _get_req_id()
path = request.full_path.rstrip('?')
status = response.status_code
is_quiet = any(path.startswith(p) for p in _QUIET_PREFIXES)
if status >= 500:
_lifecycle_log.error('[%s] ← %s %s %d (%.3fs)', rid, request.method, path, status, elapsed)
elif status >= 400:
if status == 404 and request.path.startswith('/.well-known/'):
_lifecycle_log.debug('[%s] ← %s %s %d (%.3fs)', rid, request.method, path, status, elapsed)
else:
_lifecycle_log.warning('[%s] ← %s %s %d (%.3fs)', rid, request.method, path, status, elapsed)
elif elapsed >= _SLOW_THRESHOLD_S and not is_quiet:
_lifecycle_log.warning('[%s] ← %s %s %d SLOW (%.3fs)', rid, request.method, path, status, elapsed)
elif not is_quiet:
_lifecycle_log.info('[%s] ← %s %s %d (%.3fs)', rid, request.method, path, status, elapsed)
else:
_lifecycle_log.debug('[%s] ← %s %s %d (%.3fs)', rid, request.method, path, status, elapsed)
response.headers['X-Request-ID'] = rid
return response
@app.teardown_request
async def _clear_req_id(exc):
if exc:
rid = _get_req_id()
# Client disconnect mid-request (CancelledError during body read) is
# benign — log at debug. Real handler exceptions are already logged
# by _handle_uncaught with full context, so reaching teardown with
# any other exception means the framework swallowed it; warn so it's
# still visible without the alarming ERROR + traceback.
if isinstance(exc, asyncio.CancelledError):
_lifecycle_log.debug('[%s] Request teardown: client disconnected', rid)
else:
_lifecycle_log.warning('[%s] Request teardown with exception: %s', rid, exc)
set_req_id(None)
# ── DB teardown ──
app.teardown_appcontext(close_db)
# ── Install the tofu-search bridge (LLM + browser + auth seams) ──
# Must run before any search/fetch call; idempotent, re-synced on config reload.
from lib.search_bridge import install_search_bridge
install_search_bridge()
# ── Register all Blueprints ──
from routes import register_all
register_all(app)
# ── Unified auth gate (single middleware) ──
# Replaces the legacy dual scheme (tunnel_auth + bearer_auth). One
# before_request hook resolves an AuthContext from any of:
# - Authorization: Bearer / x-api-key header
# - tofu_session cookie (set on first browser visit via ?token=…)
# - X-Tunnel-Token / TUNNEL_TOKEN (deprecated back-compat shim)
# Public routes (static, /, /api/health, /api/v1/capabilities, etc.)
# bypass the gate — see _PUBLIC_EXACT in routes/api_v1/auth.py.
from routes.api_v1.auth import attach_rate_headers, auth_before_request
app.before_request(auth_before_request)
app.after_request(attach_rate_headers)
# ── First-boot personal key bootstrap ──
# Only relevant when the auth gate is in ``private`` or ``multi-user``
# mode. In ``open`` mode (the default for personal installs) no
# credential is required and minting a key would just confuse the
# operator. When in private/multi-user mode and the key store is
# empty AND no TUNNEL_TOKEN is configured, mint a personal admin key
# so the local UI and SDK "just work". The plaintext is printed once
# to stderr and persisted (0600) at data/config/.first_run_token.
# Disable with TOFU_AUTO_KEY=0.
_BOOTSTRAP_TOKEN = ''
try:
from lib.auth_mode import get_mode as _get_auth_mode
_AUTH_MODE = _get_auth_mode()
except Exception as _e:
logging.getLogger('server.boot').warning(
'[AuthMode] could not resolve mode: %s', _e)
_AUTH_MODE = 'open'
def _bootstrap_personal_key_if_needed():
global _BOOTSTRAP_TOKEN
if (os.environ.get('TOFU_AUTO_KEY', '1') or '1').strip() == '0':
return
if _AUTH_MODE == 'open':
return # gate is open — no credential needed at all
if TUNNEL_TOKEN:
return # legacy mode — user explicitly chose a shared secret
try:
from lib.api_keys import bootstrap_personal_key, has_any_key
except Exception as _e:
logging.getLogger('server.boot').warning(
'[Auth] could not import bootstrap helpers: %s', _e)
return
if has_any_key():
return
plaintext = bootstrap_personal_key(name='personal')
if plaintext:
_BOOTSTRAP_TOKEN = plaintext
_bootstrap_personal_key_if_needed()
# ── Billing janitor: release stale credit reservations ──
# Spawns one daemon thread that sweeps the ledger every 5 minutes.
# A no-op if multi-user mode never gets used (the sweep just finds 0
# rows). Disabled with TOFU_BILLING_JANITOR=0.
try:
from lib.billing.janitor import start_janitor as _start_billing_janitor
_start_billing_janitor()
except Exception as _e:
logging.getLogger('server.boot').warning(
'[Billing] janitor failed to start: %s', _e)
# ── Static file cache headers ──
@app.after_request
async def add_cache_headers(response):
if request.path.startswith('/static/'):
if request.path.endswith('.js'):
response.content_type = 'text/javascript; charset=utf-8'
elif request.path.endswith('.css'):
response.content_type = 'text/css; charset=utf-8'
if '/vendor/' in request.path or '/bundle-' in request.path:
response.headers['Cache-Control'] = 'public, max-age=31536000, immutable'
elif request.path.endswith(('.js', '.css')):
if 'v=' in request.query_string.decode('ascii', errors='ignore'):
response.headers['Cache-Control'] = 'public, max-age=604800, immutable'
else:
response.headers['Cache-Control'] = 'public, max-age=300, must-revalidate'
else:
response.headers['Cache-Control'] = 'public, max-age=86400'
return response
# ── Proxy config (reuse server.py logic) ──
try:
from routes.config import _read_server_config
from lib.proxy import set_bypass_domains, set_proxy_config
_saved_cfg = _read_server_config()
_saved_pc = _saved_cfg.get('proxy_config', {})
if _saved_pc and any(_saved_pc.get(k) for k in ('http_proxy', 'https_proxy')):
set_proxy_config(
http_proxy=_saved_pc.get('http_proxy', ''),
https_proxy=_saved_pc.get('https_proxy', ''),
)
_saved_proxy = _saved_cfg.get('proxy_bypass_domains', [])
if _saved_proxy:
set_bypass_domains(_saved_proxy)
except Exception as _e:
_lifecycle_log.warning('Failed to load proxy config: %s', _e)
# ── Global error handlers ──
from lib.api_response import (
api_internal_error,
api_method_not_allowed,
api_not_found,
api_payload_too_large,
)
def _is_api_request():
return request.path.startswith('/api/')
def _ws_safe_method_path():
"""Return (method, path) tolerating contexts where request is unavailable."""
try:
return request.method, request.path
except RuntimeError:
from quart import websocket as _ws
try:
return 'WS', _ws.path
except RuntimeError:
return '?', '?'
@app.errorhandler(404)
async def _handle_404(exc):
if request.path.startswith('/.well-known/'):
_lifecycle_log.debug('404 (well-known probe): %s', request.path)
else:
_lifecycle_log.warning('404 Not Found: %s %s', request.method, request.path)
if _is_api_request():
return api_not_found('Not Found: %s' % request.path)
return await _orig_make_response_async(
'<h2>404 — Not Found</h2><p>The requested URL was not found.</p>', 404)
@app.errorhandler(413)
async def _handle_413(exc):
if _is_api_request():
return api_payload_too_large(app.config['MAX_CONTENT_LENGTH'])
return await _orig_make_response_async('<h2>413 — Payload Too Large</h2>', 413)
@app.errorhandler(405)
async def _handle_405(exc):
if _is_api_request():
return api_method_not_allowed()
return await _orig_make_response_async('<h2>405 — Method Not Allowed</h2>', 405)
@app.errorhandler(500)
async def _handle_500(exc):
rid = _get_req_id() or '-'
method, path = _ws_safe_method_path()
_lifecycle_log.error('500 ISE: [%s] %s %s', rid, method, path, exc_info=exc)
if path.startswith('/api/'):
return api_internal_error(exc, log_traceback=False)
return await _orig_make_response_async(
f'<h2>500</h2><p>Request ID: <code>{rid}</code></p>', 500)
@app.errorhandler(Exception)
async def _handle_uncaught(exc):
from werkzeug.exceptions import HTTPException
if isinstance(exc, HTTPException):
return exc
rid = _get_req_id() or '-'
method, path = _ws_safe_method_path()
_lifecycle_log.error('[%s] Uncaught: %s %s: %s', rid, method, path, exc, exc_info=True)
if path.startswith('/api/'):
return api_internal_error(exc, log_traceback=False)
return await _orig_make_response_async(
f'<h2>500</h2><p>Request ID: <code>{rid}</code></p>', 500)
# ═══════════════════════════════════════════════════════════════════════
# Startup & Main
# ═══════════════════════════════════════════════════════════════════════
_server_log = logging.getLogger('server')
# ── JS bundle ──
try:
from lib.js_bundler import build_bundle
build_bundle()
except Exception as _bundle_err:
_server_log.warning('JS bundle build failed: %s', _bundle_err)
def _init_database():
"""Initialize database (runs in app context)."""
_boot('Initialising database…')
init_db()
warmup_db()
try:
from lib.database import heal_toast_corruption
heal_toast_corruption()
except Exception as e:
_server_log.warning('TOAST auto-heal failed: %s', e)
_boot('Database ready.')
try:
from lib.tasks_pkg import recover_stale_tasks_on_startup
recover_stale_tasks_on_startup()
except Exception as e:
_server_log.warning('Stale task recovery failed: %s', e)
# Resume swarm sub-agents that were mid-flight when the server stopped.
# DB-backed round-level resume (see lib/swarm/persistence.py): rehydrates
# each conversation-scoped session and re-spawns its unfinished agents
# from their checkpointed message history.
try:
from lib.swarm.integration import rehydrate_swarms_on_startup
rehydrate_swarms_on_startup()
except Exception as e:
_server_log.warning('Swarm rehydration failed: %s', e)
def _validate_imports():
"""Validate critical imports at startup."""
_CRITICAL_IMPORTS = [
'lib.tasks_pkg.orchestrator',
'lib.tasks_pkg.executor',
'tofu_search.fetch',
'tofu_search.search',
'lib.search_bridge',
'lib.llm',
]
_boot('Validating critical imports…')
failures = []
for mod_name in _CRITICAL_IMPORTS:
_boot(' • importing %s', mod_name)
try:
__import__(mod_name)
except ImportError as ie:
failures.append((mod_name, ie))
_server_log.error('Critical import failed: %s — %s', mod_name, ie)
if failures:
msgs = [f' {m}: {e}' for m, e in failures]
raise ImportError('Missing dependencies:\n' + '\n'.join(msgs))
_boot('All critical imports validated.')
# ── Eager-load heavy C extensions so mlockall pins their pages ──
# These are the .so modules seen in past SIGBUS faulthandler dumps.
# Loading them now (under mlockall MCL_FUTURE) ensures their code
# pages are resident before any request arrives — the demand-fault
# window that causes Bus errors on FUSE is eliminated.
_NATIVE_PRELOADS = [
'PIL._imaging',
'lxml.etree',
'greenlet._greenlet',
'yaml._yaml',
'numpy.core._multiarray_umath',
'markupsafe._speedups',
'charset_normalizer.md',
]
# These are optional — may not be installed in all environments
_NATIVE_PRELOADS_OPTIONAL = [
'pymupdf._extra',
'psycopg2._psycopg',
]
_boot('Eager-loading native extensions (FUSE SIGBUS mitigation)…')
for _mod in _NATIVE_PRELOADS:
try:
__import__(_mod)
except ImportError as _ie:
_server_log.warning('Native preload failed (required): %s — %s', _mod, _ie)
for _mod in _NATIVE_PRELOADS_OPTIONAL:
try:
__import__(_mod)
except ImportError:
pass # optional — not all deployments have these