fsanyoto commited on
Commit
0d5fa3c
·
verified ·
1 Parent(s): 9900332

Deploy AIOS web (React glide grid + FastAPI slice)

Browse files
RELEASES.json CHANGED
@@ -1,497 +1,4 @@
1
  {
2
- "current": "5a226b6c1f70",
3
- "releases": [
4
- {
5
- "version": "v83",
6
- "sha": "9b7471968e79",
7
- "date": "2026-10-21",
8
- "subject": "release v83"
9
- },
10
- {
11
- "version": "v82",
12
- "sha": "9c61b1ee94d0",
13
- "date": "2026-10-17",
14
- "subject": "release v82"
15
- },
16
- {
17
- "version": "v81",
18
- "sha": "e141ccc85ef0",
19
- "date": "2026-10-15",
20
- "subject": "release v81"
21
- },
22
- {
23
- "version": "v80",
24
- "sha": "869b8fcc62ac",
25
- "date": "2026-10-13",
26
- "subject": "release v80"
27
- },
28
- {
29
- "version": "v79",
30
- "sha": "406417b8ded0",
31
- "date": "2026-10-13",
32
- "subject": "release v79"
33
- },
34
- {
35
- "version": "v78",
36
- "sha": "aad6561d63e6",
37
- "date": "2026-10-09",
38
- "subject": "release v78: SDLC 71 with the D-7536 and D-7537 repairs"
39
- },
40
- {
41
- "version": "v77",
42
- "sha": "e741e5ca604d",
43
- "date": "2026-10-05",
44
- "subject": "release v77"
45
- },
46
- {
47
- "version": "v76",
48
- "sha": "7990f03e0254",
49
- "date": "2026-10-01",
50
- "subject": "release v76"
51
- },
52
- {
53
- "version": "v75",
54
- "sha": "2c5146981ccf",
55
- "date": "2026-09-19",
56
- "subject": "release v75"
57
- },
58
- {
59
- "version": "v74",
60
- "sha": "40dceba9262e",
61
- "date": "2026-09-14",
62
- "subject": "release v74: waves 58+59 incl. the Amazon credential door (W59-T21/22/23) and sync producer (W59-T42); hotfix promote so"
63
- },
64
- {
65
- "version": "v73",
66
- "sha": "bb94de5665e6",
67
- "date": "2026-09-13",
68
- "subject": "release v73"
69
- },
70
- {
71
- "version": "v71",
72
- "sha": "96143dc6fb65",
73
- "date": "2026-09-04",
74
- "subject": "release v71 - wave 51, QA'd 24 PASS / 6 FAIL / 5 UNPROVEN"
75
- },
76
- {
77
- "version": "v70",
78
- "sha": "eedfb6a00163",
79
- "date": "2026-09-03",
80
- "subject": "release v70 - wave 50 (grid analytics, the Sheet as a spreadsheet, view trash and audit)"
81
- },
82
- {
83
- "version": "v69",
84
- "sha": "8598f56293a0",
85
- "date": "2026-09-02",
86
- "subject": "release v69: wave48-continuation validated + hotfixes v67/v68/d4e3e31 + go-live QA repairs (Supplier option limit, optio"
87
- },
88
- {
89
- "version": "v68",
90
- "sha": "62b065a2610a",
91
- "date": "2026-09-01",
92
- "subject": "release v68: unified holiday date offsets"
93
- },
94
- {
95
- "version": "v67",
96
- "sha": "3b2241982cf7",
97
- "date": "2026-09-01",
98
- "subject": "release v67"
99
- },
100
- {
101
- "version": "v66",
102
- "sha": "73c94f076740",
103
- "date": "2026-08-31",
104
- "subject": "release v66"
105
- },
106
- {
107
- "version": "v65",
108
- "sha": "9f6cfedb8a43",
109
- "date": "2026-08-31",
110
- "subject": "release v65"
111
- },
112
- {
113
- "version": "v64",
114
- "sha": "bb8985c6e73e",
115
- "date": "2026-08-31",
116
- "subject": "release v64"
117
- },
118
- {
119
- "version": "v63",
120
- "sha": "ec75cb8f3412",
121
- "date": "2026-08-31",
122
- "subject": "release v63"
123
- },
124
- {
125
- "version": "v62",
126
- "sha": "2c8f702c964f",
127
- "date": "2026-08-31",
128
- "subject": "release v62"
129
- },
130
- {
131
- "version": "v61",
132
- "sha": "bbc4c0760ab5",
133
- "date": "2026-08-30",
134
- "subject": "v61 - wave 46 hotfix: D-590 schema overlay reaches the grid read (all three assemblies), D-599 route to zone association"
135
- },
136
- {
137
- "version": "v60",
138
- "sha": "dd1b0620210e",
139
- "date": "2026-08-30",
140
- "subject": "v60 - wave 46"
141
- },
142
- {
143
- "version": "v59",
144
- "sha": "3cb73ce8021e",
145
- "date": "2026-08-29",
146
- "subject": "release v59"
147
- },
148
- {
149
- "version": "v58",
150
- "sha": "c526cede82f9",
151
- "date": "2026-08-29",
152
- "subject": "release v58"
153
- },
154
- {
155
- "version": "v57",
156
- "sha": "4859c035e6a7",
157
- "date": "2026-08-29",
158
- "subject": "release v57"
159
- },
160
- {
161
- "version": "v56",
162
- "sha": "2970b729e8f0",
163
- "date": "2026-08-28",
164
- "subject": "release v56"
165
- },
166
- {
167
- "version": "v55",
168
- "sha": "b766b904b8fa",
169
- "date": "2026-08-28",
170
- "subject": "release v55"
171
- },
172
- {
173
- "version": "v54",
174
- "sha": "6a8e99a064d9",
175
- "date": "2026-08-27",
176
- "subject": "release v54"
177
- },
178
- {
179
- "version": "v53",
180
- "sha": "89ed2cb867da",
181
- "date": "2026-08-24",
182
- "subject": "release v53"
183
- },
184
- {
185
- "version": "v52",
186
- "sha": "826349376241",
187
- "date": "2026-08-23",
188
- "subject": "release v52"
189
- },
190
- {
191
- "version": "v51",
192
- "sha": "5740d20fa585",
193
- "date": "2026-08-23",
194
- "subject": "release v51"
195
- },
196
- {
197
- "version": "v50",
198
- "sha": "8027c83d1da6",
199
- "date": "2026-08-23",
200
- "subject": "release v50"
201
- },
202
- {
203
- "version": "v49",
204
- "sha": "9a14c01a48a4",
205
- "date": "2026-08-23",
206
- "subject": "release v49"
207
- },
208
- {
209
- "version": "v48",
210
- "sha": "3c69fd93da7c",
211
- "date": "2026-08-23",
212
- "subject": "release v48"
213
- },
214
- {
215
- "version": "v47",
216
- "sha": "917f9a0eb509",
217
- "date": "2026-08-23",
218
- "subject": "release v47"
219
- },
220
- {
221
- "version": "v46",
222
- "sha": "a8dc531b5078",
223
- "date": "2026-08-23",
224
- "subject": "release v46"
225
- },
226
- {
227
- "version": "v45",
228
- "sha": "0b2b5010d86f",
229
- "date": "2026-08-23",
230
- "subject": "release v45"
231
- },
232
- {
233
- "version": "v44",
234
- "sha": "a7877aaf20ff",
235
- "date": "2026-08-23",
236
- "subject": "Hotfix: allow renaming the active Map view"
237
- },
238
- {
239
- "version": "v43",
240
- "sha": "e2ba4616fa0f",
241
- "date": "2026-08-23",
242
- "subject": "release v43"
243
- },
244
- {
245
- "version": "v42",
246
- "sha": "25b885e6373a",
247
- "date": "2026-08-23",
248
- "subject": "release v42"
249
- },
250
- {
251
- "version": "v41",
252
- "sha": "1d6adcc3c3ac",
253
- "date": "2026-08-22",
254
- "subject": "v41 vertical route navigation"
255
- },
256
- {
257
- "version": "v40",
258
- "sha": "1451837168f3",
259
- "date": "2026-08-22",
260
- "subject": "release v40 route UI correction"
261
- },
262
- {
263
- "version": "v39",
264
- "sha": "a78bdf00e38e",
265
- "date": "2026-08-21",
266
- "subject": "W39 clean stale migration targets"
267
- },
268
- {
269
- "version": "v38",
270
- "sha": "a556631cf979",
271
- "date": "2026-08-21",
272
- "subject": "W39 reversible Live store cutover"
273
- },
274
- {
275
- "version": "v37",
276
- "sha": "1ddd8cf82379",
277
- "date": "2026-08-21",
278
- "subject": "W39: coalesce derived workspace reads"
279
- },
280
- {
281
- "version": "v36",
282
- "sha": "51db3b7fee2c",
283
- "date": "2026-08-21",
284
- "subject": "W39 share-field oid repair"
285
- },
286
- {
287
- "version": "v35",
288
- "sha": "0f642129c8f7",
289
- "date": "2026-08-21",
290
- "subject": "W39 visual QA fixture"
291
- },
292
- {
293
- "version": "v34",
294
- "sha": "cb4caa188e05",
295
- "date": "2026-08-21",
296
- "subject": "W39 Staging PostgreSQL egress controls"
297
- },
298
- {
299
- "version": "v33",
300
- "sha": "2b115e573d6a",
301
- "date": "2026-08-21",
302
- "subject": "W39: stop nonproduction Postgres background egress"
303
- },
304
- {
305
- "version": "v32",
306
- "sha": "b47b27ca7b2a",
307
- "date": "2026-08-21",
308
- "subject": "W39: repair Staging Postgres projection"
309
- },
310
- {
311
- "version": "v31",
312
- "sha": "8880a49a244e",
313
- "date": "2026-08-21",
314
- "subject": "v31: Wave 39 staging candidate"
315
- },
316
- {
317
- "version": "v30",
318
- "sha": "71776ca6c8df",
319
- "date": "2026-08-20",
320
- "subject": "v30: W39-T29 Member-owned View first-share hotfix"
321
- },
322
- {
323
- "version": "v29",
324
- "sha": "55ced5018ff2",
325
- "date": "2026-08-18",
326
- "subject": "v29: the build staging has been running since 2026-08-18 (55ced50)."
327
- },
328
- {
329
- "version": "v28",
330
- "sha": "2793e4ba53dc",
331
- "date": "2026-08-18",
332
- "subject": "release v28"
333
- },
334
- {
335
- "version": "v27",
336
- "sha": "c05a794837fc",
337
- "date": "2026-08-18",
338
- "subject": "release v27"
339
- },
340
- {
341
- "version": "v25",
342
- "sha": "bc248295ed66",
343
- "date": "2026-08-14",
344
- "subject": "v25 - wave 32's LIVE build (deployed 2026-08-13, bc24829)"
345
- },
346
- {
347
- "version": "v26",
348
- "sha": "6a642e139cfa",
349
- "date": "2026-08-14",
350
- "subject": "v26 - wave 33's build (deployed 2026-08-14, 6a642e1)"
351
- },
352
- {
353
- "version": "v24",
354
- "sha": "514d8292ce73",
355
- "date": "2026-08-09",
356
- "subject": "release v24"
357
- },
358
- {
359
- "version": "v23",
360
- "sha": "44cc28380fca",
361
- "date": "2026-08-09",
362
- "subject": "release v23"
363
- },
364
- {
365
- "version": "v22",
366
- "sha": "f14e199ff32f",
367
- "date": "2026-08-09",
368
- "subject": "release v22"
369
- },
370
- {
371
- "version": "v21",
372
- "sha": "5fdffce8b558",
373
- "date": "2026-08-09",
374
- "subject": "release v21"
375
- },
376
- {
377
- "version": "v20",
378
- "sha": "442e4a1430a7",
379
- "date": "2026-08-09",
380
- "subject": "release v20"
381
- },
382
- {
383
- "version": "v19",
384
- "sha": "94cb4a4dfdfe",
385
- "date": "2026-08-09",
386
- "subject": "release v19"
387
- },
388
- {
389
- "version": "v18",
390
- "sha": "0106ef949805",
391
- "date": "2026-08-09",
392
- "subject": "release v18"
393
- },
394
- {
395
- "version": "v17",
396
- "sha": "308240cc75e8",
397
- "date": "2026-08-09",
398
- "subject": "release v17"
399
- },
400
- {
401
- "version": "v16",
402
- "sha": "a804df54430f",
403
- "date": "2026-08-09",
404
- "subject": "release v16"
405
- },
406
- {
407
- "version": "v15",
408
- "sha": "6b84d7264c81",
409
- "date": "2026-08-09",
410
- "subject": "release v15"
411
- },
412
- {
413
- "version": "v14",
414
- "sha": "bee5eaf5b5cb",
415
- "date": "2026-08-07",
416
- "subject": "release v14"
417
- },
418
- {
419
- "version": "v13",
420
- "sha": "314c0e368b1d",
421
- "date": "2026-08-06",
422
- "subject": "release v13"
423
- },
424
- {
425
- "version": "v12",
426
- "sha": "4908f399785d",
427
- "date": "2026-08-06",
428
- "subject": "release v12"
429
- },
430
- {
431
- "version": "v11",
432
- "sha": "312661712b98",
433
- "date": "2026-08-05",
434
- "subject": "release v11"
435
- },
436
- {
437
- "version": "v10",
438
- "sha": "43f804287496",
439
- "date": "2026-08-05",
440
- "subject": "release v10"
441
- },
442
- {
443
- "version": "v9",
444
- "sha": "2e26f57396b1",
445
- "date": "2026-08-05",
446
- "subject": "release v9"
447
- },
448
- {
449
- "version": "v8",
450
- "sha": "8073b3ecbac3",
451
- "date": "2026-08-05",
452
- "subject": "release v8"
453
- },
454
- {
455
- "version": "v7",
456
- "sha": "b1c5a7e9b848",
457
- "date": "2026-08-05",
458
- "subject": "release v7"
459
- },
460
- {
461
- "version": "v6",
462
- "sha": "a344020f114a",
463
- "date": "2026-08-05",
464
- "subject": "release v6"
465
- },
466
- {
467
- "version": "v5",
468
- "sha": "c0b0fbb40a7b",
469
- "date": "2026-08-05",
470
- "subject": "release v5"
471
- },
472
- {
473
- "version": "v4",
474
- "sha": "de5037ff1c8f",
475
- "date": "2026-08-05",
476
- "subject": "release v4"
477
- },
478
- {
479
- "version": "v3",
480
- "sha": "6b9fa62d93e8",
481
- "date": "2026-08-05",
482
- "subject": "release v3"
483
- },
484
- {
485
- "version": "v2",
486
- "sha": "fd0586199858",
487
- "date": "2026-08-04",
488
- "subject": "release v2"
489
- },
490
- {
491
- "version": "v1",
492
- "sha": "5b4e2c4bc3f3",
493
- "date": "2026-08-04",
494
- "subject": "release v1"
495
- }
496
- ]
497
  }
 
1
  {
2
+ "current": "0b0e7c4c14",
3
+ "releases": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  }
VERSION CHANGED
@@ -1 +1 @@
1
- 5a226b6c1f70
 
1
+ 0b0e7c4c14
api/main.py CHANGED
@@ -140,7 +140,16 @@ _PASSTHROUGH_KEYS = {f["key"] for f in FIELDS
140
  if f["source"] == "odoo" and f["type"] in ("text", "status", "select",
141
  "date")}
142
 
143
- app = FastAPI(title="AIOS web API")
 
 
 
 
 
 
 
 
 
144
 
145
  # The customers payload measured 1.15 MB of JSON on the live Space, shipped UNCOMPRESSED — with
146
  # the 754 KB bundle behind it, most of "the app is slow" was bytes on the wire. gzip takes the
 
140
  if f["source"] == "odoo" and f["type"] in ("text", "status", "select",
141
  "date")}
142
 
143
+ from request_timing import RequestTiming # noqa: E402
144
+
145
+
146
+ class _TimedFastAPI(FastAPI):
147
+ def build_middleware_stack(self):
148
+ # Include error responses, freeze refusals, admission and compressed body sends.
149
+ return RequestTiming(super().build_middleware_stack())
150
+
151
+
152
+ app = _TimedFastAPI(title="AIOS web API")
153
 
154
  # The customers payload measured 1.15 MB of JSON on the live Space, shipped UNCOMPRESSED — with
155
  # the 754 KB bundle behind it, most of "the app is slow" was bytes on the wire. gzip takes the
api/request_class.py CHANGED
@@ -1,342 +1,288 @@
1
- """Heavy request admission: a bounded queue so CPU-heavy reads never starve light requests.
2
-
3
- One web process on 2 vCPU serves everyone. `classify` names the heavy doors in ONE registry, and
4
- `HeavyGate` lets at most `AIOS_HEAVY_CONCURRENCY` of them run at once. A heavy request that waits
5
- longer than `AIOS_HEAVY_QUEUE_SECONDS` gets a 503 `server_busy`. Light requests never wait.
6
- """
7
- from __future__ import annotations
8
-
9
- import asyncio
10
- import json
11
- import os
12
- import re
13
- import threading
14
- from urllib.parse import parse_qs
15
-
16
- BUSY_CODE = "server_busy"
17
- BUSY_MESSAGE = "Loopable is busy. Try again in a moment."
18
- RETRY_AFTER_SECONDS = 5
19
- MCP_PATH = "/api/v1/external/mcp"
20
- # The MCP body is peeked to classify it. A larger body is heavy without parsing.
21
- MCP_PEEK_LIMIT = 1 << 20
22
-
23
- # FastAPI reads these strings as True for a bool query parameter.
24
- _TRUE = {"1", "true", "on", "yes", "y", "t"}
25
-
26
-
27
- def _flag(query, name):
28
- return any(str(v).strip().lower() in _TRUE for v in query.get(name, []))
29
-
30
-
31
- def _sheet_read_heavy(query):
32
- # Only the cell-free metadata read is light. A bare GET or any other `tabs` value reads cells.
33
- tabs = (query.get("tabs") or [""])[-1]
34
- return not (tabs == "meta" and not _flag(query, "opening"))
35
-
36
-
37
- # (methods, path regex, query predicate or None). A request is heavy when one row matches.
38
- HEAVY = (
39
- ({"GET"}, r"/api/v1/workspace", lambda q: not _flag(q, "fields_only")),
40
- ({"GET"}, r"/api/v1/products", None),
41
- ({"GET"}, r"/api/v1/customers", None),
42
- ({"GET", "POST"}, r"/api/v1/odoo-tables/[^/]+/rows", None),
43
- ({"GET", "POST"}, r"/api/v1/connected-tables/[^/]+/rows", None),
44
- ({"GET"}, r"/api/v1/tables/[^/]+/rows", None),
45
- ({"GET"}, r"/api/v1/sheets/(?!index$|import$|automation$)[^/]+", _sheet_read_heavy),
46
- ({"GET"}, r"/api/v1/sheets/[^/]+/tabs/[^/]+", None),
47
- # In the web role a refresh only queues for the sync worker; it computes only in worker or all.
48
- ({"POST"}, r"/api/v1/sheets/[^/]+/refresh", lambda q: os.environ.get("AIOS_PROCESS_ROLE") != "web"),
49
- ({"GET", "POST"}, r"/api/v1/.*(?:/export|-export)(?:/.*)?", None),
50
- ({"POST"}, r"/api/v1/(?:external|typed)/databases/[^/]+/query", None),
51
- ({"POST"}, r"/api/v1/external/explorer/run", None),
52
- )
53
- _HEAVY = tuple((methods, re.compile(rx), pred) for methods, rx, pred in HEAVY)
54
-
55
- # MCP tools/call names that read sheets or query databases.
56
- MCP_HEAVY_TOOLS = {"sheet", "table", "data"}
57
- _MCP_LIGHT_ACTIONS = re.compile(r"(?:^|[._])(?:describe|list|guide|schema)(?:$|[._])")
58
-
59
-
60
- def _mcp_heavy(body):
61
- """True when an MCP JSON-RPC body is a tools/call on a sheet or query tool."""
62
- if body is None:
63
- return False
64
- if len(body) > MCP_PEEK_LIMIT:
65
- return True
66
- try:
67
- req = json.loads(body)
68
- except (ValueError, UnicodeError):
69
- return False
70
- calls = req if isinstance(req, list) else [req]
71
- for call in calls:
72
- if not isinstance(call, dict) or call.get("method") != "tools/call":
73
- continue
74
- params = call.get("params") if isinstance(call.get("params"), dict) else {}
75
- name = params.get("name")
76
- if not isinstance(name, str):
77
- continue
78
- group, _, dotted = name.partition(".")
79
- if group not in MCP_HEAVY_TOOLS and not name.startswith("sheet_") and "query" not in name:
80
- continue
81
- args = params.get("arguments") if isinstance(params.get("arguments"), dict) else {}
82
- action = dotted or str(args.get("action") or "")
83
- if action and _MCP_LIGHT_ACTIONS.search(action) and "query" not in action:
84
- continue
85
- return True
86
- return False
87
-
88
-
89
- def classify(method, path, query="", body=None):
90
- """Return "heavy" or "light" for one request. `query` is a raw query string or a parsed dict."""
91
- method = (method or "").upper()
92
- if isinstance(query, bytes):
93
- query = query.decode("latin-1")
94
- q = parse_qs(query, keep_blank_values=True) if isinstance(query, str) else (query or {})
95
- for methods, rx, pred in _HEAVY:
96
- if method in methods and rx.fullmatch(path or "") and (pred is None or pred(q)):
97
- return "heavy"
98
- if method == "POST" and path == MCP_PATH and _mcp_heavy(body):
99
- return "heavy"
100
- return "light"
101
-
102
-
103
- class _Counters:
104
- """Process-wide heavy request counters. Middleware instances report into this."""
105
-
106
- def __init__(self):
107
- self.lock = threading.Lock()
108
- self.in_flight = 0
109
- self.queued = 0
110
- self.busy_refusals = 0
111
- self.admitted = 0
112
- self.limit = None
113
- self.queue_seconds = None
114
-
115
- def add(self, name, n):
116
- with self.lock:
117
- setattr(self, name, getattr(self, name) + n)
118
-
119
-
120
- _COUNTERS = _Counters()
121
-
122
-
123
- class _StackSampler:
124
- """Opt-in (AIOS_HEAVY_PROFILE=1): samples app frames while heavy requests run, logs the hot ones."""
125
-
126
- INTERVAL = 0.2
127
-
128
- def __init__(self):
129
- self.lock = threading.Lock()
130
- self.active = 0
131
- self.counts = {}
132
- self.thread = None
133
-
134
- def begin(self):
135
- with self.lock:
136
- self.active += 1
137
- if self.active == 1:
138
- self.counts = {}
139
- if self.thread is None or not self.thread.is_alive():
140
- import process_role
141
- self.thread = process_role.thread(target=self._run, name="heavy-profile", kind="light")
142
- self.thread.start()
143
-
144
- def end(self, label, seconds):
145
- with self.lock:
146
- self.active -= 1
147
- counts = dict(self.counts)
148
- if seconds < 3 or not counts:
149
- return
150
- top = sorted(counts.items(), key=lambda kv: -kv[1])[:18]
151
- print(f"[heavy-profile] {label} {seconds:.1f}s " + " | ".join(
152
- f"{name} {n * self.INTERVAL:.1f}s" for name, n in top), flush=True)
153
-
154
- def _run(self):
155
- import sys
156
- import time
157
- me = threading.get_ident()
158
- while True:
159
- time.sleep(self.INTERVAL)
160
- with self.lock:
161
- if self.active <= 0:
162
- self.thread = None
163
- return
164
- for tid, frame in sys._current_frames().items():
165
- if tid == me:
166
- continue
167
- seen = set()
168
- while frame is not None:
169
- code = frame.f_code
170
- path = code.co_filename.replace("\\", "/")
171
- if ("/api/" in path or "/platform/" in path) and "site-packages" not in path:
172
- name = f"{code.co_name}@{path.rsplit('/', 1)[-1]}"
173
- if name not in seen:
174
- seen.add(name)
175
- self.counts[name] = self.counts.get(name, 0) + 1
176
- frame = frame.f_back
177
-
178
-
179
- _SAMPLER = _StackSampler() if os.environ.get("AIOS_HEAVY_PROFILE") == "1" else None
180
-
181
-
182
- def status():
183
- """Snapshot of heavy request admission for a settings or health surface."""
184
- c = _COUNTERS
185
- with c.lock:
186
- return {"limit": c.limit if c.limit is not None else _env_int("AIOS_HEAVY_CONCURRENCY", 2),
187
- "queueSeconds": (c.queue_seconds if c.queue_seconds is not None
188
- else _env_float("AIOS_HEAVY_QUEUE_SECONDS", 90.0)),
189
- "inFlight": c.in_flight, "queued": c.queued,
190
- "busyRefusals": c.busy_refusals, "admitted": c.admitted}
191
-
192
-
193
- def _env_int(name, default):
194
- try:
195
- return max(1, int(os.environ.get(name) or default))
196
- except ValueError:
197
- return default
198
-
199
-
200
- def _env_float(name, default):
201
- try:
202
- return max(0.0, float(os.environ.get(name) or default))
203
- except ValueError:
204
- return default
205
-
206
-
207
- async def _send_busy(send):
208
- body = json.dumps({"error": {"code": BUSY_CODE, "message": BUSY_MESSAGE}}).encode()
209
- await send({"type": "http.response.start", "status": 503,
210
- "headers": [(b"content-type", b"application/json"),
211
- (b"content-length", str(len(body)).encode()),
212
- (b"retry-after", str(RETRY_AFTER_SECONDS).encode()),
213
- (b"cache-control", b"no-store")]})
214
- await send({"type": "http.response.body", "body": body, "more_body": False})
215
-
216
-
217
- class HeavyGate:
218
- """Pure ASGI middleware. Heavy requests take a permit; light ones pass straight through."""
219
-
220
- def __init__(self, app, concurrency=None, queue_seconds=None, counters=None):
221
- self.app = app
222
- self.concurrency = concurrency or _env_int("AIOS_HEAVY_CONCURRENCY", 2)
223
- self.queue_seconds = (queue_seconds if queue_seconds is not None
224
- else _env_float("AIOS_HEAVY_QUEUE_SECONDS", 90.0))
225
- self.counters = counters or _COUNTERS
226
- self.counters.limit, self.counters.queue_seconds = self.concurrency, self.queue_seconds
227
- self._sem = None
228
- self._loop = None
229
-
230
- def _semaphore(self):
231
- # A Semaphore binds to the first loop that waits on it. One per loop.
232
- loop = asyncio.get_running_loop()
233
- if self._sem is None or self._loop is not loop:
234
- self._sem, self._loop = asyncio.Semaphore(self.concurrency), loop
235
- return self._sem
236
-
237
- async def __call__(self, scope, receive, send):
238
- if scope.get("type") != "http":
239
- return await self.app(scope, receive, send)
240
- method, path = scope.get("method", ""), scope.get("path", "")
241
- body_msgs = None
242
- peek = None
243
- if method == "POST" and path == MCP_PATH:
244
- body_msgs, peek = await _read_body(receive, MCP_PEEK_LIMIT)
245
- if peek is None: # The client left while sending the body.
246
- return
247
- if classify(method, path, scope.get("query_string", b""), peek) != "heavy":
248
- return await self.app(scope, _replay(body_msgs, receive), send)
249
- if body_msgs is None:
250
- body_msgs, _ = await _read_body(receive, None)
251
- if body_msgs is None:
252
- return
253
- await self._run_heavy(scope, body_msgs, receive, send)
254
-
255
- async def _run_heavy(self, scope, body_msgs, receive, send):
256
- sem = self._semaphore()
257
- c = self.counters
258
- c.add("queued", 1)
259
- acquire = asyncio.ensure_future(sem.acquire())
260
- gone = asyncio.ensure_future(_wait_disconnect(receive))
261
- try:
262
- done, _ = await asyncio.wait({acquire, gone}, timeout=self.queue_seconds,
263
- return_when=asyncio.FIRST_COMPLETED)
264
- finally:
265
- c.add("queued", -1)
266
- if not acquire.done():
267
- acquire.cancel()
268
- if not gone.done():
269
- gone.cancel()
270
- # A permit can land in the same tick as a timeout or disconnect. Hold it only if used.
271
- got = acquire.done() and not acquire.cancelled() and acquire.exception() is None
272
- if acquire not in done or gone in done:
273
- if got:
274
- sem.release()
275
- if gone not in done:
276
- c.add("busy_refusals", 1)
277
- await _send_busy(send)
278
- return
279
- released = False
280
-
281
- def release():
282
- nonlocal released
283
- if not released:
284
- released = True
285
- c.add("in_flight", -1)
286
- sem.release()
287
-
288
- async def send_wrapped(message):
289
- await send(message)
290
- if message.get("type") == "http.response.body" and not message.get("more_body"):
291
- release()
292
-
293
- c.add("in_flight", 1)
294
- c.add("admitted", 1)
295
- started = asyncio.get_running_loop().time()
296
- if _SAMPLER is not None:
297
- _SAMPLER.begin()
298
- try:
299
- await self.app(scope, _replay(body_msgs, receive), send_wrapped)
300
- finally:
301
- release()
302
- if _SAMPLER is not None:
303
- q = scope.get("query_string", b"").decode("latin-1")[:60]
304
- _SAMPLER.end(f"{scope.get('method')} {scope.get('path')}?{q}",
305
- asyncio.get_running_loop().time() - started)
306
-
307
-
308
- async def _read_body(receive, limit):
309
- """Buffer the request body. Returns (messages, bytes or None if the client disconnected)."""
310
- msgs, chunks, size = [], [], 0
311
- while True:
312
- message = await receive()
313
- if message.get("type") == "http.disconnect":
314
- return None, None
315
- msgs.append(message)
316
- chunk = message.get("body", b"") or b""
317
- # Keep the chunk that crosses the limit, so an oversized body reads as oversized.
318
- if limit is None or size <= limit:
319
- chunks.append(chunk)
320
- size += len(chunk)
321
- if not message.get("more_body"):
322
- return msgs, b"".join(chunks)
323
-
324
-
325
- async def _wait_disconnect(receive):
326
- while True:
327
- message = await receive()
328
- if message.get("type") == "http.disconnect":
329
- return True
330
-
331
-
332
- def _replay(msgs, receive):
333
- """A receive that hands back the buffered body first, then defers to the real one."""
334
- if not msgs:
335
- return receive
336
- pending = list(msgs)
337
-
338
- async def replay():
339
- if pending:
340
- return pending.pop(0)
341
- return await receive()
342
- return replay
 
1
+ """Heavy request admission: a bounded queue so CPU-heavy reads never starve light requests.
2
+
3
+ One web process on 2 vCPU serves everyone. `classify` names the heavy doors in ONE registry, and
4
+ `HeavyGate` lets at most `AIOS_HEAVY_CONCURRENCY` of them run at once. A heavy request that waits
5
+ longer than `AIOS_HEAVY_QUEUE_SECONDS` gets a 503 `server_busy`. Light requests never wait.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import asyncio
10
+ import json
11
+ import os
12
+ import re
13
+ import sys
14
+ import threading
15
+ import time
16
+ from urllib.parse import parse_qs
17
+
18
+ BUSY_CODE = "server_busy"
19
+ BUSY_MESSAGE = "Loopable is busy. Try again in a moment."
20
+ RETRY_AFTER_SECONDS = 5
21
+ MCP_PATH = "/api/v1/external/mcp"
22
+ # The MCP body is peeked to classify it. A larger body is heavy without parsing.
23
+ MCP_PEEK_LIMIT = 1 << 20
24
+
25
+ # FastAPI reads these strings as True for a bool query parameter.
26
+ _TRUE = {"1", "true", "on", "yes", "y", "t"}
27
+
28
+
29
+ def _flag(query, name):
30
+ return any(str(v).strip().lower() in _TRUE for v in query.get(name, []))
31
+
32
+
33
+ def _sheet_read_heavy(query):
34
+ # Only the cell-free metadata read is light. A bare GET or any other `tabs` value reads cells.
35
+ tabs = (query.get("tabs") or [""])[-1]
36
+ return not (tabs == "meta" and not _flag(query, "opening"))
37
+
38
+
39
+ # (methods, path regex, query predicate or None). A request is heavy when one row matches.
40
+ HEAVY = (
41
+ ({"GET"}, r"/api/v1/workspace", lambda q: not _flag(q, "fields_only")),
42
+ ({"GET"}, r"/api/v1/products", None),
43
+ ({"GET"}, r"/api/v1/customers", None),
44
+ ({"GET", "POST"}, r"/api/v1/odoo-tables/[^/]+/rows", None),
45
+ ({"GET", "POST"}, r"/api/v1/connected-tables/[^/]+/rows", None),
46
+ ({"GET"}, r"/api/v1/tables/[^/]+/rows", None),
47
+ ({"GET"}, r"/api/v1/sheets/(?!index$|import$|automation$)[^/]+", _sheet_read_heavy),
48
+ ({"GET"}, r"/api/v1/sheets/[^/]+/tabs/[^/]+", None),
49
+ # In the web role a refresh only queues for the sync worker; it computes only in worker or all.
50
+ ({"POST"}, r"/api/v1/sheets/[^/]+/refresh", lambda q: os.environ.get("AIOS_PROCESS_ROLE") != "web"),
51
+ ({"GET", "POST"}, r"/api/v1/.*(?:/export|-export)(?:/.*)?", None),
52
+ ({"POST"}, r"/api/v1/(?:external|typed)/databases/[^/]+/query", None),
53
+ ({"POST"}, r"/api/v1/external/explorer/run", None),
54
+ )
55
+ _HEAVY = tuple((methods, re.compile(rx), pred) for methods, rx, pred in HEAVY)
56
+
57
+ # MCP tools/call names that read sheets or query databases.
58
+ MCP_HEAVY_TOOLS = {"sheet", "table", "data"}
59
+ _MCP_LIGHT_ACTIONS = re.compile(r"(?:^|[._])(?:describe|list|guide|schema)(?:$|[._])")
60
+
61
+
62
+ def _mcp_heavy(body):
63
+ """True when an MCP JSON-RPC body is a tools/call on a sheet or query tool."""
64
+ if body is None:
65
+ return False
66
+ if len(body) > MCP_PEEK_LIMIT:
67
+ return True
68
+ try:
69
+ req = json.loads(body)
70
+ except (ValueError, UnicodeError):
71
+ return False
72
+ calls = req if isinstance(req, list) else [req]
73
+ for call in calls:
74
+ if not isinstance(call, dict) or call.get("method") != "tools/call":
75
+ continue
76
+ params = call.get("params") if isinstance(call.get("params"), dict) else {}
77
+ name = params.get("name")
78
+ if not isinstance(name, str):
79
+ continue
80
+ group, _, dotted = name.partition(".")
81
+ if group not in MCP_HEAVY_TOOLS and not name.startswith("sheet_") and "query" not in name:
82
+ continue
83
+ args = params.get("arguments") if isinstance(params.get("arguments"), dict) else {}
84
+ action = dotted or str(args.get("action") or "")
85
+ if action and _MCP_LIGHT_ACTIONS.search(action) and "query" not in action:
86
+ continue
87
+ return True
88
+ return False
89
+
90
+
91
+ def classify(method, path, query="", body=None):
92
+ """Return "heavy" or "light" for one request. `query` is a raw query string or a parsed dict."""
93
+ method = (method or "").upper()
94
+ if isinstance(query, bytes):
95
+ query = query.decode("latin-1")
96
+ q = parse_qs(query, keep_blank_values=True) if isinstance(query, str) else (query or {})
97
+ for methods, rx, pred in _HEAVY:
98
+ if method in methods and rx.fullmatch(path or "") and (pred is None or pred(q)):
99
+ return "heavy"
100
+ if method == "POST" and path == MCP_PATH and _mcp_heavy(body):
101
+ return "heavy"
102
+ return "light"
103
+
104
+
105
+ class _Counters:
106
+ """Process-wide heavy request counters. Middleware instances report into this."""
107
+
108
+ def __init__(self):
109
+ self.lock = threading.Lock()
110
+ self.in_flight = 0
111
+ self.queued = 0
112
+ self.busy_refusals = 0
113
+ self.admitted = 0
114
+ self.limit = None
115
+ self.queue_seconds = None
116
+
117
+ def add(self, name, n):
118
+ with self.lock:
119
+ setattr(self, name, getattr(self, name) + n)
120
+
121
+
122
+ _COUNTERS = _Counters()
123
+
124
+
125
+ def status():
126
+ """Snapshot of heavy request admission for a settings or health surface."""
127
+ c = _COUNTERS
128
+ with c.lock:
129
+ return {"limit": c.limit if c.limit is not None else _env_int("AIOS_HEAVY_CONCURRENCY", 2),
130
+ "queueSeconds": (c.queue_seconds if c.queue_seconds is not None
131
+ else _env_float("AIOS_HEAVY_QUEUE_SECONDS", 90.0)),
132
+ "inFlight": c.in_flight, "queued": c.queued,
133
+ "busyRefusals": c.busy_refusals, "admitted": c.admitted}
134
+
135
+
136
+ def _env_int(name, default):
137
+ try:
138
+ return max(1, int(os.environ.get(name) or default))
139
+ except ValueError:
140
+ return default
141
+
142
+
143
+ def _env_float(name, default):
144
+ try:
145
+ return max(0.0, float(os.environ.get(name) or default))
146
+ except ValueError:
147
+ return default
148
+
149
+
150
+ async def _send_busy(send):
151
+ body = json.dumps({"error": {"code": BUSY_CODE, "message": BUSY_MESSAGE}}).encode()
152
+ await send({"type": "http.response.start", "status": 503,
153
+ "headers": [(b"content-type", b"application/json"),
154
+ (b"content-length", str(len(body)).encode()),
155
+ (b"retry-after", str(RETRY_AFTER_SECONDS).encode()),
156
+ (b"cache-control", b"no-store")]})
157
+ await send({"type": "http.response.body", "body": body, "more_body": False})
158
+
159
+
160
+ class HeavyGate:
161
+ """Pure ASGI middleware. Heavy requests take a permit; light ones pass straight through."""
162
+
163
+ def __init__(self, app, concurrency=None, queue_seconds=None, counters=None):
164
+ self.app = app
165
+ self.concurrency = concurrency or _env_int("AIOS_HEAVY_CONCURRENCY", 2)
166
+ self.queue_seconds = (queue_seconds if queue_seconds is not None
167
+ else _env_float("AIOS_HEAVY_QUEUE_SECONDS", 90.0))
168
+ self.counters = counters or _COUNTERS
169
+ self.counters.limit, self.counters.queue_seconds = self.concurrency, self.queue_seconds
170
+ self._sem = None
171
+ self._loop = None
172
+
173
+ def _semaphore(self):
174
+ # A Semaphore binds to the first loop that waits on it. One per loop.
175
+ loop = asyncio.get_running_loop()
176
+ if self._sem is None or self._loop is not loop:
177
+ self._sem, self._loop = asyncio.Semaphore(self.concurrency), loop
178
+ return self._sem
179
+
180
+ async def __call__(self, scope, receive, send):
181
+ if scope.get("type") != "http":
182
+ return await self.app(scope, receive, send)
183
+ method, path = scope.get("method", ""), scope.get("path", "")
184
+ body_msgs = None
185
+ peek = None
186
+ if method == "POST" and path == MCP_PATH:
187
+ body_msgs, peek = await _read_body(receive, MCP_PEEK_LIMIT)
188
+ if peek is None: # The client left while sending the body.
189
+ return
190
+ if classify(method, path, scope.get("query_string", b""), peek) != "heavy":
191
+ return await self.app(scope, _replay(body_msgs, receive), send)
192
+ if body_msgs is None:
193
+ body_msgs, _ = await _read_body(receive, None)
194
+ if body_msgs is None:
195
+ return
196
+ await self._run_heavy(scope, body_msgs, receive, send)
197
+
198
+ async def _run_heavy(self, scope, body_msgs, receive, send):
199
+ queued_at = time.perf_counter()
200
+ sem = self._semaphore()
201
+ c = self.counters
202
+ c.add("queued", 1)
203
+ acquire = asyncio.ensure_future(sem.acquire())
204
+ gone = asyncio.ensure_future(_wait_disconnect(receive))
205
+ try:
206
+ done, _ = await asyncio.wait({acquire, gone}, timeout=self.queue_seconds,
207
+ return_when=asyncio.FIRST_COMPLETED)
208
+ except BaseException:
209
+ # Cancellation can race a successful acquire before the handler owns the permit.
210
+ if acquire.done() and not acquire.cancelled() and acquire.exception() is None:
211
+ sem.release()
212
+ raise
213
+ finally:
214
+ # The classifier can also run alone, without the app or an active timing scope.
215
+ timing = sys.modules.get("core.request_timing")
216
+ if timing is not None:
217
+ timing.record_queue((time.perf_counter() - queued_at) * 1000)
218
+ c.add("queued", -1)
219
+ if not acquire.done():
220
+ acquire.cancel()
221
+ if not gone.done():
222
+ gone.cancel()
223
+ # A permit can land in the same tick as a timeout or disconnect. Hold it only if used.
224
+ got = acquire.done() and not acquire.cancelled() and acquire.exception() is None
225
+ if acquire not in done or gone in done:
226
+ if got:
227
+ sem.release()
228
+ if gone not in done:
229
+ c.add("busy_refusals", 1)
230
+ await _send_busy(send)
231
+ return
232
+ released = False
233
+
234
+ def release():
235
+ nonlocal released
236
+ if not released:
237
+ released = True
238
+ c.add("in_flight", -1)
239
+ sem.release()
240
+
241
+ async def send_wrapped(message):
242
+ await send(message)
243
+ if message.get("type") == "http.response.body" and not message.get("more_body"):
244
+ release()
245
+
246
+ c.add("in_flight", 1)
247
+ c.add("admitted", 1)
248
+ try:
249
+ await self.app(scope, _replay(body_msgs, receive), send_wrapped)
250
+ finally:
251
+ release()
252
+
253
+
254
+ async def _read_body(receive, limit):
255
+ """Buffer the request body. Returns (messages, bytes or None if the client disconnected)."""
256
+ msgs, chunks, size = [], [], 0
257
+ while True:
258
+ message = await receive()
259
+ if message.get("type") == "http.disconnect":
260
+ return None, None
261
+ msgs.append(message)
262
+ chunk = message.get("body", b"") or b""
263
+ # Keep the chunk that crosses the limit, so an oversized body reads as oversized.
264
+ if limit is None or size <= limit:
265
+ chunks.append(chunk)
266
+ size += len(chunk)
267
+ if not message.get("more_body"):
268
+ return msgs, b"".join(chunks)
269
+
270
+
271
+ async def _wait_disconnect(receive):
272
+ while True:
273
+ message = await receive()
274
+ if message.get("type") == "http.disconnect":
275
+ return True
276
+
277
+
278
+ def _replay(msgs, receive):
279
+ """A receive that hands back the buffered body first, then defers to the real one."""
280
+ if not msgs:
281
+ return receive
282
+ pending = list(msgs)
283
+
284
+ async def replay():
285
+ if pending:
286
+ return pending.pop(0)
287
+ return await receive()
288
+ return replay
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/request_timing.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Outermost ASGI timing, bounded route statistics and request-associated stack sampling."""
2
+ from __future__ import annotations
3
+
4
+ import asyncio
5
+ import contextvars
6
+ import json
7
+ import logging
8
+ import math
9
+ import os
10
+ import sys
11
+ import threading
12
+ import time
13
+ from collections import OrderedDict, deque
14
+
15
+ from core import request_timing as timing
16
+
17
+ LOG = logging.getLogger("aios.request_timing")
18
+ _STATS_LOCK = threading.Lock()
19
+ _STATS = OrderedDict()
20
+ _ROUTE_LIMIT = 512
21
+
22
+
23
+ def slow_threshold_ms():
24
+ try:
25
+ value = float(os.environ.get("AIOS_SLOW_REQUEST_MS", "1000"))
26
+ return max(0.0, value) if math.isfinite(value) else 1000.0
27
+ except ValueError:
28
+ return 1000.0
29
+
30
+
31
+ def route_template(scope):
32
+ route = scope.get("route")
33
+ return getattr(route, "path", None) or scope.get("_timing_route", "<unmatched>")
34
+
35
+
36
+ def _resolve_route(scope):
37
+ # Refusals can finish before routing. Match registered templates without invoking a handler.
38
+ from starlette.routing import Match
39
+ for route in getattr(scope.get("app"), "routes", ()):
40
+ match, child = route.matches(scope)
41
+ if match == Match.FULL:
42
+ scope["_timing_route"] = getattr(route, "path", None) or "<unmatched>"
43
+ scope["_timing_endpoint"] = child.get("endpoint")
44
+ return
45
+
46
+
47
+ def _save_stats(scope, status, milliseconds):
48
+ key = (scope.get("method", ""), route_template(scope))
49
+ with _STATS_LOCK:
50
+ values = _STATS.setdefault(key, deque(maxlen=500))
51
+ _STATS.move_to_end(key)
52
+ values.append((milliseconds, int(status is not None and status >= 500)))
53
+ while len(_STATS) > _ROUTE_LIMIT:
54
+ _STATS.popitem(last=False)
55
+
56
+
57
+ def route_stats():
58
+ """Return process-wide route timings without tenant, user or request identifiers."""
59
+ with _STATS_LOCK:
60
+ snapshot = [(key, list(values)) for key, values in _STATS.items()]
61
+ rows = []
62
+ for (method, route), values in snapshot:
63
+ ordered = sorted(value[0] for value in values)
64
+ rows.append({"method": method, "route": route, "count": len(values),
65
+ "5xx": sum(value[1] for value in values),
66
+ "p50": round(ordered[math.ceil(len(ordered) * .50) - 1], 2),
67
+ "p95": round(ordered[math.ceil(len(ordered) * .95) - 1], 2),
68
+ "max": round(ordered[-1], 2)})
69
+ return {"window": 500, "unit": "ms", "routes": rows}
70
+
71
+
72
+ def _app_frames(frame):
73
+ names = set()
74
+ while frame is not None:
75
+ code = frame.f_code
76
+ path = code.co_filename.replace("\\", "/")
77
+ if ("/api/" in path or "/platform/" in path) and "site-packages" not in path:
78
+ names.add(f"{code.co_name}@{path.rsplit('/', 1)[-1]}")
79
+ frame = frame.f_back
80
+ return names
81
+
82
+
83
+ def _coroutine_frames(task):
84
+ frames, seen = [], set()
85
+ coro = task.get_coro()
86
+ while coro is not None and id(coro) not in seen:
87
+ seen.add(id(coro))
88
+ frame = getattr(coro, "cr_frame", None) or getattr(coro, "gi_frame", None)
89
+ if frame is not None:
90
+ frames.append(frame)
91
+ coro = getattr(coro, "cr_await", None) or getattr(coro, "gi_yieldfrom", None)
92
+ return frames
93
+
94
+
95
+ class _StackSampler:
96
+ """Sample only matched worker contexts and coroutine scopes, including light requests."""
97
+
98
+ INTERVAL = .2
99
+
100
+ def __init__(self):
101
+ self.lock = threading.Lock()
102
+ self.active = {}
103
+ self.thread = None
104
+
105
+ def begin(self, acc, scope):
106
+ import process_role
107
+ with self.lock:
108
+ self.active[acc] = (scope, asyncio.get_running_loop())
109
+ if self.thread is None:
110
+ job = process_role.thread(target=self._run, name="request-profile", kind="light")
111
+ if process_role.admitted(job):
112
+ self.thread = job
113
+ job.start()
114
+
115
+ def end(self, acc):
116
+ with self.lock:
117
+ self.active.pop(acc, None)
118
+
119
+ def sample(self):
120
+ with self.lock:
121
+ active = dict(self.active)
122
+ matched = {acc: set() for acc in active}
123
+ scopes = {id(scope): acc for acc, (scope, _loop) in active.items()}
124
+ threads = timing.associated_threads()
125
+ for tid, frame in sys._current_frames().items():
126
+ acc = threads.get(tid)
127
+ probe = frame
128
+ while probe is not None and acc not in active:
129
+ candidate = probe.f_locals.get("scope")
130
+ owner = scopes.get(id(candidate))
131
+ if owner is not None and active[owner][0] is candidate:
132
+ acc = owner
133
+ path = probe.f_code.co_filename.replace("\\", "/")
134
+ if "/anyio/" in path and probe.f_code.co_name == "run":
135
+ context = probe.f_locals.get("context")
136
+ if isinstance(context, contextvars.Context):
137
+ acc = context.get(timing.CURRENT)
138
+ probe = probe.f_back
139
+ if acc in active:
140
+ matched[acc].update(_app_frames(frame))
141
+ for loop in {loop for _scope, loop in active.values()}:
142
+ try:
143
+ tasks = asyncio.all_tasks(loop)
144
+ except RuntimeError:
145
+ continue
146
+ for task in tasks:
147
+ frames = _coroutine_frames(task)
148
+ owners = set()
149
+ for frame in frames:
150
+ candidate = frame.f_locals.get("scope")
151
+ owner = scopes.get(id(candidate))
152
+ if owner is not None and active[owner][0] is candidate:
153
+ owners.add(owner)
154
+ for acc in owners:
155
+ for frame in frames:
156
+ matched[acc].update(_app_frames(frame))
157
+ for acc, names in matched.items():
158
+ with acc.lock:
159
+ if not acc.closed:
160
+ acc.samples.update(names)
161
+
162
+ def _run(self):
163
+ while True:
164
+ time.sleep(self.INTERVAL)
165
+ with self.lock:
166
+ if not self.active:
167
+ self.thread = None
168
+ return
169
+ try:
170
+ self.sample()
171
+ except Exception:
172
+ LOG.debug("request sampler skipped a changing stack", exc_info=True)
173
+
174
+
175
+ _SAMPLER = _StackSampler()
176
+
177
+
178
+ class RequestTiming:
179
+ """Measure final body sends without buffering or consuming the receive channel."""
180
+
181
+ def __init__(self, app):
182
+ self.app = app
183
+
184
+ async def __call__(self, scope, receive, send):
185
+ if scope.get("type") != "http":
186
+ return await self.app(scope, receive, send)
187
+ with timing.request_scope() as acc:
188
+ status, failed, disconnected, complete = None, False, False, False
189
+ threshold = slow_threshold_ms()
190
+ _resolve_route(scope)
191
+ _SAMPLER.begin(acc, scope)
192
+
193
+ async def receive_timed():
194
+ nonlocal disconnected
195
+ message = await receive()
196
+ if message["type"] == "http.disconnect":
197
+ disconnected = True
198
+ return message
199
+
200
+ async def send_timed(message):
201
+ nonlocal status, complete
202
+ if message["type"] == "http.response.start":
203
+ values = acc.snapshot()
204
+ elapsed = (time.perf_counter() - acc.started) * 1000
205
+ app_ms = max(0., elapsed - values["queue_ms"] - values["store_ms"] - values["duckdb_ms"])
206
+ # Headers describe time until headers. Final streaming time is logged after completion.
207
+ header = (f'queue;dur={values["queue_ms"]:.2f}, store;dur={values["store_ms"]:.2f}, '
208
+ f'duckdb;dur={values["duckdb_ms"]:.2f}, app;dur={app_ms:.2f}')
209
+ message = dict(message)
210
+ message["headers"] = [(key, value) for key, value in message.get("headers", [])
211
+ if key.lower() != b"server-timing"] + [(b"server-timing", header.encode("ascii"))]
212
+ await send(message)
213
+ if message["type"] == "http.response.start":
214
+ status = message["status"]
215
+ elif message["type"] == "http.response.body":
216
+ with acc.lock:
217
+ acc.response_bytes += len(message.get("body", b""))
218
+ complete = not message.get("more_body", False)
219
+
220
+ try:
221
+ await self.app(scope, receive_timed, send_timed)
222
+ except BaseException:
223
+ failed = True
224
+ raise
225
+ finally:
226
+ elapsed = (time.perf_counter() - acc.started) * 1000
227
+ _SAMPLER.end(acc)
228
+ with acc.lock:
229
+ acc.closed = True
230
+ samples = sorted(acc.samples.items(), key=lambda item: -item[1])[:18]
231
+ _save_stats(scope, status, elapsed)
232
+ if elapsed >= threshold:
233
+ endpoint = scope.get("endpoint") or scope.get("_timing_endpoint")
234
+ values = acc.snapshot()
235
+ values["app_ms"] = round(max(0., elapsed - values["queue_ms"] - values["store_ms"]
236
+ - values["duckdb_ms"]), 2)
237
+ values.update(method=scope.get("method", ""), route=route_template(scope), status=status,
238
+ total_ms=round(elapsed, 2), failed=failed, disconnected=disconnected,
239
+ complete=complete, endpoint=getattr(endpoint, "__name__", "<unresolved>"),
240
+ samples={name: round(count * _SAMPLER.INTERVAL * 1000, 2) for name, count in samples})
241
+ LOG.warning("[slow-request] %s", json.dumps(values, separators=(",", ":"), ensure_ascii=True))
api/routes_admin.py CHANGED
@@ -2805,6 +2805,7 @@ def settings(session: Session = Depends(require_session)):
2805
  modules = perms.allowed_modules(session.user)
2806
  from core import freeze_notice
2807
  import process_role
 
2808
  roles = process_role.status()
2809
  worker = roles["worker"]
2810
  if not perms.is_admin(session.user):
@@ -2813,6 +2814,7 @@ def settings(session: Session = Depends(require_session)):
2813
  return {
2814
  # Which process role answered, and whether its sync worker is alive and beating.
2815
  "processRole": roles["role"],
 
2816
  "worker": worker,
2817
  "freezeNotice": freeze_notice.notice(),
2818
  # The build this Space is running. Behind the session on purpose — see `main.py::health`,
 
2805
  modules = perms.allowed_modules(session.user)
2806
  from core import freeze_notice
2807
  import process_role
2808
+ from request_timing import route_stats
2809
  roles = process_role.status()
2810
  worker = roles["worker"]
2811
  if not perms.is_admin(session.user):
 
2814
  return {
2815
  # Which process role answered, and whether its sync worker is alive and beating.
2816
  "processRole": roles["role"],
2817
+ "requestTiming": route_stats() if perms.is_admin(session.user) else None,
2818
  "worker": worker,
2819
  "freezeNotice": freeze_notice.notice(),
2820
  # The build this Space is running. Behind the session on purpose — see `main.py::health`,
api/routes_odoo_tables.py CHANGED
@@ -3771,7 +3771,7 @@ def _compiled_read(table_key, session, *, filters=None, filterConj="and", sorts=
3771
 
3772
 
3773
  @contextmanager
3774
- def _materialised_read(table_key, session, query, related, ids=None):
3775
  """One compiler for native rows and their aggregate sibling, over the walled payload.
3776
 
3777
  The temporary connection contains only this request's already stripped, pinned rows and is
@@ -3779,7 +3779,9 @@ def _materialised_read(table_key, session, query, related, ids=None):
3779
  """
3780
  import duckdb
3781
 
3782
- if table_key == "customer_data":
 
 
3783
  from routes_customers import _payload
3784
  session.require(table_key)
3785
  payload = _payload(session, related=related)
@@ -3796,7 +3798,8 @@ def _materialised_read(table_key, session, query, related, ids=None):
3796
  if int(row["pid"]) in wanted]}
3797
  fields = payload["fields"]
3798
  with duckdb.connect(":memory:") as cur:
3799
- cur.execute("CREATE TABLE pinned_rows AS SELECT value AS payload FROM json_each(?)",
 
3800
  [json.dumps(payload["rows"], default=str)])
3801
  bindings = {}
3802
  for field in fields:
@@ -3901,7 +3904,20 @@ def _materialised_window(table_key, session, offset, limit, filters, filterConj,
3901
 
3902
  query = _selection_query({"filters": _json_arg(filters, "filters"), "filterConj": filterConj,
3903
  "sorts": _json_arg(sorts, "sorts"), "search": search})
3904
- with _materialised_read(table_key, session, query, related, ids=ids) as read:
 
 
 
 
 
 
 
 
 
 
 
 
 
3905
  spec = read["spec"]
3906
  order = _fs().compile_order_by(query["sorts"], read["cols"], tiebreak_sql=spec["id"])
3907
  where, params = read["where"], list(read["params"])
@@ -3909,21 +3925,57 @@ def _materialised_window(table_key, session, offset, limit, filters, filterConj,
3909
  if gsql:
3910
  where = f"({where}) AND {gsql}" if where else gsql
3911
  params.extend(gparams)
3912
- win = datastore.window(select=f'{spec["id"]} AS pid', where=where,
3913
  params=tuple(params), order_by=order or spec["id"],
3914
- offset=offset, limit=limit or None, cur=read["cur"], **read["frm"])
3915
  payload = read["payload"]
3916
- by_id = {int(row["pid"]): row for row in payload["rows"]}
3917
- rows = [by_id[int(row[0])] for row in win["rows"]]
3918
  if win["clamped"]:
3919
  read["limits"].append({"subject": "limit", "effect": "window_clamped",
3920
  "cause": f"One response carries at most {datastore.WINDOW_MAX} rows to bound response memory.",
3921
  "recommendation": "Continue paging with offset. Every matching row remains available."})
3922
  return {**payload, "ok": True, "rows": rows, "total": win["total"],
3923
- "totalUnfiltered": len(by_id), "offset": win["offset"], "limit": win["limit"],
 
3924
  "limits": read["limits"], "scope": {"table": table_key, "readThrough": True}}
3925
 
3926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3927
  @router.get("/odoo-tables/{table_key}/rows")
3928
  @router.post("/odoo-tables/{table_key}/rows")
3929
  def odoo_table_rows(table_key: str,
 
3771
 
3772
 
3773
  @contextmanager
3774
+ def _materialised_read(table_key, session, query, related, ids=None, *, payload=None):
3775
  """One compiler for native rows and their aggregate sibling, over the walled payload.
3776
 
3777
  The temporary connection contains only this request's already stripped, pinned rows and is
 
3779
  """
3780
  import duckdb
3781
 
3782
+ if payload is not None:
3783
+ pass
3784
+ elif table_key == "customer_data":
3785
  from routes_customers import _payload
3786
  session.require(table_key)
3787
  payload = _payload(session, related=related)
 
3798
  if int(row["pid"]) in wanted]}
3799
  fields = payload["fields"]
3800
  with duckdb.connect(":memory:") as cur:
3801
+ cur.execute("CREATE TABLE pinned_rows AS SELECT CAST(key AS BIGINT) AS _row_index, "
3802
+ "value AS payload FROM json_each(?)",
3803
  [json.dumps(payload["rows"], default=str)])
3804
  bindings = {}
3805
  for field in fields:
 
3904
 
3905
  query = _selection_query({"filters": _json_arg(filters, "filters"), "filterConj": filterConj,
3906
  "sorts": _json_arg(sorts, "sorts"), "search": search})
3907
+ payload = None
3908
+ window_offset = offset
3909
+ if table_key == 'ut_activity':
3910
+ payload, offset, limit = _activity_window_payload(session, offset, limit, related)
3911
+ if ids is not None:
3912
+ wanted = set(ids)
3913
+ payload = dict(payload, rows=[row for row in payload['rows'] if int(row['pid']) in wanted])
3914
+ if not (query['filters'] or query['sorts'] or query['search']
3915
+ or (isinstance(groupBy, str) and groupBy.strip())):
3916
+ return {**payload, 'ok': True, 'total': len(payload['rows']),
3917
+ 'totalUnfiltered': len(payload['rows']), 'offset': offset, 'limit': limit,
3918
+ 'scope': {'table': table_key, 'readThrough': True}}
3919
+ window_offset = 0
3920
+ with _materialised_read(table_key, session, query, related, ids=ids, payload=payload) as read:
3921
  spec = read["spec"]
3922
  order = _fs().compile_order_by(query["sorts"], read["cols"], tiebreak_sql=spec["id"])
3923
  where, params = read["where"], list(read["params"])
 
3925
  if gsql:
3926
  where = f"({where}) AND {gsql}" if where else gsql
3927
  params.extend(gparams)
3928
+ win = datastore.window(select='_row_index', where=where,
3929
  params=tuple(params), order_by=order or spec["id"],
3930
+ offset=window_offset, limit=limit or None, cur=read["cur"], **read["frm"])
3931
  payload = read["payload"]
3932
+ rows = [payload['rows'][int(row[0])] for row in win['rows']]
 
3933
  if win["clamped"]:
3934
  read["limits"].append({"subject": "limit", "effect": "window_clamped",
3935
  "cause": f"One response carries at most {datastore.WINDOW_MAX} rows to bound response memory.",
3936
  "recommendation": "Continue paging with offset. Every matching row remains available."})
3937
  return {**payload, "ok": True, "rows": rows, "total": win["total"],
3938
+ "totalUnfiltered": len(payload['rows']),
3939
+ "offset": offset if table_key == 'ut_activity' else win["offset"], "limit": win["limit"],
3940
  "limits": read["limits"], "scope": {"table": table_key, "readThrough": True}}
3941
 
3942
 
3943
+ def _activity_window_payload(session, offset, limit, related):
3944
+ """Walk native cursors to the requested offset without retaining skipped event values."""
3945
+ from core import activity_log
3946
+ from harness import datastore
3947
+ from routes_tables import table_rows
3948
+
3949
+ offset = max(0, int(offset or 0))
3950
+ requested = int(limit or datastore.WINDOW_DEFAULT)
3951
+ limit = min(max(requested, 1), datastore.WINDOW_MAX, activity_log.MAX_PAGE)
3952
+ remaining, cursor = offset, None
3953
+ for _ in range(16):
3954
+ size = min(remaining, activity_log.MAX_PAGE) if remaining else limit
3955
+ payload = table_rows('ut_activity', session=session, related=related, cursor=cursor, limit=size)
3956
+ next_cursor = payload.get('next_cursor')
3957
+ if next_cursor is not None and next_cursor == cursor:
3958
+ raise err(503, 'activity_cursor_stalled', 'Reload Activity to continue reading events.')
3959
+ if not remaining:
3960
+ break
3961
+ remaining -= len(payload['rows'])
3962
+ if next_cursor is None:
3963
+ payload = dict(payload, rows=[])
3964
+ break
3965
+ cursor = next_cursor
3966
+ else:
3967
+ raise HTTPException(status_code=409, detail={'error': {
3968
+ 'code': 'activity_cursor_required',
3969
+ 'message': 'Continue this deep Activity window through the native cursor route.'},
3970
+ 'next_cursor': cursor})
3971
+ if requested != limit:
3972
+ payload = dict(payload, limits=[*(payload.get('limits') or []), {
3973
+ 'subject': 'limit', 'effect': 'window_clamped',
3974
+ 'cause': f'One response carries at most {limit} Activity rows.',
3975
+ 'recommendation': 'Continue with the returned Activity cursor or the next offset.'}])
3976
+ return payload, offset, limit
3977
+
3978
+
3979
  @router.get("/odoo-tables/{table_key}/rows")
3980
  @router.post("/odoo-tables/{table_key}/rows")
3981
  def odoo_table_rows(table_key: str,
api/routes_sheet.py CHANGED
@@ -1570,8 +1570,21 @@ def activity_read_policy(session: Session, object):
1570
  source_identity = identity[:3] + (databases,)
1571
  sources = memo.setdefault("activity_sources", {}) if isinstance(memo, dict) else {}
1572
  admitted = sources.setdefault(source_identity, {})
 
 
1573
 
1574
  def cell_allowed(cell):
 
 
 
 
 
 
 
 
 
 
 
1575
  if cell is None:
1576
  return True
1577
  if not isinstance(cell, dict):
 
1570
  source_identity = identity[:3] + (databases,)
1571
  sources = memo.setdefault("activity_sources", {}) if isinstance(memo, dict) else {}
1572
  admitted = sources.setdefault(source_identity, {})
1573
+ policies = memo.setdefault("activity_bindings", {}) if isinstance(memo, dict) else {}
1574
+ bindings = policies.setdefault(source_identity, {})
1575
 
1576
  def cell_allowed(cell):
1577
+ if not isinstance(cell, dict) or cell.get("kind") != "bound":
1578
+ return resolve_cell(cell)
1579
+ signature = json.dumps(cell, sort_keys=True, separators=(",", ":"), allow_nan=False)
1580
+ if signature not in bindings:
1581
+ result = resolve_cell(cell)
1582
+ if len(bindings) >= 256:
1583
+ bindings.clear()
1584
+ bindings[signature] = result
1585
+ return bindings[signature]
1586
+
1587
+ def resolve_cell(cell):
1588
  if cell is None:
1589
  return True
1590
  if not isinstance(cell, dict):
api/routes_tables.py CHANGED
@@ -704,7 +704,7 @@ def _query_activity_page(session: Session, *, object=None, by=None, since=None,
704
  continue
705
  source_pids[database] = frozenset(str(pid) for pid in contexts[database]['pids'])
706
  scoped = perm_scope.apply_row_scope(raw['records'], session.user, key, own['fields'],
707
- st=session.runtime, defn=own)
708
  for event in scoped:
709
  obj = event['object']
710
  clean = None
@@ -726,7 +726,7 @@ def _query_activity_page(session: Session, *, object=None, by=None, since=None,
726
  declared = {f['key'] for f in (g.get('defn') or {}).get('fields', g['fields'])}
727
  if field_key not in declared and isinstance(previous, dict):
728
  corpus = list(g['fields']) + [previous]
729
- hidden = _ut_hidden(session, database, corpus, st=session.runtime)
730
  if field_key not in hidden and field_key not in g.get('hidden', ()):
731
  visible = visible | {field_key}
732
  admitted = field_key in visible
@@ -737,7 +737,7 @@ def _query_activity_page(session: Session, *, object=None, by=None, since=None,
737
  and database not in connected
738
  and changes and all(pair.get('after') is None for pair in changes.values())
739
  and not perm_scope.row_scope_applies(
740
- session.user, database, st=session.runtime, defn=g['defn'])):
741
  admitted = True
742
  if admitted:
743
  clean = (changes if changes.keys() <= visible else
 
704
  continue
705
  source_pids[database] = frozenset(str(pid) for pid in contexts[database]['pids'])
706
  scoped = perm_scope.apply_row_scope(raw['records'], session.user, key, own['fields'],
707
+ st=context_store, defn=own)
708
  for event in scoped:
709
  obj = event['object']
710
  clean = None
 
726
  declared = {f['key'] for f in (g.get('defn') or {}).get('fields', g['fields'])}
727
  if field_key not in declared and isinstance(previous, dict):
728
  corpus = list(g['fields']) + [previous]
729
+ hidden = _ut_hidden(session, database, corpus, st=context_store)
730
  if field_key not in hidden and field_key not in g.get('hidden', ()):
731
  visible = visible | {field_key}
732
  admitted = field_key in visible
 
737
  and database not in connected
738
  and changes and all(pair.get('after') is None for pair in changes.values())
739
  and not perm_scope.row_scope_applies(
740
+ session.user, database, st=context_store, defn=g['defn'])):
741
  admitted = True
742
  if admitted:
743
  clean = (changes if changes.keys() <= visible else
platform/core/activity_log.py CHANGED
@@ -5,6 +5,9 @@ from contextlib import contextmanager
5
  from contextvars import ContextVar
6
  from datetime import datetime, timezone
7
  import json
 
 
 
8
  import re
9
 
10
  DATABASE_KEY = 'ut_activity'
@@ -27,8 +30,8 @@ _READ_SCOPE = ContextVar('activity_read_scope', default=None)
27
 
28
  @contextmanager
29
  def read_scope(rt):
30
- """Share one outbox drain and bounded history pages within one read-only request."""
31
- state = {'runtime': rt, 'backend': None, 'drained': False, 'now': None,
32
  'pending': [], 'histories': {}}
33
  token = _READ_SCOPE.set(state)
34
  try:
@@ -228,9 +231,10 @@ def drain(rt, *, keys=None):
228
  pending = table.get(OUTBOX_KEY) or {}
229
  delivered = {}
230
  try:
231
- for key, entry in pending.items():
232
- if keys is not None and key not in keys:
233
- continue
 
234
  append(rt, **entry)
235
  delivered[key] = entry['_delivery']['version']
236
  finally:
@@ -249,6 +253,68 @@ def drain(rt, *, keys=None):
249
  return len(delivered)
250
 
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  def prune(rt, *, limit=PRUNE_BATCH, now=None):
253
  """Remove a bounded oldest-first batch strictly older than thirteen calendar months."""
254
  if type(limit) is not int or not 1 <= limit <= MAX_SCAN:
@@ -287,10 +353,12 @@ def read(rt, *, can_read_object, object=None, by=None, since=None, until=None,
287
  or type(cursor[1]) is not int or cursor[1] < 1):
288
  raise ValueError('Use the next_cursor returned by the activity reader.')
289
  before = (_time(cursor[0]).isoformat(), cursor[1])
290
- if scope is None or not scope['drained']:
291
- drain(rt)
292
- if scope is not None:
293
- scope['drained'], scope['now'] = True, now
 
 
294
  prefetched = None
295
  if scope is not None and whole_history:
296
  workbook = target[1]
@@ -310,6 +378,17 @@ def read(rt, *, can_read_object, object=None, by=None, since=None, until=None,
310
  batch = backend.page(target=target, actor=actor, since=since.isoformat(),
311
  until=until.isoformat(), before=before,
312
  limit=batch_limit)
 
 
 
 
 
 
 
 
 
 
 
313
  if not batch:
314
  exhausted = True
315
  break
@@ -422,10 +501,12 @@ class PgActivityStore:
422
  columns = _COLUMNS[1:]
423
  conflict = ''
424
  if delivery is not None:
 
 
425
  columns += ('delivery_key', 'delivery_version')
426
  values = (*values, *delivery)
427
  assignments = ', '.join(f'{column} = EXCLUDED.{column}' for column in columns
428
- if column != 'delivery_key')
429
  conflict = (f' ON CONFLICT (delivery_key) DO UPDATE SET {assignments} '
430
  f'WHERE {self.table}.delivery_version < EXCLUDED.delivery_version')
431
  stored = con.execute(
@@ -501,6 +582,26 @@ class PgActivityStore:
501
  events.append(_event(row[:-1], decoded_json=(obj_id, changes)))
502
  return events
503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
504
  def workbook_pages(self, workbooks, *, since, until, limit):
505
  """Read bounded first history pages through the existing tenant and object-time index."""
506
  if not 1 <= len(workbooks) <= 16 or not 1 <= limit <= 256:
 
5
  from contextvars import ContextVar
6
  from datetime import datetime, timezone
7
  import json
8
+ import hashlib
9
+ import heapq
10
+ from itertools import islice
11
  import re
12
 
13
  DATABASE_KEY = 'ut_activity'
 
30
 
31
  @contextmanager
32
  def read_scope(rt):
33
+ """Share committed outbox input and bounded history pages within one read-only request."""
34
+ state = {'runtime': rt, 'backend': None, 'outbox': None, 'now': None,
35
  'pending': [], 'histories': {}}
36
  token = _READ_SCOPE.set(state)
37
  try:
 
231
  pending = table.get(OUTBOX_KEY) or {}
232
  delivered = {}
233
  try:
234
+ # A successful writer also retries older committed deliveries left by failed writers.
235
+ ordered = sorted(pending, key=lambda key: key not in keys) if keys is not None else pending
236
+ for key in ordered:
237
+ entry = pending[key]
238
  append(rt, **entry)
239
  delivered[key] = entry['_delivery']['version']
240
  finally:
 
253
  return len(delivered)
254
 
255
 
256
+ def _delivery_id(key):
257
+ """Reserve an exact JSON integer for a delivery before its first persistent insert."""
258
+ digest = hashlib.sha256(_text(key, 'delivery key').encode()).digest()
259
+ return (1 << 52) | (int.from_bytes(digest[:8], 'big') & ((1 << 52) - 1))
260
+
261
+
262
+ def _outbox(rt):
263
+ """Read only this tenant's private committed Activity entry."""
264
+ from core import user_tables
265
+ while isinstance(rt, user_tables._Lent):
266
+ rt = rt._runtime
267
+ target = rt._store() if callable(getattr(rt, '_store', None)) else rt
268
+ if callable(getattr(rt, 'get_entry', None)):
269
+ target = rt
270
+ reader = getattr(target, 'get_entry', None)
271
+ if callable(reader):
272
+ table = reader(user_tables.STORE_KEY, DATABASE_KEY) or {}
273
+ elif isinstance(target, FixtureAdapter) and not callable(getattr(target, 'get', None)):
274
+ return {}
275
+ else:
276
+ table = (target.get(user_tables.STORE_KEY) or {}).get(DATABASE_KEY) or {}
277
+ return table.get(OUTBOX_KEY) or {}
278
+
279
+
280
+ def _pending_page(backend, pending, *, target, actor, since, until, before, limit):
281
+ """Project a bounded pending page with persistent delivery identities and version deduplication."""
282
+ def events():
283
+ entries = iter(pending.items())
284
+ while chunk := list(islice(entries, 256)):
285
+ delivered = backend.deliveries([key for key, _ in chunk])
286
+ occupied = {receipt['id']: key for key, (_, receipt) in delivered.items()}
287
+ for key, entry in chunk:
288
+ delivery = entry['_delivery']
289
+ if delivery['key'] != key:
290
+ raise ValueError('Activity delivery key does not match its committed intent.')
291
+ prior = delivered.get(key)
292
+ stamp = _time(delivery['occurred_at']).isoformat()
293
+ kind, name, identity = _object(entry['object'])
294
+ event_id = prior[1]['id'] if prior is not None else _delivery_id(key)
295
+ if event_id in occupied and occupied[event_id] != key:
296
+ raise ValueError('Activity delivery identity conflicts with another event.')
297
+ if (not since <= stamp <= until or (actor is not None and entry['by'] != actor)
298
+ or (target is not None and (kind, name) != target[:2])
299
+ or (target is not None and target[2] is not None and identity != target[2])):
300
+ continue
301
+ if before is not None and (stamp, event_id) >= before:
302
+ continue
303
+ if prior is not None and prior[0] >= delivery['version']:
304
+ yield prior[1]
305
+ continue
306
+ changes, truncated, original_bytes = _bounded_changes(entry.get('changes'))
307
+ yield _event((event_id, stamp, entry['kind'], kind, name, identity,
308
+ entry['by'], entry.get('via'), entry.get('on_behalf_of'),
309
+ changes, truncated, original_bytes),
310
+ decoded_json=(_decode_json(identity), changes))
311
+
312
+ page = heapq.nlargest(limit + 1, events(), key=lambda event: (event['occurred_at'], event['id']))
313
+ if len({event['id'] for event in page}) != len(page):
314
+ raise ValueError('Activity pending deliveries have conflicting identities.')
315
+ return EventPage(page[:limit], has_more=len(page) > limit)
316
+
317
+
318
  def prune(rt, *, limit=PRUNE_BATCH, now=None):
319
  """Remove a bounded oldest-first batch strictly older than thirteen calendar months."""
320
  if type(limit) is not int or not 1 <= limit <= MAX_SCAN:
 
353
  or type(cursor[1]) is not int or cursor[1] < 1):
354
  raise ValueError('Use the next_cursor returned by the activity reader.')
355
  before = (_time(cursor[0]).isoformat(), cursor[1])
356
+ if scope is not None:
357
+ if scope['outbox'] is None:
358
+ scope['outbox'], scope['now'] = _outbox(rt), now
359
+ pending = scope['outbox']
360
+ else:
361
+ pending = _outbox(rt)
362
  prefetched = None
363
  if scope is not None and whole_history:
364
  workbook = target[1]
 
378
  batch = backend.page(target=target, actor=actor, since=since.isoformat(),
379
  until=until.isoformat(), before=before,
380
  limit=batch_limit)
381
+ if pending:
382
+ overlay = _pending_page(backend, pending, target=target, actor=actor,
383
+ since=since.isoformat(), until=until.isoformat(),
384
+ before=before, limit=batch_limit)
385
+ merged = {event['id']: event for event in batch}
386
+ merged.update((event['id'], event) for event in overlay)
387
+ ordered = heapq.nlargest(batch_limit + 1, merged.values(),
388
+ key=lambda event: (event['occurred_at'], event['id']))
389
+ batch = EventPage(ordered[:batch_limit], has_more=(len(ordered) > batch_limit
390
+ or getattr(batch, 'has_more', len(batch) == batch_limit)
391
+ or overlay.has_more))
392
  if not batch:
393
  exhausted = True
394
  break
 
501
  columns = _COLUMNS[1:]
502
  conflict = ''
503
  if delivery is not None:
504
+ columns = ('id', *columns)
505
+ values = (_delivery_id(delivery[0]), *values)
506
  columns += ('delivery_key', 'delivery_version')
507
  values = (*values, *delivery)
508
  assignments = ', '.join(f'{column} = EXCLUDED.{column}' for column in columns
509
+ if column not in ('id', 'delivery_key'))
510
  conflict = (f' ON CONFLICT (delivery_key) DO UPDATE SET {assignments} '
511
  f'WHERE {self.table}.delivery_version < EXCLUDED.delivery_version')
512
  stored = con.execute(
 
582
  events.append(_event(row[:-1], decoded_json=(obj_id, changes)))
583
  return events
584
 
585
+ def deliveries(self, keys):
586
+ """Resolve at most 256 committed delivery receipts without changing the store."""
587
+ if not keys or len(keys) > 256:
588
+ raise ValueError('Read between 1 and 256 delivery receipts.')
589
+ columns = ', '.join('CAST(changes AS TEXT) AS changes' if col == 'changes' else col
590
+ for col in _COLUMNS)
591
+ try:
592
+ with self._connection() as con:
593
+ reserved = [_delivery_id(key) for key in keys]
594
+ placeholders = ', '.join(['%s'] * len(keys))
595
+ values = con.execute(f'SELECT {columns}, delivery_key, delivery_version '
596
+ f'FROM {self.table} WHERE delivery_key IN '
597
+ f'({placeholders}) OR id IN ({placeholders})',
598
+ [*keys, *reserved]).fetchall()
599
+ except Exception as exc:
600
+ if getattr(exc, 'sqlstate', None) != '42P01':
601
+ raise
602
+ return {}
603
+ return {row[-2]: (row[-1], _event(row[:-2])) for row in values}
604
+
605
  def workbook_pages(self, workbooks, *, since, until, limit):
606
  """Read bounded first history pages through the existing tenant and object-time index."""
607
  if not 1 <= len(workbooks) <= 16 or not 1 <= limit <= 256:
platform/core/request_timing.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Request-local measurements shared by async tasks and copied threadpool contexts."""
2
+ from __future__ import annotations
3
+
4
+ import re
5
+ import hashlib
6
+ import threading
7
+ import time
8
+ from collections import Counter
9
+ from contextlib import contextmanager
10
+ from contextvars import ContextVar
11
+ from functools import wraps
12
+
13
+ CURRENT = ContextVar("aios_request_timing", default=None)
14
+ _STORE_SPAN = ContextVar("aios_store_timing_span", default=None)
15
+ _THREAD_LOCK = threading.Lock()
16
+ _THREADS = {}
17
+ _SQL_VALUES = re.compile(
18
+ r"--[^\n]*|/\*.*?\*/|\$([A-Za-z_][A-Za-z_0-9]*|)\$.*?\$\1\$|"
19
+ r"(?:[eEbBxX])?'(?:''|\\.|[^'])*'|\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b", re.S)
20
+
21
+
22
+ class Accumulator:
23
+ """Mutable request state, including measurements from anyio's copied contexts."""
24
+
25
+ def __init__(self):
26
+ self.lock = threading.RLock()
27
+ self.started = time.perf_counter()
28
+ self.closed = False
29
+ self.queue_ms = 0.0
30
+ self.store_ms = 0.0
31
+ self.store_reads = 0
32
+ self.store_bytes = 0
33
+ self.store_write_bytes = 0
34
+ self.store_operations = Counter()
35
+ self.store_keys = {}
36
+ self.duckdb_ms = 0.0
37
+ self.duckdb_count = 0
38
+ self.slowest_sql = ""
39
+ self.slowest_sql_ms = 0.0
40
+ self.response_bytes = 0
41
+ self.samples = Counter()
42
+
43
+ def snapshot(self):
44
+ with self.lock:
45
+ return {"queue_ms": self.queue_ms, "store_ms": self.store_ms,
46
+ "store_reads": self.store_reads, "store_bytes": self.store_bytes,
47
+ "store_write_bytes": self.store_write_bytes,
48
+ "store_operations": dict(self.store_operations),
49
+ "store_keys": {key: dict(value) for key, value in self.store_keys.items()},
50
+ "duckdb_ms": self.duckdb_ms, "duckdb_count": self.duckdb_count,
51
+ "slowest_sql": self.slowest_sql, "slowest_sql_ms": self.slowest_sql_ms,
52
+ "response_bytes": self.response_bytes}
53
+
54
+
55
+ @contextmanager
56
+ def request_scope(accumulator=None):
57
+ """Bind a mutable accumulator and restore the caller's context on every exit."""
58
+ acc = accumulator if accumulator is not None else Accumulator()
59
+ token = CURRENT.set(acc)
60
+ try:
61
+ yield acc
62
+ finally:
63
+ CURRENT.reset(token)
64
+
65
+
66
+ @contextmanager
67
+ def thread_scope():
68
+ """Associate only this operation's thread with the current request for sampling."""
69
+ acc = CURRENT.get()
70
+ if acc is None or acc.closed:
71
+ yield
72
+ return
73
+ tid = threading.get_ident()
74
+ with _THREAD_LOCK:
75
+ previous = _THREADS.get(tid)
76
+ _THREADS[tid] = acc
77
+ try:
78
+ yield
79
+ finally:
80
+ with _THREAD_LOCK:
81
+ if previous is None:
82
+ _THREADS.pop(tid, None)
83
+ else:
84
+ _THREADS[tid] = previous
85
+
86
+
87
+ def associated_threads():
88
+ with _THREAD_LOCK:
89
+ return dict(_THREADS)
90
+
91
+
92
+ def text_bytes(value):
93
+ """Measure document payload bytes without serializing decoded caller objects."""
94
+ if isinstance(value, str):
95
+ return len(value.encode("utf-8"))
96
+ if isinstance(value, (bytes, bytearray, memoryview)):
97
+ return len(value)
98
+ return 0
99
+
100
+
101
+ def record_store(operation, keys, milliseconds=0.0, *, bytes_read=0, bytes_written=0, count=1):
102
+ """Record exclusive operation time and measured payload bytes, without document values."""
103
+ acc = CURRENT.get()
104
+ if acc is None or acc.closed:
105
+ return
106
+ keys = [keys] if isinstance(keys, str) else list(keys)
107
+ keys = keys or ["<none>"]
108
+ with acc.lock:
109
+ if acc.closed:
110
+ return
111
+ acc.store_ms += milliseconds
112
+ acc.store_operations[operation] += count
113
+ acc.store_reads += count if operation != "update" else 0
114
+ acc.store_bytes += bytes_read
115
+ acc.store_write_bytes += bytes_written
116
+ for key in keys:
117
+ key = str(key)
118
+ if not re.fullmatch(r"[a-z][a-z0-9_]{0,63}", key):
119
+ prefix = re.split(r"[/.:]", key, maxsplit=1)[0]
120
+ prefix = prefix if re.fullmatch(r"[a-z][a-z_]{0,31}", prefix) else "document"
121
+ key = prefix + "#" + hashlib.sha256(key.encode("utf-8")).hexdigest()[:12]
122
+ if key not in acc.store_keys and len(acc.store_keys) >= 64:
123
+ key = "<other>"
124
+ row = acc.store_keys.setdefault(key, {"count": 0, "ms": 0.0, "bytes": 0, "write_bytes": 0})
125
+ row["count"] += count
126
+ row["ms"] += milliseconds / len(keys)
127
+ row["bytes"] += bytes_read / len(keys)
128
+ row["write_bytes"] += bytes_written / len(keys)
129
+
130
+
131
+ def store_operation(operation, *, many=False):
132
+ """Measure caller wait, acquisition and parse once, excluding nested store operation time."""
133
+ def decorate(fn):
134
+ @wraps(fn)
135
+ def measured(*args, **kwargs):
136
+ if CURRENT.get() is None:
137
+ return fn(*args, **kwargs)
138
+ keys = args[0] if args else kwargs.get("names" if many else "name", "<none>")
139
+ if many:
140
+ keys = tuple(dict.fromkeys(str(key) for key in (keys or ()) if str(key)))
141
+ if args:
142
+ args = (keys,) + args[1:]
143
+ else:
144
+ kwargs["names"] = keys
145
+ else:
146
+ keys = [str(keys)]
147
+ parent = _STORE_SPAN.get()
148
+ span = [0.0]
149
+ token = _STORE_SPAN.set(span)
150
+ started = time.perf_counter()
151
+ try:
152
+ with thread_scope():
153
+ return fn(*args, **kwargs)
154
+ finally:
155
+ elapsed = (time.perf_counter() - started) * 1000
156
+ _STORE_SPAN.reset(token)
157
+ if parent is not None:
158
+ parent[0] += elapsed
159
+ record_store(operation, keys, max(0.0, elapsed - span[0]))
160
+ return measured
161
+ return decorate
162
+
163
+
164
+ def sql_text(statement):
165
+ """Keep bounded SQL structure and remove inline literals and comments."""
166
+ if not isinstance(statement, str):
167
+ return "<statement>"
168
+ return " ".join(_SQL_VALUES.sub("?", statement).split())[:300]
169
+
170
+
171
+ def record_duckdb(statement, milliseconds, *, count=1, statement_ms=None):
172
+ """Record execution or fetch time without accepting a parameter argument."""
173
+ acc = CURRENT.get()
174
+ if acc is None or acc.closed:
175
+ return
176
+ with acc.lock:
177
+ if acc.closed:
178
+ return
179
+ acc.duckdb_ms += milliseconds
180
+ acc.duckdb_count += count
181
+ duration = milliseconds if statement_ms is None else statement_ms
182
+ if duration >= acc.slowest_sql_ms:
183
+ acc.slowest_sql_ms = duration
184
+ acc.slowest_sql = sql_text(statement)
185
+
186
+
187
+ def record_queue(milliseconds):
188
+ acc = CURRENT.get()
189
+ if acc is not None and not acc.closed:
190
+ with acc.lock:
191
+ if not acc.closed:
192
+ acc.queue_ms += milliseconds
platform/core/store_pg.py CHANGED
@@ -54,6 +54,7 @@ from urllib.parse import urlparse
54
 
55
  import core.data_binding as data_binding
56
  from core import freeze_notice
 
57
  from core.user_tables import enforce_byte_budget as _BYTE_BUDGET_ENFORCER
58
 
59
  _URL_ENV = 'DATABASE_URL'
@@ -544,6 +545,7 @@ def _parsed(text):
544
  return dict(value) if isinstance(value, dict) else value
545
 
546
 
 
547
  def get(name, fresh=False, tenant_slug=None):
548
  """The value stored under `name`, or `{}` when the key is genuinely absent.
549
 
@@ -555,7 +557,9 @@ def get(name, fresh=False, tenant_slug=None):
555
  row = con.execute(
556
  _q('SELECT value::text FROM {tbl} WHERE key = %s', 'store_kv', tenant_slug),
557
  (str(name),)).fetchone()
558
- return row[0] if row else None
 
 
559
 
560
  return _parsed(fetch() if fresh else _shared_read(name, None, tenant_slug, fetch))
561
 
@@ -583,6 +587,7 @@ def _remember_projection(key, revision, stamp, text):
583
  _PROJECTION_TEXT_BYTES -= removed[3]
584
 
585
 
 
586
  def get_projection(name, drop=(), tenant_slug=None):
587
  """`core.store.get_projection` with the DROP performed inside Postgres.
588
 
@@ -614,7 +619,10 @@ def get_projection(name, drop=(), tenant_slug=None):
614
  row = con.execute(
615
  _q('SELECT value::text FROM {tbl} WHERE key = %s', 'store_kv', tenant_slug),
616
  (str(name),)).fetchone()
617
- return row[0] if row else None
 
 
 
618
  row = con.execute(
619
  _q('SELECT kv.rev, kv.updated_at, CASE '
620
  'WHEN kv.rev = %s AND kv.updated_at = %s THEN NULL ELSE (CASE '
@@ -629,6 +637,8 @@ def get_projection(name, drop=(), tenant_slug=None):
629
  list(drops), str(name))).fetchone()
630
  # The SELECT always checks the live row. Only its unchanged JSON reconstruction is reused.
631
  text = row[2] if row else None
 
 
632
  if row and text is None:
633
  if cached is None or row[:2] != cached[:2]:
634
  raise RuntimeError('A projected read returned no content for its current revision')
@@ -769,6 +779,7 @@ def _amazon_backup_policy(*, trigger_at, checked_at, tenant_slug=None):
769
  'cause': 'provider_backup_expiry_verification_unavailable'}
770
 
771
 
 
772
  def _get_projections(names, drop=(), tenant_slug=None, timeout_s=None):
773
  """Several projected documents in one round trip, keyed by their requested store key.
774
 
@@ -791,16 +802,20 @@ def _get_projections(names, drop=(), tenant_slug=None, timeout_s=None):
791
  con.execute("SELECT set_config('statement_timeout', %s, true)",
792
  (str(timeout_ms),))
793
  rows = con.execute(
794
- _q('SELECT kv.key, CASE '
795
  "WHEN jsonb_typeof(kv.value) = 'object' THEN "
796
  "COALESCE((SELECT jsonb_object_agg(e.entry_key, "
797
  "CASE WHEN jsonb_typeof(e.entry_value) = 'object' "
798
  'THEN e.entry_value - %s::text[] ELSE e.entry_value END) '
799
  'FROM jsonb_each(kv.value) AS e(entry_key, entry_value)), '
800
- "'{{}}'::jsonb) ELSE kv.value END FROM {tbl} AS kv "
801
  'WHERE kv.key = ANY(%s::text[])', 'store_kv', tenant_slug),
802
  (list(drops), keys)).fetchall()
803
- found = {str(key): value for key, value in rows}
 
 
 
 
804
  dropped = frozenset(drops)
805
  return {
806
  key: _hf._project(
@@ -957,6 +972,7 @@ def put(name, data, tenant_slug=None):
957
  return data
958
 
959
 
 
960
  def update(name, fn, flush='sync', tenant_slug=None, expect_rev=None, audit_row=None):
961
  """Read-modify-write inside ONE transaction, with the row LOCKED for the duration.
962
 
@@ -1017,19 +1033,21 @@ def _update_locked(name, fn, tenant_slug, expect_rev, audit_row):
1017
  (f'{int(wait_s * 1000)}ms',))
1018
  try:
1019
  row = con.execute(
1020
- _q('SELECT value, rev FROM {tbl} WHERE key = %s FOR UPDATE',
1021
  'store_kv', tenant_slug),
1022
  (str(name),)).fetchone()
1023
  except LockNotAvailable:
1024
  print(f'[store-pg] lock wait over {wait_s:g}s on '
1025
  f'{_effective_slug(tenant_slug)}/{name}', flush=True)
1026
  raise
 
 
1027
  if expect_rev is not None:
1028
  current = int(row[1]) if row and row[1] is not None else 0
1029
  if current != int(expect_rev):
1030
  from core.view_audit import ViewRevisionStale
1031
  raise ViewRevisionStale(rev_current=current, rev_sent=int(expect_rev))
1032
- data = dict(row[0]) if row and isinstance(row[0], dict) else (row[0] if row else {})
1033
  result = fn(data)
1034
  data = result if result is not None else data
1035
  payload = _payload(name, data, con, tenant_slug)
@@ -1038,6 +1056,8 @@ def _update_locked(name, fn, tenant_slug, expect_rev, audit_row):
1038
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
1039
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
1040
  (str(name), payload))
 
 
1041
  # ⚠ A LIST, OR ONE DICT, AND THE LIST IS NOT A CONVENIENCE. Moving views between
1042
  # folders is ONE workspace write that changes several views' placements, so the
1043
  # trail for it is several rows or it is wrong; a single-row keyword would have
@@ -1095,6 +1115,7 @@ def delete_path(path_in_repo, tenant_slug=None):
1095
  return True
1096
 
1097
 
 
1098
  def revision(name, tenant_slug=None):
1099
  """`{'rev', 'updated_at', 'token'}` for ONE bucket — wave 29, item 20 / contract C6.
1100
 
@@ -1115,8 +1136,12 @@ def revision(name, tenant_slug=None):
1115
  """
1116
  with _pool().connection() as con:
1117
  row = con.execute(
1118
- _q('SELECT rev, updated_at FROM {tbl} WHERE key = %s', 'store_kv', tenant_slug),
 
 
1119
  (str(name),)).fetchone()
 
 
1120
  rev = int(row[0]) if row and row[0] is not None else 0
1121
  stamp = row[1] if row else None
1122
  # ⚠ EPOCH SECONDS ON BOTH BACKENDS. `core.store` records `time.time()`; this column is a
 
54
 
55
  import core.data_binding as data_binding
56
  from core import freeze_notice
57
+ from core import request_timing as _request_timing
58
  from core.user_tables import enforce_byte_budget as _BYTE_BUDGET_ENFORCER
59
 
60
  _URL_ENV = 'DATABASE_URL'
 
545
  return dict(value) if isinstance(value, dict) else value
546
 
547
 
548
+ @_request_timing.store_operation("get")
549
  def get(name, fresh=False, tenant_slug=None):
550
  """The value stored under `name`, or `{}` when the key is genuinely absent.
551
 
 
557
  row = con.execute(
558
  _q('SELECT value::text FROM {tbl} WHERE key = %s', 'store_kv', tenant_slug),
559
  (str(name),)).fetchone()
560
+ text = row[0] if row else None
561
+ _request_timing.record_store("get", str(name), bytes_read=_request_timing.text_bytes(text), count=0)
562
+ return text
563
 
564
  return _parsed(fetch() if fresh else _shared_read(name, None, tenant_slug, fetch))
565
 
 
587
  _PROJECTION_TEXT_BYTES -= removed[3]
588
 
589
 
590
+ @_request_timing.store_operation("get_projection")
591
  def get_projection(name, drop=(), tenant_slug=None):
592
  """`core.store.get_projection` with the DROP performed inside Postgres.
593
 
 
619
  row = con.execute(
620
  _q('SELECT value::text FROM {tbl} WHERE key = %s', 'store_kv', tenant_slug),
621
  (str(name),)).fetchone()
622
+ text = row[0] if row else None
623
+ _request_timing.record_store("get_projection", str(name),
624
+ bytes_read=_request_timing.text_bytes(text), count=0)
625
+ return text
626
  row = con.execute(
627
  _q('SELECT kv.rev, kv.updated_at, CASE '
628
  'WHEN kv.rev = %s AND kv.updated_at = %s THEN NULL ELSE (CASE '
 
637
  list(drops), str(name))).fetchone()
638
  # The SELECT always checks the live row. Only its unchanged JSON reconstruction is reused.
639
  text = row[2] if row else None
640
+ _request_timing.record_store("get_projection", str(name),
641
+ bytes_read=_request_timing.text_bytes(text), count=0)
642
  if row and text is None:
643
  if cached is None or row[:2] != cached[:2]:
644
  raise RuntimeError('A projected read returned no content for its current revision')
 
779
  'cause': 'provider_backup_expiry_verification_unavailable'}
780
 
781
 
782
+ @_request_timing.store_operation("get_projections", many=True)
783
  def _get_projections(names, drop=(), tenant_slug=None, timeout_s=None):
784
  """Several projected documents in one round trip, keyed by their requested store key.
785
 
 
802
  con.execute("SELECT set_config('statement_timeout', %s, true)",
803
  (str(timeout_ms),))
804
  rows = con.execute(
805
+ _q('SELECT kv.key, (CASE '
806
  "WHEN jsonb_typeof(kv.value) = 'object' THEN "
807
  "COALESCE((SELECT jsonb_object_agg(e.entry_key, "
808
  "CASE WHEN jsonb_typeof(e.entry_value) = 'object' "
809
  'THEN e.entry_value - %s::text[] ELSE e.entry_value END) '
810
  'FROM jsonb_each(kv.value) AS e(entry_key, entry_value)), '
811
+ "'{{}}'::jsonb) ELSE kv.value END)::text FROM {tbl} AS kv "
812
  'WHERE kv.key = ANY(%s::text[])', 'store_kv', tenant_slug),
813
  (list(drops), keys)).fetchall()
814
+ found = {}
815
+ for key, value in rows:
816
+ _request_timing.record_store("get_projections", str(key),
817
+ bytes_read=_request_timing.text_bytes(value), count=0)
818
+ found[str(key)] = _parsed(value)
819
  dropped = frozenset(drops)
820
  return {
821
  key: _hf._project(
 
972
  return data
973
 
974
 
975
+ @_request_timing.store_operation("update")
976
  def update(name, fn, flush='sync', tenant_slug=None, expect_rev=None, audit_row=None):
977
  """Read-modify-write inside ONE transaction, with the row LOCKED for the duration.
978
 
 
1033
  (f'{int(wait_s * 1000)}ms',))
1034
  try:
1035
  row = con.execute(
1036
+ _q('SELECT value::text, rev FROM {tbl} WHERE key = %s FOR UPDATE',
1037
  'store_kv', tenant_slug),
1038
  (str(name),)).fetchone()
1039
  except LockNotAvailable:
1040
  print(f'[store-pg] lock wait over {wait_s:g}s on '
1041
  f'{_effective_slug(tenant_slug)}/{name}', flush=True)
1042
  raise
1043
+ _request_timing.record_store("update", str(name),
1044
+ bytes_read=_request_timing.text_bytes(row[0] if row else None), count=0)
1045
  if expect_rev is not None:
1046
  current = int(row[1]) if row and row[1] is not None else 0
1047
  if current != int(expect_rev):
1048
  from core.view_audit import ViewRevisionStale
1049
  raise ViewRevisionStale(rev_current=current, rev_sent=int(expect_rev))
1050
+ data = _parsed(row[0] if row else None)
1051
  result = fn(data)
1052
  data = result if result is not None else data
1053
  payload = _payload(name, data, con, tenant_slug)
 
1056
  'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, '
1057
  'updated_at = now(), rev = {bare}.rev + 1', 'store_kv', tenant_slug),
1058
  (str(name), payload))
1059
+ _request_timing.record_store("update", str(name),
1060
+ bytes_written=_request_timing.text_bytes(payload), count=0)
1061
  # ⚠ A LIST, OR ONE DICT, AND THE LIST IS NOT A CONVENIENCE. Moving views between
1062
  # folders is ONE workspace write that changes several views' placements, so the
1063
  # trail for it is several rows or it is wrong; a single-row keyword would have
 
1115
  return True
1116
 
1117
 
1118
+ @_request_timing.store_operation("revision")
1119
  def revision(name, tenant_slug=None):
1120
  """`{'rev', 'updated_at', 'token'}` for ONE bucket — wave 29, item 20 / contract C6.
1121
 
 
1136
  """
1137
  with _pool().connection() as con:
1138
  row = con.execute(
1139
+ _q('SELECT revision.rev, revision.updated_at, octet_length(revision.rev::text) + '
1140
+ 'octet_length(revision.updated_at::text) '
1141
+ 'FROM (SELECT rev, updated_at FROM {tbl} WHERE key = %s) AS revision', 'store_kv', tenant_slug),
1142
  (str(name),)).fetchone()
1143
+ _request_timing.record_store("revision", str(name), bytes_read=int(row[2] or 0) if row and len(row) > 2 else 0,
1144
+ count=0)
1145
  rev = int(row[0]) if row and row[0] is not None else 0
1146
  stamp = row[1] if row else None
1147
  # ⚠ EPOCH SECONDS ON BOTH BACKENDS. `core.store` records `time.time()`; this column is a
platform/harness/datastore.py CHANGED
@@ -29,6 +29,7 @@ from pathlib import Path
29
  import duckdb
30
 
31
  import core.odoo as O
 
32
 
33
  _STORE_DIR = Path(__file__).resolve().parents[1] / "data" / "store"
34
 
@@ -505,7 +506,7 @@ def snapshot_status(tenant_key, source, *, rt=None):
505
  result.update(saved)
506
  if not path.exists():
507
  return result
508
- con = ro_cursor() if path.resolve() == DB_PATH.resolve() else duckdb.connect(str(path))
509
  try:
510
  manifest = _publication(con, source)
511
  if manifest:
@@ -868,9 +869,72 @@ def _instance():
868
  return _INSTANCE["con"]
869
 
870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
871
  def ro_cursor():
872
  """A read cursor on the shared instance — the contention-free way to read the store."""
873
- return _instance().cursor()
874
 
875
 
876
  def ready():
@@ -1412,6 +1476,8 @@ def connect():
1412
  cur = _candidate_connection()
1413
  if cur is None:
1414
  cur = _instance().cursor()
 
 
1415
  _ensure_sync_state(cur)
1416
  return cur
1417
 
 
29
  import duckdb
30
 
31
  import core.odoo as O
32
+ from core import request_timing as _request_timing
33
 
34
  _STORE_DIR = Path(__file__).resolve().parents[1] / "data" / "store"
35
 
 
506
  result.update(saved)
507
  if not path.exists():
508
  return result
509
+ con = ro_cursor() if path.resolve() == DB_PATH.resolve() else _TimedCursor(duckdb.connect(str(path)))
510
  try:
511
  manifest = _publication(con, source)
512
  if manifest:
 
869
  return _INSTANCE["con"]
870
 
871
 
872
+ class _TimedCursor:
873
+ """Keep cursor chaining and thread-local identity while timing execute and result conversion."""
874
+
875
+ _FETCH = {"fetchone", "fetchall", "fetchmany", "fetchdf", "fetch_df", "df", "fetchnumpy",
876
+ "fetch_arrow_table", "fetch_record_batch", "arrow", "pl", "fetch_df_chunk"}
877
+
878
+ def __init__(self, cursor):
879
+ self._cursor = cursor
880
+ self._statement = ""
881
+ self._statement_ms = 0.0
882
+ self._request = None
883
+
884
+ def _call(self, name, *args, **kwargs):
885
+ acc = _request_timing.CURRENT.get()
886
+ execute = name in {"execute", "executemany"}
887
+ if execute:
888
+ statement = args[0] if args else kwargs.get("query", "<statement>")
889
+ self._statement = _request_timing.sql_text(statement) if acc is not None else ""
890
+ self._statement_ms = 0.0
891
+ self._request = acc
892
+ started = time.perf_counter()
893
+ try:
894
+ with _request_timing.thread_scope():
895
+ result = getattr(self._cursor, name)(*args, **kwargs)
896
+ return self if result is self._cursor else result
897
+ finally:
898
+ if acc is not None and self._request is acc:
899
+ elapsed = (time.perf_counter() - started) * 1000
900
+ self._statement_ms += elapsed
901
+ _request_timing.record_duckdb(self._statement, elapsed, count=int(execute),
902
+ statement_ms=self._statement_ms)
903
+
904
+ def execute(self, *args, **kwargs):
905
+ return self._call("execute", *args, **kwargs)
906
+
907
+ def executemany(self, *args, **kwargs):
908
+ return self._call("executemany", *args, **kwargs)
909
+
910
+ def cursor(self, *args, **kwargs):
911
+ return _TimedCursor(self._cursor.cursor(*args, **kwargs))
912
+
913
+ def __getattr__(self, name):
914
+ if name in self._FETCH:
915
+ return lambda *args, **kwargs: self._call(name, *args, **kwargs)
916
+ return getattr(self._cursor, name)
917
+
918
+ def __enter__(self):
919
+ self._cursor.__enter__()
920
+ return self
921
+
922
+ def __exit__(self, *args):
923
+ return self._cursor.__exit__(*args)
924
+
925
+ def __iter__(self):
926
+ return self
927
+
928
+ def __next__(self):
929
+ row = self.fetchone()
930
+ if row is None:
931
+ raise StopIteration
932
+ return row
933
+
934
+
935
  def ro_cursor():
936
  """A read cursor on the shared instance — the contention-free way to read the store."""
937
+ return _TimedCursor(_instance().cursor())
938
 
939
 
940
  def ready():
 
1476
  cur = _candidate_connection()
1477
  if cur is None:
1478
  cur = _instance().cursor()
1479
+ if _request_timing.CURRENT.get() is not None and not isinstance(cur, _TimedCursor):
1480
+ cur = _TimedCursor(cur)
1481
  _ensure_sync_state(cur)
1482
  return cur
1483
 
web/public/AIOS_DEPLOY_RECEIPT CHANGED
@@ -1 +1 @@
1
- b2281a18c5d730b1a0a2df0e348763c697399946982678a0948b9446503bd6ba
 
1
+ 6ae85c0d1e743b4cd7aebc1d4973add1436f58fa47535c297abe2732cedbdbcd