-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathruntime_guards.py
More file actions
1677 lines (1565 loc) · 74.7 KB
/
Copy pathruntime_guards.py
File metadata and controls
1677 lines (1565 loc) · 74.7 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
"""Dependency-light process policies that must run before optional imports.
This module intentionally uses only the Python standard library. Importing a
``lib.*`` helper first executes ``lib/__init__.py`` and is therefore too late
for policies that protect the very beginning of ``server.py`` / healthcheck /
pytest collection.
"""
from __future__ import annotations
import os
import re
import shutil
import sys
from collections.abc import Mapping, MutableMapping
from dataclasses import dataclass, field
from pathlib import Path, PurePosixPath
from typing import TypedDict
from urllib.parse import parse_qs, urlsplit
__all__ = [
'DeploymentConfiguration',
'RESOURCE_BUDGET_AUTOMATIC_ENV',
'RESOURCE_BUDGET_ENV_KEYS',
'RESOURCE_BUDGET_POLICY_ENV',
'RESOURCE_BUDGET_POLICY_VERSION',
'ResourceBudgetManifest',
'SystemResourceSnapshot',
'deployment_resource_default',
'distributed_preview_is_read_only',
'enforce_deployment_configuration',
'install_process_resource_defaults',
'install_pymupdf_classic_policy',
'install_runtime_resource_defaults',
'load_deployment_configuration',
'probe_system_resources',
'resolve_deployment_mode',
'resource_budget_manifest',
'resolve_resource_budget',
'task_concurrency_hard_ceiling',
]
_DEPLOYMENT_MODES = frozenset({'personal', 'distributed'})
_PROCESS_ROLES = frozenset({'all', 'api', 'worker', 'scheduler'})
_REMOVED_DEPLOYMENT_ENV = (
'TOFU_REQUIRE_PG',
'TOFU_REPLICA_RING',
'TOFU_STORAGE_MODE',
)
_REPLICA_ID = re.compile(r'^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$')
_MIB = 1024 * 1024
_RESOURCE_FALLBACKS = {
'personal': {
'TOFU_MALLOC_ARENA_MAX': 1,
'TOFU_STORAGE_RPC_CAPACITY': 2,
'TOFU_STORAGE_RPC_INFLIGHT_MAX_MIB': 128,
'TOFU_STORAGE_SQLITE_READ_POOL': 2,
'TOFU_STORAGE_SQLITE_WRITER_QUEUE_CAPACITY': 8,
'TOFU_STORAGE_SQLITE_WRITER_CACHE_MIB': 32,
'TOFU_STORAGE_TURN_PROJECTION_CACHE_MIB': 16,
'TOFU_TURN_SEARCH_PROJECTION_MAX_MIB': 512,
'TOFU_BROWSER_STAGING_MAX_MIB': 256,
'TOFU_RAW_ARCHIVE_BUDGET_MIB': 256,
'TOFU_BROWSER_POLL_MAX_INFLIGHT': 8,
'TOFU_BROWSER_POLL_MAX_WAITERS': 8,
'TOFU_BROWSER_CLIENT_REGISTRY_CAPACITY': 64,
'TOFU_BROWSER_SESSION_LEASE_CAPACITY': 64,
'TOFU_BROWSER_POLL_BODY_MAX_MIB': 32,
'TOFU_MAX_SSE_PER_PRINCIPAL': 12,
'TOFU_RUN_PYTHON_CACHE_MAX_MIB': 64,
'TOFU_SERVER_PYTHON_CACHE_MAX_MIB': 64,
'TOFU_TOKEN_COUNT_CACHE_CAPACITY': 128,
'TOFU_USAGE_CACHE_CAPACITY': 128,
'TOFU_RATE_LIMIT_MEMORY_BUCKET_CAPACITY': 512,
'TOFU_TOOL_SEARCH_TERM_CACHE_CAPACITY': 512,
'TOFU_TOOL_RESULT_CACHE_CAPACITY': 64,
'TOFU_CHAT_TASK_TERMINAL_TTL_SECONDS': 600,
'TOFU_TIMER_LIVE_CAP': 8,
'TOFU_MEMORY_METADATA_CACHE_CAPACITY': 512,
'TOFU_MEMORY_METADATA_CACHE_MAX_MIB': 4,
'TOFU_PAPER_QA_SOURCE_CACHE_CAPACITY': 1,
'TOFU_TRANSLATE_CACHE_MAX_MIB': 128,
'TOFU_TRANSLATE_MAX_429_ATTEMPTS': 4,
'TOFU_TRANSLATE_WORKERS': 1,
'TOFU_TRANSLATE_QUEUE_CAPACITY': 4,
'TOFU_TRANSLATE_WORKER_IDLE_SECONDS': 60,
'TOFU_OPTIONAL_LLM_MAX_429_ATTEMPTS': 2,
'TOFU_PRODUCTION_LLM_FANOUT': 1,
'TOFU_PRODUCTION_LLM_MAX_429_ATTEMPTS': 4,
'TOFU_PRODUCTION_IMAGE_FANOUT': 1,
'TOFU_PRODUCTION_IMAGE_MAX_429_ATTEMPTS': 4,
'TOFU_PRODUCTION_TTS_FANOUT': 1,
'TOFU_PDF_PROCESSES': 1,
'TOFU_PDF_PARSE_CAPACITY': 3,
'TOFU_PDF_MAX_PAGES': 256,
'TOFU_PDF_MAX_TEXT_MIB': 2,
'TOFU_PDF_PARSE_TIMEOUT': 300,
'TOFU_PDF_WORKER_IDLE_SECONDS': 60,
'TOFU_PDF_VLM_TASK_WORKERS': 1,
'TOFU_PDF_VLM_QUEUE_CAPACITY': 2,
'TOFU_PDF_VLM_WORKER_IDLE_SECONDS': 60,
'TOFU_PDF_VLM_CALL_WORKERS': 1,
'TOFU_PDF_VLM_MAX_PAGES': 64,
'TOFU_PDF_VLM_TASK_TIMEOUT_SECONDS': 1920,
'TOFU_PDF_VLM_MAX_429_ATTEMPTS': 4,
'TOFU_KNOWLEDGE_ENRICH_WORKERS': 1,
'TOFU_KNOWLEDGE_ENRICH_OWNER_CAPACITY': 4,
'TOFU_KNOWLEDGE_ENRICH_WORKER_IDLE_SECONDS': 60,
'TOFU_SWARM_GLOBAL_WORKERS': 1,
'TOFU_SWARM_MAX_PARALLEL': 1,
'TOFU_SWARM_MAX_AGENTS_PER_WAVE': 2,
'TOFU_SWARM_MAX_AGENTS_PER_SESSION': 6,
'TOFU_SWARM_MAX_RETRIES': 1,
'TOFU_SWARM_SESSION_CAPACITY': 2,
'TOFU_CONTROL_RPC_WORKERS': 4,
'TOFU_PROJECT_REFRESH_QUEUE_CAPACITY': 16,
'TOFU_PROJECT_REFRESH_IDLE_SECONDS': 60,
'TOFU_PROJECT_UNDO_CACHE_CAPACITY': 64,
'TOFU_TREE_INDEX_WALK_JOBS': 2,
'TOFU_TREE_INDEX_MAX_ENTRIES': 100_000,
'TOFU_TREE_INDEX_MEM_ROOTS': 2,
'TOFU_INCREMENTAL_TRANSLATE_ACTIVE': 2,
'TOFU_INCREMENTAL_TRANSLATE_QUEUE_CAPACITY': 8,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_SEGMENTS': 32,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_DEADLINE_SECONDS': 30,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_MIN_CHARS': 256,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_MAX_429_ATTEMPTS': 1,
'TOFU_MAX_INFLIGHT_TASKS': 1,
'TOFU_TASK_RSS_RESERVE_MB': 1024,
'TOFU_SYNC_WORKERS': 2,
'TOFU_AGENT_WORKERS': 1,
'TOOL_MAX_PARALLEL_WORKERS': 1,
'TOFU_NUMERIC_THREADS': 1,
'TOFU_MCP_CRED_PROBE_WORKERS': 1,
'TOFU_MCP_STDIO_IDLE_SECONDS': 300,
'TOFU_EXECUTOR_IDLE_SECONDS': 600,
'TOFU_LOG_TOTAL_BUDGET_MB': 128,
'TOFU_STORAGE_MIN_FREE_BYTES': 256 * _MIB,
'TOFU_STORAGE_RECOVERY_COPY_BUDGET_MIB': 64 * 1024,
'TOFU_STORAGE_SQLITE_BACKUP_TIMEOUT_SECONDS': 5896,
'TOFU_STORAGE_FASTPATH_WAL_REBASE_MAX_MIB': 512,
'TOFU_ATTEMPT_EVENT_TTL_DAYS': 1,
'TOFU_PROCESS_RSS_RELIEF_MB': 1024,
'TOFU_PROCESS_RSS_RECYCLE_MB': 1536,
},
'distributed': {
'TOFU_MALLOC_ARENA_MAX': 8,
'TOFU_STORAGE_RPC_CAPACITY': 64,
'TOFU_STORAGE_RPC_INFLIGHT_MAX_MIB': 1024,
'TOFU_STORAGE_SQLITE_READ_POOL': 16,
'TOFU_STORAGE_SQLITE_WRITER_QUEUE_CAPACITY': 128,
'TOFU_STORAGE_SQLITE_WRITER_CACHE_MIB': 64,
'TOFU_STORAGE_TURN_PROJECTION_CACHE_MIB': 256,
'TOFU_TURN_SEARCH_PROJECTION_MAX_MIB': 1024,
'TOFU_BROWSER_STAGING_MAX_MIB': 4096,
'TOFU_RAW_ARCHIVE_BUDGET_MIB': 4096,
'TOFU_BROWSER_POLL_MAX_INFLIGHT': 128,
'TOFU_BROWSER_POLL_MAX_WAITERS': 128,
'TOFU_BROWSER_CLIENT_REGISTRY_CAPACITY': 2048,
'TOFU_BROWSER_SESSION_LEASE_CAPACITY': 2048,
'TOFU_BROWSER_POLL_BODY_MAX_MIB': 64,
'TOFU_MAX_SSE_PER_PRINCIPAL': 64,
'TOFU_RUN_PYTHON_CACHE_MAX_MIB': 128,
'TOFU_SERVER_PYTHON_CACHE_MAX_MIB': 128,
'TOFU_TOKEN_COUNT_CACHE_CAPACITY': 1024,
'TOFU_USAGE_CACHE_CAPACITY': 4096,
'TOFU_RATE_LIMIT_MEMORY_BUCKET_CAPACITY': 4096,
'TOFU_TOOL_SEARCH_TERM_CACHE_CAPACITY': 4096,
'TOFU_TOOL_RESULT_CACHE_CAPACITY': 512,
'TOFU_CHAT_TASK_TERMINAL_TTL_SECONDS': 3600,
'TOFU_TIMER_LIVE_CAP': 64,
'TOFU_MEMORY_METADATA_CACHE_CAPACITY': 8192,
'TOFU_MEMORY_METADATA_CACHE_MAX_MIB': 64,
'TOFU_PAPER_QA_SOURCE_CACHE_CAPACITY': 8,
'TOFU_TRANSLATE_CACHE_MAX_MIB': 1024,
'TOFU_TRANSLATE_MAX_429_ATTEMPTS': 16,
'TOFU_TRANSLATE_WORKERS': 16,
'TOFU_TRANSLATE_QUEUE_CAPACITY': 128,
'TOFU_TRANSLATE_WORKER_IDLE_SECONDS': 600,
'TOFU_OPTIONAL_LLM_MAX_429_ATTEMPTS': 8,
'TOFU_PRODUCTION_LLM_FANOUT': 4,
'TOFU_PRODUCTION_LLM_MAX_429_ATTEMPTS': 16,
'TOFU_PRODUCTION_IMAGE_FANOUT': 4,
'TOFU_PRODUCTION_IMAGE_MAX_429_ATTEMPTS': 16,
'TOFU_PRODUCTION_TTS_FANOUT': 4,
'TOFU_PDF_PROCESSES': 4,
'TOFU_PDF_PARSE_CAPACITY': 16,
'TOFU_PDF_MAX_PAGES': 2048,
'TOFU_PDF_MAX_TEXT_MIB': 16,
'TOFU_PDF_PARSE_TIMEOUT': 3600,
'TOFU_PDF_WORKER_IDLE_SECONDS': 600,
'TOFU_PDF_VLM_TASK_WORKERS': 4,
'TOFU_PDF_VLM_QUEUE_CAPACITY': 32,
'TOFU_PDF_VLM_WORKER_IDLE_SECONDS': 600,
'TOFU_PDF_VLM_CALL_WORKERS': 8,
'TOFU_PDF_VLM_MAX_PAGES': 512,
'TOFU_PDF_VLM_TASK_TIMEOUT_SECONDS': 14_400,
'TOFU_PDF_VLM_MAX_429_ATTEMPTS': 16,
'TOFU_KNOWLEDGE_ENRICH_WORKERS': 8,
'TOFU_KNOWLEDGE_ENRICH_OWNER_CAPACITY': 128,
'TOFU_KNOWLEDGE_ENRICH_WORKER_IDLE_SECONDS': 600,
'TOFU_SWARM_GLOBAL_WORKERS': 16,
'TOFU_SWARM_MAX_PARALLEL': 8,
'TOFU_SWARM_MAX_AGENTS_PER_WAVE': 16,
'TOFU_SWARM_MAX_AGENTS_PER_SESSION': 64,
'TOFU_SWARM_MAX_RETRIES': 2,
'TOFU_SWARM_SESSION_CAPACITY': 32,
'TOFU_CONTROL_RPC_WORKERS': 32,
'TOFU_PROJECT_REFRESH_QUEUE_CAPACITY': 512,
'TOFU_PROJECT_REFRESH_IDLE_SECONDS': 600,
'TOFU_PROJECT_UNDO_CACHE_CAPACITY': 512,
'TOFU_TREE_INDEX_WALK_JOBS': 16,
'TOFU_TREE_INDEX_MAX_ENTRIES': 600_000,
'TOFU_TREE_INDEX_MEM_ROOTS': 4,
'TOFU_INCREMENTAL_TRANSLATE_ACTIVE': 32,
'TOFU_INCREMENTAL_TRANSLATE_QUEUE_CAPACITY': 64,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_SEGMENTS': 256,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_DEADLINE_SECONDS': 60,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_MIN_CHARS': 256,
'TOFU_INCREMENTAL_TRANSLATE_PREVIEW_MAX_429_ATTEMPTS': 1,
'TOFU_MAX_INFLIGHT_TASKS': 16,
'TOFU_TASK_RSS_RESERVE_MB': 512,
'TOFU_SYNC_WORKERS': 16,
'TOFU_AGENT_WORKERS': 16,
'TOOL_MAX_PARALLEL_WORKERS': 8,
'TOFU_NUMERIC_THREADS': 4,
'TOFU_MCP_CRED_PROBE_WORKERS': 8,
'TOFU_MCP_STDIO_IDLE_SECONDS': 1800,
'TOFU_EXECUTOR_IDLE_SECONDS': 3600,
'TOFU_LOG_TOTAL_BUDGET_MB': 512,
'TOFU_STORAGE_MIN_FREE_BYTES': 1024 * _MIB,
'TOFU_STORAGE_RECOVERY_COPY_BUDGET_MIB': 1024 * 1024,
'TOFU_STORAGE_SQLITE_BACKUP_TIMEOUT_SECONDS': 21600,
'TOFU_STORAGE_FASTPATH_WAL_REBASE_MAX_MIB': 16_384,
'TOFU_ATTEMPT_EVENT_TTL_DAYS': 7,
'TOFU_PROCESS_RSS_RELIEF_MB': 4096,
'TOFU_PROCESS_RSS_RECYCLE_MB': 8192,
},
}
_RESOURCE_NAMES = tuple(_RESOURCE_FALLBACKS['personal'])
RESOURCE_BUDGET_ENV_KEYS = frozenset(_RESOURCE_NAMES)
RESOURCE_BUDGET_POLICY_VERSION = '2026-08-31.2'
RESOURCE_BUDGET_POLICY_ENV = 'TOFU_RESOURCE_BUDGET_POLICY_VERSION'
RESOURCE_BUDGET_AUTOMATIC_ENV = 'TOFU_RESOURCE_BUDGET_AUTOMATIC_DEFAULTS'
_RESOURCE_SNAPSHOT_CACHE: dict[str, 'SystemResourceSnapshot'] = {}
class ResourceBudgetManifest(TypedDict):
policy_version: str
deployment_mode: str
adaptive: bool
probe: dict[str, object]
defaults: dict[str, int]
overrides: dict[str, str]
automatic: list[str]
@dataclass(frozen=True, slots=True)
class SystemResourceSnapshot:
"""One bounded, dependency-free view of resources visible to this process.
Values use MiB and logical CPUs. ``None`` means that the platform did not
expose a trustworthy value; budget derivation then uses the conservative
personal fallback instead of guessing from a server-sized host value.
"""
host_cpu_count: int
affinity_cpu_count: int | None
cgroup_cpu_count: int | None
effective_cpu_count: int
host_memory_total_mb: int | None
host_memory_available_mb: int | None
cgroup_memory_limit_mb: int | None
cgroup_memory_current_mb: int | None
effective_memory_capacity_mb: int | None
effective_memory_available_mb: int | None
disk_total_mb: int | None
disk_free_mb: int | None
def as_dict(self) -> dict[str, int | None]:
return {
field_name: getattr(self, field_name)
for field_name in self.__dataclass_fields__
}
def _read_first_text(paths: tuple[str, ...]) -> str | None:
for raw_path in paths:
try:
return Path(raw_path).read_text(encoding='utf-8').strip()
except (OSError, UnicodeError):
continue
return None
def _finite_positive_bytes(raw: str | None) -> int | None:
try:
value = int(raw or '')
except (TypeError, ValueError, OverflowError):
return None
# cgroup v1 represents unlimited memory with a huge page-aligned sentinel.
return value if 0 < value < (1 << 60) else None
def _decode_mount_path(value: str) -> str:
for encoded, decoded in (
('\\040', ' '), ('\\011', '\t'), ('\\012', '\n'), ('\\134', '\\')):
value = value.replace(encoded, decoded)
return value
def _cgroup_file_paths(
controller: str,
filename: str,
fallbacks: tuple[str, ...],
) -> tuple[str, ...]:
"""Resolve this process's v1/v2 cgroup file before fixed legacy paths."""
memberships: list[tuple[bool, str]] = []
membership_text = _read_first_text(('/proc/self/cgroup',)) or ''
for line in membership_text.splitlines():
fields = line.split(':', 2)
if len(fields) != 3:
continue
controllers = {item for item in fields[1].split(',') if item}
if not controllers or controller in controllers:
memberships.append((not controllers, fields[2] or '/'))
resolved: list[str] = []
mount_text = _read_first_text(('/proc/self/mountinfo',)) or ''
for line in mount_text.splitlines():
before, separator, after = line.partition(' - ')
if not separator:
continue
mount_fields = before.split()
fs_fields = after.split()
if len(mount_fields) < 5 or len(fs_fields) < 3:
continue
filesystem = fs_fields[0]
mount_controllers = set(fs_fields[2].split(','))
for unified, membership in memberships:
if unified != (filesystem == 'cgroup2'):
continue
if not unified and (
filesystem != 'cgroup'
or controller not in mount_controllers):
continue
root = PurePosixPath(_decode_mount_path(mount_fields[3]))
member = PurePosixPath(membership)
try:
relative = member.relative_to(root)
except ValueError:
continue
mountpoint = Path(_decode_mount_path(mount_fields[4]))
resolved.append(str(mountpoint / Path(str(relative)) / filename))
return tuple(dict.fromkeys([*resolved, *fallbacks]))
def _cgroup_cpu_count() -> int | None:
v2 = _read_first_text(_cgroup_file_paths(
'cpu', 'cpu.max', ('/sys/fs/cgroup/cpu.max',)))
if v2:
fields = v2.split()
if len(fields) >= 2 and fields[0] != 'max':
try:
quota, period = int(fields[0]), int(fields[1])
if quota > 0 and period > 0:
return max(1, quota // period)
except (TypeError, ValueError, OverflowError):
pass
quota = _finite_positive_bytes(_read_first_text(_cgroup_file_paths(
'cpu', 'cpu.cfs_quota_us', (
'/sys/fs/cgroup/cpu/cpu.cfs_quota_us',
'/sys/fs/cgroup/cpu.cfs_quota_us',
))))
period = _finite_positive_bytes(_read_first_text(_cgroup_file_paths(
'cpu', 'cpu.cfs_period_us', (
'/sys/fs/cgroup/cpu/cpu.cfs_period_us',
'/sys/fs/cgroup/cpu.cfs_period_us',
))))
if quota is None or period is None:
return None
return max(1, quota // period)
def _host_memory_bytes() -> tuple[int | None, int | None]:
"""Return host total/available memory without importing psutil."""
total = None
available = None
text = _read_first_text(('/proc/meminfo',))
if text:
values: dict[str, int] = {}
for line in text.splitlines():
name, separator, raw_value = line.partition(':')
if not separator:
continue
fields = raw_value.split()
try:
values[name] = int(fields[0]) * 1024
except (IndexError, TypeError, ValueError, OverflowError):
continue
total = values.get('MemTotal')
available = values.get('MemAvailable')
def _sysconf_bytes(page_name: str) -> int | None:
try:
pages = int(os.sysconf(page_name))
page_size = int(os.sysconf('SC_PAGE_SIZE'))
except (AttributeError, OSError, TypeError, ValueError, OverflowError):
return None
value = pages * page_size
return value if value > 0 else None
total = total or _sysconf_bytes('SC_PHYS_PAGES')
available = available or _sysconf_bytes('SC_AVPHYS_PAGES')
if os.name == 'nt' and (total is None or available is None):
try:
import ctypes
class _MemoryStatus(ctypes.Structure):
_fields_ = [
('length', ctypes.c_ulong),
('memory_load', ctypes.c_ulong),
('total_physical', ctypes.c_ulonglong),
('available_physical', ctypes.c_ulonglong),
('total_page_file', ctypes.c_ulonglong),
('available_page_file', ctypes.c_ulonglong),
('total_virtual', ctypes.c_ulonglong),
('available_virtual', ctypes.c_ulonglong),
('available_extended_virtual', ctypes.c_ulonglong),
]
status = _MemoryStatus()
status.length = ctypes.sizeof(status)
if ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(status)):
total = total or int(status.total_physical)
available = available or int(status.available_physical)
except (AttributeError, OSError, TypeError, ValueError):
pass
return total, available
def _cgroup_memory_bytes() -> tuple[int | None, int | None]:
v2_limit = _read_first_text(_cgroup_file_paths(
'memory', 'memory.max', ('/sys/fs/cgroup/memory.max',)))
v1_limit = _read_first_text(_cgroup_file_paths(
'memory', 'memory.limit_in_bytes', (
'/sys/fs/cgroup/memory/memory.limit_in_bytes',)))
limit = _finite_positive_bytes(v2_limit or v1_limit)
v2_current = _read_first_text(_cgroup_file_paths(
'memory', 'memory.current', ('/sys/fs/cgroup/memory.current',)))
v1_current = _read_first_text(_cgroup_file_paths(
'memory', 'memory.usage_in_bytes', (
'/sys/fs/cgroup/memory/memory.usage_in_bytes',)))
current = _finite_positive_bytes(v2_current or v1_current)
return limit, current
def _persistent_data_path(environment: Mapping[str, str]) -> Path:
"""Mirror the stdlib-visible data-layout choice without importing lib.*."""
raw_path = (environment.get('TOFU_PROJECT_PATH') or '').strip()
root = Path(raw_path) if raw_path else Path(__file__).resolve().parent
explicit_data = (environment.get('TOFU_DATA_DIR') or '').strip()
if explicit_data:
configured = Path(os.path.abspath(explicit_data))
return (
configured if configured.name == 'data'
else configured / 'data')
layout = (environment.get('TOFU_DATA_LAYOUT') or 'auto').strip().lower()
intree_data = root / 'data'
if layout == 'intree':
return intree_data
if sys.platform.startswith('win'):
user_base = Path(
environment.get('LOCALAPPDATA') or os.path.expanduser('~')) / 'Tofu'
elif sys.platform == 'darwin':
user_base = Path(os.path.expanduser('~')) / 'Library' \
/ 'Application Support' / 'Tofu'
else:
user_base = Path(
environment.get('XDG_DATA_HOME')
or Path(os.path.expanduser('~')) / '.local' / 'share') / 'Tofu'
per_user_data = user_base / 'data'
if layout == 'xdg':
return per_user_data
# runtime_paths treats unknown values as auto: preserve populated legacy
# installs in place, while a fresh source checkout uses the per-user root.
try:
with os.scandir(intree_data) as entries:
if next(entries, None) is not None:
return intree_data
except OSError:
pass
return per_user_data
def _project_disk_bytes(
environment: Mapping[str, str],
) -> tuple[int | None, int | None]:
data_path = _persistent_data_path(environment)
# Persistent data is commonly a separate Docker/NAS mount; probing the
# source/overlay filesystem would report capacity the SQLite authority
# cannot actually use.
path = data_path
while True:
try:
usage = shutil.disk_usage(path)
return int(usage.total), int(usage.free)
except (FileNotFoundError, NotADirectoryError):
parent = path.parent
if parent == path:
break
path = parent
except (OSError, TypeError, ValueError, OverflowError):
break
return None, None
def probe_system_resources(
environment: Mapping[str, str] | None = None,
*,
refresh: bool = False,
) -> SystemResourceSnapshot:
"""Probe effective personal-computer resources with safe platform fallbacks.
CPU capacity is the minimum of host count, process affinity/cpuset, and a
finite cgroup quota. Memory capacity/headroom are likewise the minimum of
host and cgroup views. The result is cached per resolved data path so every
default selected during one process boot comes from the same observation.
"""
env = os.environ if environment is None else environment
cache_key = str(_persistent_data_path(env))
if not refresh and cache_key in _RESOURCE_SNAPSHOT_CACHE:
return _RESOURCE_SNAPSHOT_CACHE[cache_key]
try:
host_cpus = max(1, int(os.cpu_count() or 1))
except (TypeError, ValueError, OverflowError):
host_cpus = 1
try:
affinity_cpus = max(1, len(os.sched_getaffinity(0)))
except (AttributeError, OSError, TypeError, ValueError):
affinity_cpus = None
cgroup_cpus = _cgroup_cpu_count()
effective_cpus = min(
value for value in (host_cpus, affinity_cpus, cgroup_cpus)
if value is not None)
host_total, host_available = _host_memory_bytes()
cgroup_limit, cgroup_current = _cgroup_memory_bytes()
capacity_candidates = [
value for value in (host_total, cgroup_limit)
if value is not None]
effective_capacity = min(capacity_candidates) if capacity_candidates else None
cgroup_is_explicit_budget = bool(
cgroup_limit is not None
and (host_total is None or cgroup_limit < host_total * 0.90))
cgroup_available = (
max(0, cgroup_limit - cgroup_current)
if cgroup_is_explicit_budget and cgroup_current is not None else
cgroup_limit if cgroup_is_explicit_budget else None)
available_candidates = [
value for value in (host_available, cgroup_available)
if value is not None]
effective_available = min(available_candidates) if available_candidates else None
if effective_capacity is not None and effective_available is not None:
effective_available = min(effective_available, effective_capacity)
disk_total, disk_free = _project_disk_bytes(env)
def _mb(value: int | None) -> int | None:
return max(0, value // _MIB) if value is not None else None
snapshot = SystemResourceSnapshot(
host_cpu_count=host_cpus,
affinity_cpu_count=affinity_cpus,
cgroup_cpu_count=cgroup_cpus,
effective_cpu_count=max(1, effective_cpus),
host_memory_total_mb=_mb(host_total),
host_memory_available_mb=_mb(host_available),
cgroup_memory_limit_mb=_mb(cgroup_limit),
cgroup_memory_current_mb=_mb(cgroup_current),
effective_memory_capacity_mb=_mb(effective_capacity),
effective_memory_available_mb=_mb(effective_available),
disk_total_mb=_mb(disk_total),
disk_free_mb=_mb(disk_free),
)
_RESOURCE_SNAPSHOT_CACHE[cache_key] = snapshot
return snapshot
def _task_slots_from_rss_budget(
hard_rss_mb: int,
task_reserve_mb: int,
) -> int:
"""Return task slots that fit beside the non-task process working set."""
if hard_rss_mb <= 0:
return 256
reserve = max(64, int(task_reserve_mb))
# Imports, route state, storage clients, terminal settlement, and transient
# response copies need a quarter of the process budget (at least 512 MiB)
# even with zero active agents. Tasks consume only the remainder.
process_baseline_mb = max(512, int(hard_rss_mb) // 4)
task_budget_mb = max(0, int(hard_rss_mb) - process_baseline_mb)
return max(1, task_budget_mb // reserve)
def _personal_resource_defaults(
snapshot: SystemResourceSnapshot,
) -> dict[str, int]:
"""Derive useful concurrency while preserving OS/browser headroom."""
cpus = max(1, snapshot.effective_cpu_count)
capacity_known = snapshot.effective_memory_capacity_mb is not None
capacity_mb = snapshot.effective_memory_capacity_mb or 4096
available_mb = snapshot.effective_memory_available_mb
# One concurrency unit needs 2 GiB of installed/effective capacity and at
# least 1 GiB that is currently available. This prevents a large but busy
# workstation from being treated like an empty dedicated server.
memory_units = (
max(1, (capacity_mb + 1023) // 2048) if capacity_known else 1)
if available_mb is not None:
memory_units = min(
memory_units, max(1, (available_mb + 511) // 1024))
cgroup_is_explicit_budget = bool(
snapshot.cgroup_memory_limit_mb
and snapshot.host_memory_total_mb
and snapshot.cgroup_memory_limit_mb
< snapshot.host_memory_total_mb * 0.90)
hard_fraction = 0.70 if cgroup_is_explicit_budget else 0.375
hard_floor_mb = min(1536, max(768, int(capacity_mb * 0.50)))
# Scale the worker envelope on actual servers instead of pinning every
# personal deployment to the old 6 GiB workstation ceiling. The fraction
# still reserves most host memory for the OS/browser (or 30% of an explicit
# application cgroup), while the absolute 64 GiB ceiling prevents a huge
# host probe from creating an unbounded single-process budget.
hard_rss_mb = max(
hard_floor_mb, min(64 * 1024, int(capacity_mb * hard_fraction)))
if available_mb is not None:
hard_rss_mb = min(
hard_rss_mb, max(hard_floor_mb, int(available_mb * 0.75)))
soft_target_mb = (
int(capacity_mb * 0.50)
if cgroup_is_explicit_budget else
int(hard_rss_mb * (2.0 / 3.0)))
if available_mb is not None:
soft_target_mb = min(soft_target_mb, int(available_mb * 0.50))
soft_rss_mb = max(512, soft_target_mb)
if hard_rss_mb - soft_rss_mb < 256:
soft_rss_mb = max(512, hard_rss_mb - 256)
# A 3 GiB worker on the 8 GiB reference computer historically sustains
# four ordinary tasks with a 512 MiB live-state envelope. Larger worker
# envelopes reserve 1 GiB per root so task concurrency scales only with the
# memory the process is actually allowed to retain.
task_rss_reserve_mb = (
512 if capacity_known and hard_rss_mb <= 3072 else
_RESOURCE_FALLBACKS['personal']['TOFU_TASK_RSS_RESERVE_MB'])
rss_task_units = _task_slots_from_rss_budget(
hard_rss_mb, task_rss_reserve_mb)
# The absolute 64-GiB worker envelope minus its 25% process baseline fits
# at most 48 default 1-GiB root tasks. Align this CPU/capacity ceiling to
# that measured RSS boundary so a large personal server is not stranded at
# an unrelated 32-task cap; smaller hosts remain constrained first by CPU,
# current memory headroom, and ``rss_task_units`` below.
general_parallelism = max(1, min(48, cpus, memory_units))
task_parallelism = max(
1, min(general_parallelism, rss_task_units))
# This is a PER-TASK fan-out pool. Letting it grow with root-task count
# multiplies threads quadratically on a large server, so retain a separate
# four-way ceiling while root concurrency consumes the wider host budget.
tool_parallelism = max(1, min(4, cpus, memory_units))
io_parallelism = max(2, min(12, cpus * 2, memory_units * 2))
# storage.v1 permits one 64 MiB frame so historical slot-only admission
# exposed ``rpc_capacity * 64 MiB`` of serialized buffers. Give each
# launch-probed memory unit 32 MiB while retaining room for at least one
# maximum request and one maximum response. The Sidecar consumes this as
# one process-wide weighted budget, independently of handler count.
rpc_inflight_max_mib = (
_RESOURCE_FALLBACKS['personal'][
'TOFU_STORAGE_RPC_INFLIGHT_MAX_MIB']
if not capacity_known else
max(128, min(512, memory_units * 32))
)
sync_workers = max(2, min(16, cpus * 2, memory_units * 2))
# Browser polls are async and normally retain only one small coroutine per
# installed device. Keep enough headroom for several personal computers
# and result-flush overlap, while bounding malicious unique-client churn.
# The request-body cap below is an independent multiplier, so the inflight
# ceiling deliberately grows more slowly than the general sync pool.
browser_poll_max_inflight = max(8, min(16, sync_workers * 2))
browser_poll_max_waiters = browser_poll_max_inflight
browser_client_registry_capacity = max(
64, min(256, browser_poll_max_waiters * 8))
if not capacity_known:
browser_poll_body_max_mib = _RESOURCE_FALLBACKS['personal'][
'TOFU_BROWSER_POLL_BODY_MAX_MIB']
max_sse_per_principal = _RESOURCE_FALLBACKS['personal'][
'TOFU_MAX_SSE_PER_PRINCIPAL']
else:
browser_poll_body_max_mib = max(16, min(32, capacity_mb // 256))
if available_mb is not None:
browser_poll_body_max_mib = min(
browser_poll_body_max_mib,
max(16, available_mb // 128),
)
max_sse_per_principal = max(
8, min(24, general_parallelism * 3))
numeric_threads = max(
1, min(4, cpus, max(1, (memory_units + 1) // 2)))
# Local MCP stdio servers are optional helper processes, but each npm/uv
# launcher and its child can retain tens to hundreds of MiB while idle.
# Keep a longer warm window when the personal computer has comfortable
# capacity; reclaim aggressively on the 8 GiB reference machine. The
# bridge retains the discovered catalog and reconnects transparently, so
# this changes transport residency rather than tool availability.
if not capacity_known:
mcp_stdio_idle_seconds = _RESOURCE_FALLBACKS['personal'][
'TOFU_MCP_STDIO_IDLE_SECONDS']
elif capacity_mb <= 8 * 1024 or (
available_mb is not None and available_mb <= 4 * 1024):
mcp_stdio_idle_seconds = 180
elif capacity_mb <= 16 * 1024:
mcp_stdio_idle_seconds = 300
else:
mcp_stdio_idle_seconds = 600
# Serving-loop executors remain at full historical high-water thread count
# forever unless their owner rotates them. Preserve a modest warm window;
# smaller personal machines benefit sooner from releasing thread stacks
# and per-thread allocator caches after a burst.
if not capacity_known:
executor_idle_seconds = _RESOURCE_FALLBACKS['personal'][
'TOFU_EXECUTOR_IDLE_SECONDS']
elif capacity_mb <= 8 * 1024 or (
available_mb is not None and available_mb <= 4 * 1024):
executor_idle_seconds = 300
elif capacity_mb <= 16 * 1024:
executor_idle_seconds = 600
else:
executor_idle_seconds = 1800
# Project status/watch/summary refreshes are reconstructible and arrive in
# short event bursts. Thread creation is negligible next to their storage
# and optional LLM work, so keep only a short warm window on personal
# machines while distributed replicas can favor steadier throughput.
if not capacity_known:
project_refresh_idle_seconds = _RESOURCE_FALLBACKS['personal'][
'TOFU_PROJECT_REFRESH_IDLE_SECONDS']
elif capacity_mb <= 8 * 1024 or (
available_mb is not None and available_mb <= 4 * 1024):
project_refresh_idle_seconds = 30
elif capacity_mb <= 16 * 1024:
project_refresh_idle_seconds = 60
else:
project_refresh_idle_seconds = 300
# VLM PDF transcription retains compressed source bytes and rendered page
# images, then fans out paid model calls. Bound pages before rendering and
# grant a second whole-document worker only when the launch probe shows
# substantial task headroom. Thirty seconds per admitted page gives the
# task deadline a conservative finite envelope across all batches.
if not capacity_known:
vlm_max_pages = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_VLM_MAX_PAGES']
else:
vlm_memory_mb = min(
capacity_mb,
available_mb if available_mb is not None else capacity_mb,
)
if vlm_memory_mb <= 4096:
vlm_max_pages = 64
elif vlm_memory_mb <= 8192:
vlm_max_pages = 128
elif vlm_memory_mb <= 16 * 1024:
vlm_max_pages = 192
else:
vlm_max_pages = 256
vlm_task_timeout_seconds = max(
1800, min(7200, vlm_max_pages * 30))
# Classic extraction retains the compressed source, parser-native state,
# Markdown output, and an IPC copy when it runs in the process pool. Keep
# the 8 GiB reference machine at one worker / three unfinished documents,
# while larger personal machines grow only to two workers. Page and text
# ceilings bound CPU and durable/context amplification independently of the
# historical 200 MiB compressed-input gate.
if not capacity_known:
classic_pdf_processes = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_PROCESSES']
classic_pdf_capacity = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_PARSE_CAPACITY']
classic_pdf_max_pages = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_MAX_PAGES']
classic_pdf_max_text_mib = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_MAX_TEXT_MIB']
classic_pdf_timeout = _RESOURCE_FALLBACKS['personal'][
'TOFU_PDF_PARSE_TIMEOUT']
else:
classic_pdf_processes = 2 if task_parallelism >= 16 else 1
classic_pdf_capacity = max(3, classic_pdf_processes * 3)
# Classic extraction does not render a page-image batch or retain paid
# call inputs. Scale its page budget separately from VLM: installed
# capacity grants one page per 16 MiB and current headroom grants one
# per 8 MiB. Thus the 8/4 GiB reference machine keeps 512 pages while
# a 4/2 GiB host falls back to 256.
classic_pdf_max_pages = max(
256, min(1024, capacity_mb // 16))
if available_mb is not None:
classic_pdf_max_pages = min(
classic_pdf_max_pages,
max(256, available_mb // 8),
)
classic_pdf_max_text_mib = max(
2, min(8, (classic_pdf_max_pages + 127) // 128))
classic_pdf_timeout = max(
300, min(1800, classic_pdf_max_pages * 2))
# Swarm agents outlive the root turn and historically created one private
# thread/API pool per conversation. Derive both the process-wide expensive
# execution ceiling and each session's smaller share from the root-task
# envelope, then bound accepted waves/results separately. On the 8 GiB
# reference host this is two executing agents, four agents per wave, and
# twelve total agents in one live session.
swarm_global_workers = max(
1, min(4, max(1, (task_parallelism + 1) // 2)))
swarm_max_agents_per_wave = max(
2, min(8, swarm_global_workers * 2))
swarm_max_agents_per_session = max(
6, min(24, swarm_max_agents_per_wave * 3))
swarm_session_capacity = max(
2, min(8, task_parallelism))
# SQLite's default cache is only 2 MiB per connection. That is adequate
# for bounded readers, but the sole writer repeatedly touches hot indexes
# from every domain; evicting those pages turns a small UPSERT into random
# filesystem reads. Give only the writer an adaptive, hard-capped cache.
# On the 8 GiB reference computer this is 64 MiB; a 4 GiB/2 GiB-free host
# resolves to 32 MiB, and even a failed availability probe stays bounded.
sqlite_writer_cache_mib = max(8, min(64, capacity_mb // 128))
if available_mb is not None:
sqlite_writer_cache_mib = min(
sqlite_writer_cache_mib,
max(8, available_mb // 64),
)
# A revision-keyed public Turn baseline avoids repeatedly transferring and
# decoding the same multi-MiB writer row. Charge hydrated JSON bytes under
# an independent Sidecar-process budget: the 8 GiB reference gets 32 MiB,
# a 4/2 GiB host gets 16 MiB, and probe failure stays at the lean fallback.
# The consumer also enforces entry count, idle lifetime, and a 1 GiB hard
# ceiling for explicit distributed overrides.
if not capacity_known:
turn_projection_cache_mib = _RESOURCE_FALLBACKS['personal'][
'TOFU_STORAGE_TURN_PROJECTION_CACHE_MIB']
else:
turn_projection_cache_mib = max(
8, min(128, capacity_mb // 256))
if available_mb is not None:
turn_projection_cache_mib = min(
turn_projection_cache_mib,
max(8, available_mb // 128),
)
disk_free_mb = snapshot.disk_free_mb
if disk_free_mb is not None and disk_free_mb < 4096:
log_budget_mb = 64
elif disk_free_mb is not None and disk_free_mb < 16 * 1024:
log_budget_mb = 128
else:
log_budget_mb = (
256 if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal']['TOFU_LOG_TOTAL_BUDGET_MB'])
storage_reserve_mb = (
max(256, min(2048, int(snapshot.disk_total_mb * 0.01)))
if snapshot.disk_total_mb is not None else 256)
# Recovery copies preserve durable state but still multiply its physical
# footprint. On the 500 GiB reference computer, reserve at least half the
# disk for the authority, OS, browser, and ordinary user files; a shared
# multi-petabyte mount must not turn the personal default into unbounded
# copy retention. Probe failure falls back to one explicit, lean ceiling.
recovery_copy_budget_mib = (
max(4096, min(512 * 1024, int(snapshot.disk_total_mb * 0.50)))
if snapshot.disk_total_mb is not None else
_RESOURCE_FALLBACKS['personal'][
'TOFU_STORAGE_RECOVERY_COPY_BUDGET_MIB']
)
# A verified backup performs a sequential image write plus full integrity
# and checksum reads. The old fixed 1,800-second deadline repeatedly lost
# all progress on large network-backed authorities. Derive one finite
# overnight window from the same recovery-copy budget at a conservative
# effective 16 MiB/s, retaining a 30-minute floor and six-hour default cap.
storage_backup_timeout_seconds = max(
1800,
min(21600, 1800 + recovery_copy_budget_mib // 16),
)
# A WAL rebase writes one full database image to durable storage. Bound
# the WAL ceiling from the same launch-time disk observation so a large
# authority does not turn a tiny fixed threshold into continuous full-copy
# churn. Both the local WAL and its durable mirror fit inside four percent
# of observed free space; the shipper additionally scales the effective
# trigger to the authority size. Probe failure stays lean and explicit.
fastpath_wal_rebase_max_mib = (
max(64, min(16_384, int(disk_free_mb * 0.02)))
if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal'][
'TOFU_STORAGE_FASTPATH_WAL_REBASE_MAX_MIB']
)
# Conversation search is a disposable local projection, not durable
# authority. Keep it useful on the 500 GB reference machine without
# letting a pathological transcript corpus consume the volume: at most
# two percent of currently free space, with a lean fallback when the
# launch probe cannot establish a trustworthy capacity.
search_projection_max_mib = (
max(128, min(4096, int(disk_free_mb * 0.02)))
if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal'][
'TOFU_TURN_SEARCH_PROJECTION_MAX_MIB']
)
# Browser-authenticated files are reconstructible staging, not durable
# user state. Give their whole directory one percent of observed free
# space, with a small-machine floor and a hard personal-computer ceiling.
browser_staging_max_mib = (
max(64, min(2048, int(disk_free_mb * 0.01)))
if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal']['TOFU_BROWSER_STAGING_MAX_MIB']
)
# Durable Request Inspector raw evidence has no TTL or silent eviction.
# Bound the whole archive authority to one percent of the launch-time data
# volume's available space, capped at 4 GiB; an unknown probe stays at the
# explicit 256 MiB fallback. The writer independently preserves the
# storage minimum-free floor on every archive commit.
raw_archive_budget_mib = (
max(1, min(4096, int(disk_free_mb * 0.01)))
if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal']['TOFU_RAW_ARCHIVE_BUDGET_MIB']
)
# Python bytecode is reconstructible and useful only as a small local
# acceleration layer. Scale its hard process-wide ceiling from the same
# launch-time disk observation as every other zero-config disk budget;
# the runtime additionally verifies capacity on the actual cache volume.
if disk_free_mb is not None and disk_free_mb < 4096:
python_cache_max_mib = 16
elif disk_free_mb is not None and disk_free_mb < 16 * 1024:
python_cache_max_mib = 32
else:
python_cache_max_mib = (
64 if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal']['TOFU_RUN_PYTHON_CACHE_MAX_MIB'])
# Translation results are reconstructible API-cost caches, not durable
# user state. Bound the whole sharded directory from the same data-volume
# observation: 0.25% of currently free space, with a small-machine floor
# and a personal-computer ceiling. The cache owner divides this exact
# budget across all 256 hash shards, so skew cannot create unbounded disk
# growth. Probe failure remains lean and explicit.
translate_cache_max_mib = (
max(32, min(512, int(disk_free_mb) // 400))
if disk_free_mb is not None else
_RESOURCE_FALLBACKS['personal']['TOFU_TRANSLATE_CACHE_MAX_MIB']
)
# Memory-list metadata is reconstructible from user-owned Markdown files.
# Cache only parsed frontmatter (never bodies or eligibility decisions),
# and scale both identity cardinality and estimated Python residency from
# the one launch-time memory probe. The consumer repeats hard ceilings.
if not capacity_known:
memory_metadata_cache_capacity = _RESOURCE_FALLBACKS['personal'][
'TOFU_MEMORY_METADATA_CACHE_CAPACITY']
memory_metadata_cache_max_mib = _RESOURCE_FALLBACKS['personal'][
'TOFU_MEMORY_METADATA_CACHE_MAX_MIB']
else:
memory_metadata_cache_capacity = max(
512, min(4096, capacity_mb // 4))
memory_metadata_cache_max_mib = max(
4, min(32, capacity_mb // 512))
if available_mb is not None:
memory_metadata_cache_capacity = min(
memory_metadata_cache_capacity,
max(512, available_mb // 2),
)
memory_metadata_cache_max_mib = min(
memory_metadata_cache_max_mib,
max(4, available_mb // 256),
)
# Control RPC executes bounded, read-only filesystem requests away from
# the event loop. It shares the launch-time I/O parallelism observation,
# but stays below the general storage pool so several browser tabs cannot
# manufacture an unbounded set of blocked filesystem threads.
control_rpc_workers = max(2, min(8, io_parallelism))
# Tree walks are metadata-heavy on the network/FUSE filesystems this
# index exists to protect. More than eight concurrent scandir jobs slowed
# the retained project in measurement, while the build-row objects remain
# a material peak before they are converted to compact columns. Give all
# concurrent roots one shared launch-probed scan and retention budget.
tree_index_walk_jobs = max(2, min(8, io_parallelism))
if not capacity_known:
tree_index_max_entries = _RESOURCE_FALLBACKS['personal'][
'TOFU_TREE_INDEX_MAX_ENTRIES']
tree_index_mem_roots = _RESOURCE_FALLBACKS['personal'][
'TOFU_TREE_INDEX_MEM_ROOTS']
else:
tree_index_max_entries = max(
50_000, min(600_000, capacity_mb * 50))
if available_mb is not None: