Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Deploy AIOS web (React glide grid + FastAPI slice)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- Dockerfile +9 -1
- RELEASES.json +13 -457
- VERSION +1 -1
- api/agent_directory.py +1 -1
- api/agent_inbox.py +65 -4
- api/aios_session.py +59 -0
- api/amazon_relational.py +127 -20
- api/application_registry.py +103 -9
- api/assistant_memory.py +38 -0
- api/automation_engine.py +78 -17
- api/catalog_store.py +97 -0
- api/connectors_amazon.py +18 -4
- api/deps.py +1 -1
- api/external_freshness.py +129 -2
- api/formula_runtime.py +2 -0
- api/intelligence_store.py +246 -26
- api/loopy_catalog_tools.py +93 -4
- api/loopy_links.py +28 -3
- api/main.py +44 -0
- api/odoo_attachments.py +103 -0
- api/odoo_relational.py +3 -0
- api/routes_agent_harness.py +54 -5
- api/routes_amazon_profit.py +200 -0
- api/routes_auth.py +65 -4
- api/routes_automation.py +67 -11
- api/routes_competitor_watch.py +217 -0
- api/routes_connectors.py +126 -35
- api/routes_external.py +853 -142
- api/routes_forms.py +4 -3
- api/routes_geo.py +54 -12
- api/routes_grid.py +6 -5
- api/routes_nav.py +5 -2
- api/routes_odoo_attachments.py +31 -0
- api/routes_odoo_tables.py +15 -7
- api/routes_publish.py +3 -1
- api/routes_query.py +239 -18
- api/routes_schedules.py +48 -2
- api/routes_security.py +39 -5
- api/routes_sheet.py +91 -33
- api/routes_slack.py +99 -12
- api/routes_starred.py +86 -13
- api/routes_tables.py +61 -2
- api/routes_templates.py +20 -5
- api/routes_tickets.py +10 -0
- api/schedule_envelope.py +240 -1
- api/schedules.py +43 -24
- api/sheet_warm.py +33 -9
- api/test_website_runtime.py +105 -0
- api/ticket_tools.py +8 -5
- api/tickets.py +142 -34
Dockerfile
CHANGED
|
@@ -42,8 +42,16 @@ COPY web/ ./
|
|
| 42 |
RUN npm run build # -> /web/dist
|
| 43 |
|
| 44 |
# ---- stage 2: python API serving the bundle ----
|
| 45 |
-
FROM python:3.12-slim
|
| 46 |
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
COPY requirements.txt ./
|
| 48 |
# W59-T35, 2026-09-13: pin the locally installed resolver instead of upgrading pip implicitly.
|
| 49 |
# The Space uploader still sends aios-web/requirements.txt with open bounds and excludes the
|
|
|
|
| 42 |
RUN npm run build # -> /web/dist
|
| 43 |
|
| 44 |
# ---- stage 2: python API serving the bundle ----
|
| 45 |
+
FROM python:3.12-slim-trixie
|
| 46 |
WORKDIR /app
|
| 47 |
+
# Chromium's libraries match the reviewed Playwright Debian 13 dependency list.
|
| 48 |
+
RUN apt-get update \
|
| 49 |
+
&& apt-get install -y --no-install-recommends \
|
| 50 |
+
fonts-liberation libasound2t64 libatk-bridge2.0-0t64 libatk1.0-0t64 \
|
| 51 |
+
libatspi2.0-0t64 libcairo2 libcups2t64 libdbus-1-3 libdrm2 libgbm1 \
|
| 52 |
+
libglib2.0-0t64 libnspr4 libnss3 libpango-1.0-0 libx11-6 libxcb1 \
|
| 53 |
+
libxcomposite1 libxdamage1 libxext6 libxfixes3 libxkbcommon0 libxrandr2 \
|
| 54 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 55 |
COPY requirements.txt ./
|
| 56 |
# W59-T35, 2026-09-13: pin the locally installed resolver instead of upgrading pip implicitly.
|
| 57 |
# The Space uploader still sends aios-web/requirements.txt with open bounds and excludes the
|
RELEASES.json
CHANGED
|
@@ -1,491 +1,47 @@
|
|
| 1 |
{
|
| 2 |
-
"current": "
|
| 3 |
"releases": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
{
|
| 5 |
"version": "v82",
|
| 6 |
-
"sha": "
|
| 7 |
"date": "2026-10-17",
|
| 8 |
"subject": "release v82"
|
| 9 |
},
|
| 10 |
{
|
| 11 |
"version": "v81",
|
| 12 |
-
"sha": "
|
| 13 |
"date": "2026-10-15",
|
| 14 |
"subject": "release v81"
|
| 15 |
},
|
| 16 |
{
|
| 17 |
"version": "v80",
|
| 18 |
-
"sha": "
|
| 19 |
"date": "2026-10-13",
|
| 20 |
"subject": "release v80"
|
| 21 |
},
|
| 22 |
{
|
| 23 |
"version": "v79",
|
| 24 |
-
"sha": "
|
| 25 |
"date": "2026-10-13",
|
| 26 |
"subject": "release v79"
|
| 27 |
},
|
| 28 |
{
|
| 29 |
"version": "v78",
|
| 30 |
-
"sha": "
|
| 31 |
"date": "2026-10-09",
|
| 32 |
"subject": "release v78: SDLC 71 with the D-7536 and D-7537 repairs"
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"version": "v77",
|
| 36 |
-
"sha": "
|
| 37 |
"date": "2026-10-05",
|
| 38 |
"subject": "release v77"
|
| 39 |
-
},
|
| 40 |
-
{
|
| 41 |
-
"version": "v76",
|
| 42 |
-
"sha": "7990f03e0254",
|
| 43 |
-
"date": "2026-10-01",
|
| 44 |
-
"subject": "release v76"
|
| 45 |
-
},
|
| 46 |
-
{
|
| 47 |
-
"version": "v75",
|
| 48 |
-
"sha": "2c5146981ccf",
|
| 49 |
-
"date": "2026-09-19",
|
| 50 |
-
"subject": "release v75"
|
| 51 |
-
},
|
| 52 |
-
{
|
| 53 |
-
"version": "v74",
|
| 54 |
-
"sha": "40dceba9262e",
|
| 55 |
-
"date": "2026-09-14",
|
| 56 |
-
"subject": "release v74: waves 58+59 incl. the Amazon credential door (W59-T21/22/23) and sync producer (W59-T42); hotfix promote so"
|
| 57 |
-
},
|
| 58 |
-
{
|
| 59 |
-
"version": "v73",
|
| 60 |
-
"sha": "bb94de5665e6",
|
| 61 |
-
"date": "2026-09-13",
|
| 62 |
-
"subject": "release v73"
|
| 63 |
-
},
|
| 64 |
-
{
|
| 65 |
-
"version": "v71",
|
| 66 |
-
"sha": "96143dc6fb65",
|
| 67 |
-
"date": "2026-09-04",
|
| 68 |
-
"subject": "release v71 - wave 51, QA'd 24 PASS / 6 FAIL / 5 UNPROVEN"
|
| 69 |
-
},
|
| 70 |
-
{
|
| 71 |
-
"version": "v70",
|
| 72 |
-
"sha": "eedfb6a00163",
|
| 73 |
-
"date": "2026-09-03",
|
| 74 |
-
"subject": "release v70 - wave 50 (grid analytics, the Sheet as a spreadsheet, view trash and audit)"
|
| 75 |
-
},
|
| 76 |
-
{
|
| 77 |
-
"version": "v69",
|
| 78 |
-
"sha": "8598f56293a0",
|
| 79 |
-
"date": "2026-09-02",
|
| 80 |
-
"subject": "release v69: wave48-continuation validated + hotfixes v67/v68/d4e3e31 + go-live QA repairs (Supplier option limit, optio"
|
| 81 |
-
},
|
| 82 |
-
{
|
| 83 |
-
"version": "v68",
|
| 84 |
-
"sha": "62b065a2610a",
|
| 85 |
-
"date": "2026-09-01",
|
| 86 |
-
"subject": "release v68: unified holiday date offsets"
|
| 87 |
-
},
|
| 88 |
-
{
|
| 89 |
-
"version": "v67",
|
| 90 |
-
"sha": "3b2241982cf7",
|
| 91 |
-
"date": "2026-09-01",
|
| 92 |
-
"subject": "release v67"
|
| 93 |
-
},
|
| 94 |
-
{
|
| 95 |
-
"version": "v66",
|
| 96 |
-
"sha": "73c94f076740",
|
| 97 |
-
"date": "2026-08-31",
|
| 98 |
-
"subject": "release v66"
|
| 99 |
-
},
|
| 100 |
-
{
|
| 101 |
-
"version": "v65",
|
| 102 |
-
"sha": "9f6cfedb8a43",
|
| 103 |
-
"date": "2026-08-31",
|
| 104 |
-
"subject": "release v65"
|
| 105 |
-
},
|
| 106 |
-
{
|
| 107 |
-
"version": "v64",
|
| 108 |
-
"sha": "bb8985c6e73e",
|
| 109 |
-
"date": "2026-08-31",
|
| 110 |
-
"subject": "release v64"
|
| 111 |
-
},
|
| 112 |
-
{
|
| 113 |
-
"version": "v63",
|
| 114 |
-
"sha": "ec75cb8f3412",
|
| 115 |
-
"date": "2026-08-31",
|
| 116 |
-
"subject": "release v63"
|
| 117 |
-
},
|
| 118 |
-
{
|
| 119 |
-
"version": "v62",
|
| 120 |
-
"sha": "2c8f702c964f",
|
| 121 |
-
"date": "2026-08-31",
|
| 122 |
-
"subject": "release v62"
|
| 123 |
-
},
|
| 124 |
-
{
|
| 125 |
-
"version": "v61",
|
| 126 |
-
"sha": "bbc4c0760ab5",
|
| 127 |
-
"date": "2026-08-30",
|
| 128 |
-
"subject": "v61 - wave 46 hotfix: D-590 schema overlay reaches the grid read (all three assemblies), D-599 route to zone association"
|
| 129 |
-
},
|
| 130 |
-
{
|
| 131 |
-
"version": "v60",
|
| 132 |
-
"sha": "dd1b0620210e",
|
| 133 |
-
"date": "2026-08-30",
|
| 134 |
-
"subject": "v60 - wave 46"
|
| 135 |
-
},
|
| 136 |
-
{
|
| 137 |
-
"version": "v59",
|
| 138 |
-
"sha": "3cb73ce8021e",
|
| 139 |
-
"date": "2026-08-29",
|
| 140 |
-
"subject": "release v59"
|
| 141 |
-
},
|
| 142 |
-
{
|
| 143 |
-
"version": "v58",
|
| 144 |
-
"sha": "c526cede82f9",
|
| 145 |
-
"date": "2026-08-29",
|
| 146 |
-
"subject": "release v58"
|
| 147 |
-
},
|
| 148 |
-
{
|
| 149 |
-
"version": "v57",
|
| 150 |
-
"sha": "4859c035e6a7",
|
| 151 |
-
"date": "2026-08-29",
|
| 152 |
-
"subject": "release v57"
|
| 153 |
-
},
|
| 154 |
-
{
|
| 155 |
-
"version": "v56",
|
| 156 |
-
"sha": "2970b729e8f0",
|
| 157 |
-
"date": "2026-08-28",
|
| 158 |
-
"subject": "release v56"
|
| 159 |
-
},
|
| 160 |
-
{
|
| 161 |
-
"version": "v55",
|
| 162 |
-
"sha": "b766b904b8fa",
|
| 163 |
-
"date": "2026-08-28",
|
| 164 |
-
"subject": "release v55"
|
| 165 |
-
},
|
| 166 |
-
{
|
| 167 |
-
"version": "v54",
|
| 168 |
-
"sha": "6a8e99a064d9",
|
| 169 |
-
"date": "2026-08-27",
|
| 170 |
-
"subject": "release v54"
|
| 171 |
-
},
|
| 172 |
-
{
|
| 173 |
-
"version": "v53",
|
| 174 |
-
"sha": "89ed2cb867da",
|
| 175 |
-
"date": "2026-08-24",
|
| 176 |
-
"subject": "release v53"
|
| 177 |
-
},
|
| 178 |
-
{
|
| 179 |
-
"version": "v52",
|
| 180 |
-
"sha": "826349376241",
|
| 181 |
-
"date": "2026-08-23",
|
| 182 |
-
"subject": "release v52"
|
| 183 |
-
},
|
| 184 |
-
{
|
| 185 |
-
"version": "v51",
|
| 186 |
-
"sha": "5740d20fa585",
|
| 187 |
-
"date": "2026-08-23",
|
| 188 |
-
"subject": "release v51"
|
| 189 |
-
},
|
| 190 |
-
{
|
| 191 |
-
"version": "v50",
|
| 192 |
-
"sha": "8027c83d1da6",
|
| 193 |
-
"date": "2026-08-23",
|
| 194 |
-
"subject": "release v50"
|
| 195 |
-
},
|
| 196 |
-
{
|
| 197 |
-
"version": "v49",
|
| 198 |
-
"sha": "9a14c01a48a4",
|
| 199 |
-
"date": "2026-08-23",
|
| 200 |
-
"subject": "release v49"
|
| 201 |
-
},
|
| 202 |
-
{
|
| 203 |
-
"version": "v48",
|
| 204 |
-
"sha": "3c69fd93da7c",
|
| 205 |
-
"date": "2026-08-23",
|
| 206 |
-
"subject": "release v48"
|
| 207 |
-
},
|
| 208 |
-
{
|
| 209 |
-
"version": "v47",
|
| 210 |
-
"sha": "917f9a0eb509",
|
| 211 |
-
"date": "2026-08-23",
|
| 212 |
-
"subject": "release v47"
|
| 213 |
-
},
|
| 214 |
-
{
|
| 215 |
-
"version": "v46",
|
| 216 |
-
"sha": "a8dc531b5078",
|
| 217 |
-
"date": "2026-08-23",
|
| 218 |
-
"subject": "release v46"
|
| 219 |
-
},
|
| 220 |
-
{
|
| 221 |
-
"version": "v45",
|
| 222 |
-
"sha": "0b2b5010d86f",
|
| 223 |
-
"date": "2026-08-23",
|
| 224 |
-
"subject": "release v45"
|
| 225 |
-
},
|
| 226 |
-
{
|
| 227 |
-
"version": "v44",
|
| 228 |
-
"sha": "a7877aaf20ff",
|
| 229 |
-
"date": "2026-08-23",
|
| 230 |
-
"subject": "Hotfix: allow renaming the active Map view"
|
| 231 |
-
},
|
| 232 |
-
{
|
| 233 |
-
"version": "v43",
|
| 234 |
-
"sha": "e2ba4616fa0f",
|
| 235 |
-
"date": "2026-08-23",
|
| 236 |
-
"subject": "release v43"
|
| 237 |
-
},
|
| 238 |
-
{
|
| 239 |
-
"version": "v42",
|
| 240 |
-
"sha": "25b885e6373a",
|
| 241 |
-
"date": "2026-08-23",
|
| 242 |
-
"subject": "release v42"
|
| 243 |
-
},
|
| 244 |
-
{
|
| 245 |
-
"version": "v41",
|
| 246 |
-
"sha": "1d6adcc3c3ac",
|
| 247 |
-
"date": "2026-08-22",
|
| 248 |
-
"subject": "v41 vertical route navigation"
|
| 249 |
-
},
|
| 250 |
-
{
|
| 251 |
-
"version": "v40",
|
| 252 |
-
"sha": "1451837168f3",
|
| 253 |
-
"date": "2026-08-22",
|
| 254 |
-
"subject": "release v40 route UI correction"
|
| 255 |
-
},
|
| 256 |
-
{
|
| 257 |
-
"version": "v39",
|
| 258 |
-
"sha": "a78bdf00e38e",
|
| 259 |
-
"date": "2026-08-21",
|
| 260 |
-
"subject": "W39 clean stale migration targets"
|
| 261 |
-
},
|
| 262 |
-
{
|
| 263 |
-
"version": "v38",
|
| 264 |
-
"sha": "a556631cf979",
|
| 265 |
-
"date": "2026-08-21",
|
| 266 |
-
"subject": "W39 reversible Live store cutover"
|
| 267 |
-
},
|
| 268 |
-
{
|
| 269 |
-
"version": "v37",
|
| 270 |
-
"sha": "1ddd8cf82379",
|
| 271 |
-
"date": "2026-08-21",
|
| 272 |
-
"subject": "W39: coalesce derived workspace reads"
|
| 273 |
-
},
|
| 274 |
-
{
|
| 275 |
-
"version": "v36",
|
| 276 |
-
"sha": "51db3b7fee2c",
|
| 277 |
-
"date": "2026-08-21",
|
| 278 |
-
"subject": "W39 share-field oid repair"
|
| 279 |
-
},
|
| 280 |
-
{
|
| 281 |
-
"version": "v35",
|
| 282 |
-
"sha": "0f642129c8f7",
|
| 283 |
-
"date": "2026-08-21",
|
| 284 |
-
"subject": "W39 visual QA fixture"
|
| 285 |
-
},
|
| 286 |
-
{
|
| 287 |
-
"version": "v34",
|
| 288 |
-
"sha": "cb4caa188e05",
|
| 289 |
-
"date": "2026-08-21",
|
| 290 |
-
"subject": "W39 Staging PostgreSQL egress controls"
|
| 291 |
-
},
|
| 292 |
-
{
|
| 293 |
-
"version": "v33",
|
| 294 |
-
"sha": "2b115e573d6a",
|
| 295 |
-
"date": "2026-08-21",
|
| 296 |
-
"subject": "W39: stop nonproduction Postgres background egress"
|
| 297 |
-
},
|
| 298 |
-
{
|
| 299 |
-
"version": "v32",
|
| 300 |
-
"sha": "b47b27ca7b2a",
|
| 301 |
-
"date": "2026-08-21",
|
| 302 |
-
"subject": "W39: repair Staging Postgres projection"
|
| 303 |
-
},
|
| 304 |
-
{
|
| 305 |
-
"version": "v31",
|
| 306 |
-
"sha": "8880a49a244e",
|
| 307 |
-
"date": "2026-08-21",
|
| 308 |
-
"subject": "v31: Wave 39 staging candidate"
|
| 309 |
-
},
|
| 310 |
-
{
|
| 311 |
-
"version": "v30",
|
| 312 |
-
"sha": "71776ca6c8df",
|
| 313 |
-
"date": "2026-08-20",
|
| 314 |
-
"subject": "v30: W39-T29 Member-owned View first-share hotfix"
|
| 315 |
-
},
|
| 316 |
-
{
|
| 317 |
-
"version": "v29",
|
| 318 |
-
"sha": "55ced5018ff2",
|
| 319 |
-
"date": "2026-08-18",
|
| 320 |
-
"subject": "v29: the build staging has been running since 2026-08-18 (55ced50)."
|
| 321 |
-
},
|
| 322 |
-
{
|
| 323 |
-
"version": "v28",
|
| 324 |
-
"sha": "2793e4ba53dc",
|
| 325 |
-
"date": "2026-08-18",
|
| 326 |
-
"subject": "release v28"
|
| 327 |
-
},
|
| 328 |
-
{
|
| 329 |
-
"version": "v27",
|
| 330 |
-
"sha": "c05a794837fc",
|
| 331 |
-
"date": "2026-08-18",
|
| 332 |
-
"subject": "release v27"
|
| 333 |
-
},
|
| 334 |
-
{
|
| 335 |
-
"version": "v25",
|
| 336 |
-
"sha": "bc248295ed66",
|
| 337 |
-
"date": "2026-08-14",
|
| 338 |
-
"subject": "v25 - wave 32's LIVE build (deployed 2026-08-13, bc24829)"
|
| 339 |
-
},
|
| 340 |
-
{
|
| 341 |
-
"version": "v26",
|
| 342 |
-
"sha": "6a642e139cfa",
|
| 343 |
-
"date": "2026-08-14",
|
| 344 |
-
"subject": "v26 - wave 33's build (deployed 2026-08-14, 6a642e1)"
|
| 345 |
-
},
|
| 346 |
-
{
|
| 347 |
-
"version": "v24",
|
| 348 |
-
"sha": "514d8292ce73",
|
| 349 |
-
"date": "2026-08-09",
|
| 350 |
-
"subject": "release v24"
|
| 351 |
-
},
|
| 352 |
-
{
|
| 353 |
-
"version": "v23",
|
| 354 |
-
"sha": "44cc28380fca",
|
| 355 |
-
"date": "2026-08-09",
|
| 356 |
-
"subject": "release v23"
|
| 357 |
-
},
|
| 358 |
-
{
|
| 359 |
-
"version": "v22",
|
| 360 |
-
"sha": "f14e199ff32f",
|
| 361 |
-
"date": "2026-08-09",
|
| 362 |
-
"subject": "release v22"
|
| 363 |
-
},
|
| 364 |
-
{
|
| 365 |
-
"version": "v21",
|
| 366 |
-
"sha": "5fdffce8b558",
|
| 367 |
-
"date": "2026-08-09",
|
| 368 |
-
"subject": "release v21"
|
| 369 |
-
},
|
| 370 |
-
{
|
| 371 |
-
"version": "v20",
|
| 372 |
-
"sha": "442e4a1430a7",
|
| 373 |
-
"date": "2026-08-09",
|
| 374 |
-
"subject": "release v20"
|
| 375 |
-
},
|
| 376 |
-
{
|
| 377 |
-
"version": "v19",
|
| 378 |
-
"sha": "94cb4a4dfdfe",
|
| 379 |
-
"date": "2026-08-09",
|
| 380 |
-
"subject": "release v19"
|
| 381 |
-
},
|
| 382 |
-
{
|
| 383 |
-
"version": "v18",
|
| 384 |
-
"sha": "0106ef949805",
|
| 385 |
-
"date": "2026-08-09",
|
| 386 |
-
"subject": "release v18"
|
| 387 |
-
},
|
| 388 |
-
{
|
| 389 |
-
"version": "v17",
|
| 390 |
-
"sha": "308240cc75e8",
|
| 391 |
-
"date": "2026-08-09",
|
| 392 |
-
"subject": "release v17"
|
| 393 |
-
},
|
| 394 |
-
{
|
| 395 |
-
"version": "v16",
|
| 396 |
-
"sha": "a804df54430f",
|
| 397 |
-
"date": "2026-08-09",
|
| 398 |
-
"subject": "release v16"
|
| 399 |
-
},
|
| 400 |
-
{
|
| 401 |
-
"version": "v15",
|
| 402 |
-
"sha": "6b84d7264c81",
|
| 403 |
-
"date": "2026-08-09",
|
| 404 |
-
"subject": "release v15"
|
| 405 |
-
},
|
| 406 |
-
{
|
| 407 |
-
"version": "v14",
|
| 408 |
-
"sha": "bee5eaf5b5cb",
|
| 409 |
-
"date": "2026-08-07",
|
| 410 |
-
"subject": "release v14"
|
| 411 |
-
},
|
| 412 |
-
{
|
| 413 |
-
"version": "v13",
|
| 414 |
-
"sha": "314c0e368b1d",
|
| 415 |
-
"date": "2026-08-06",
|
| 416 |
-
"subject": "release v13"
|
| 417 |
-
},
|
| 418 |
-
{
|
| 419 |
-
"version": "v12",
|
| 420 |
-
"sha": "4908f399785d",
|
| 421 |
-
"date": "2026-08-06",
|
| 422 |
-
"subject": "release v12"
|
| 423 |
-
},
|
| 424 |
-
{
|
| 425 |
-
"version": "v11",
|
| 426 |
-
"sha": "312661712b98",
|
| 427 |
-
"date": "2026-08-05",
|
| 428 |
-
"subject": "release v11"
|
| 429 |
-
},
|
| 430 |
-
{
|
| 431 |
-
"version": "v10",
|
| 432 |
-
"sha": "43f804287496",
|
| 433 |
-
"date": "2026-08-05",
|
| 434 |
-
"subject": "release v10"
|
| 435 |
-
},
|
| 436 |
-
{
|
| 437 |
-
"version": "v9",
|
| 438 |
-
"sha": "2e26f57396b1",
|
| 439 |
-
"date": "2026-08-05",
|
| 440 |
-
"subject": "release v9"
|
| 441 |
-
},
|
| 442 |
-
{
|
| 443 |
-
"version": "v8",
|
| 444 |
-
"sha": "8073b3ecbac3",
|
| 445 |
-
"date": "2026-08-05",
|
| 446 |
-
"subject": "release v8"
|
| 447 |
-
},
|
| 448 |
-
{
|
| 449 |
-
"version": "v7",
|
| 450 |
-
"sha": "b1c5a7e9b848",
|
| 451 |
-
"date": "2026-08-05",
|
| 452 |
-
"subject": "release v7"
|
| 453 |
-
},
|
| 454 |
-
{
|
| 455 |
-
"version": "v6",
|
| 456 |
-
"sha": "a344020f114a",
|
| 457 |
-
"date": "2026-08-05",
|
| 458 |
-
"subject": "release v6"
|
| 459 |
-
},
|
| 460 |
-
{
|
| 461 |
-
"version": "v5",
|
| 462 |
-
"sha": "c0b0fbb40a7b",
|
| 463 |
-
"date": "2026-08-05",
|
| 464 |
-
"subject": "release v5"
|
| 465 |
-
},
|
| 466 |
-
{
|
| 467 |
-
"version": "v4",
|
| 468 |
-
"sha": "de5037ff1c8f",
|
| 469 |
-
"date": "2026-08-05",
|
| 470 |
-
"subject": "release v4"
|
| 471 |
-
},
|
| 472 |
-
{
|
| 473 |
-
"version": "v3",
|
| 474 |
-
"sha": "6b9fa62d93e8",
|
| 475 |
-
"date": "2026-08-05",
|
| 476 |
-
"subject": "release v3"
|
| 477 |
-
},
|
| 478 |
-
{
|
| 479 |
-
"version": "v2",
|
| 480 |
-
"sha": "fd0586199858",
|
| 481 |
-
"date": "2026-08-04",
|
| 482 |
-
"subject": "release v2"
|
| 483 |
-
},
|
| 484 |
-
{
|
| 485 |
-
"version": "v1",
|
| 486 |
-
"sha": "5b4e2c4bc3f3",
|
| 487 |
-
"date": "2026-08-04",
|
| 488 |
-
"subject": "release v1"
|
| 489 |
}
|
| 490 |
]
|
| 491 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"current": "v83 (9b7471968)",
|
| 3 |
"releases": [
|
| 4 |
+
{
|
| 5 |
+
"version": "v83",
|
| 6 |
+
"sha": "9b7471968",
|
| 7 |
+
"date": "2026-10-21",
|
| 8 |
+
"subject": "release v83"
|
| 9 |
+
},
|
| 10 |
{
|
| 11 |
"version": "v82",
|
| 12 |
+
"sha": "9c61b1ee9",
|
| 13 |
"date": "2026-10-17",
|
| 14 |
"subject": "release v82"
|
| 15 |
},
|
| 16 |
{
|
| 17 |
"version": "v81",
|
| 18 |
+
"sha": "e141ccc85",
|
| 19 |
"date": "2026-10-15",
|
| 20 |
"subject": "release v81"
|
| 21 |
},
|
| 22 |
{
|
| 23 |
"version": "v80",
|
| 24 |
+
"sha": "869b8fcc6",
|
| 25 |
"date": "2026-10-13",
|
| 26 |
"subject": "release v80"
|
| 27 |
},
|
| 28 |
{
|
| 29 |
"version": "v79",
|
| 30 |
+
"sha": "406417b8d",
|
| 31 |
"date": "2026-10-13",
|
| 32 |
"subject": "release v79"
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"version": "v78",
|
| 36 |
+
"sha": "aad6561d6",
|
| 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": "e741e5ca6",
|
| 43 |
"date": "2026-10-05",
|
| 44 |
"subject": "release v77"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
]
|
| 47 |
}
|
VERSION
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
|
|
|
|
| 1 |
+
v83 (9b7471968)
|
api/agent_directory.py
CHANGED
|
@@ -18,7 +18,7 @@ REFUSAL = "The directory of your workspace could not be read. Try again in a mom
|
|
| 18 |
NOT_FOUND = "No one by that name uses Loopable in your workspace."
|
| 19 |
|
| 20 |
# Shell.tsx opens these workspace modules for every signed-in account.
|
| 21 |
-
_WORKSPACE_MODULES = ("Sheet", "
|
| 22 |
|
| 23 |
_OPS = {
|
| 24 |
"eq": "is", "neq": "is not", "contains": "contains", "doesNotContain": "does not contain",
|
|
|
|
| 18 |
NOT_FOUND = "No one by that name uses Loopable in your workspace."
|
| 19 |
|
| 20 |
# Shell.tsx opens these workspace modules for every signed-in account.
|
| 21 |
+
_WORKSPACE_MODULES = ("Sheet", "Table", "App")
|
| 22 |
|
| 23 |
_OPS = {
|
| 24 |
"eq": "is", "neq": "is not", "contains": "contains", "doesNotContain": "does not contain",
|
api/agent_inbox.py
CHANGED
|
@@ -7,7 +7,7 @@ R81: the inbox has no screen of its own. It lives in the chat.
|
|
| 7 |
R84: a recipient is resolved only among the caller's own workspace accounts.
|
| 8 |
`send` is the one door: the `agent_message` tool and the T28 route both use it.
|
| 9 |
R93 (S72-T28): an access request goes to the item's owner, and to the Administrators for a
|
| 10 |
-
database.
|
| 11 |
"""
|
| 12 |
import copy
|
| 13 |
import datetime as _dt
|
|
@@ -30,6 +30,9 @@ ACTIONS = ["approve", "not_now"]
|
|
| 30 |
C6_KEYS = ("type", "request_id", "sender", "target", "text", "actions", "state")
|
| 31 |
TEXT_LIMIT = 280
|
| 32 |
FOLD_SECONDS = 24 * 3600
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
NOT_IN_WORKSPACE = "That person is not in your workspace."
|
| 35 |
TWO_NAMES = "Two people share that name. Use their username."
|
|
@@ -585,8 +588,7 @@ def agent_message(session, args):
|
|
| 585 |
|
| 586 |
|
| 587 |
# ── S72-T28: a person answers an access request (C6 route only) ───────────────────────────────
|
| 588 |
-
#
|
| 589 |
-
# click (R93). `verify_agent_inbox` walks every registered tool to prove it.
|
| 590 |
def _may_answer(session, ref):
|
| 591 |
"""R93: the owner answers a sheet, view or app. An Administrator answers a database."""
|
| 592 |
if ref["kind"] == "database":
|
|
@@ -783,12 +785,71 @@ def _decide_app_share(session, record, action):
|
|
| 783 |
return {"state": final["state"]}
|
| 784 |
|
| 785 |
|
| 786 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 787 |
"""Answer one access request on a person's click (C6, R93). Returns `{"state"}`.
|
| 788 |
|
| 789 |
The decision is claimed before the grant, so a parallel click can never leave a grant under
|
| 790 |
a declined record. A failed grant puts the claim back.
|
| 791 |
"""
|
|
|
|
|
|
|
| 792 |
if action not in ACTIONS:
|
| 793 |
raise Denied(400, "bad_action", BAD_ACTION)
|
| 794 |
rt, me = session.runtime, _lower(session.uname)
|
|
|
|
| 7 |
R84: a recipient is resolved only among the caller's own workspace accounts.
|
| 8 |
`send` is the one door: the `agent_message` tool and the T28 route both use it.
|
| 9 |
R93 (S72-T28): an access request goes to the item's owner, and to the Administrators for a
|
| 10 |
+
database. Access grants require a person's click on the C6 route. Team decisions grant no access.
|
| 11 |
"""
|
| 12 |
import copy
|
| 13 |
import datetime as _dt
|
|
|
|
| 30 |
C6_KEYS = ("type", "request_id", "sender", "target", "text", "actions", "state")
|
| 31 |
TEXT_LIMIT = 280
|
| 32 |
FOLD_SECONDS = 24 * 3600
|
| 33 |
+
TEAM_CHANGE_TITLE = "Review a proposed change."
|
| 34 |
+
TEAM_CHANGE_PREFIX = "Proposed change: "
|
| 35 |
+
TEAM_AUTHORIZATION_NOTE = "Approval authorizes the recipient AI only. No target change is executed."
|
| 36 |
|
| 37 |
NOT_IN_WORKSPACE = "That person is not in your workspace."
|
| 38 |
TWO_NAMES = "Two people share that name. Use their username."
|
|
|
|
| 588 |
|
| 589 |
|
| 590 |
# ── S72-T28: a person answers an access request (C6 route only) ───────────────────────────────
|
| 591 |
+
# Access grants need a person's click. Confirmed team decisions never enter a grant writer.
|
|
|
|
| 592 |
def _may_answer(session, ref):
|
| 593 |
"""R93: the owner answers a sheet, view or app. An Administrator answers a database."""
|
| 594 |
if ref["kind"] == "database":
|
|
|
|
| 785 |
return {"state": final["state"]}
|
| 786 |
|
| 787 |
|
| 788 |
+
def team_approval(rt, request_id, by, *, source_check=None):
|
| 789 |
+
"""Admit only a recipient's sourced team proposal, excluding access grants and App shares."""
|
| 790 |
+
import tickets
|
| 791 |
+
_key, row = tickets._ticket(rt, request_id)
|
| 792 |
+
task = tickets.get_request_attribution(rt, request_id, by, source_check=source_check)
|
| 793 |
+
if (task["kind"] != "approval" or task["title"] != TEAM_CHANGE_TITLE
|
| 794 |
+
or not task["body"].startswith(TEAM_CHANGE_PREFIX)
|
| 795 |
+
or not task["body"].endswith(TEAM_AUTHORIZATION_NOTE)
|
| 796 |
+
or row.get("approvalAction") or row.get("target")
|
| 797 |
+
or by not in task["to"] or by == task["from"]["person"]):
|
| 798 |
+
raise PermissionError("Only the recipient can answer a team change proposal.")
|
| 799 |
+
if not task["sourceVisible"] or task["source"] is None:
|
| 800 |
+
raise PermissionError("You cannot see the source data this request needs.")
|
| 801 |
+
return task
|
| 802 |
+
|
| 803 |
+
|
| 804 |
+
def _decide_team_approval(session, request_id, decision, source_check):
|
| 805 |
+
"""Record one explicit authorization decision without invoking any target writer or grant."""
|
| 806 |
+
import tickets
|
| 807 |
+
from core import user_tables
|
| 808 |
+
rt, me = session.runtime, _lower(session.uname)
|
| 809 |
+
if decision not in ("Approve", "Deny"):
|
| 810 |
+
raise ValueError("Choose Approve or Deny.")
|
| 811 |
+
proposal = team_approval(rt, request_id, me, source_check=source_check)
|
| 812 |
+
key, _row = tickets._ticket(rt, request_id)
|
| 813 |
+
state = "completed" if decision == "Approve" else "rejected"
|
| 814 |
+
result = {}
|
| 815 |
+
|
| 816 |
+
def write(data):
|
| 817 |
+
result.clear()
|
| 818 |
+
row = data[key]["rows"].get(str(request_id))
|
| 819 |
+
# Recheck the locked candidate as well as the public source projection.
|
| 820 |
+
if (not isinstance(row, dict) or row.get("kind") != "approval"
|
| 821 |
+
or row.get("title") != TEAM_CHANGE_TITLE or row.get("approvalAction") or row.get("target")
|
| 822 |
+
or me not in tickets._request_recipients(row) or row.get("from_person") == me):
|
| 823 |
+
raise PermissionError("Only the recipient can answer a team change proposal.")
|
| 824 |
+
current = tickets.project_request({**row, "id": str(request_id)})
|
| 825 |
+
if any(current[field] != proposal[field] for field in ("from", "to", "title", "body", "source")):
|
| 826 |
+
raise ValueError("This proposal changed. Read it and preview your decision again.")
|
| 827 |
+
tickets._request_reader(rt, row, me, sender=False)
|
| 828 |
+
if source_check:
|
| 829 |
+
source_check(tickets.project_request(row)["source"])
|
| 830 |
+
if row.get("state") not in ("submitted", "input-required"):
|
| 831 |
+
if row.get("state") != state or row.get("decided_by") != me:
|
| 832 |
+
raise ValueError("This proposal has already been answered.")
|
| 833 |
+
else:
|
| 834 |
+
row.update(tickets._transition(rt, row, me, state, decision + ". " + TEAM_AUTHORIZATION_NOTE))
|
| 835 |
+
row.update(status="Resolved", decided_by=me, decided_at=tickets._utc().isoformat(),
|
| 836 |
+
updated_at=tickets._utc().isoformat(), closed_at=tickets._utc().isoformat())
|
| 837 |
+
result.update(id=str(request_id), decision=decision, authorized=decision == "Approve", executed=False,
|
| 838 |
+
state=state, notice=TEAM_AUTHORIZATION_NOTE)
|
| 839 |
+
return data
|
| 840 |
+
|
| 841 |
+
rt.update(user_tables.STORE_KEY, write, flush="sync")
|
| 842 |
+
return result
|
| 843 |
+
|
| 844 |
+
|
| 845 |
+
def decide_request(session, request_id, action, *, authorization_only=False, source_check=None):
|
| 846 |
"""Answer one access request on a person's click (C6, R93). Returns `{"state"}`.
|
| 847 |
|
| 848 |
The decision is claimed before the grant, so a parallel click can never leave a grant under
|
| 849 |
a declined record. A failed grant puts the claim back.
|
| 850 |
"""
|
| 851 |
+
if authorization_only:
|
| 852 |
+
return _decide_team_approval(session, request_id, action, source_check)
|
| 853 |
if action not in ACTIONS:
|
| 854 |
raise Denied(400, "bad_action", BAD_ACTION)
|
| 855 |
rt, me = session.runtime, _lower(session.uname)
|
api/aios_session.py
CHANGED
|
@@ -31,6 +31,9 @@ from hashlib import sha256
|
|
| 31 |
COOKIE_NAME = "aios_session"
|
| 32 |
IDLE_SECONDS = 8 * 60 * 60 # D2: reissued on every authenticated request
|
| 33 |
ABSOLUTE_SECONDS = 30 * 24 * 60 * 60 # D2: a hard ceiling no amount of activity extends
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
_env_secret = os.environ.get("AIOS_SESSION_SECRET") or ""
|
| 36 |
EPHEMERAL_SECRET = not _env_secret
|
|
@@ -122,3 +125,59 @@ def clear_cookie(response, request):
|
|
| 122 |
# deleting, and a mismatched delete silently leaves the cookie in place.
|
| 123 |
response.delete_cookie(COOKIE_NAME, path="/", httponly=True, samesite="lax",
|
| 124 |
secure=is_secure_request(request))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
COOKIE_NAME = "aios_session"
|
| 32 |
IDLE_SECONDS = 8 * 60 * 60 # D2: reissued on every authenticated request
|
| 33 |
ABSOLUTE_SECONDS = 30 * 24 * 60 * 60 # D2: a hard ceiling no amount of activity extends
|
| 34 |
+
TRUST_COOKIE_NAME = "aios_trusted_browser"
|
| 35 |
+
TRUST_SECONDS = 7 * 24 * 60 * 60
|
| 36 |
+
_TRUST_PURPOSE = "aios-trusted-browser-v1"
|
| 37 |
|
| 38 |
_env_secret = os.environ.get("AIOS_SESSION_SECRET") or ""
|
| 39 |
EPHEMERAL_SECRET = not _env_secret
|
|
|
|
| 125 |
# deleting, and a mismatched delete silently leaves the cookie in place.
|
| 126 |
response.delete_cookie(COOKIE_NAME, path="/", httponly=True, samesite="lax",
|
| 127 |
secure=is_secure_request(request))
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def _trust_sign(payload):
|
| 131 |
+
key = hmac.new(_SECRET, _TRUST_PURPOSE.encode("ascii"), sha256).digest()
|
| 132 |
+
return _b64(hmac.new(key, payload, sha256).digest())
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def mint_trusted_browser(tenant, username, epoch, generation, *, now=None, original=None):
|
| 136 |
+
"""Carry the original proof window when a password login rotates its session epoch."""
|
| 137 |
+
now = int(time.time() if now is None else now)
|
| 138 |
+
issued = original["a"] if original is not None else now
|
| 139 |
+
expiry = original["x"] if original is not None else now + TRUST_SECONDS
|
| 140 |
+
claims = {"p": _TRUST_PURPOSE, "t": tenant, "u": username, "e": epoch,
|
| 141 |
+
"g": generation, "a": issued, "x": expiry}
|
| 142 |
+
payload = json.dumps(claims, separators=(",", ":"), sort_keys=True).encode("utf-8")
|
| 143 |
+
return f"{_b64(payload)}.{_trust_sign(payload)}", claims
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def read_trusted_browser(value, tenant, username, epoch, generation, *, now=None):
|
| 147 |
+
"""Refuse every malformed or mismatched claim without admitting a session."""
|
| 148 |
+
if not isinstance(value, str) or len(value) > 2048 or value.count(".") != 1:
|
| 149 |
+
return None
|
| 150 |
+
try:
|
| 151 |
+
body, signature = value.split(".")
|
| 152 |
+
payload = _unb64(body)
|
| 153 |
+
if _b64(payload) != body or not hmac.compare_digest(signature, _trust_sign(payload)):
|
| 154 |
+
return None
|
| 155 |
+
claims = json.loads(payload)
|
| 156 |
+
if not isinstance(claims, dict) or set(claims) != {"p", "t", "u", "e", "g", "a", "x"}:
|
| 157 |
+
return None
|
| 158 |
+
if any(type(claims[k]) is not int for k in ("e", "a", "x")):
|
| 159 |
+
return None
|
| 160 |
+
if any(type(claims[k]) is not str or not claims[k] for k in ("p", "t", "u", "g")):
|
| 161 |
+
return None
|
| 162 |
+
if (claims["p"] != _TRUST_PURPOSE or claims["t"] != tenant
|
| 163 |
+
or claims["u"] != username or claims["e"] != epoch
|
| 164 |
+
or claims["e"] < 0 or claims["g"] != generation):
|
| 165 |
+
return None
|
| 166 |
+
now = int(time.time() if now is None else now)
|
| 167 |
+
if not (0 <= claims["a"] <= now < claims["x"]
|
| 168 |
+
and claims["x"] - claims["a"] == TRUST_SECONDS):
|
| 169 |
+
return None
|
| 170 |
+
return claims
|
| 171 |
+
except (ValueError, TypeError, OverflowError, UnicodeError):
|
| 172 |
+
return None
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def set_trusted_browser_cookie(response, request, value, claims):
|
| 176 |
+
remaining = max(0, min(TRUST_SECONDS, claims["x"] - int(time.time())))
|
| 177 |
+
response.set_cookie(TRUST_COOKIE_NAME, value, httponly=True, samesite="lax",
|
| 178 |
+
secure=is_secure_request(request), max_age=remaining, path="/")
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def clear_trusted_browser_cookie(response, request):
|
| 182 |
+
response.delete_cookie(TRUST_COOKIE_NAME, path="/", httponly=True, samesite="lax",
|
| 183 |
+
secure=is_secure_request(request))
|
api/amazon_relational.py
CHANGED
|
@@ -34,6 +34,7 @@ them to read-through too. The shared document holds their schemas, never a trunc
|
|
| 34 |
import hashlib
|
| 35 |
import json
|
| 36 |
import os
|
|
|
|
| 37 |
import sys
|
| 38 |
from collections import OrderedDict
|
| 39 |
from pathlib import Path
|
|
@@ -132,9 +133,61 @@ _MONEY.update({"estimated_fee_total", "estimated_referral_fee_per_unit",
|
|
| 132 |
"estimated_pick_pack_fee_per_unit", "estimated_weight_handling_fee_per_unit",
|
| 133 |
"expected_fulfillment_fee_per_unit", "estimated_monthly_storage_fee"})
|
| 134 |
|
| 135 |
-
#:
|
| 136 |
-
#:
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# Inventory reports remain VARCHAR when a quantity column mixes numbers and blanks.
|
| 140 |
# Names identify units; identifiers such as SKU and FNSKU must stay text.
|
|
@@ -161,7 +214,9 @@ def _ftype(col, duck_type):
|
|
| 161 |
return "int"
|
| 162 |
if any(col == s or col.endswith(s) for s in _DATEISH):
|
| 163 |
return "date"
|
| 164 |
-
if
|
|
|
|
|
|
|
| 165 |
return "currency"
|
| 166 |
base = str(duck_type or "").upper().split("(")[0]
|
| 167 |
if base in ("DOUBLE", "FLOAT", "DECIMAL"):
|
|
@@ -171,6 +226,18 @@ def _ftype(col, duck_type):
|
|
| 171 |
return "text"
|
| 172 |
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
def mirror_columns(cur, table):
|
| 175 |
"""[(name, type)] for a mirror table, or [] when it has never been synced."""
|
| 176 |
try:
|
|
@@ -197,6 +264,8 @@ def mirror_columns(cur, table):
|
|
| 197 |
LISTINGS = PREFIX + "listings"
|
| 198 |
LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN = "lookup_parent_asin", "lookup_sku", "lookup_asin"
|
| 199 |
LOOKUP_KEYS = frozenset((LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN))
|
|
|
|
|
|
|
| 200 |
_LOOKUP_LABELS = {LOOKUP_PARENT: "Parent ASIN", LOOKUP_SKU: "SKU", LOOKUP_ASIN: "ASIN"}
|
| 201 |
_LOOKUP_DESCRIPTIONS = {
|
| 202 |
LOOKUP_PARENT: ("The parent ASIN of this row's product, from Amazon's catalog, else the "
|
|
@@ -342,9 +411,11 @@ def lookups(cur, key, names=None):
|
|
| 342 |
elif sku and latest:
|
| 343 |
ref = f"TRIM(CAST({table}.{_quoted(sku)} AS VARCHAR))"
|
| 344 |
via = sku_via if "seller_sku" in listing_names else None
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
| 348 |
if parents:
|
| 349 |
add(LOOKUP_PARENT, sku,
|
| 350 |
f"(SELECT string_agg(DISTINCT _p.parent_asin, ', ' ORDER BY _p.parent_asin) "
|
|
@@ -359,6 +430,8 @@ def _fields_for(cur, key, *, read_through=False):
|
|
| 359 |
if not cols:
|
| 360 |
return []
|
| 361 |
declared = {field["name"]: field for field in connector_spec.AMAZON_FIELDS.get(key, ())}
|
|
|
|
|
|
|
| 362 |
names = {c[0] for c in cols}
|
| 363 |
pinned = PINNED.get(key) if PINNED.get(key) in names else None
|
| 364 |
# S67-T30: each lookup lands IMMEDIATELY after the column it derives from, shown by default,
|
|
@@ -373,13 +446,15 @@ def _fields_for(cur, key, *, read_through=False):
|
|
| 373 |
# report's first column is whatever Amazon put there (`item_name`, `_data_origin`), so a
|
| 374 |
# listing opened from an order must lead with its seller SKU, as Odoo's pinned fields do.
|
| 375 |
for name, dtype in sorted(cols, key=lambda col: col[0] != pinned):
|
|
|
|
| 376 |
out.append(_odoo._preset({
|
| 377 |
-
"key": name,
|
|
|
|
| 378 |
"type": _ftype(name, dtype),
|
| 379 |
"source": "overlay",
|
| 380 |
-
"default": name == pinned or name in
|
| 381 |
"pinned": name == pinned,
|
| 382 |
-
**({"format":
|
| 383 |
}, flow="amazon_relational"))
|
| 384 |
for lookup in derived.get(name, ()):
|
| 385 |
field = {"key": lookup["key"], "label": lookup["label"], "type": "text",
|
|
@@ -402,9 +477,15 @@ def _fields_for(cur, key, *, read_through=False):
|
|
| 402 |
return out
|
| 403 |
|
| 404 |
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
"amazon_order_id", "purchase_date", "order_status", "sku", "seller_sku", "asin", "asin1",
|
| 409 |
"quantity", "quantity_purchased", "item_price", "item_status", "product_name", "item_name",
|
| 410 |
"settlement_id", "amount", "amount_type", "amount_description", "posted_date",
|
|
@@ -413,7 +494,10 @@ _DEFAULT_COLUMNS = {
|
|
| 413 |
"backend_keywords",
|
| 414 |
"month", "search_query", "impressions", "clicks", "cart_adds", "purchases", "source_basis",
|
| 415 |
"keyword", "date", "organic_rank", "sponsored_rank", "sponsored_rank_basis",
|
| 416 |
-
}
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
|
| 419 |
#: The original seven used first-field identities. Retained for legacy-id migration
|
|
@@ -549,9 +633,11 @@ def grid_source(cur, key):
|
|
| 549 |
continue
|
| 550 |
kind = _ftype(name, dtype)
|
| 551 |
expr = _quoted(name)
|
| 552 |
-
fractional = kind
|
| 553 |
-
if kind in ("currency", "int"):
|
| 554 |
expr = f"TRY_CAST({expr} AS {'DOUBLE' if fractional else 'BIGINT'})"
|
|
|
|
|
|
|
| 555 |
cols[name] = (expr, _number if fractional else {"int": _integer}.get(kind, _text))
|
| 556 |
# S67-T30: the product lookups bind like any mirror column, so a filter (`=` a parent), a
|
| 557 |
# group (count by parent) and a sort compile against them in SQL with no per-row Python.
|
|
@@ -613,7 +699,7 @@ def _read(cur, key):
|
|
| 613 |
quoted = ", ".join(spec["cols"][c][0] for c in cols)
|
| 614 |
personal = set(amazon_store.personal_columns(key))
|
| 615 |
numeric = {name for name, dtype in mirror_columns(cur, SOURCE[key])
|
| 616 |
-
if name not in personal and _ftype(name, dtype) in ("currency", "int")}
|
| 617 |
# S67-T30: a stored snapshot carries the same product lookups, computed by the SAME SQL
|
| 618 |
# expressions the read-through binding uses, in this one SELECT (never per row).
|
| 619 |
derived = [(lookup["key"], lookup["expr"]) for lookup in lookups(cur, key)]
|
|
@@ -729,6 +815,11 @@ def plan(cur, rt=None):
|
|
| 729 |
built[key] = {"readThrough": True,
|
| 730 |
"fields": _fields_for(cur, key, read_through=True), "count": n}
|
| 731 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 732 |
rows = _read(cur, key)
|
| 733 |
if rows:
|
| 734 |
built[key] = rows
|
|
@@ -750,7 +841,9 @@ def plan_fingerprint(cur, built):
|
|
| 750 |
rows = built.get(key) or []
|
| 751 |
canonical = sorted(rows, key=lambda row: json.dumps(
|
| 752 |
row, sort_keys=True, ensure_ascii=True, separators=(",", ":"), default=str))
|
| 753 |
-
|
|
|
|
|
|
|
| 754 |
payload = json.dumps(tables, sort_keys=True, ensure_ascii=True,
|
| 755 |
separators=(",", ":"), default=str)
|
| 756 |
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
|
@@ -979,10 +1072,15 @@ def apply_plan(rt, built, username="amazon", today=None):
|
|
| 979 |
not_stored.clear()
|
| 980 |
live_columns = {key: {field["key"] for field in table.get("fields", ())}
|
| 981 |
for key, table in doc.items() if key in SOURCE and isinstance(table, dict)}
|
|
|
|
|
|
|
|
|
|
| 982 |
live_columns.update({key: {field["key"] for field in fields}
|
| 983 |
-
for key, _label, fields, _rows in plans})
|
| 984 |
generated = connector_spec.system_link_fields("amazon", live_columns)
|
| 985 |
for key, label, fields, rows in plans:
|
|
|
|
|
|
|
| 986 |
stub = isinstance(rows, dict) and rows.get("readThrough") is True
|
| 987 |
|
| 988 |
def unbacked_refreshed(field):
|
|
@@ -1038,6 +1136,15 @@ def apply_plan(rt, built, username="amazon", today=None):
|
|
| 1038 |
removed = len((doc.get(key) or {}).get("rows") or {}) if stub else 0
|
| 1039 |
written[key] = _odoo._ensure_table_inplace(doc, key, label, fields, None if stub else rows,
|
| 1040 |
username, stamp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1041 |
if stub:
|
| 1042 |
doc[key]["readThrough"] = True
|
| 1043 |
doc[key]["rows"] = {}
|
|
@@ -1048,7 +1155,7 @@ def apply_plan(rt, built, username="amazon", today=None):
|
|
| 1048 |
# The shared merger compares string spellings, so an old "2" and a new
|
| 1049 |
# integer 2 otherwise compare equal and leave the old cell untyped.
|
| 1050 |
numeric = {f["key"] for f in doc[key].get("fields", [])
|
| 1051 |
-
if f.get("type") in ("currency", "int")
|
| 1052 |
and (f.get("automation") or {}).get("flowId") == "amazon_relational"
|
| 1053 |
and not _odoo._ut().user_edited(f)}
|
| 1054 |
stored = doc[key].get("rows") or {}
|
|
|
|
| 34 |
import hashlib
|
| 35 |
import json
|
| 36 |
import os
|
| 37 |
+
import re
|
| 38 |
import sys
|
| 39 |
from collections import OrderedDict
|
| 40 |
from pathlib import Path
|
|
|
|
| 133 |
"estimated_pick_pack_fee_per_unit", "estimated_weight_handling_fee_per_unit",
|
| 134 |
"expected_fulfillment_fee_per_unit", "estimated_monthly_storage_fee"})
|
| 135 |
|
| 136 |
+
#: S78-T01: money the report stores as text or names without an `_amount` / `_fee` suffix.
|
| 137 |
+
#: ⚠ No `_price` suffix rule: `attributes_list_price` and the `*_median_*_price` details are JSON.
|
| 138 |
+
_MONEY.update({"business_price", "label_cost", "fee_total", "sales_price", "item_price_per_unit",
|
| 139 |
+
"featuredoffer_price", "lowest_price_new_plus_shipping", "lowest_price_used",
|
| 140 |
+
"recommended_sales_price", "estimated_cost_savings_of_recommended_actions",
|
| 141 |
+
"estimated_storage_cost_next_month", "sales_last_30_days", "amount_charged",
|
| 142 |
+
"rate_surcharge", "estimated_referral_fee_per_item", "amount_per_unit",
|
| 143 |
+
"amount_total", "base_rate", "est_base_msf", "est_sus", "utilization_surcharge_rate",
|
| 144 |
+
"estimated_future_fee_current_selling_on_amazon_future_fulfillment_fees",
|
| 145 |
+
"estimated_future_order_handling_fee_per_order",
|
| 146 |
+
"estimated_future_pick_pack_fee_per_unit",
|
| 147 |
+
"estimated_future_weight_handling_fee_per_unit",
|
| 148 |
+
"expected_future_fulfillment_fee_per_unit", "lost_revenue_due_to_oos",
|
| 149 |
+
"non_subscriber_average_revenue", "subscriber_average_revenue",
|
| 150 |
+
"total_subscriptions_revenue"})
|
| 151 |
+
_MONEY_PREFIX = ("cost_", "sales_shipped_last_", "estimated_ais_", "sns_sale_price_week_",
|
| 152 |
+
"revenue_from_")
|
| 153 |
+
_MONEY_INFIX = ("_life_time_value_",)
|
| 154 |
+
|
| 155 |
+
#: ⭐ S78-T01: rates and shares are `pct`, and the grid shows a `pct` value as the number plus
|
| 156 |
+
#: "%" with no scaling. So the scale is MEASURED per column on the staging mirror (2026-10-20):
|
| 157 |
+
#: `_PERCENT` columns already hold 0 to 100, `_FRACTION_PERCENT` columns hold 0 to 1 and are
|
| 158 |
+
#: multiplied by 100 in the read path (`grid_source`), so 0.15 reads "15%" and never "0.15%".
|
| 159 |
+
#: ⛔ A rate whose scale was not measured stays out of both sets (see `oos_rate_week_*`).
|
| 160 |
+
_PERCENT_SUFFIX = ("_percentage", "_percentage_b2_b")
|
| 161 |
+
_PERCENT = {"sales_by_asin_refund_rate", "sales_by_asin_refund_rate_b2_b",
|
| 162 |
+
"cart_add_data_asin_cart_add_share", "click_data_asin_click_share",
|
| 163 |
+
"impression_data_asin_impression_share", "purchase_data_asin_purchase_share",
|
| 164 |
+
"cart_add_data_total_cart_add_rate", "click_data_total_click_rate",
|
| 165 |
+
"purchase_data_total_purchase_rate", "revenue_penetration",
|
| 166 |
+
"coupons_revenue_penetration", "share_of_coupon_subscriptions",
|
| 167 |
+
"subscriber_retention_for30_days", "subscriber_retention_for90_days",
|
| 168 |
+
*(f"{metric}_for{n}_percent_seller_funding" for metric in
|
| 169 |
+
("revenue_penetration", "signup_conversion") for n in (0, 5, 10))}
|
| 170 |
+
_FRACTION_PERCENT = {"click_share", "conversion_share", "click_data_click_rate",
|
| 171 |
+
"purchase_data_conversion_rate", "combination_pct"}
|
| 172 |
+
#: Account health reports each rate metric and its target as a 0 to 1 fraction.
|
| 173 |
+
_FRACTION_PERCENT_RE = re.compile(r"[a-z_]+_rate(?:_afn|_mfn)?_(?:rate|target_value)")
|
| 174 |
+
|
| 175 |
+
#: Non-money numbers stored as text. With the DOUBLE columns that are not money, they take
|
| 176 |
+
#: `currency` (the grid's only fractional type) with an EMPTY currency symbol, so no "$" shows.
|
| 177 |
+
_NUMBER = {"storage_utilization_ratio"}
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _is_money(col):
|
| 181 |
+
return (col in _MONEY or col.endswith(("_amount", "_fee", "_fees")) or col.startswith(_MONEY_PREFIX)
|
| 182 |
+
or any(part in col for part in _MONEY_INFIX))
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def _is_fraction_percent(col):
|
| 186 |
+
return col in _FRACTION_PERCENT or bool(_FRACTION_PERCENT_RE.fullmatch(col))
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def _is_percent(col):
|
| 190 |
+
return col in _PERCENT or col.endswith(_PERCENT_SUFFIX) or _is_fraction_percent(col)
|
| 191 |
|
| 192 |
# Inventory reports remain VARCHAR when a quantity column mixes numbers and blanks.
|
| 193 |
# Names identify units; identifiers such as SKU and FNSKU must stay text.
|
|
|
|
| 214 |
return "int"
|
| 215 |
if any(col == s or col.endswith(s) for s in _DATEISH):
|
| 216 |
return "date"
|
| 217 |
+
if _is_percent(col):
|
| 218 |
+
return "pct"
|
| 219 |
+
if _is_money(col) or col in _NUMBER:
|
| 220 |
return "currency"
|
| 221 |
base = str(duck_type or "").upper().split("(")[0]
|
| 222 |
if base in ("DOUBLE", "FLOAT", "DECIMAL"):
|
|
|
|
| 226 |
return "text"
|
| 227 |
|
| 228 |
|
| 229 |
+
def _format(col, duck_type):
|
| 230 |
+
"""The display format a field ships with, or None for the grid's default."""
|
| 231 |
+
kind = _ftype(col, duck_type)
|
| 232 |
+
if col == "per_unit_volume":
|
| 233 |
+
return {"decimals": 4}
|
| 234 |
+
if kind == "pct":
|
| 235 |
+
return {"decimals": 2}
|
| 236 |
+
if kind == "currency" and not _is_money(col):
|
| 237 |
+
return {"currencySymbol": "", "decimals": 2}
|
| 238 |
+
return None
|
| 239 |
+
|
| 240 |
+
|
| 241 |
def mirror_columns(cur, table):
|
| 242 |
"""[(name, type)] for a mirror table, or [] when it has never been synced."""
|
| 243 |
try:
|
|
|
|
| 264 |
LISTINGS = PREFIX + "listings"
|
| 265 |
LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN = "lookup_parent_asin", "lookup_sku", "lookup_asin"
|
| 266 |
LOOKUP_KEYS = frozenset((LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN))
|
| 267 |
+
#: Native columns that already show a row's ASIN (orders, order items, replenishment carry `asin`).
|
| 268 |
+
_NATIVE_ASIN = frozenset(("asin", "asin1", "child_asin"))
|
| 269 |
_LOOKUP_LABELS = {LOOKUP_PARENT: "Parent ASIN", LOOKUP_SKU: "SKU", LOOKUP_ASIN: "ASIN"}
|
| 270 |
_LOOKUP_DESCRIPTIONS = {
|
| 271 |
LOOKUP_PARENT: ("The parent ASIN of this row's product, from Amazon's catalog, else the "
|
|
|
|
| 411 |
elif sku and latest:
|
| 412 |
ref = f"TRIM(CAST({table}.{_quoted(sku)} AS VARCHAR))"
|
| 413 |
via = sku_via if "seller_sku" in listing_names else None
|
| 414 |
+
# S78-T01: a table that carries its own ASIN column gains no second "ASIN".
|
| 415 |
+
if not names & _NATIVE_ASIN:
|
| 416 |
+
add(LOOKUP_ASIN, sku,
|
| 417 |
+
f"(SELECT string_agg(DISTINCT _l.asin, ', ' ORDER BY _l.asin) "
|
| 418 |
+
f"FROM ({latest}) AS _l WHERE _l.sku = {ref})", via)
|
| 419 |
if parents:
|
| 420 |
add(LOOKUP_PARENT, sku,
|
| 421 |
f"(SELECT string_agg(DISTINCT _p.parent_asin, ', ' ORDER BY _p.parent_asin) "
|
|
|
|
| 430 |
if not cols:
|
| 431 |
return []
|
| 432 |
declared = {field["name"]: field for field in connector_spec.AMAZON_FIELDS.get(key, ())}
|
| 433 |
+
written = connector_spec.AMAZON_LABELS.get(key, {})
|
| 434 |
+
shown = default_columns(key)
|
| 435 |
names = {c[0] for c in cols}
|
| 436 |
pinned = PINNED.get(key) if PINNED.get(key) in names else None
|
| 437 |
# S67-T30: each lookup lands IMMEDIATELY after the column it derives from, shown by default,
|
|
|
|
| 446 |
# report's first column is whatever Amazon put there (`item_name`, `_data_origin`), so a
|
| 447 |
# listing opened from an order must lead with its seller SKU, as Odoo's pinned fields do.
|
| 448 |
for name, dtype in sorted(cols, key=lambda col: col[0] != pinned):
|
| 449 |
+
fmt = _format(name, dtype)
|
| 450 |
out.append(_odoo._preset({
|
| 451 |
+
"key": name,
|
| 452 |
+
"label": declared.get(name, {}).get("label") or written.get(name) or _label(name),
|
| 453 |
"type": _ftype(name, dtype),
|
| 454 |
"source": "overlay",
|
| 455 |
+
"default": name == pinned or name in shown or name in product,
|
| 456 |
"pinned": name == pinned,
|
| 457 |
+
**({"format": fmt} if fmt else {}),
|
| 458 |
}, flow="amazon_relational"))
|
| 459 |
for lookup in derived.get(name, ()):
|
| 460 |
field = {"key": lookup["key"], "label": lookup["label"], "type": "text",
|
|
|
|
| 477 |
return out
|
| 478 |
|
| 479 |
|
| 480 |
+
def default_columns(key):
|
| 481 |
+
"""The columns `key` opens on beyond the pinned, product, lookup and link fields."""
|
| 482 |
+
declared = connector_spec.AMAZON_DEFAULT_COLUMNS.get(key)
|
| 483 |
+
return frozenset(declared) if declared is not None else _UNDECLARED_DEFAULT_COLUMNS
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
#: The shared fallback for a table `connector_spec.AMAZON_DEFAULT_COLUMNS` does not declare yet
|
| 487 |
+
#: (one the staging mirror has never synced). A declared table reads only its own list.
|
| 488 |
+
_UNDECLARED_DEFAULT_COLUMNS = frozenset({
|
| 489 |
"amazon_order_id", "purchase_date", "order_status", "sku", "seller_sku", "asin", "asin1",
|
| 490 |
"quantity", "quantity_purchased", "item_price", "item_status", "product_name", "item_name",
|
| 491 |
"settlement_id", "amount", "amount_type", "amount_description", "posted_date",
|
|
|
|
| 494 |
"backend_keywords",
|
| 495 |
"month", "search_query", "impressions", "clicks", "cart_adds", "purchases", "source_basis",
|
| 496 |
"keyword", "date", "organic_rank", "sponsored_rank", "sponsored_rank_basis",
|
| 497 |
+
})
|
| 498 |
+
|
| 499 |
+
#: Every name any Amazon table may open on (`verify_agent_knowledge` reads it by name).
|
| 500 |
+
_DEFAULT_COLUMNS = _UNDECLARED_DEFAULT_COLUMNS.union(*connector_spec.AMAZON_DEFAULT_COLUMNS.values())
|
| 501 |
|
| 502 |
|
| 503 |
#: The original seven used first-field identities. Retained for legacy-id migration
|
|
|
|
| 633 |
continue
|
| 634 |
kind = _ftype(name, dtype)
|
| 635 |
expr = _quoted(name)
|
| 636 |
+
fractional = kind in ("currency", "pct") or name == "per_unit_volume"
|
| 637 |
+
if kind in ("currency", "pct", "int"):
|
| 638 |
expr = f"TRY_CAST({expr} AS {'DOUBLE' if fractional else 'BIGINT'})"
|
| 639 |
+
if kind == "pct" and _is_fraction_percent(name):
|
| 640 |
+
expr = f"ROUND({expr} * 100, 8)" # 8 places drops float noise, keeps every digit
|
| 641 |
cols[name] = (expr, _number if fractional else {"int": _integer}.get(kind, _text))
|
| 642 |
# S67-T30: the product lookups bind like any mirror column, so a filter (`=` a parent), a
|
| 643 |
# group (count by parent) and a sort compile against them in SQL with no per-row Python.
|
|
|
|
| 699 |
quoted = ", ".join(spec["cols"][c][0] for c in cols)
|
| 700 |
personal = set(amazon_store.personal_columns(key))
|
| 701 |
numeric = {name for name, dtype in mirror_columns(cur, SOURCE[key])
|
| 702 |
+
if name not in personal and _ftype(name, dtype) in ("currency", "pct", "int")}
|
| 703 |
# S67-T30: a stored snapshot carries the same product lookups, computed by the SAME SQL
|
| 704 |
# expressions the read-through binding uses, in this one SELECT (never per row).
|
| 705 |
derived = [(lookup["key"], lookup["expr"]) for lookup in lookups(cur, key)]
|
|
|
|
| 815 |
built[key] = {"readThrough": True,
|
| 816 |
"fields": _fields_for(cur, key, read_through=True), "count": n}
|
| 817 |
continue
|
| 818 |
+
if not n:
|
| 819 |
+
# S78-T02: an emptied mirror table is an empty population, never a skip, or the stored
|
| 820 |
+
# definition keeps rows the source no longer holds. `apply_plan` creates no table for it.
|
| 821 |
+
built[key] = []
|
| 822 |
+
continue
|
| 823 |
rows = _read(cur, key)
|
| 824 |
if rows:
|
| 825 |
built[key] = rows
|
|
|
|
| 841 |
rows = built.get(key) or []
|
| 842 |
canonical = sorted(rows, key=lambda row: json.dumps(
|
| 843 |
row, sort_keys=True, ensure_ascii=True, separators=(",", ":"), default=str))
|
| 844 |
+
# S78-T02: an empty population differs from an absent table, so a stale copy is rewritten.
|
| 845 |
+
empty = {"empty": True} if built.get(key) == [] else {}
|
| 846 |
+
tables.append({"key": key, "fields": _fields_for(cur, key), "rows": canonical, **empty})
|
| 847 |
payload = json.dumps(tables, sort_keys=True, ensure_ascii=True,
|
| 848 |
separators=(",", ":"), default=str)
|
| 849 |
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
|
|
|
| 1072 |
not_stored.clear()
|
| 1073 |
live_columns = {key: {field["key"] for field in table.get("fields", ())}
|
| 1074 |
for key, table in doc.items() if key in SOURCE and isinstance(table, dict)}
|
| 1075 |
+
# S78-T02: an empty population reconciles an EXISTING definition only; it creates none.
|
| 1076 |
+
unborn = {key for key, _label, _fields, rows in plans
|
| 1077 |
+
if isinstance(rows, list) and not rows and not isinstance(doc.get(key), dict)}
|
| 1078 |
live_columns.update({key: {field["key"] for field in fields}
|
| 1079 |
+
for key, _label, fields, _rows in plans if key not in unborn})
|
| 1080 |
generated = connector_spec.system_link_fields("amazon", live_columns)
|
| 1081 |
for key, label, fields, rows in plans:
|
| 1082 |
+
if key in unborn:
|
| 1083 |
+
continue
|
| 1084 |
stub = isinstance(rows, dict) and rows.get("readThrough") is True
|
| 1085 |
|
| 1086 |
def unbacked_refreshed(field):
|
|
|
|
| 1136 |
removed = len((doc.get(key) or {}).get("rows") or {}) if stub else 0
|
| 1137 |
written[key] = _odoo._ensure_table_inplace(doc, key, label, fields, None if stub else rows,
|
| 1138 |
username, stamp)
|
| 1139 |
+
# S78-T01: `_ensure_table_inplace` does not forward `format`, so a stored preset field
|
| 1140 |
+
# would keep showing "$" on a weight or 0 decimals on a percent.
|
| 1141 |
+
formats = {f["key"]: f["format"] for f in fields if f.get("format")}
|
| 1142 |
+
for stored_field in doc[key].get("fields", []):
|
| 1143 |
+
if (stored_field.get("key") in formats
|
| 1144 |
+
and (stored_field.get("automation") or {}).get("flowId") == "amazon_relational"
|
| 1145 |
+
and (stored_field.get("automation") or {}).get("preset")
|
| 1146 |
+
and not _odoo._ut().user_edited(stored_field)):
|
| 1147 |
+
stored_field["format"] = dict(formats[stored_field["key"]])
|
| 1148 |
if stub:
|
| 1149 |
doc[key]["readThrough"] = True
|
| 1150 |
doc[key]["rows"] = {}
|
|
|
|
| 1155 |
# The shared merger compares string spellings, so an old "2" and a new
|
| 1156 |
# integer 2 otherwise compare equal and leave the old cell untyped.
|
| 1157 |
numeric = {f["key"] for f in doc[key].get("fields", [])
|
| 1158 |
+
if f.get("type") in ("currency", "pct", "int")
|
| 1159 |
and (f.get("automation") or {}).get("flowId") == "amazon_relational"
|
| 1160 |
and not _odoo._ut().user_edited(f)}
|
| 1161 |
stored = doc[key].get("rows") or {}
|
api/application_registry.py
CHANGED
|
@@ -69,6 +69,76 @@ _TEMPLATE_CONTRACTS = {
|
|
| 69 |
APPLICATION_WIZARD_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS)
|
| 70 |
APPLICATION_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
class ApplicationError(ValueError):
|
| 74 |
pass
|
|
@@ -132,8 +202,19 @@ def own_template_apps(runtime, user):
|
|
| 132 |
return owned
|
| 133 |
|
| 134 |
|
| 135 |
-
def template_contracts():
|
| 136 |
-
"""The trusted renderer contracts offered by the Application creation flow.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
return [copy.deepcopy(contract) for contract in _TEMPLATE_CONTRACTS.values()]
|
| 138 |
|
| 139 |
|
|
@@ -627,7 +708,7 @@ def _refuse_legacy_catalog_changes(app, config):
|
|
| 627 |
|
| 628 |
def _template_definition(kind, payload, *, semantic_records=None, field_contracts=None,
|
| 629 |
allowed_databases=None):
|
| 630 |
-
contract = _TEMPLATE_CONTRACTS.get(kind)
|
| 631 |
if contract is None:
|
| 632 |
raise ApplicationError("application template kind is not recognized")
|
| 633 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
|
@@ -636,6 +717,10 @@ def _template_definition(kind, payload, *, semantic_records=None, field_contract
|
|
| 636 |
source = str(payload.get("sourceDatabase") or "").strip()[:120]
|
| 637 |
if not source:
|
| 638 |
raise ApplicationError("sourceDatabase is required")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
if source not in set(allowed_databases or ()):
|
| 640 |
raise PermissionError("an Application cannot bind a database you cannot open")
|
| 641 |
if "compatibleDatabases" in payload:
|
|
@@ -706,6 +791,14 @@ def validate_application(payload, tenant, *, semantic_records=None, field_contra
|
|
| 706 |
if "tenant" in payload and str(payload.get("tenant") or "") != str(tenant):
|
| 707 |
raise PermissionError("an Application cannot be created for another tenant")
|
| 708 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
if template_kind and template_kind not in _TEMPLATE_CONTRACTS:
|
| 710 |
raise ApplicationError("application template kind is not recognized")
|
| 711 |
expected_type = (_TEMPLATE_CONTRACTS[template_kind]["applicationType"]
|
|
@@ -843,7 +936,7 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co
|
|
| 843 |
"""Build the same validated record for authored Apps and boot migrations."""
|
| 844 |
payload=payload if isinstance(payload,dict) else {}
|
| 845 |
template_kind=str(payload.get("templateKind") or "").strip().lower()
|
| 846 |
-
expected_type = (
|
| 847 |
kind=str(payload.get("type") or expected_type or "").strip().lower()
|
| 848 |
definition = validate_application(
|
| 849 |
payload, tenant, semantic_records=semantic_records, field_contracts=field_contracts,
|
|
@@ -858,7 +951,7 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co
|
|
| 858 |
raw_config = payload.get("config")
|
| 859 |
if definition and raw_config is None:
|
| 860 |
raw_config = {"schemaVersion": 1,
|
| 861 |
-
"display": {"mode":
|
| 862 |
app={"id":app_id,"tenant":str(tenant),"type":kind,"name":name,
|
| 863 |
"compatibleDatabases":compatible,
|
| 864 |
"semanticRoleBindings":_governed_bindings(
|
|
@@ -867,8 +960,8 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co
|
|
| 867 |
"share":{"mode":"private","users":[]},"starredBy":[],"createdBy":user,
|
| 868 |
"createdAt":now,"updatedAt":now,"revision":1}
|
| 869 |
app.update(definition)
|
| 870 |
-
if definition and
|
| 871 |
-
app["deepLink"] =
|
| 872 |
if kind == "code":
|
| 873 |
app.update(deepLink=f"/app/code/{app_id}", origin=None)
|
| 874 |
app["origin"] = _server_origin(origin_door, via)
|
|
@@ -1237,7 +1330,8 @@ def mutate(runtime, app_id, user, is_admin, patch, *, semantic_records=None, fie
|
|
| 1237 |
if not _is_finance(app) and not _allowed(app.get("compatibleDatabases"), allowed_databases):
|
| 1238 |
if not _database_free_code(app):
|
| 1239 |
raise ApplicationError("application does not exist")
|
| 1240 |
-
if app.get("templateKind") in APPLICATION_TEMPLATE_KINDS
|
|
|
|
| 1241 |
raise ApplicationError("a template Application's sourceDatabase is immutable")
|
| 1242 |
star_only = set(patch) <= {"starred"}
|
| 1243 |
if star_only:
|
|
@@ -1522,7 +1616,7 @@ def _retirement_plan(runtime, tenant):
|
|
| 1522 |
for app in applications.values()):
|
| 1523 |
raise ApplicationError("the Application registry has an invalid tenant record")
|
| 1524 |
removed = {identity: app for identity, app in applications.items()
|
| 1525 |
-
if app.get("templateKind") not in ("route", "catalog")}
|
| 1526 |
tables = _retirement_read(runtime, user_tables.STORE_KEY)
|
| 1527 |
databases = {"customer_data", "product_data"} | {key for key in tables if key.startswith("ut_")}
|
| 1528 |
for app in applications.values():
|
|
|
|
| 69 |
APPLICATION_WIZARD_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS)
|
| 70 |
APPLICATION_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS)
|
| 71 |
|
| 72 |
+
#: S78-T17 (C4, R20, R21): templates offered only to a tenant that passes the kind's gate.
|
| 73 |
+
#: They stay out of the ungated tuples above, which other gates pin to Routing and Catalog.
|
| 74 |
+
BRAND_ANALYTICS_DATABASE = "ut_amzn_brand_analytics"
|
| 75 |
+
_GATED_TEMPLATE_CONTRACTS = {
|
| 76 |
+
"competitor_watch": {
|
| 77 |
+
"kind": "competitor_watch",
|
| 78 |
+
"label": "Competitor watch",
|
| 79 |
+
# The screen reads the competitor watch routes; the source names the Amazon database.
|
| 80 |
+
"applicationType": "map",
|
| 81 |
+
"displayMode": "map",
|
| 82 |
+
"fieldRoles": [
|
| 83 |
+
{"key": "term", "label": "Search term", "required": True, "types": []},
|
| 84 |
+
],
|
| 85 |
+
"defaultPlacement": {"section": "app", "order": 0},
|
| 86 |
+
"sourceDatabase": BRAND_ANALYTICS_DATABASE,
|
| 87 |
+
"sourceLabel": "Amazon brand analytics",
|
| 88 |
+
},
|
| 89 |
+
}
|
| 90 |
+
GATED_TEMPLATE_KINDS = tuple(_GATED_TEMPLATE_CONTRACTS)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def _template_contract(kind):
|
| 94 |
+
"""The contract for an ungated or gated template kind, or None."""
|
| 95 |
+
return _TEMPLATE_CONTRACTS.get(kind) or _GATED_TEMPLATE_CONTRACTS.get(kind)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def brand_analytics_holds(connected, enabled, refused):
|
| 99 |
+
"""R21: Amazon is connected, brand analytics is switched on and Amazon has not refused it."""
|
| 100 |
+
return connected is True and enabled is True and refused is not True
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _brand_analytics_refused(runtime):
|
| 104 |
+
"""True when the mirror's last brand analytics pass was refused; unreadable is no evidence."""
|
| 105 |
+
try:
|
| 106 |
+
import duckdb
|
| 107 |
+
from pathlib import Path
|
| 108 |
+
from harness import amazon_store as az, datastore
|
| 109 |
+
path = az._tenant_mirror_path(runtime.key, rt=runtime)
|
| 110 |
+
if not path.is_file():
|
| 111 |
+
return False
|
| 112 |
+
query = "SELECT phase FROM _sync_state WHERE entity = ?"
|
| 113 |
+
key = [az.entity_key("brand_analytics")]
|
| 114 |
+
if path.resolve() == Path(datastore.DB_PATH).resolve():
|
| 115 |
+
row = datastore.ro_cursor().execute(query, key).fetchone()
|
| 116 |
+
else:
|
| 117 |
+
with duckdb.connect(str(path), read_only=True) as cursor:
|
| 118 |
+
row = cursor.execute(query, key).fetchone()
|
| 119 |
+
return bool(row) and row[0] == "refused"
|
| 120 |
+
except Exception: # noqa: BLE001
|
| 121 |
+
return False
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def brand_analytics_offered(session):
|
| 125 |
+
"""R21 gate for the Competitor watch template. Any read failure withholds the template."""
|
| 126 |
+
try:
|
| 127 |
+
import connectors_amazon as cx
|
| 128 |
+
from routes_connectors import _entry_types
|
| 129 |
+
types, _scopes, failure = _entry_types(session.runtime, session.uname)
|
| 130 |
+
connected = failure is None and cx.ENTRY_TYPE in set(types)
|
| 131 |
+
enablement = cx.read_database_enablement(session.runtime)
|
| 132 |
+
enabled = (enablement.get("available") is True
|
| 133 |
+
and (enablement.get("databases") or {}).get(BRAND_ANALYTICS_DATABASE) is True)
|
| 134 |
+
refused = _brand_analytics_refused(session.runtime) if connected and enabled else None
|
| 135 |
+
return brand_analytics_holds(connected, enabled, refused)
|
| 136 |
+
except Exception: # noqa: BLE001
|
| 137 |
+
return False
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
_TEMPLATE_GATES = {"competitor_watch": brand_analytics_offered}
|
| 141 |
+
|
| 142 |
|
| 143 |
class ApplicationError(ValueError):
|
| 144 |
pass
|
|
|
|
| 202 |
return owned
|
| 203 |
|
| 204 |
|
| 205 |
+
def template_contracts(session=None):
|
| 206 |
+
"""The trusted renderer contracts offered by the Application creation flow.
|
| 207 |
+
|
| 208 |
+
A gated template joins only for a session whose tenant passes its gate (R21).
|
| 209 |
+
"""
|
| 210 |
+
rows = _ungated_template_contracts()
|
| 211 |
+
if session is not None:
|
| 212 |
+
rows.extend(copy.deepcopy(contract) for kind, contract in _GATED_TEMPLATE_CONTRACTS.items()
|
| 213 |
+
if _TEMPLATE_GATES[kind](session))
|
| 214 |
+
return rows
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def _ungated_template_contracts():
|
| 218 |
return [copy.deepcopy(contract) for contract in _TEMPLATE_CONTRACTS.values()]
|
| 219 |
|
| 220 |
|
|
|
|
| 708 |
|
| 709 |
def _template_definition(kind, payload, *, semantic_records=None, field_contracts=None,
|
| 710 |
allowed_databases=None):
|
| 711 |
+
contract = _TEMPLATE_CONTRACTS.get(kind) or _GATED_TEMPLATE_CONTRACTS.get(kind)
|
| 712 |
if contract is None:
|
| 713 |
raise ApplicationError("application template kind is not recognized")
|
| 714 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
|
|
|
| 717 |
source = str(payload.get("sourceDatabase") or "").strip()[:120]
|
| 718 |
if not source:
|
| 719 |
raise ApplicationError("sourceDatabase is required")
|
| 720 |
+
if contract.get("sourceDatabase") and source != contract["sourceDatabase"]:
|
| 721 |
+
raise ApplicationError(f"{contract['label']} reads "
|
| 722 |
+
f"{contract.get('sourceLabel') or contract['sourceDatabase']} only. "
|
| 723 |
+
"Choose it as the source database.")
|
| 724 |
if source not in set(allowed_databases or ()):
|
| 725 |
raise PermissionError("an Application cannot bind a database you cannot open")
|
| 726 |
if "compatibleDatabases" in payload:
|
|
|
|
| 791 |
if "tenant" in payload and str(payload.get("tenant") or "") != str(tenant):
|
| 792 |
raise PermissionError("an Application cannot be created for another tenant")
|
| 793 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
| 794 |
+
gated = _GATED_TEMPLATE_CONTRACTS.get(template_kind)
|
| 795 |
+
if gated is not None:
|
| 796 |
+
if str(payload.get("type") or gated["applicationType"]).strip().lower() != gated["applicationType"]:
|
| 797 |
+
raise ApplicationError(
|
| 798 |
+
f"{template_kind} template Applications require type {gated['applicationType']!r}")
|
| 799 |
+
return _template_definition(
|
| 800 |
+
template_kind, payload, semantic_records=semantic_records,
|
| 801 |
+
field_contracts=field_contracts, allowed_databases=allowed_databases)
|
| 802 |
if template_kind and template_kind not in _TEMPLATE_CONTRACTS:
|
| 803 |
raise ApplicationError("application template kind is not recognized")
|
| 804 |
expected_type = (_TEMPLATE_CONTRACTS[template_kind]["applicationType"]
|
|
|
|
| 936 |
"""Build the same validated record for authored Apps and boot migrations."""
|
| 937 |
payload=payload if isinstance(payload,dict) else {}
|
| 938 |
template_kind=str(payload.get("templateKind") or "").strip().lower()
|
| 939 |
+
expected_type = (_template_contract(template_kind) or {}).get("applicationType")
|
| 940 |
kind=str(payload.get("type") or expected_type or "").strip().lower()
|
| 941 |
definition = validate_application(
|
| 942 |
payload, tenant, semantic_records=semantic_records, field_contracts=field_contracts,
|
|
|
|
| 951 |
raw_config = payload.get("config")
|
| 952 |
if definition and raw_config is None:
|
| 953 |
raw_config = {"schemaVersion": 1,
|
| 954 |
+
"display": {"mode": _template_contract(template_kind)["displayMode"]}}
|
| 955 |
app={"id":app_id,"tenant":str(tenant),"type":kind,"name":name,
|
| 956 |
"compatibleDatabases":compatible,
|
| 957 |
"semanticRoleBindings":_governed_bindings(
|
|
|
|
| 960 |
"share":{"mode":"private","users":[]},"starredBy":[],"createdBy":user,
|
| 961 |
"createdAt":now,"updatedAt":now,"revision":1}
|
| 962 |
app.update(definition)
|
| 963 |
+
if definition and _template_contract(template_kind).get("deepLink"):
|
| 964 |
+
app["deepLink"] = _template_contract(template_kind)["deepLink"].replace("<id>", app_id)
|
| 965 |
if kind == "code":
|
| 966 |
app.update(deepLink=f"/app/code/{app_id}", origin=None)
|
| 967 |
app["origin"] = _server_origin(origin_door, via)
|
|
|
|
| 1330 |
if not _is_finance(app) and not _allowed(app.get("compatibleDatabases"), allowed_databases):
|
| 1331 |
if not _database_free_code(app):
|
| 1332 |
raise ApplicationError("application does not exist")
|
| 1333 |
+
if (app.get("templateKind") in APPLICATION_TEMPLATE_KINDS + GATED_TEMPLATE_KINDS
|
| 1334 |
+
and "compatibleDatabases" in patch):
|
| 1335 |
raise ApplicationError("a template Application's sourceDatabase is immutable")
|
| 1336 |
star_only = set(patch) <= {"starred"}
|
| 1337 |
if star_only:
|
|
|
|
| 1616 |
for app in applications.values()):
|
| 1617 |
raise ApplicationError("the Application registry has an invalid tenant record")
|
| 1618 |
removed = {identity: app for identity, app in applications.items()
|
| 1619 |
+
if app.get("templateKind") not in ("route", "catalog", *GATED_TEMPLATE_KINDS)}
|
| 1620 |
tables = _retirement_read(runtime, user_tables.STORE_KEY)
|
| 1621 |
databases = {"customer_data", "product_data"} | {key for key in tables if key.startswith("ut_")}
|
| 1622 |
for app in applications.values():
|
api/assistant_memory.py
CHANGED
|
@@ -238,6 +238,44 @@ def remember(rt, username, text, *, sources, door):
|
|
| 238 |
return entry["id"]
|
| 239 |
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
def forget(rt, username, id):
|
| 242 |
"""Erase only an own entry and retain its normalized text against stale extraction."""
|
| 243 |
if not any(entry.get("id") == id for entry in document(rt, username)["entries"]):
|
|
|
|
| 238 |
return entry["id"]
|
| 239 |
|
| 240 |
|
| 241 |
+
def remember_schedule_correction(rt, username, schedule, result_id, change):
|
| 242 |
+
"""Keep a structured replacement outside model compaction and bind its original action."""
|
| 243 |
+
import schedule_envelope
|
| 244 |
+
from schedules import scheduled_change
|
| 245 |
+
|
| 246 |
+
if schedule.get("owner") != username:
|
| 247 |
+
raise ValueError("That schedule is not available.")
|
| 248 |
+
action = schedule["action"]
|
| 249 |
+
change = scheduled_change(change)
|
| 250 |
+
original = action.get("change") or {}
|
| 251 |
+
if (change["record_id"] != original.get("record_id")
|
| 252 |
+
or set(change["values"]) != set(original.get("values") or {})):
|
| 253 |
+
raise ValueError("A correction can change only this scheduled field's value.")
|
| 254 |
+
entry = {"change": copy.deepcopy(change), "resultId": result_id, "at": _now(),
|
| 255 |
+
"binding": schedule_envelope._digest(action)}
|
| 256 |
+
|
| 257 |
+
def update(raw):
|
| 258 |
+
doc = _document(raw)
|
| 259 |
+
doc.setdefault("scheduleCorrections", {})[schedule["id"]] = entry
|
| 260 |
+
doc["updatedAt"] = entry["at"]
|
| 261 |
+
return doc
|
| 262 |
+
|
| 263 |
+
rt.update(_key(rt, username), update, flush="sync")
|
| 264 |
+
return copy.deepcopy(entry)
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def schedule_correction(rt, username, schedule):
|
| 268 |
+
"""Return only a replacement for this owner's unchanged saved action."""
|
| 269 |
+
import schedule_envelope
|
| 270 |
+
|
| 271 |
+
if schedule.get("owner") != username:
|
| 272 |
+
return None
|
| 273 |
+
entry = document(rt, username).get("scheduleCorrections", {}).get(schedule["id"])
|
| 274 |
+
if not isinstance(entry, dict) or entry.get("binding") != schedule_envelope._digest(schedule["action"]):
|
| 275 |
+
return None
|
| 276 |
+
return copy.deepcopy(entry)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
def forget(rt, username, id):
|
| 280 |
"""Erase only an own entry and retain its normalized text against stale extraction."""
|
| 281 |
if not any(entry.get("id") == id for entry in document(rt, username)["entries"]):
|
api/automation_engine.py
CHANGED
|
@@ -2518,6 +2518,8 @@ def clean_definition(raw, previous=None, username="", notes=None, rt=None):
|
|
| 2518 |
# a set the vendor is already building (and already counting against the funds gate).
|
| 2519 |
"state": dict(prev.get("state") or {}),
|
| 2520 |
"created": prev.get("created") or _authored_iso(),
|
|
|
|
|
|
|
| 2521 |
"createdBy": prev.get("createdBy") or username,
|
| 2522 |
# ⭐⭐ WAVE 35 · T37 — `system` IS STICKY ACROSS A PATCH, AND IT IS **NEVER READ FROM `raw`**.
|
| 2523 |
#
|
|
@@ -3439,6 +3441,7 @@ def create(rt, raw, username="", notes=None):
|
|
| 3439 |
f"{RETIRED_KIND_REPLACEMENT.get(defn['kind'], '')}")
|
| 3440 |
# Retired run histories reserve their ids so a new definition cannot overwrite them.
|
| 3441 |
defn["id"] = _new_id(rt.get(STORE_KEY) or {})
|
|
|
|
| 3442 |
|
| 3443 |
def _up(cur):
|
| 3444 |
cur = cur if isinstance(cur, dict) else {}
|
|
@@ -3480,6 +3483,7 @@ def patch(rt, auto_id, raw, username="", notes=None):
|
|
| 3480 |
if err:
|
| 3481 |
return None, err
|
| 3482 |
defn["id"] = str(auto_id)
|
|
|
|
| 3483 |
|
| 3484 |
def _up(cur):
|
| 3485 |
cur = cur if isinstance(cur, dict) else {}
|
|
@@ -9068,7 +9072,7 @@ def run_plain(rt, defn, username="automation", log=print, step=_no_step, rows=No
|
|
| 9068 |
# first time one is added. The trigger knows exactly which rows fired; `run_now` threads them
|
| 9069 |
# here. `rows=None` (manual, schedule, Run now) still means the whole table, which is what
|
| 9070 |
# those genuinely mean.
|
| 9071 |
-
ids = (
|
| 9072 |
else sorted(all_rows, key=_rid_num))
|
| 9073 |
|
| 9074 |
# An enrichment-only flow is the one exception to the ordinary manual-run rule above. Its
|
|
@@ -9150,7 +9154,7 @@ def run_plain(rt, defn, username="automation", log=print, step=_no_step, rows=No
|
|
| 9150 |
# database, and a per-STEP narrowing (an enrich step's `fromView`) is a different count that
|
| 9151 |
# this sentence never claimed to report. What was wrong was that nothing said so.
|
| 9152 |
label = str((ut_get(rt, table) or {}).get("label") or "").strip() or table
|
| 9153 |
-
scope = ("the records that fired the trigger" if rows
|
| 9154 |
else ("the records selected for Instagram enrichment"
|
| 9155 |
if len(enabled_direct) == 1
|
| 9156 |
and enabled_direct[0].get("kind") == "enrich_instagram"
|
|
@@ -16078,12 +16082,7 @@ def _automation_slack_reply_text(result):
|
|
| 16078 |
|
| 16079 |
|
| 16080 |
def dispatch_authorized_slack_automation(rt, defn, result=None, *, post=None):
|
| 16081 |
-
"""Deliver
|
| 16082 |
-
|
| 16083 |
-
This is part of the runner rather than a generic route helper. It first re-resolves the
|
| 16084 |
-
stored definition and its one channel-agent grant, then and only then composes the status
|
| 16085 |
-
sentence. No channel grant means no body generation, no outbound request, and no broadcast.
|
| 16086 |
-
"""
|
| 16087 |
definition = defn if isinstance(defn, dict) else {}
|
| 16088 |
aid = str(definition.get("id") or "").strip()
|
| 16089 |
if not aid:
|
|
@@ -16101,17 +16100,69 @@ def dispatch_authorized_slack_automation(rt, defn, result=None, *, post=None):
|
|
| 16101 |
if not evented:
|
| 16102 |
return {"sent": False, "reason": "automation_not_evented"}
|
| 16103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16104 |
import routes_slack # noqa: PLC0415
|
| 16105 |
# The context is intentionally resolved BEFORE `_automation_slack_reply_text`: a lack of
|
| 16106 |
# grant is a refusal before answer composition, not a delivery failure after composition.
|
| 16107 |
context = routes_slack.automation_answer_context(rt, aid)
|
| 16108 |
if context is None:
|
| 16109 |
return {"sent": False, "reason": "not_authorized"}
|
| 16110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16111 |
return routes_slack.deliver_channel_reply(
|
| 16112 |
rt, context["channel"], text, principal=context["principal"], automation_id=aid,
|
| 16113 |
-
provenance=
|
| 16114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16115 |
|
| 16116 |
|
| 16117 |
def run_now(rt, tenant, auto_id, username="automation", log=print, rows=None):
|
|
@@ -16278,16 +16329,26 @@ def run_now(rt, tenant, auto_id, username="automation", log=print, rows=None):
|
|
| 16278 |
log(f"[aios-auto] actions on {auto_id} failed: {type(e).__name__}: {e}")
|
| 16279 |
state = "partial" if state != "error" else state
|
| 16280 |
summary = f"{summary}. The actions did not finish ({type(e).__name__})"
|
| 16281 |
-
#
|
| 16282 |
-
|
| 16283 |
-
# notification trouble must never rewrite the truthful outcome of work that already ran.
|
| 16284 |
try:
|
| 16285 |
if (defn.get("trigger") or {}).get("key") != "once":
|
| 16286 |
-
dispatch_authorized_slack_automation(
|
| 16287 |
rt, defn, {"state": state, "summary": summary,
|
| 16288 |
"rowIds": affected or []})
|
| 16289 |
-
except Exception
|
| 16290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16291 |
return _commit_run(rt, auto_id, state, summary, counts, state != "error", affected,
|
| 16292 |
steps, notes=run_notes)
|
| 16293 |
finally:
|
|
|
|
| 2518 |
# a set the vendor is already building (and already counting against the funds gate).
|
| 2519 |
"state": dict(prev.get("state") or {}),
|
| 2520 |
"created": prev.get("created") or _authored_iso(),
|
| 2521 |
+
# S76-T37: carried here, stamped only by `create` and `patch` (a re-clean is not an update).
|
| 2522 |
+
"updatedAt": prev.get("updatedAt") or prev.get("created") or "",
|
| 2523 |
"createdBy": prev.get("createdBy") or username,
|
| 2524 |
# ⭐⭐ WAVE 35 · T37 — `system` IS STICKY ACROSS A PATCH, AND IT IS **NEVER READ FROM `raw`**.
|
| 2525 |
#
|
|
|
|
| 3441 |
f"{RETIRED_KIND_REPLACEMENT.get(defn['kind'], '')}")
|
| 3442 |
# Retired run histories reserve their ids so a new definition cannot overwrite them.
|
| 3443 |
defn["id"] = _new_id(rt.get(STORE_KEY) or {})
|
| 3444 |
+
defn["updatedAt"] = defn.get("created") or _authored_iso()
|
| 3445 |
|
| 3446 |
def _up(cur):
|
| 3447 |
cur = cur if isinstance(cur, dict) else {}
|
|
|
|
| 3483 |
if err:
|
| 3484 |
return None, err
|
| 3485 |
defn["id"] = str(auto_id)
|
| 3486 |
+
defn["updatedAt"] = _authored_iso()
|
| 3487 |
|
| 3488 |
def _up(cur):
|
| 3489 |
cur = cur if isinstance(cur, dict) else {}
|
|
|
|
| 9072 |
# first time one is added. The trigger knows exactly which rows fired; `run_now` threads them
|
| 9073 |
# here. `rows=None` (manual, schedule, Run now) still means the whole table, which is what
|
| 9074 |
# those genuinely mean.
|
| 9075 |
+
ids = (list(dict.fromkeys(str(r) for r in rows if str(r) in all_rows)) if rows is not None
|
| 9076 |
else sorted(all_rows, key=_rid_num))
|
| 9077 |
|
| 9078 |
# An enrichment-only flow is the one exception to the ordinary manual-run rule above. Its
|
|
|
|
| 9154 |
# database, and a per-STEP narrowing (an enrich step's `fromView`) is a different count that
|
| 9155 |
# this sentence never claimed to report. What was wrong was that nothing said so.
|
| 9156 |
label = str((ut_get(rt, table) or {}).get("label") or "").strip() or table
|
| 9157 |
+
scope = ("the records that fired the trigger" if rows is not None
|
| 9158 |
else ("the records selected for Instagram enrichment"
|
| 9159 |
if len(enabled_direct) == 1
|
| 9160 |
and enabled_direct[0].get("kind") == "enrich_instagram"
|
|
|
|
| 16082 |
|
| 16083 |
|
| 16084 |
def dispatch_authorized_slack_automation(rt, defn, result=None, *, post=None):
|
| 16085 |
+
"""Deliver one authorized event result, with readable linked records for an enters_view cohort."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16086 |
definition = defn if isinstance(defn, dict) else {}
|
| 16087 |
aid = str(definition.get("id") or "").strip()
|
| 16088 |
if not aid:
|
|
|
|
| 16100 |
if not evented:
|
| 16101 |
return {"sent": False, "reason": "automation_not_evented"}
|
| 16102 |
|
| 16103 |
+
if trigger_key == "enters_view":
|
| 16104 |
+
row_ids = list(dict.fromkeys(str(rid) for rid in ((result or {}).get("rowIds") or [])))
|
| 16105 |
+
if not row_ids:
|
| 16106 |
+
return {"sent": False, "reason": "empty_cohort"}
|
| 16107 |
+
|
| 16108 |
import routes_slack # noqa: PLC0415
|
| 16109 |
# The context is intentionally resolved BEFORE `_automation_slack_reply_text`: a lack of
|
| 16110 |
# grant is a refusal before answer composition, not a delivery failure after composition.
|
| 16111 |
context = routes_slack.automation_answer_context(rt, aid)
|
| 16112 |
if context is None:
|
| 16113 |
return {"sent": False, "reason": "not_authorized"}
|
| 16114 |
+
provenance = {"basis": "model_guidance", "citationIds": [], "modelGuidance": False}
|
| 16115 |
+
if trigger_key == "enters_view":
|
| 16116 |
+
module = str(trigger.get("table") or _flow_table(stored) or "")
|
| 16117 |
+
agent = routes_slack._agents(rt).get(context["agentId"])
|
| 16118 |
+
if not module or not routes_slack.agent_may_read(agent, module):
|
| 16119 |
+
return {"sent": False, "reason": "digest_not_readable"}
|
| 16120 |
+
table = ut_get(rt, module) or {}
|
| 16121 |
+
table_rows = table.get("rows") or {}
|
| 16122 |
+
if any(rid not in table_rows for rid in row_ids):
|
| 16123 |
+
return {"sent": False, "reason": "digest_not_readable"}
|
| 16124 |
+
import core.table_store as table_store
|
| 16125 |
+
view_id = str(trigger.get("viewId") or "")
|
| 16126 |
+
found = table_store.make(f"{module}_table_workspace", st=rt).find_view(view_id)
|
| 16127 |
+
if not found:
|
| 16128 |
+
return {"sent": False, "reason": "view_not_found"}
|
| 16129 |
+
provenance = {"basis": "authorized_app_data", "citationIds": ["view:" + view_id],
|
| 16130 |
+
"modelGuidance": False}
|
| 16131 |
+
text = routes_slack.permission_safe_view_entry_digest(
|
| 16132 |
+
rt, context, module=module, view=found[1], view_id=view_id,
|
| 16133 |
+
fields=table.get("fields") or [],
|
| 16134 |
+
rows=[{**table_rows[rid], "pid": rid} for rid in row_ids], provenance=provenance)
|
| 16135 |
+
if text is None:
|
| 16136 |
+
return {"sent": False, "reason": "digest_not_readable"}
|
| 16137 |
+
else:
|
| 16138 |
+
text = _automation_slack_reply_text(result)
|
| 16139 |
return routes_slack.deliver_channel_reply(
|
| 16140 |
rt, context["channel"], text, principal=context["principal"], automation_id=aid,
|
| 16141 |
+
provenance=provenance, post=post)
|
| 16142 |
+
|
| 16143 |
+
|
| 16144 |
+
def _slack_run_refusal(reason):
|
| 16145 |
+
"""Return a safe delivery refusal and its actionable setup fix."""
|
| 16146 |
+
fixes = {
|
| 16147 |
+
"not_authorized": "Grant this automation to exactly one active channel agent in Slack connector settings.",
|
| 16148 |
+
"not_configured": "Connect Slack in connector settings.",
|
| 16149 |
+
"digest_not_readable": "Give the destination channel agent access to the database, every affected record and its title field. Check the application base URL.",
|
| 16150 |
+
"view_not_found": "Select an existing saved view on the trigger.",
|
| 16151 |
+
"not_in_channel": "Invite the Slack app to the destination channel.",
|
| 16152 |
+
"channel_not_found": "Check the destination channel and invite the Slack app.",
|
| 16153 |
+
"missing_scope": "Reconnect Slack with permission to post messages.",
|
| 16154 |
+
"invalid_auth": "Reconnect Slack in connector settings.",
|
| 16155 |
+
"token_revoked": "Reconnect Slack in connector settings.",
|
| 16156 |
+
"transport_error": "Retry when Slack is reachable.",
|
| 16157 |
+
"malformed_response": "Retry after Slack returns a valid response.",
|
| 16158 |
+
}
|
| 16159 |
+
code = reason if reason in fixes or reason in {
|
| 16160 |
+
"account_inactive", "is_archived", "restricted_action", "no_permission", "ratelimited",
|
| 16161 |
+
"rate_limited", "msg_too_long", "invalid_blocks", "invalid_arguments", "internal_error",
|
| 16162 |
+
"fatal_error", "request_timeout", "team_access_not_granted", "org_login_required",
|
| 16163 |
+
"ekm_access_denied", "access_denied", "not_authed", "http_error", "slack_error",
|
| 16164 |
+
} else "delivery_failed"
|
| 16165 |
+
return f"Slack notification failed: {code}. " + fixes.get(code, "Check the Slack connection and destination channel, then retry.")
|
| 16166 |
|
| 16167 |
|
| 16168 |
def run_now(rt, tenant, auto_id, username="automation", log=print, rows=None):
|
|
|
|
| 16329 |
log(f"[aios-auto] actions on {auto_id} failed: {type(e).__name__}: {e}")
|
| 16330 |
state = "partial" if state != "error" else state
|
| 16331 |
summary = f"{summary}. The actions did not finish ({type(e).__name__})"
|
| 16332 |
+
# Delivery trouble is partial work, with a safe refusal persisted before the run commits.
|
| 16333 |
+
delivery = None
|
|
|
|
| 16334 |
try:
|
| 16335 |
if (defn.get("trigger") or {}).get("key") != "once":
|
| 16336 |
+
delivery = dispatch_authorized_slack_automation(
|
| 16337 |
rt, defn, {"state": state, "summary": summary,
|
| 16338 |
"rowIds": affected or []})
|
| 16339 |
+
except Exception: # noqa: BLE001
|
| 16340 |
+
delivery = {"sent": False, "reason": "delivery_failed"}
|
| 16341 |
+
if delivery and delivery.get("sent"):
|
| 16342 |
+
counts = {**(counts or {}), "slackSent": 1}
|
| 16343 |
+
elif delivery and delivery.get("reason") not in ("empty_cohort", "automation_not_evented"):
|
| 16344 |
+
reason = delivery.get("reason")
|
| 16345 |
+
# Automations without a Slack destination keep their existing non-view behavior.
|
| 16346 |
+
if reason != "not_authorized" or (defn.get("trigger") or {}).get("key") == "enters_view":
|
| 16347 |
+
refusal = _slack_run_refusal(reason)
|
| 16348 |
+
run_notes.insert(0, refusal)
|
| 16349 |
+
counts = {**(counts or {}), "slackFailed": 1}
|
| 16350 |
+
state = "partial" if state != "error" else state
|
| 16351 |
+
summary = f"{summary}. {refusal}"
|
| 16352 |
return _commit_run(rt, auto_id, state, summary, counts, state != "error", affected,
|
| 16353 |
steps, notes=run_notes)
|
| 16354 |
finally:
|
api/catalog_store.py
CHANGED
|
@@ -381,3 +381,100 @@ def delete_catalog(session, catalog_id):
|
|
| 381 |
return {"deleted": catalog_id}
|
| 382 |
|
| 383 |
return _update(session.runtime, apply)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
return {"deleted": catalog_id}
|
| 382 |
|
| 383 |
return _update(session.runtime, apply)
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
# Long PDF exports: an MCP caller polls one of these through operation_status.
|
| 387 |
+
EXPORT_KEY = "catalog_exports_v1"
|
| 388 |
+
EXPORT_RETENTION_SECONDS = 24 * 3600
|
| 389 |
+
# A running export older than this lost its worker (a restart), so it reads as failed.
|
| 390 |
+
EXPORT_STALE_SECONDS = 30 * 60
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
def _export_bindings(session):
|
| 394 |
+
from routes_external import _operation_bindings
|
| 395 |
+
return _operation_bindings(session)
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
def _exports(raw, now):
|
| 399 |
+
doc = raw if isinstance(raw, dict) and isinstance(raw.get("exports"), dict) else {"exports": {}}
|
| 400 |
+
doc = copy.deepcopy(doc)
|
| 401 |
+
doc["exports"] = {key: value for key, value in doc["exports"].items()
|
| 402 |
+
if isinstance(value, dict) and float(value.get("retainUntil") or 0) >= now}
|
| 403 |
+
return doc
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def start_export(session, catalog_id):
|
| 407 |
+
"""Record a running export bound to this caller's account and connection, and return its id."""
|
| 408 |
+
import secrets
|
| 409 |
+
import time
|
| 410 |
+
|
| 411 |
+
operation_id = secrets.token_hex(32)
|
| 412 |
+
now = time.time()
|
| 413 |
+
item = {"bindings": _export_bindings(session), "catalogId": catalog_id, "status": "running",
|
| 414 |
+
"startedAt": now, "retainUntil": now + EXPORT_RETENTION_SECONDS}
|
| 415 |
+
|
| 416 |
+
def apply(raw):
|
| 417 |
+
doc = _exports(raw, now)
|
| 418 |
+
doc["exports"][operation_id] = item
|
| 419 |
+
return doc
|
| 420 |
+
|
| 421 |
+
session.runtime.update(EXPORT_KEY, apply, flush="sync")
|
| 422 |
+
return operation_id
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
def _export_error(error):
|
| 426 |
+
from fastapi import HTTPException
|
| 427 |
+
from core import sheets
|
| 428 |
+
|
| 429 |
+
if isinstance(error, sheets.SheetRefusal):
|
| 430 |
+
return {"code": error.code, "message": error.message, "status": 409}
|
| 431 |
+
if isinstance(error, HTTPException) and isinstance(error.detail, dict):
|
| 432 |
+
info = error.detail.get("error") or {}
|
| 433 |
+
if isinstance(info.get("code"), str) and isinstance(info.get("message"), str):
|
| 434 |
+
return {"code": info["code"], "message": info["message"], "status": error.status_code}
|
| 435 |
+
return {"code": "catalog_export_failed", "status": 503,
|
| 436 |
+
"message": "The PDF export did not finish. Call apps.catalog_pdf again with the same catalog_id."}
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def finish_export(session, operation_id, result, error=None):
|
| 440 |
+
"""Store the worker's outcome on its export record."""
|
| 441 |
+
import time
|
| 442 |
+
|
| 443 |
+
now = time.time()
|
| 444 |
+
|
| 445 |
+
def apply(raw):
|
| 446 |
+
doc = _exports(raw, now)
|
| 447 |
+
item = doc["exports"].get(operation_id)
|
| 448 |
+
if item is not None:
|
| 449 |
+
item.update(status="failed" if error is not None else "done", finishedAt=now,
|
| 450 |
+
retainUntil=now + EXPORT_RETENTION_SECONDS)
|
| 451 |
+
if error is not None:
|
| 452 |
+
item["error"] = _export_error(error)
|
| 453 |
+
else:
|
| 454 |
+
item["result"] = copy.deepcopy(result)
|
| 455 |
+
return doc
|
| 456 |
+
|
| 457 |
+
session.runtime.update(EXPORT_KEY, apply, flush="sync")
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
def export_status(session, operation_id):
|
| 461 |
+
"""Read this caller's export by id, or None when no export of theirs has that id."""
|
| 462 |
+
import time
|
| 463 |
+
|
| 464 |
+
now = time.time()
|
| 465 |
+
try:
|
| 466 |
+
raw = session.runtime.get(EXPORT_KEY, fresh=True)
|
| 467 |
+
except Exception:
|
| 468 |
+
# The confirmation read that follows reports an unavailable store with its own code.
|
| 469 |
+
return None
|
| 470 |
+
item = _exports(raw, now)["exports"].get(operation_id)
|
| 471 |
+
if not isinstance(item, dict) or item.get("bindings") != _export_bindings(session):
|
| 472 |
+
return None
|
| 473 |
+
base = {"operation_id": operation_id, "catalog_id": item.get("catalogId")}
|
| 474 |
+
if item.get("status") == "done":
|
| 475 |
+
return {**copy.deepcopy(item["result"]), **base, "state": "done", "retry": False}
|
| 476 |
+
if item.get("status") == "failed" or now - float(item.get("startedAt") or 0) > EXPORT_STALE_SECONDS:
|
| 477 |
+
error = item.get("error") or _export_error(None)
|
| 478 |
+
return {**base, "ok": False, "state": "failed", "retry": False, "error": copy.deepcopy(error)}
|
| 479 |
+
return {**base, "state": "running", "retry": True,
|
| 480 |
+
"message": "The PDF is still rendering. Call operation_status again shortly."}
|
api/connectors_amazon.py
CHANGED
|
@@ -429,9 +429,11 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None):
|
|
| 429 |
# An unreadable input cannot support a cadence promise for any table in the rotation.
|
| 430 |
plan = az.sync_plan(states, now=clock) if not bad_ticks and clock is not None else None
|
| 431 |
first_load, load_unknown = None, ""
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
|
|
|
|
|
|
| 435 |
if load:
|
| 436 |
try:
|
| 437 |
progress = json.loads(load.get("cursor") or "")
|
|
@@ -447,10 +449,18 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None):
|
|
| 447 |
if refused_reports:
|
| 448 |
first_load = {**(first_load or {"remaining": remaining, "etaSeconds": eta}),
|
| 449 |
"refused": refused_reports}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
except (ValueError, TypeError, KeyError, OverflowError, RecursionError):
|
| 451 |
load_unknown = "First-load progress is unknown because its saved marker could not be read. Retry sync to refresh it."
|
| 452 |
elif (entities.get(az.entity_key(az.FIRST_LOAD_END_KEY)) or {}).get("phase") not in (None, "done"):
|
| 453 |
load_unknown = "First-load progress is not available yet. Keep the connection enabled and check again after this pass."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
answer = dict(declared)
|
| 456 |
for table in az.TABLES:
|
|
@@ -532,6 +542,10 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None):
|
|
| 532 |
limit = "History progress is unreadable. Retry sync to refresh its saved date."
|
| 533 |
if limit:
|
| 534 |
unknown.append(limit)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
if table == "catalog_items":
|
| 536 |
marker = entities.get(az.entity_key("limit_" + table)) or {}
|
| 537 |
try:
|
|
@@ -584,7 +598,7 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None):
|
|
| 584 |
"stuck": stuck, "stuckCause": stuck_cause,
|
| 585 |
"unknownCause": " ".join(unknown),
|
| 586 |
}
|
| 587 |
-
if table == az.SQP_TABLE or table in az.GROUP_A_TABLES or table
|
| 588 |
answer[table]["limit"] = limit
|
| 589 |
answer["sku_keywords"] = {
|
| 590 |
"rowsLoaded": mirrored.get("sku_keywords"),
|
|
|
|
| 429 |
# An unreadable input cannot support a cadence promise for any table in the rotation.
|
| 430 |
plan = az.sync_plan(states, now=clock) if not bad_ticks and clock is not None else None
|
| 431 |
first_load, load_unknown = None, ""
|
| 432 |
+
markers = {key: {"state": value.get("phase"), "detail": value.get("cursor")}
|
| 433 |
+
for key, value in entities.items()}
|
| 434 |
+
refused_reports = az.refused_reports(markers)
|
| 435 |
+
enabled = az.read_database_enablement(rt)["databases"]
|
| 436 |
+
load = entities.get(az.entity_key(az.FIRST_LOAD_STATUS_KEY)) or {}
|
| 437 |
if load:
|
| 438 |
try:
|
| 439 |
progress = json.loads(load.get("cursor") or "")
|
|
|
|
| 449 |
if refused_reports:
|
| 450 |
first_load = {**(first_load or {"remaining": remaining, "etaSeconds": eta}),
|
| 451 |
"refused": refused_reports}
|
| 452 |
+
if first_load:
|
| 453 |
+
first_load = {**(first_load or {}), **az.first_load_payload(markers, remaining, eta,
|
| 454 |
+
state=load.get("phase"), tables=[table for table in az.TABLES
|
| 455 |
+
if enabled.get(az.amazon_schema.DATABASE_PREFIX + table)])}
|
| 456 |
except (ValueError, TypeError, KeyError, OverflowError, RecursionError):
|
| 457 |
load_unknown = "First-load progress is unknown because its saved marker could not be read. Retry sync to refresh it."
|
| 458 |
elif (entities.get(az.entity_key(az.FIRST_LOAD_END_KEY)) or {}).get("phase") not in (None, "done"):
|
| 459 |
load_unknown = "First-load progress is not available yet. Keep the connection enabled and check again after this pass."
|
| 460 |
+
if first_load is None and refused_reports and not load:
|
| 461 |
+
first_load = az.first_load_payload(markers, 0, 0, state="failed",
|
| 462 |
+
tables=[table for table in az.TABLES if enabled.get(az.amazon_schema.DATABASE_PREFIX + table)])
|
| 463 |
+
first_load["etaDays"] = None
|
| 464 |
|
| 465 |
answer = dict(declared)
|
| 466 |
for table in az.TABLES:
|
|
|
|
| 542 |
limit = "History progress is unreadable. Retry sync to refresh its saved date."
|
| 543 |
if limit:
|
| 544 |
unknown.append(limit)
|
| 545 |
+
if table == "brand_analytics":
|
| 546 |
+
# R18's weekly retention is a code fact: its copy never waits for a weekly pull's marker.
|
| 547 |
+
limit = az.BRAND_ANALYTICS_LIMIT
|
| 548 |
+
unknown.append(limit)
|
| 549 |
if table == "catalog_items":
|
| 550 |
marker = entities.get(az.entity_key("limit_" + table)) or {}
|
| 551 |
try:
|
|
|
|
| 598 |
"stuck": stuck, "stuckCause": stuck_cause,
|
| 599 |
"unknownCause": " ".join(unknown),
|
| 600 |
}
|
| 601 |
+
if table == az.SQP_TABLE or table in az.GROUP_A_TABLES or table in ("transactions", "brand_analytics"):
|
| 602 |
answer[table]["limit"] = limit
|
| 603 |
answer["sku_keywords"] = {
|
| 604 |
"rowsLoaded": mirrored.get("sku_keywords"),
|
api/deps.py
CHANGED
|
@@ -802,7 +802,7 @@ def _assistant_activity_table(session, definition, fields, filters, mode, member
|
|
| 802 |
break
|
| 803 |
seen.add(cursor)
|
| 804 |
_assistant_refuse(409, "assistant_snapshot_unavailable",
|
| 805 |
-
"This activity history is too large for an Assistant snapshot. Read it in
|
| 806 |
return rows, canonical, {"read": "field contract"}, definition.get("label") or "Activity"
|
| 807 |
|
| 808 |
|
|
|
|
| 802 |
break
|
| 803 |
seen.add(cursor)
|
| 804 |
_assistant_refuse(409, "assistant_snapshot_unavailable",
|
| 805 |
+
"This activity history is too large for an Assistant snapshot. Read it in Table by page.")
|
| 806 |
return rows, canonical, {"read": "field contract"}, definition.get("label") or "Activity"
|
| 807 |
|
| 808 |
|
api/external_freshness.py
CHANGED
|
@@ -59,6 +59,126 @@ def _sync_date(value):
|
|
| 59 |
return None
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def _mirror_refreshes(session, definitions, keys):
|
| 63 |
"""Read authorized mirror stamps with cached successful cursors and no product-row reads."""
|
| 64 |
import assistant_mirror
|
|
@@ -149,7 +269,9 @@ def snapshot(session, databases):
|
|
| 149 |
except Exception:
|
| 150 |
docs[name] = {}
|
| 151 |
odoo = odoo_relational.sync_freshness(user_tables.lend(rt, **docs))
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
if not user_tables.materialises(key, st=lent, defn=definitions[key])}
|
| 154 |
mirror = _mirror_refreshes(session, definitions, mirror_keys) if mirror_keys else {}
|
| 155 |
facts = {}
|
|
@@ -168,7 +290,7 @@ def snapshot(session, databases):
|
|
| 168 |
successes.append(odoo.get("lastGoodSyncAt"))
|
| 169 |
if job.get("state") == "ready":
|
| 170 |
successes.append(job.get("at"))
|
| 171 |
-
values = [mirror.get(key), *successes]
|
| 172 |
stamp = next((stamp for value in values if (stamp := utc_stamp(value)) is not None), None)
|
| 173 |
synced_on = next((day for value in values if (day := _sync_date(value)) is not None), None)
|
| 174 |
if stamp is not None:
|
|
@@ -203,6 +325,11 @@ def add(result, session, *, facts=None):
|
|
| 203 |
result.pop("source_synced_on", None)
|
| 204 |
result.pop("last_edited_at", None)
|
| 205 |
result.update(source_synced_at=None, source_sync="never_synced")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
refreshed = _refresh_value(version) if kind == "mirror_user_table" else None
|
| 207 |
if kind == "mirror_user_table" and isinstance(version, dict):
|
| 208 |
stamp = utc_stamp(refreshed)
|
|
|
|
| 59 |
return None
|
| 60 |
|
| 61 |
|
| 62 |
+
#: S78-T02 (R11): a monthly reconciliation proof, `amzn_reconcile_<table>_<yyyy>_<mm>`.
|
| 63 |
+
_PERIOD_PROOF = re.compile(r"_\d{4}_\d{2}")
|
| 64 |
+
#: A derived view is as fresh as its oldest input. ⛔ Never fresher than the data it reads.
|
| 65 |
+
_AMAZON_DERIVED_INPUTS = {"sku_keywords": ("listing_items", "search_query_performance")}
|
| 66 |
+
#: A derived table whose rows arrive by an import with its own receipt.
|
| 67 |
+
_AMAZON_DERIVED_RECEIPTS = {"keyword_ranks": "amzn_apify_keyword_ranks"}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def amazon_sync_state(cur):
|
| 71 |
+
"""`{entity: updated_at}` for every `done` Amazon `_sync_state` row, in one read."""
|
| 72 |
+
return {str(entity): at for entity, at in cur.execute(
|
| 73 |
+
"SELECT entity, updated_at FROM _sync_state "
|
| 74 |
+
"WHERE phase = 'done' AND starts_with(entity, 'amzn_')").fetchall()}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _amazon_table_stamp(state, table):
|
| 78 |
+
"""The mark, else the legacy raw receipt, else the newest reconciliation proof, or None.
|
| 79 |
+
|
| 80 |
+
⛔ `sync_tick_` is never a receipt: a refused pass also writes it `done`.
|
| 81 |
+
"""
|
| 82 |
+
for entity in ("amzn_sync_mark_" + table, "amzn_" + table):
|
| 83 |
+
if state.get(entity) is not None:
|
| 84 |
+
return state[entity]
|
| 85 |
+
proof = "amzn_reconcile_" + table
|
| 86 |
+
return _latest(*(at for entity, at in state.items()
|
| 87 |
+
if entity == proof or entity.startswith(proof + "_")
|
| 88 |
+
and _PERIOD_PROOF.fullmatch(entity[len(proof):])))
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def amazon_sync_stamp(cur, table_key, state=None):
|
| 92 |
+
"""A table's true last successful sync from its `_sync_state` receipts, or None.
|
| 93 |
+
|
| 94 |
+
R11: a refusal overwrites the raw receipt and a pass with no window writes no mark, so the
|
| 95 |
+
reconciliation proofs also count. The time is the receipt's own, never a restore or read time.
|
| 96 |
+
"""
|
| 97 |
+
import amazon_relational
|
| 98 |
+
|
| 99 |
+
raw = amazon_relational.SOURCE.get(table_key)
|
| 100 |
+
if not raw or (cur is None and state is None):
|
| 101 |
+
return None
|
| 102 |
+
if state is None:
|
| 103 |
+
state = amazon_sync_state(cur)
|
| 104 |
+
table = raw.removeprefix("amzn_")
|
| 105 |
+
if table in _AMAZON_DERIVED_RECEIPTS:
|
| 106 |
+
return state.get(_AMAZON_DERIVED_RECEIPTS[table])
|
| 107 |
+
if table in _AMAZON_DERIVED_INPUTS:
|
| 108 |
+
stamps = [stamp for name in _AMAZON_DERIVED_INPUTS[table]
|
| 109 |
+
if (stamp := utc_stamp(_amazon_table_stamp(state, name))) is not None]
|
| 110 |
+
return min(stamps, key=lambda stamp: datetime.fromisoformat(stamp.replace("Z", "+00:00"))) \
|
| 111 |
+
if stamps else None
|
| 112 |
+
return _amazon_table_stamp(state, table)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _amazon_refreshes(session, keys):
|
| 116 |
+
"""Read only sync metadata for the authorized Amazon databases on this tenant's cursor."""
|
| 117 |
+
import assistant_mirror
|
| 118 |
+
|
| 119 |
+
cur, state, refreshed = None, None, {}
|
| 120 |
+
for key in keys:
|
| 121 |
+
try:
|
| 122 |
+
if cur is None:
|
| 123 |
+
cur, refusal = assistant_mirror.open_cursor(session, key)
|
| 124 |
+
if refusal is not None or cur is None:
|
| 125 |
+
cur = None
|
| 126 |
+
continue
|
| 127 |
+
if state is None:
|
| 128 |
+
state = amazon_sync_state(cur)
|
| 129 |
+
refreshed[key] = amazon_sync_stamp(cur, key, state=state)
|
| 130 |
+
except Exception:
|
| 131 |
+
continue
|
| 132 |
+
return refreshed
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def amazon_nav(session, pages):
|
| 136 |
+
"""Add `group` and `sync` to each Amazon nav row in place; returns `pages`. Never raises.
|
| 137 |
+
|
| 138 |
+
`sync` is `{"at": <UTC stamp> | None, "empty": bool | None}` and is read on the request,
|
| 139 |
+
never stored: one `_sync_state` read and one EXISTS per mirror table, no product value.
|
| 140 |
+
It is absent when the mirror cannot answer, so "unknown" never reads as "never synced".
|
| 141 |
+
"""
|
| 142 |
+
try:
|
| 143 |
+
from core import connector_spec
|
| 144 |
+
rows = [page for page in pages if str(page.get("key") or "").startswith("ut_amzn_")]
|
| 145 |
+
for page in rows:
|
| 146 |
+
group = connector_spec.table_group(page["key"])
|
| 147 |
+
if group:
|
| 148 |
+
page["group"] = group
|
| 149 |
+
if not rows:
|
| 150 |
+
return pages
|
| 151 |
+
import amazon_relational
|
| 152 |
+
import assistant_mirror
|
| 153 |
+
|
| 154 |
+
cur, refusal = assistant_mirror.open_cursor(session, rows[0]["key"])
|
| 155 |
+
if refusal is not None or cur is None:
|
| 156 |
+
return pages
|
| 157 |
+
state = amazon_sync_state(cur)
|
| 158 |
+
if not state:
|
| 159 |
+
# An unrestored or absent mirror holds no receipt at all: unknown, not never synced.
|
| 160 |
+
return pages
|
| 161 |
+
bases = {str(name) for (name,) in cur.execute(
|
| 162 |
+
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'main' "
|
| 163 |
+
"AND table_type = 'BASE TABLE' AND starts_with(table_name, 'amzn_')").fetchall()}
|
| 164 |
+
raws = {page["key"]: amazon_relational.SOURCE.get(page["key"]) for page in rows}
|
| 165 |
+
probed = sorted({raw for raw in raws.values() if raw in bases})
|
| 166 |
+
held = {}
|
| 167 |
+
if probed:
|
| 168 |
+
held = {str(name): bool(has) for name, has in cur.execute(" UNION ALL ".join(
|
| 169 |
+
f"SELECT '{raw}' AS name, EXISTS (SELECT 1 FROM \"{raw}\") AS has"
|
| 170 |
+
for raw in probed)).fetchall()}
|
| 171 |
+
for page in rows:
|
| 172 |
+
raw = raws.get(page["key"])
|
| 173 |
+
if not raw:
|
| 174 |
+
continue
|
| 175 |
+
stamp = utc_stamp(amazon_sync_stamp(cur, page["key"], state=state))
|
| 176 |
+
page["sync"] = {"at": stamp, "empty": (not held[raw]) if raw in held else None}
|
| 177 |
+
except Exception:
|
| 178 |
+
pass
|
| 179 |
+
return pages
|
| 180 |
+
|
| 181 |
+
|
| 182 |
def _mirror_refreshes(session, definitions, keys):
|
| 183 |
"""Read authorized mirror stamps with cached successful cursors and no product-row reads."""
|
| 184 |
import assistant_mirror
|
|
|
|
| 269 |
except Exception:
|
| 270 |
docs[name] = {}
|
| 271 |
odoo = odoo_relational.sync_freshness(user_tables.lend(rt, **docs))
|
| 272 |
+
amazon_keys = {key for key in connected if key.startswith("ut_amzn_")}
|
| 273 |
+
amazon = _amazon_refreshes(session, amazon_keys) if amazon_keys else {}
|
| 274 |
+
mirror_keys = {key for key in connected - amazon_keys
|
| 275 |
if not user_tables.materialises(key, st=lent, defn=definitions[key])}
|
| 276 |
mirror = _mirror_refreshes(session, definitions, mirror_keys) if mirror_keys else {}
|
| 277 |
facts = {}
|
|
|
|
| 290 |
successes.append(odoo.get("lastGoodSyncAt"))
|
| 291 |
if job.get("state") == "ready":
|
| 292 |
successes.append(job.get("at"))
|
| 293 |
+
values = [amazon.get(key)] if key.startswith("ut_amzn_") else [mirror.get(key), *successes]
|
| 294 |
stamp = next((stamp for value in values if (stamp := utc_stamp(value)) is not None), None)
|
| 295 |
synced_on = next((day for value in values if (day := _sync_date(value)) is not None), None)
|
| 296 |
if stamp is not None:
|
|
|
|
| 325 |
result.pop("source_synced_on", None)
|
| 326 |
result.pop("last_edited_at", None)
|
| 327 |
result.update(source_synced_at=None, source_sync="never_synced")
|
| 328 |
+
if str(result.get("database") or "").startswith("ut_amzn_"):
|
| 329 |
+
if facts is None:
|
| 330 |
+
facts = snapshot(session, [result.get("database")])
|
| 331 |
+
result.update(facts.get(result.get("database"), {}))
|
| 332 |
+
return result
|
| 333 |
refreshed = _refresh_value(version) if kind == "mirror_user_table" else None
|
| 334 |
if kind == "mirror_user_table" and isinstance(version, dict):
|
| 335 |
stamp = utc_stamp(refreshed)
|
api/formula_runtime.py
CHANGED
|
@@ -3695,6 +3695,8 @@ def _document_spec(cell, sheet):
|
|
| 3695 |
|
| 3696 |
def _sheet_error_message(code):
|
| 3697 |
from modules.sheet import REFUSALS
|
|
|
|
|
|
|
| 3698 |
if code in REFUSALS:
|
| 3699 |
return REFUSALS[code].format(subject="that database")
|
| 3700 |
return {
|
|
|
|
| 3695 |
|
| 3696 |
def _sheet_error_message(code):
|
| 3697 |
from modules.sheet import REFUSALS
|
| 3698 |
+
if code == "field_not_aggregable":
|
| 3699 |
+
return "That field cannot be aggregated."
|
| 3700 |
if code in REFUSALS:
|
| 3701 |
return REFUSALS[code].format(subject="that database")
|
| 3702 |
return {
|
api/intelligence_store.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
"""Workspace Markdown files, collaboration metadata and immutable versions in the tenant store."""
|
| 2 |
|
| 3 |
-
from copy import deepcopy
|
| 4 |
from datetime import datetime, timedelta, timezone
|
| 5 |
from functools import lru_cache
|
| 6 |
from pathlib import Path
|
| 7 |
import importlib
|
| 8 |
import difflib
|
|
|
|
| 9 |
import json
|
| 10 |
import re
|
| 11 |
|
|
@@ -267,10 +268,10 @@ def _document(raw):
|
|
| 267 |
return raw
|
| 268 |
|
| 269 |
|
| 270 |
-
def _files(session, *, include_generated=False, wiki_only=False):
|
| 271 |
"""Read authored files, adding reader-scoped system pages only for Wiki requests."""
|
| 272 |
try:
|
| 273 |
-
raw = _document(session.runtime.get(STORE_KEY))
|
| 274 |
_check_file_capacity(session, raw)
|
| 275 |
import wiki_log
|
| 276 |
files = _project_templates(session, raw)
|
|
@@ -332,9 +333,9 @@ def _database_note_record(session, key, *, stored=None, with_body=False):
|
|
| 332 |
if not definition or not can_read(session, {"reads": [definition["key"]]}):
|
| 333 |
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 334 |
note = user_tables.shared_note("database", key, st=session.runtime)
|
| 335 |
-
if not note:
|
| 336 |
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 337 |
-
meta = {"title": definition["label"], "section": "database-notes", "owner": definition["owner"],
|
| 338 |
"description": f"Shared note for {definition['label']}.", "reads": [definition["key"]],
|
| 339 |
"approvers": [], "approvals_needed": 1, "tools": [], "databaseKey": key}
|
| 340 |
text = note.get("body") or ""
|
|
@@ -391,7 +392,7 @@ def _database_note_file(session, path, *, version=0):
|
|
| 391 |
"latestVersion": current["version"], "history": history, "versions": len(history), "limits": limits()}
|
| 392 |
|
| 393 |
|
| 394 |
-
def _put_database_note(session, key, body, actor, base_version, base_updated):
|
| 395 |
"""Let the canonical writer enforce admission and its exact base on every atomic attempt."""
|
| 396 |
from core import user_tables
|
| 397 |
|
|
@@ -409,7 +410,13 @@ def _put_database_note(session, key, body, actor, base_version, base_updated):
|
|
| 409 |
prior = (_document(raw).get("database:" + key) or {})
|
| 410 |
if int(prior.get("version") or 0) != base_version or prior.get("updated") != base_updated:
|
| 411 |
raise IntelligenceError("version_conflict", "The note changed. Reload it before saving.", 409)
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
return session.runtime.update(bucket, checked, **kwargs)
|
| 415 |
|
|
@@ -694,6 +701,8 @@ def _row(record, entry=None, *, session=None):
|
|
| 694 |
**{key: deepcopy(entry[key]) for key in ("agent", "onBehalfOf", "approvedBy", "proposalId", "restoredFrom") if key in entry},
|
| 695 |
"created": record["created"], "lastUsed": record.get("lastUsed"),
|
| 696 |
**{key: record[key] for key in ("databaseKey", "canEdit", "revisionHistory") if key in record},
|
|
|
|
|
|
|
| 697 |
"versions": sum(session is None or can_read(session, version["metadata"])
|
| 698 |
for version in record["versions"]), "trimmed": record.get("trimmed", 0),
|
| 699 |
**({"fieldCheck": _field_check(session, meta)} if session and meta.get("required_fields") else {})}
|
|
@@ -716,6 +725,65 @@ def _file(record, entry=None, *, session=None):
|
|
| 716 |
"latestVersion": _head(record)["version"], "history": history, "limits": limits()}
|
| 717 |
|
| 718 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 719 |
def list_files(session, *, section="", query="", folder="", offset=0, limit=100, direct=False):
|
| 720 |
"""List or search readable titles and descriptions with section, folder and page filters.
|
| 721 |
|
|
@@ -726,26 +794,113 @@ def list_files(session, *, section="", query="", folder="", offset=0, limit=100,
|
|
| 726 |
if type(offset) is not int or type(limit) is not int or offset < 0 or not 1 <= limit <= MAX_PAGE:
|
| 727 |
raise IntelligenceError("bad_page", "Use an offset of zero or more and a page size from 1 to 100.")
|
| 728 |
needle = str(query or "").strip().casefold()
|
| 729 |
-
|
| 730 |
wiki_requested = selected == "wiki" or prefix.casefold().startswith("wiki/")
|
| 731 |
-
|
| 732 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
continue
|
| 734 |
-
|
| 735 |
-
|
|
|
|
|
|
|
| 736 |
continue
|
| 737 |
-
if
|
| 738 |
continue
|
| 739 |
-
|
| 740 |
-
if needle and needle not in (row["title"] + " " + row["description"]).casefold():
|
| 741 |
continue
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
return {"files": rows
|
| 748 |
-
"limit": limit, "sections": sections(), "folders": folders, "limits": limits()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
|
| 750 |
|
| 751 |
def _summaries(session):
|
|
@@ -1859,15 +2014,76 @@ def decide_proposal(session, proposal_id, decision):
|
|
| 1859 |
return saved
|
| 1860 |
|
| 1861 |
|
| 1862 |
-
def
|
| 1863 |
-
"""
|
| 1864 |
session, identity = trusted_identity(caller)
|
| 1865 |
_authorize(session)
|
| 1866 |
path = normalize_path(path)
|
| 1867 |
_assert_authored_path(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1868 |
if _database_note_path(path):
|
| 1869 |
-
|
| 1870 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1871 |
removed = {}
|
| 1872 |
|
| 1873 |
def remove(raw):
|
|
@@ -1875,6 +2091,10 @@ def delete_file(caller, path):
|
|
| 1875 |
files = _editable_files(session, raw, path)
|
| 1876 |
record = _readable_record(session, files, path)
|
| 1877 |
removed["version"] = _head(record)["version"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1878 |
if identity.get("agent") and (record.get("memoryScope") != [identity["agent"], session.uname]
|
| 1879 |
or not _head(record) or _head(record)["metadata"]["section"] != "agent-memory"):
|
| 1880 |
raise IntelligenceError("approval_required", "An agent cannot delete a shared file.", 403)
|
|
|
|
| 1 |
"""Workspace Markdown files, collaboration metadata and immutable versions in the tenant store."""
|
| 2 |
|
| 3 |
+
from copy import copy, deepcopy
|
| 4 |
from datetime import datetime, timedelta, timezone
|
| 5 |
from functools import lru_cache
|
| 6 |
from pathlib import Path
|
| 7 |
import importlib
|
| 8 |
import difflib
|
| 9 |
+
import hashlib
|
| 10 |
import json
|
| 11 |
import re
|
| 12 |
|
|
|
|
| 268 |
return raw
|
| 269 |
|
| 270 |
|
| 271 |
+
def _files(session, *, include_generated=False, wiki_only=False, raw=None):
|
| 272 |
"""Read authored files, adding reader-scoped system pages only for Wiki requests."""
|
| 273 |
try:
|
| 274 |
+
raw = _document(session.runtime.get(STORE_KEY) if raw is None else raw)
|
| 275 |
_check_file_capacity(session, raw)
|
| 276 |
import wiki_log
|
| 277 |
files = _project_templates(session, raw)
|
|
|
|
| 333 |
if not definition or not can_read(session, {"reads": [definition["key"]]}):
|
| 334 |
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 335 |
note = user_tables.shared_note("database", key, st=session.runtime)
|
| 336 |
+
if not note or note.get("deleted"):
|
| 337 |
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 338 |
+
meta = {"title": note.get("title") or definition["label"], "section": "database-notes", "owner": definition["owner"],
|
| 339 |
"description": f"Shared note for {definition['label']}.", "reads": [definition["key"]],
|
| 340 |
"approvers": [], "approvals_needed": 1, "tools": [], "databaseKey": key}
|
| 341 |
text = note.get("body") or ""
|
|
|
|
| 392 |
"latestVersion": current["version"], "history": history, "versions": len(history), "limits": limits()}
|
| 393 |
|
| 394 |
|
| 395 |
+
def _put_database_note(session, key, body, actor, base_version, base_updated, *, title=None, deleted=False):
|
| 396 |
"""Let the canonical writer enforce admission and its exact base on every atomic attempt."""
|
| 397 |
from core import user_tables
|
| 398 |
|
|
|
|
| 410 |
prior = (_document(raw).get("database:" + key) or {})
|
| 411 |
if int(prior.get("version") or 0) != base_version or prior.get("updated") != base_updated:
|
| 412 |
raise IntelligenceError("version_conflict", "The note changed. Reload it before saving.", 409)
|
| 413 |
+
updated = callback(raw)
|
| 414 |
+
note = updated["database:" + key]
|
| 415 |
+
if title is not None or prior.get("title"):
|
| 416 |
+
note["title"] = title if title is not None else prior["title"]
|
| 417 |
+
if deleted:
|
| 418 |
+
note["deleted"] = True
|
| 419 |
+
return updated
|
| 420 |
|
| 421 |
return session.runtime.update(bucket, checked, **kwargs)
|
| 422 |
|
|
|
|
| 701 |
**{key: deepcopy(entry[key]) for key in ("agent", "onBehalfOf", "approvedBy", "proposalId", "restoredFrom") if key in entry},
|
| 702 |
"created": record["created"], "lastUsed": record.get("lastUsed"),
|
| 703 |
**{key: record[key] for key in ("databaseKey", "canEdit", "revisionHistory") if key in record},
|
| 704 |
+
"canEdit": bool(session and (record.get("canEdit") if "databaseKey" in record
|
| 705 |
+
else can_edit(session, _head(record)["metadata"]))),
|
| 706 |
"versions": sum(session is None or can_read(session, version["metadata"])
|
| 707 |
for version in record["versions"]), "trimmed": record.get("trimmed", 0),
|
| 708 |
**({"fieldCheck": _field_check(session, meta)} if session and meta.get("required_fields") else {})}
|
|
|
|
| 725 |
"latestVersion": _head(record)["version"], "history": history, "limits": limits()}
|
| 726 |
|
| 727 |
|
| 728 |
+
def _folder_index(files):
|
| 729 |
+
"""Index paths for this catalog only, without admitting any file or folder."""
|
| 730 |
+
index = {"": []}
|
| 731 |
+
for path in sorted(files):
|
| 732 |
+
index[""].append(path)
|
| 733 |
+
parts = path.split("/")
|
| 734 |
+
for depth in range(1, len(parts)):
|
| 735 |
+
index.setdefault("/".join(parts[:depth]) + "/", []).append(path)
|
| 736 |
+
return index
|
| 737 |
+
|
| 738 |
+
|
| 739 |
+
class _FolderReadRuntime:
|
| 740 |
+
"""Reuse store input within one listing while every permission predicate still runs."""
|
| 741 |
+
|
| 742 |
+
def __init__(self, runtime):
|
| 743 |
+
self._runtime = runtime
|
| 744 |
+
self._documents, self._projections, self._entries = {}, {}, {}
|
| 745 |
+
|
| 746 |
+
def _store(self):
|
| 747 |
+
reader = getattr(self._runtime, "_store", None)
|
| 748 |
+
return reader() if callable(reader) else self._runtime
|
| 749 |
+
|
| 750 |
+
def get(self, name, fresh=False):
|
| 751 |
+
if fresh:
|
| 752 |
+
return self._runtime.get(name, fresh=True)
|
| 753 |
+
if name not in self._documents:
|
| 754 |
+
if name == STORE_KEY:
|
| 755 |
+
reader = getattr(self._runtime, "get_many", None)
|
| 756 |
+
values = reader([STORE_KEY, TEMPLATE_USAGE_KEY, "keychain", "shared_notes"]) if callable(reader) else None
|
| 757 |
+
if values is not None:
|
| 758 |
+
self._documents.update(values)
|
| 759 |
+
if name not in self._documents:
|
| 760 |
+
self._documents[name] = self._runtime.get(name)
|
| 761 |
+
return self._documents[name]
|
| 762 |
+
|
| 763 |
+
def get_projection(self, name, drop=()):
|
| 764 |
+
key = (name, tuple(drop))
|
| 765 |
+
if key not in self._projections:
|
| 766 |
+
self._projections[key] = self._runtime.get_projection(name, drop=drop)
|
| 767 |
+
return self._projections[key]
|
| 768 |
+
|
| 769 |
+
def get_entry(self, name, entry):
|
| 770 |
+
key = (name, entry)
|
| 771 |
+
if key not in self._entries:
|
| 772 |
+
self._entries[key] = self._runtime.get_entry(name, entry)
|
| 773 |
+
return self._entries[key]
|
| 774 |
+
|
| 775 |
+
def update(self, *args, **kwargs):
|
| 776 |
+
try:
|
| 777 |
+
return self._runtime.update(*args, **kwargs)
|
| 778 |
+
finally:
|
| 779 |
+
self._documents.clear()
|
| 780 |
+
self._projections.clear()
|
| 781 |
+
self._entries.clear()
|
| 782 |
+
|
| 783 |
+
def __getattr__(self, name):
|
| 784 |
+
return getattr(self._runtime, name)
|
| 785 |
+
|
| 786 |
+
|
| 787 |
def list_files(session, *, section="", query="", folder="", offset=0, limit=100, direct=False):
|
| 788 |
"""List or search readable titles and descriptions with section, folder and page filters.
|
| 789 |
|
|
|
|
| 794 |
if type(offset) is not int or type(limit) is not int or offset < 0 or not 1 <= limit <= MAX_PAGE:
|
| 795 |
raise IntelligenceError("bad_page", "Use an offset of zero or more and a page size from 1 to 100.")
|
| 796 |
needle = str(query or "").strip().casefold()
|
| 797 |
+
matches, folders = [], set()
|
| 798 |
wiki_requested = selected == "wiki" or prefix.casefold().startswith("wiki/")
|
| 799 |
+
session = copy(session)
|
| 800 |
+
session.runtime = _FolderReadRuntime(session.runtime)
|
| 801 |
+
try:
|
| 802 |
+
raw = _document(session.runtime.get(STORE_KEY))
|
| 803 |
+
except IntelligenceError:
|
| 804 |
+
raise
|
| 805 |
+
except Exception:
|
| 806 |
+
raise IntelligenceError("store_unavailable", "Brain files could not be read. Try again.", 503) from None
|
| 807 |
+
files = _files(session, include_generated=wiki_requested, raw=raw)
|
| 808 |
+
index = _folder_index(files)
|
| 809 |
+
for path in index.get(prefix, []):
|
| 810 |
+
record = files[path]
|
| 811 |
+
entry = _head(record)
|
| 812 |
+
if not entry:
|
| 813 |
+
continue
|
| 814 |
+
meta = entry["metadata"]
|
| 815 |
+
if selected and meta["section"] != selected:
|
| 816 |
continue
|
| 817 |
+
parent = path.rpartition("/")[0]
|
| 818 |
+
in_folder = "/" not in path[len(prefix):]
|
| 819 |
+
# One admitted head proves a folder exists, but never admits its other files.
|
| 820 |
+
if direct and not in_folder and parent in folders:
|
| 821 |
continue
|
| 822 |
+
if not can_read(session, meta) or not _personal_record_readable(session, record):
|
| 823 |
continue
|
| 824 |
+
if needle and needle not in (meta["title"] + " " + _description(entry)).casefold():
|
|
|
|
| 825 |
continue
|
| 826 |
+
parts = path.split("/")
|
| 827 |
+
folders.update("/".join(parts[:depth]) for depth in range(1, len(parts)))
|
| 828 |
+
if not direct or in_folder:
|
| 829 |
+
matches.append(record)
|
| 830 |
+
rows = [_row(record, session=session) for record in matches[offset:offset + limit]]
|
| 831 |
+
return {"files": rows, "total": len(matches), "offset": offset,
|
| 832 |
+
"limit": limit, "sections": sections(), "folders": sorted(folders), "limits": limits(),
|
| 833 |
+
"canManageFolders": bool(getattr(session, "admin", False) and not trusted_identity(session)[1].get("agent")),
|
| 834 |
+
**({"folderRevision": _folder_revision(raw)} if getattr(session, "admin", False) else {})}
|
| 835 |
+
|
| 836 |
+
|
| 837 |
+
def _folder_revision(raw):
|
| 838 |
+
"""Bind folder changes to the exact file identities and versions in their atomic update."""
|
| 839 |
+
heads = [(path, record.get("created"), (_head(record) or {}).get("version", 0))
|
| 840 |
+
for path, record in sorted(_document(raw).items())]
|
| 841 |
+
return int(hashlib.sha256(json.dumps(heads, sort_keys=True).encode()).hexdigest()[:13], 16)
|
| 842 |
+
|
| 843 |
+
|
| 844 |
+
def change_folder(caller, path, *, name=None, expected_revision):
|
| 845 |
+
"""Rename a folder or promote its children in one revision-checked update."""
|
| 846 |
+
session, identity = trusted_identity(caller)
|
| 847 |
+
_authorize(session)
|
| 848 |
+
if not session.admin or identity.get("agent"):
|
| 849 |
+
raise IntelligenceError("folder_admin_required", "Only an Administrator can change Brain folders.", 403)
|
| 850 |
+
if type(expected_revision) is not int or expected_revision < 0:
|
| 851 |
+
raise IntelligenceError("bad_revision", "Reload the folders before trying again.")
|
| 852 |
+
path = normalize_path(path, folder=True)
|
| 853 |
+
def reserved(folder):
|
| 854 |
+
folded = folder.casefold()
|
| 855 |
+
return (folded in ("database-notes", "wiki") or folded.startswith(DATABASE_NOTES_PREFIX)
|
| 856 |
+
or any(folded == root or folded.startswith(root + "/")
|
| 857 |
+
for root in ("wiki/log", "wiki/index", "wiki/purposes")))
|
| 858 |
+
|
| 859 |
+
if reserved(path):
|
| 860 |
+
raise IntelligenceError("folder_reserved", "This folder is maintained by the workspace.", 403)
|
| 861 |
+
parent = path.rpartition("/")[0]
|
| 862 |
+
target = parent
|
| 863 |
+
if name is not None:
|
| 864 |
+
name = normalize_path(name, folder=True)
|
| 865 |
+
if "/" in name:
|
| 866 |
+
raise IntelligenceError("bad_name", "Use one folder name without slashes.")
|
| 867 |
+
target = normalize_path((parent + "/" if parent else "") + name, folder=True)
|
| 868 |
+
if reserved(target):
|
| 869 |
+
raise IntelligenceError("folder_reserved", "This folder name is reserved.", 403)
|
| 870 |
+
|
| 871 |
+
def move(raw):
|
| 872 |
+
files = dict(_document(raw))
|
| 873 |
+
if _folder_revision(files) != expected_revision:
|
| 874 |
+
raise IntelligenceError("folder_revision_stale", "Folders changed. Reload before trying again.", 409)
|
| 875 |
+
prefix = path + "/"
|
| 876 |
+
projected = _project_templates(session, files)
|
| 877 |
+
if any(key.startswith(prefix) and key in _template_catalog() and _head(record)
|
| 878 |
+
for key, record in projected.items()):
|
| 879 |
+
raise IntelligenceError("folder_reserved", "This folder contains a maintained file.", 403)
|
| 880 |
+
moving = {key: record for key, record in files.items() if key.startswith(prefix)}
|
| 881 |
+
if not moving or not any(_head(record) for record in moving.values()):
|
| 882 |
+
raise IntelligenceError("no_folder", "This folder is no longer available.", 404)
|
| 883 |
+
if name is not None and target != path and any(key.startswith(target + "/") for key in projected):
|
| 884 |
+
raise IntelligenceError("folder_exists", "A folder with that name already exists.", 409)
|
| 885 |
+
destinations = {key: normalize_path((target + "/" if target else "") + key[len(prefix):]) for key in moving}
|
| 886 |
+
if any((destination in files and destination not in moving) or destination in _template_catalog()
|
| 887 |
+
for destination in destinations.values()):
|
| 888 |
+
raise IntelligenceError("file_exists", "A child has the same path as an existing file. Rename it first.", 409)
|
| 889 |
+
for key, record in moving.items():
|
| 890 |
+
_assert_authored_path(key)
|
| 891 |
+
if key in _template_catalog() or (_head(record) or {}).get("metadata", {}).get("generated"):
|
| 892 |
+
raise IntelligenceError("folder_reserved", "This folder contains a maintained file.", 403)
|
| 893 |
+
del files[key]
|
| 894 |
+
for key, record in moving.items():
|
| 895 |
+
destination = destinations[key]
|
| 896 |
+
_assert_authored_path(destination)
|
| 897 |
+
files[destination] = {**record, "path": destination,
|
| 898 |
+
"proposals": {pid: {**proposal, "path": destination}
|
| 899 |
+
for pid, proposal in (record.get("proposals") or {}).items()}}
|
| 900 |
+
return files
|
| 901 |
+
|
| 902 |
+
_update(session, move)
|
| 903 |
+
return {"path": target} if name is not None else {"deleted": path}
|
| 904 |
|
| 905 |
|
| 906 |
def _summaries(session):
|
|
|
|
| 2014 |
return saved
|
| 2015 |
|
| 2016 |
|
| 2017 |
+
def rename_file(caller, path, title, *, expected_version):
|
| 2018 |
+
"""Append a title revision at the same path through the current file edit right."""
|
| 2019 |
session, identity = trusted_identity(caller)
|
| 2020 |
_authorize(session)
|
| 2021 |
path = normalize_path(path)
|
| 2022 |
_assert_authored_path(path)
|
| 2023 |
+
if not isinstance(title, str) or not title.strip() or len(title.strip()) > 200:
|
| 2024 |
+
raise IntelligenceError("bad_title", "Use a title of 1 to 200 characters.")
|
| 2025 |
+
if type(expected_version) is not int or expected_version < 1:
|
| 2026 |
+
raise IntelligenceError("bad_version", "Reload the file before trying again.")
|
| 2027 |
if _database_note_path(path):
|
| 2028 |
+
if identity.get("agent"):
|
| 2029 |
+
raise IntelligenceError("approval_required", "An agent cannot rename a shared note.", 403)
|
| 2030 |
+
record = _database_note_record(session, path[len(DATABASE_NOTES_PREFIX):-3], with_body=True)
|
| 2031 |
+
if record["path"] != path:
|
| 2032 |
+
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 2033 |
+
entry = _head(record)
|
| 2034 |
+
_put_database_note(session, record["databaseKey"], entry["body"], session.uname,
|
| 2035 |
+
expected_version, entry["created"], title=title.strip())
|
| 2036 |
+
saved = _database_note_file(session, path)
|
| 2037 |
+
_log_write(session, path, identity, expected_version, saved["version"])
|
| 2038 |
+
return saved
|
| 2039 |
+
saved = {}
|
| 2040 |
+
|
| 2041 |
+
def rename(raw):
|
| 2042 |
+
saved.clear()
|
| 2043 |
+
files = _editable_files(session, raw, path)
|
| 2044 |
+
record = _readable_record(session, files, path)
|
| 2045 |
+
head = _head(record)
|
| 2046 |
+
if not can_edit(session, head["metadata"]):
|
| 2047 |
+
raise IntelligenceError("file_read_only", "Your current rights do not allow renaming this file.", 403)
|
| 2048 |
+
if head["version"] != expected_version:
|
| 2049 |
+
raise IntelligenceError("version_conflict", "The file changed. Reload it before renaming.", 409)
|
| 2050 |
+
if len(record["versions"]) >= MAX_VERSIONS:
|
| 2051 |
+
raise IntelligenceError("too_many_versions", "This file already has 100 versions.", 409)
|
| 2052 |
+
metadata = {**head["metadata"], "title": title.strip()}
|
| 2053 |
+
body, metadata = prepare_markdown("---\n" + yaml.safe_dump(metadata, sort_keys=False, allow_unicode=True)
|
| 2054 |
+
+ "---\n" + head["body"].partition("\n---\n")[2], session.uname)
|
| 2055 |
+
record = {**record, "versions": [*record["versions"], _entry(body, metadata, expected_version + 1, identity)],
|
| 2056 |
+
"proposals": {key: {**row, "status": "stale"} if row["status"] in ("pending", "approving") else row
|
| 2057 |
+
for key, row in (record.get("proposals") or {}).items()}}
|
| 2058 |
+
files[path] = record
|
| 2059 |
+
saved.update(_file(record, session=session))
|
| 2060 |
+
return files
|
| 2061 |
+
|
| 2062 |
+
_update(session, rename)
|
| 2063 |
+
_log_write(session, path, identity, expected_version, saved["version"])
|
| 2064 |
+
return saved
|
| 2065 |
+
|
| 2066 |
+
|
| 2067 |
+
def delete_file(caller, path, *, expected_version=None):
|
| 2068 |
+
"""Delete an editable workspace file or clear its canonical database note."""
|
| 2069 |
+
session, identity = trusted_identity(caller)
|
| 2070 |
+
_authorize(session)
|
| 2071 |
+
path = normalize_path(path)
|
| 2072 |
+
_assert_authored_path(path)
|
| 2073 |
+
if expected_version is not None and (type(expected_version) is not int or expected_version < 1):
|
| 2074 |
+
raise IntelligenceError("bad_version", "Reload the file before trying again.")
|
| 2075 |
+
if _database_note_path(path):
|
| 2076 |
+
if identity.get("agent"):
|
| 2077 |
+
raise IntelligenceError("approval_required", "An agent cannot delete a shared note.", 403)
|
| 2078 |
+
record = _database_note_record(session, path[len(DATABASE_NOTES_PREFIX):-3], with_body=True)
|
| 2079 |
+
if record["path"] != path:
|
| 2080 |
+
raise IntelligenceError("no_file", "This workspace has no file at that path.", 404)
|
| 2081 |
+
entry = _head(record)
|
| 2082 |
+
_put_database_note(session, record["databaseKey"], "", session.uname,
|
| 2083 |
+
expected_version if expected_version is not None else entry["version"],
|
| 2084 |
+
entry["created"], deleted=True)
|
| 2085 |
+
_log_write(session, path, identity, entry["version"], None)
|
| 2086 |
+
return {"deleted": path}
|
| 2087 |
removed = {}
|
| 2088 |
|
| 2089 |
def remove(raw):
|
|
|
|
| 2091 |
files = _editable_files(session, raw, path)
|
| 2092 |
record = _readable_record(session, files, path)
|
| 2093 |
removed["version"] = _head(record)["version"]
|
| 2094 |
+
if not identity.get("agent") and not can_edit(session, _head(record)["metadata"]):
|
| 2095 |
+
raise IntelligenceError("file_read_only", "Your current rights do not allow deleting this file.", 403)
|
| 2096 |
+
if expected_version is not None and expected_version != removed["version"]:
|
| 2097 |
+
raise IntelligenceError("version_conflict", "The file changed. Reload it before deleting.", 409)
|
| 2098 |
if identity.get("agent") and (record.get("memoryScope") != [identity["agent"], session.uname]
|
| 2099 |
or not _head(record) or _head(record)["metadata"]["section"] != "agent-memory"):
|
| 2100 |
raise IntelligenceError("approval_required", "An agent cannot delete a shared file.", 403)
|
api/loopy_catalog_tools.py
CHANGED
|
@@ -172,7 +172,8 @@ def catalog_build(ctx, args):
|
|
| 172 |
keys = [str(row.get(mapping["key"], "")).strip() if row.get(mapping["key"]) is not None else "" for row in rows]
|
| 173 |
if any(not key for key in keys) or len(set(keys)) != len(keys):
|
| 174 |
_build_refusal("catalog_key_ambiguous", "Products need distinct nonempty source keys. Rebind the App's record key before building.")
|
| 175 |
-
|
|
|
|
| 176 |
kinds = ["cover", "toc", *(["product"] * len(rows)), "closing"]
|
| 177 |
pages = []
|
| 178 |
for kind in kinds:
|
|
@@ -241,13 +242,101 @@ def catalog_pdf(session, args):
|
|
| 241 |
mcp = (getattr(session, "claims", None) or {}).get("external") is True
|
| 242 |
link = (assistant_files.capability_url(session.runtime, saved["id"])
|
| 243 |
if door == "whatsapp" and not mcp else saved["link"])
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
|
| 248 |
def _schema():
|
| 249 |
return {"type": "object",
|
| 250 |
-
"description": "Make a PDF of a stored catalog's designed pages and product images.
|
|
|
|
|
|
|
|
|
|
| 251 |
"properties": {"catalog_id": {"type": "string", "minLength": 1, "maxLength": 256}},
|
| 252 |
"required": ["catalog_id"],
|
| 253 |
"additionalProperties": False}
|
|
|
|
| 172 |
keys = [str(row.get(mapping["key"], "")).strip() if row.get(mapping["key"]) is not None else "" for row in rows]
|
| 173 |
if any(not key for key in keys) or len(set(keys)) != len(keys):
|
| 174 |
_build_refusal("catalog_key_ambiguous", "Products need distinct nonempty source keys. Rebind the App's record key before building.")
|
| 175 |
+
# The store refuses long dashes in a name, so normalise an AI's name before the preview.
|
| 176 |
+
name = _no_dashes(str(args.get("name") or title or "Catalog")).strip()[:120] or "Catalog"
|
| 177 |
kinds = ["cover", "toc", *(["product"] * len(rows)), "closing"]
|
| 178 |
pages = []
|
| 179 |
for kind in kinds:
|
|
|
|
| 242 |
mcp = (getattr(session, "claims", None) or {}).get("external") is True
|
| 243 |
link = (assistant_files.capability_url(session.runtime, saved["id"])
|
| 244 |
if door == "whatsapp" and not mcp else saved["link"])
|
| 245 |
+
result = {"ok": True, "fileId": saved["id"], "link": _secure_link(link),
|
| 246 |
+
"message": "Catalog PDF ready"}
|
| 247 |
+
if mcp:
|
| 248 |
+
result.update(external_download(saved["id"], len(data)))
|
| 249 |
+
else:
|
| 250 |
+
# Declared provenance keeps the Assistant turn from marking this artifact unknown.
|
| 251 |
+
result["sources"] = [app["sourceDatabase"]]
|
| 252 |
+
return result
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
EXTERNAL_FILES_PATH = "/api/v1/external/files/"
|
| 256 |
+
FILE_RESOURCE_PREFIX = "loopable://files/"
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def external_download(file_id, size):
|
| 260 |
+
"""Name the two credential-bound ways an MCP caller fetches the PDF bytes."""
|
| 261 |
+
from loopy_file_tools import _links, _secure_link
|
| 262 |
+
|
| 263 |
+
base = _links().public_base()
|
| 264 |
+
return {"mime": "application/pdf", "bytes": size,
|
| 265 |
+
"downloadUrl": _secure_link((base or "") + EXTERNAL_FILES_PATH + file_id),
|
| 266 |
+
"resourceUri": FILE_RESOURCE_PREFIX + file_id,
|
| 267 |
+
"download": ("GET downloadUrl with this connection's Authorization header, "
|
| 268 |
+
"or read resourceUri with MCP resources/read. Both return application/pdf.")}
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
# A render past this many seconds answers with an operation_id instead of holding the call.
|
| 272 |
+
EXPORT_BUDGET_SECONDS = 20.0
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def export_budget():
|
| 276 |
+
import os
|
| 277 |
+
try:
|
| 278 |
+
value = float(os.environ.get("AIOS_CATALOG_PDF_BUDGET_SECONDS", "") or EXPORT_BUDGET_SECONDS)
|
| 279 |
+
except ValueError:
|
| 280 |
+
value = EXPORT_BUDGET_SECONDS
|
| 281 |
+
return value if value > 0 else EXPORT_BUDGET_SECONDS
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
def catalog_pdf_within_budget(session, args, render=None, budget=None):
|
| 285 |
+
"""Render in a worker thread. Past the budget, return an operation_id that operation_status reads."""
|
| 286 |
+
import contextvars
|
| 287 |
+
import sys
|
| 288 |
+
import threading
|
| 289 |
+
import catalog_store
|
| 290 |
+
|
| 291 |
+
render = render or sys.modules[__name__].catalog_pdf
|
| 292 |
+
budget = export_budget() if budget is None else budget
|
| 293 |
+
lock = threading.Lock()
|
| 294 |
+
state = {"finished": False, "operation_id": None}
|
| 295 |
+
|
| 296 |
+
def work():
|
| 297 |
+
try:
|
| 298 |
+
outcome = (render(session, args), None)
|
| 299 |
+
except BaseException as exc: # The caller or the export record receives every failure.
|
| 300 |
+
outcome = (None, exc)
|
| 301 |
+
with lock:
|
| 302 |
+
state.update(finished=True, outcome=outcome)
|
| 303 |
+
operation_id = state["operation_id"]
|
| 304 |
+
if operation_id is not None:
|
| 305 |
+
try:
|
| 306 |
+
catalog_store.finish_export(session, operation_id, *outcome)
|
| 307 |
+
except Exception as exc:
|
| 308 |
+
# The record then reads as failed once it is stale, so the caller retries.
|
| 309 |
+
import logging
|
| 310 |
+
logging.getLogger(__name__).warning("Catalog export result not saved (%s)", type(exc).__name__)
|
| 311 |
+
|
| 312 |
+
context = contextvars.copy_context()
|
| 313 |
+
worker = threading.Thread(target=context.run, args=(work,), daemon=True, name="catalog-pdf")
|
| 314 |
+
try:
|
| 315 |
+
worker.start()
|
| 316 |
+
except Exception:
|
| 317 |
+
# No worker can start here, so the call renders inline and holds until it finishes.
|
| 318 |
+
return render(session, args)
|
| 319 |
+
worker.join(budget)
|
| 320 |
+
with lock:
|
| 321 |
+
if state["finished"]:
|
| 322 |
+
result, error = state["outcome"]
|
| 323 |
+
if error is not None:
|
| 324 |
+
raise error
|
| 325 |
+
return result
|
| 326 |
+
# The worker waits on this lock, so it reads the id after the record exists.
|
| 327 |
+
state["operation_id"] = catalog_store.start_export(session, args["catalog_id"])
|
| 328 |
+
return {"operation_id": state["operation_id"], "state": "running", "retry": True,
|
| 329 |
+
"catalog_id": args["catalog_id"],
|
| 330 |
+
"message": ("The PDF is still rendering. Call table with action operation_status and this "
|
| 331 |
+
"operation_id until state is done. The done result carries fileId, link and downloadUrl.")}
|
| 332 |
|
| 333 |
|
| 334 |
def _schema():
|
| 335 |
return {"type": "object",
|
| 336 |
+
"description": ("Make a PDF of a stored catalog's designed pages and product images. Use the catalog_id that "
|
| 337 |
+
"catalog_build returned after confirm_change. Returns fileId and link for a person to send. "
|
| 338 |
+
"An MCP call also gets downloadUrl and resourceUri for the bytes, or an operation_id to poll "
|
| 339 |
+
"with operation_status when the render is slow. This tool does not send."),
|
| 340 |
"properties": {"catalog_id": {"type": "string", "minLength": 1, "maxLength": 256}},
|
| 341 |
"required": ["catalog_id"],
|
| 342 |
"additionalProperties": False}
|
api/loopy_links.py
CHANGED
|
@@ -6,12 +6,37 @@ import re
|
|
| 6 |
from urllib.parse import quote, urlsplit
|
| 7 |
|
| 8 |
FRAGMENT = re.compile(r"(?<![\w/])#/[a-z]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def public_base() -> str | None:
|
| 12 |
-
"""Return the configured HTTPS base
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if (not base or any(char.isspace() or ord(char) < 32 for char in base)
|
| 16 |
or any(char in base for char in "\\?#")):
|
| 17 |
return None
|
|
|
|
| 6 |
from urllib.parse import quote, urlsplit
|
| 7 |
|
| 8 |
FRAGMENT = re.compile(r"(?<![\w/])#/[a-z]")
|
| 9 |
+
# C3: the live deployment's canonical address. Its Space host is never a link target.
|
| 10 |
+
LIVE_BASE = "https://runloopable.com"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _deployment_base() -> str | None:
|
| 14 |
+
"""Answer the base this container's own identity implies, or None off a Space."""
|
| 15 |
+
try:
|
| 16 |
+
from core import data_binding
|
| 17 |
+
except ImportError:
|
| 18 |
+
return None
|
| 19 |
+
if data_binding.is_production_deployment():
|
| 20 |
+
return LIVE_BASE
|
| 21 |
+
# HF sets SPACE_HOST inside a Space. An identity alone (a gate box) implies no host.
|
| 22 |
+
host = os.environ.get("SPACE_HOST", "").strip().rstrip("/")
|
| 23 |
+
return _checked_base("https://" + host) if host and data_binding.deployment_id() else None
|
| 24 |
|
| 25 |
|
| 26 |
def public_base() -> str | None:
|
| 27 |
+
"""Return the configured HTTPS base, else the deployment's own. Live never answers *.hf.space."""
|
| 28 |
+
configured = _checked_base(os.environ.get("AIOS_PUBLIC_BASE", "").strip()
|
| 29 |
+
or os.environ.get("APP_BASE_URL", "").strip())
|
| 30 |
+
identity = _deployment_base()
|
| 31 |
+
if configured is not None and not (
|
| 32 |
+
identity == LIVE_BASE and (urlsplit(configured).hostname or "").endswith(".hf.space")):
|
| 33 |
+
return configured
|
| 34 |
+
return identity
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _checked_base(base: str) -> str | None:
|
| 38 |
+
"""Accept one HTTPS origin with an optional path and nothing else."""
|
| 39 |
+
base = (base or "").strip().rstrip("/")
|
| 40 |
if (not base or any(char.isspace() or ord(char) < 32 for char in base)
|
| 41 |
or any(char in base for char in "\\?#")):
|
| 42 |
return None
|
api/main.py
CHANGED
|
@@ -45,6 +45,7 @@ from starlette.exceptions import HTTPException as StarletteHTTPException # noqa
|
|
| 45 |
|
| 46 |
import aios_session # noqa: E402
|
| 47 |
import routes_admin # noqa: E402
|
|
|
|
| 48 |
import routes_alerts # noqa: E402 (wave 20 item 25 — the Alerts inbox)
|
| 49 |
import routes_assets # noqa: E402 (wave 18 C2-ASSET — catalog product imagery)
|
| 50 |
import routes_auth # noqa: E402
|
|
@@ -549,7 +550,10 @@ app.include_router(routes_tickets.router)
|
|
| 549 |
# rate-limited by POLITENESS rather than by a bill (~1 req/s, and bulk harvesting is forbidden).
|
| 550 |
app.include_router(routes_geo.router) # R14 / A11 — D's router, A's line (map providers)
|
| 551 |
app.include_router(routes_external.router)
|
|
|
|
| 552 |
_mount_router("routes_security") # W59-T43: lane F declares the router; keep before static mount.
|
|
|
|
|
|
|
| 553 |
|
| 554 |
|
| 555 |
# --- DEPRECATED ALIASES (removed when S2's shell flips; kept so the current bundle keeps working)
|
|
@@ -2897,6 +2901,46 @@ def _provision_qa_sandbox(pg=None):
|
|
| 2897 |
return False
|
| 2898 |
|
| 2899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2900 |
@app.on_event("startup")
|
| 2901 |
def _await_mirror_restore():
|
| 2902 |
"""Start restoration without hiding its progress behind the HTTP startup barrier."""
|
|
|
|
| 45 |
|
| 46 |
import aios_session # noqa: E402
|
| 47 |
import routes_admin # noqa: E402
|
| 48 |
+
import routes_competitor_watch # noqa: E402 (S78-T16 / S78-T17 competitor watch)
|
| 49 |
import routes_alerts # noqa: E402 (wave 20 item 25 — the Alerts inbox)
|
| 50 |
import routes_assets # noqa: E402 (wave 18 C2-ASSET — catalog product imagery)
|
| 51 |
import routes_auth # noqa: E402
|
|
|
|
| 550 |
# rate-limited by POLITENESS rather than by a bill (~1 req/s, and bulk harvesting is forbidden).
|
| 551 |
app.include_router(routes_geo.router) # R14 / A11 — D's router, A's line (map providers)
|
| 552 |
app.include_router(routes_external.router)
|
| 553 |
+
app.include_router(routes_competitor_watch.router) # S78-T16 / S78-T17 competitor watch
|
| 554 |
_mount_router("routes_security") # W59-T43: lane F declares the router; keep before static mount.
|
| 555 |
+
_mount_router("routes_amazon_profit") # S78-T03: the Amazon SKU profit refresh and settings doors.
|
| 556 |
+
_mount_router("routes_odoo_attachments") # S78-T14 (lane G, C5): delivery photo doors; before static mount.
|
| 557 |
|
| 558 |
|
| 559 |
# --- DEPRECATED ALIASES (removed when S2's shell flips; kept so the current bundle keeps working)
|
|
|
|
| 2901 |
return False
|
| 2902 |
|
| 2903 |
|
| 2904 |
+
WEBSITE_RUNTIME = {"state": "not_started", "attempted": False, "ready": False,
|
| 2905 |
+
"reason": "Website runtime preparation has not run."}
|
| 2906 |
+
app.state.website_runtime = WEBSITE_RUNTIME
|
| 2907 |
+
|
| 2908 |
+
|
| 2909 |
+
@app.on_event("startup")
|
| 2910 |
+
def _prepare_website_runtime():
|
| 2911 |
+
"""Prepare the optional website runtime once without changing other browser runtimes."""
|
| 2912 |
+
if data_binding.gate_process() or WEBSITE_RUNTIME["state"] != "not_started":
|
| 2913 |
+
return
|
| 2914 |
+
names = ("POMERADO_NODE", "POMERADO_PACKAGE_ROOT", "POMERADO_BROWSERS_PATH")
|
| 2915 |
+
WEBSITE_RUNTIME["state"] = "external"
|
| 2916 |
+
WEBSITE_RUNTIME["reason"] = "Explicit website runtime paths require an external smoke check."
|
| 2917 |
+
if all(os.environ.get(name) for name in names[:2]):
|
| 2918 |
+
return
|
| 2919 |
+
directory = os.environ.get("POMERADO_RUNTIME_DIR")
|
| 2920 |
+
if not directory:
|
| 2921 |
+
WEBSITE_RUNTIME.update(state="unavailable", reason="Website runtime is not configured.")
|
| 2922 |
+
return
|
| 2923 |
+
WEBSITE_RUNTIME.update(state="preparing", attempted=True)
|
| 2924 |
+
try:
|
| 2925 |
+
import website_tools
|
| 2926 |
+
prepared = website_tools.prepare_runtime(directory)
|
| 2927 |
+
settings = {name: prepared[name] for name in names}
|
| 2928 |
+
if not all(isinstance(value, str) and value for value in settings.values()):
|
| 2929 |
+
raise ValueError("Incomplete website runtime settings")
|
| 2930 |
+
for name, value in settings.items():
|
| 2931 |
+
if not os.environ.get(name):
|
| 2932 |
+
os.environ[name] = value
|
| 2933 |
+
verified = all(os.environ.get(name) == value for name, value in settings.items())
|
| 2934 |
+
WEBSITE_RUNTIME.update(
|
| 2935 |
+
state="prepared" if verified else "external", ready=verified,
|
| 2936 |
+
reason="" if verified else "Explicit website runtime paths require an external smoke check.")
|
| 2937 |
+
except Exception: # noqa: BLE001
|
| 2938 |
+
WEBSITE_RUNTIME.update(state="unavailable", ready=False,
|
| 2939 |
+
reason="Website runtime preparation failed. "
|
| 2940 |
+
"Check Node, Pomerado, Chromium and its OS libraries.")
|
| 2941 |
+
print(f"[aios-api] website unavailable: {WEBSITE_RUNTIME['reason']}")
|
| 2942 |
+
|
| 2943 |
+
|
| 2944 |
@app.on_event("startup")
|
| 2945 |
def _await_mirror_restore():
|
| 2946 |
"""Start restoration without hiding its progress behind the HTTP startup barrier."""
|
api/odoo_attachments.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Odoo delivery photos (S78-T14, contract C5): list and stream a picking's image attachments.
|
| 2 |
+
|
| 3 |
+
AntsRoute stores each proof-of-delivery photo as an `ir.attachment` on `stock.picking`. It has no
|
| 4 |
+
photo field of its own, only the `antsroute_nb_delivery_photo` count.
|
| 5 |
+
Rule 4: Odoo is read with `search_read` only. Rule 1: every photo of the picking is listed.
|
| 6 |
+
The bytes pass through the response and are never stored.
|
| 7 |
+
The caller's read wall on the delivery databases is checked BEFORE any photo byte is read.
|
| 8 |
+
"""
|
| 9 |
+
import base64
|
| 10 |
+
import binascii
|
| 11 |
+
|
| 12 |
+
from deps import err
|
| 13 |
+
|
| 14 |
+
ATTACHMENT_MODEL = "ir.attachment"
|
| 15 |
+
PICKING_MODEL = "stock.picking"
|
| 16 |
+
#: Raster types only. An SVG can carry script, so it is never served from our origin.
|
| 17 |
+
IMAGE_TYPES = ("image/jpeg", "image/png", "image/gif", "image/webp")
|
| 18 |
+
_META_FIELDS = ["id", "name", "mimetype", "res_model", "res_id", "type"]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def attachment_path(attachment_id):
|
| 22 |
+
"""The C5 bytes path for one attachment."""
|
| 23 |
+
return f"/api/v1/odoo/attachments/{int(attachment_id)}"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def _search_read(session, model, domain, fields, **kw):
|
| 27 |
+
"""One read through the caller's tenant Odoo connection."""
|
| 28 |
+
from core import odoo
|
| 29 |
+
source = session.runtime.odoo_source()
|
| 30 |
+
if source is None:
|
| 31 |
+
raise err(409, "odoo_not_connected", "Connect Odoo in Settings to see delivery photos.")
|
| 32 |
+
with source._bound():
|
| 33 |
+
return odoo.search_read(model, domain, fields, **kw)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _wall_admits(session, key, picking_id):
|
| 37 |
+
"""True when the caller's read wall on `key` returns the picking's row."""
|
| 38 |
+
import routes_odoo_tables as rot
|
| 39 |
+
page = rot.odoo_table_rows(key, offset=0, limit=1, filters=None, filterConj="and",
|
| 40 |
+
sorts=None, search=None, related=None, groupBy=None,
|
| 41 |
+
groupKey=None, session=session, body={"ids": [int(picking_id)]})
|
| 42 |
+
return any(int(row.get("pid", -1)) == int(picking_id) for row in page.get("rows") or [])
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def require_delivery_read(session, picking_id, key_allowed=None):
|
| 46 |
+
"""Return the delivery database key that admits the caller, or raise 403.
|
| 47 |
+
|
| 48 |
+
A 403 or 404 from one key falls through to the next. `key_allowed(key)` may raise 403 to
|
| 49 |
+
narrow the keys further, as an MCP credential's database scope does.
|
| 50 |
+
"""
|
| 51 |
+
import odoo_relational as rel
|
| 52 |
+
other = None
|
| 53 |
+
for key in rel.DELIVERY_KEYS:
|
| 54 |
+
try:
|
| 55 |
+
if key_allowed is not None:
|
| 56 |
+
key_allowed(key)
|
| 57 |
+
if _wall_admits(session, key, picking_id):
|
| 58 |
+
return key
|
| 59 |
+
except Exception as exc: # noqa: BLE001
|
| 60 |
+
status = getattr(exc, "status_code", None)
|
| 61 |
+
if status in (403, 404):
|
| 62 |
+
continue
|
| 63 |
+
other = other or exc
|
| 64 |
+
if other is not None:
|
| 65 |
+
raise other
|
| 66 |
+
raise err(403, "delivery_read_forbidden",
|
| 67 |
+
"You cannot read this delivery, so ask an admin for access to Odoo shipments.")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def _photo(row, base=""):
|
| 71 |
+
return {"id": int(row["id"]), "name": str(row.get("name") or ""),
|
| 72 |
+
"mimetype": str(row.get("mimetype") or ""), "url": base + attachment_path(row["id"])}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def list_photos(session, picking_id, key_allowed=None, base=""):
|
| 76 |
+
"""Every image attachment of one picking, oldest first, after the read wall passes."""
|
| 77 |
+
require_delivery_read(session, picking_id, key_allowed)
|
| 78 |
+
rows = _search_read(session, ATTACHMENT_MODEL, [
|
| 79 |
+
("res_model", "=", PICKING_MODEL), ("res_id", "=", int(picking_id)),
|
| 80 |
+
("type", "=", "binary"), ("mimetype", "in", list(IMAGE_TYPES)),
|
| 81 |
+
], ["id", "name", "mimetype"], order="id asc")
|
| 82 |
+
return [_photo(row, base) for row in rows or []]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def read_photo(session, attachment_id):
|
| 86 |
+
"""`(bytes, mimetype, name)` of one delivery photo. The bytes are read only after the wall."""
|
| 87 |
+
meta = _search_read(session, ATTACHMENT_MODEL, [("id", "=", int(attachment_id))], _META_FIELDS)
|
| 88 |
+
row = (meta or [None])[0]
|
| 89 |
+
res_id = row.get("res_id") if row else None
|
| 90 |
+
if (not row or row.get("res_model") != PICKING_MODEL or row.get("type") != "binary"
|
| 91 |
+
or row.get("mimetype") not in IMAGE_TYPES or isinstance(res_id, bool)
|
| 92 |
+
or not isinstance(res_id, int) or res_id <= 0):
|
| 93 |
+
raise err(404, "photo_not_found", "This delivery photo was not found.")
|
| 94 |
+
require_delivery_read(session, res_id)
|
| 95 |
+
data = _search_read(session, ATTACHMENT_MODEL, [("id", "=", int(attachment_id))], ["datas"])
|
| 96 |
+
raw = (data or [{}])[0].get("datas")
|
| 97 |
+
if not raw:
|
| 98 |
+
raise err(404, "photo_not_found", "This delivery photo has no image data.")
|
| 99 |
+
try:
|
| 100 |
+
body = base64.b64decode(raw, validate=False)
|
| 101 |
+
except (binascii.Error, ValueError, TypeError):
|
| 102 |
+
raise err(502, "photo_unreadable", "Odoo sent a photo that could not be read.")
|
| 103 |
+
return body, row["mimetype"], str(row.get("name") or f"photo-{int(attachment_id)}")
|
api/odoo_relational.py
CHANGED
|
@@ -106,6 +106,9 @@ from odoo_purchasing import (ORDERS_KEY as PURCHASE_ORDERS_KEY, LINES_KEY as PUR
|
|
| 106 |
source_specs as purchase_source_specs,
|
| 107 |
validate as validate_purchasing)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
#: The join column the partner-grain tables carry. Derived links resolve through it (`on`/`from`).
|
| 110 |
JOIN_KEY = "partner_id"
|
| 111 |
#: The product-grain equivalent.
|
|
|
|
| 106 |
source_specs as purchase_source_specs,
|
| 107 |
validate as validate_purchasing)
|
| 108 |
|
| 109 |
+
#: S78-T14: the databases whose row id is a `stock.picking` id. Their read wall admits a delivery photo.
|
| 110 |
+
DELIVERY_KEYS = (SHIPMENTS_KEY, PICKINGS_KEY)
|
| 111 |
+
|
| 112 |
#: The join column the partner-grain tables carry. Derived links resolve through it (`on`/`from`).
|
| 113 |
JOIN_KEY = "partner_id"
|
| 114 |
#: The product-grain equivalent.
|
api/routes_agent_harness.py
CHANGED
|
@@ -66,7 +66,10 @@ def _activity_files(session, intelligence):
|
|
| 66 |
if "files" not in scope:
|
| 67 |
# An unavailable catalog stays unavailable for this request only.
|
| 68 |
scope["files"] = None
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
if scope["files"] is None:
|
| 71 |
raise PermissionError("The current file catalog is unavailable.")
|
| 72 |
return scope["files"]
|
|
@@ -108,6 +111,7 @@ def activity_read_policy(session: Session, object):
|
|
| 108 |
def get_intelligence_files(path: str = "", section: str = "", version: int = 0,
|
| 109 |
q: str = "", folder: str = "", offset: int = 0, limit: int = 100,
|
| 110 |
direct: bool = False,
|
|
|
|
| 111 |
session: Session = Depends(module_gate("intelligence"))):
|
| 112 |
"""List a workspace section, search, or read one current or historical Markdown file."""
|
| 113 |
import intelligence_store as intelligence
|
|
@@ -115,8 +119,12 @@ def get_intelligence_files(path: str = "", section: str = "", version: int = 0,
|
|
| 115 |
try:
|
| 116 |
if path:
|
| 117 |
return intelligence.read_file(session, path, version=version)
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except intelligence.IntelligenceError as exc:
|
| 121 |
raise _intelligence_error(exc) from None
|
| 122 |
|
|
@@ -197,13 +205,54 @@ def decide_intelligence_proposal(proposal_id: str, decision: str, body: dict = B
|
|
| 197 |
|
| 198 |
|
| 199 |
@router.delete("/intelligence/files")
|
| 200 |
-
def delete_intelligence_file(path: str = "",
|
| 201 |
session: Session = Depends(module_gate("intelligence"))):
|
| 202 |
"""Delete a workspace file under the Intelligence module grant."""
|
| 203 |
import intelligence_store as intelligence
|
| 204 |
|
| 205 |
try:
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
except intelligence.IntelligenceError as exc:
|
| 208 |
raise _intelligence_error(exc) from None
|
| 209 |
|
|
|
|
| 66 |
if "files" not in scope:
|
| 67 |
# An unavailable catalog stays unavailable for this request only.
|
| 68 |
scope["files"] = None
|
| 69 |
+
from copy import copy
|
| 70 |
+
borrowed = copy(session)
|
| 71 |
+
borrowed.runtime = intelligence._FolderReadRuntime(session.runtime)
|
| 72 |
+
scope["files"] = intelligence._files(borrowed)
|
| 73 |
if scope["files"] is None:
|
| 74 |
raise PermissionError("The current file catalog is unavailable.")
|
| 75 |
return scope["files"]
|
|
|
|
| 111 |
def get_intelligence_files(path: str = "", section: str = "", version: int = 0,
|
| 112 |
q: str = "", folder: str = "", offset: int = 0, limit: int = 100,
|
| 113 |
direct: bool = False,
|
| 114 |
+
response: Response = None,
|
| 115 |
session: Session = Depends(module_gate("intelligence"))):
|
| 116 |
"""List a workspace section, search, or read one current or historical Markdown file."""
|
| 117 |
import intelligence_store as intelligence
|
|
|
|
| 119 |
try:
|
| 120 |
if path:
|
| 121 |
return intelligence.read_file(session, path, version=version)
|
| 122 |
+
result = intelligence.list_files(session, section=section, query=q, folder=folder,
|
| 123 |
+
offset=offset, limit=limit, direct=direct)
|
| 124 |
+
if response is not None:
|
| 125 |
+
# Component memory may paint a prior list while every visit rechecks current access.
|
| 126 |
+
response.headers["X-Brain-List-Cache"] = "private-revalidate"
|
| 127 |
+
return result
|
| 128 |
except intelligence.IntelligenceError as exc:
|
| 129 |
raise _intelligence_error(exc) from None
|
| 130 |
|
|
|
|
| 205 |
|
| 206 |
|
| 207 |
@router.delete("/intelligence/files")
|
| 208 |
+
def delete_intelligence_file(path: str = "", expectedVersion: int = None,
|
| 209 |
session: Session = Depends(module_gate("intelligence"))):
|
| 210 |
"""Delete a workspace file under the Intelligence module grant."""
|
| 211 |
import intelligence_store as intelligence
|
| 212 |
|
| 213 |
try:
|
| 214 |
+
if type(expectedVersion) is not int or expectedVersion < 1:
|
| 215 |
+
raise intelligence.IntelligenceError("bad_version", "Reload the file before trying again.")
|
| 216 |
+
return {"deleted": intelligence.delete_file(session, path, expected_version=expectedVersion)["deleted"]}
|
| 217 |
+
except intelligence.IntelligenceError as exc:
|
| 218 |
+
raise _intelligence_error(exc) from None
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
@router.patch("/intelligence/files")
|
| 222 |
+
def rename_intelligence_file(body: dict = Body(default=None),
|
| 223 |
+
session: Session = Depends(module_gate("intelligence"))):
|
| 224 |
+
import intelligence_store as intelligence
|
| 225 |
+
|
| 226 |
+
body = body or {}
|
| 227 |
+
try:
|
| 228 |
+
return intelligence.rename_file(session, body.get("path"), body.get("title"),
|
| 229 |
+
expected_version=body.get("expectedVersion"))
|
| 230 |
+
except intelligence.IntelligenceError as exc:
|
| 231 |
+
raise _intelligence_error(exc) from None
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
@router.patch("/intelligence/folders")
|
| 235 |
+
def rename_intelligence_folder(body: dict = Body(default=None),
|
| 236 |
+
session: Session = Depends(module_gate("intelligence"))):
|
| 237 |
+
import intelligence_store as intelligence
|
| 238 |
+
|
| 239 |
+
body = body or {}
|
| 240 |
+
try:
|
| 241 |
+
if "name" not in body or not isinstance(body["name"], str):
|
| 242 |
+
raise intelligence.IntelligenceError("bad_name", "Enter a folder name.")
|
| 243 |
+
return intelligence.change_folder(session, body.get("path"), name=body["name"],
|
| 244 |
+
expected_revision=body.get("expectedRevision"))
|
| 245 |
+
except intelligence.IntelligenceError as exc:
|
| 246 |
+
raise _intelligence_error(exc) from None
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
@router.delete("/intelligence/folders")
|
| 250 |
+
def delete_intelligence_folder(path: str = "", expectedRevision: int = None,
|
| 251 |
+
session: Session = Depends(module_gate("intelligence"))):
|
| 252 |
+
import intelligence_store as intelligence
|
| 253 |
+
|
| 254 |
+
try:
|
| 255 |
+
return intelligence.change_folder(session, path, expected_revision=expectedRevision)
|
| 256 |
except intelligence.IntelligenceError as exc:
|
| 257 |
raise _intelligence_error(exc) from None
|
| 258 |
|
api/routes_amazon_profit.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Amazon SKU profit doors: read the settings, switch the period, recompute, set the owner,
|
| 2 |
+
and confirm or reject a suggested Odoo match.
|
| 3 |
+
|
| 4 |
+
The table itself (`ut_amazon_sku_profit`) is read and edited through the ordinary table doors.
|
| 5 |
+
A refresh rewrites only computed columns, so owner, status and due date survive a period switch.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
from typing import Any
|
| 11 |
+
|
| 12 |
+
from fastapi import APIRouter, Body, Depends, HTTPException
|
| 13 |
+
|
| 14 |
+
from deps import Session, err, require_session
|
| 15 |
+
from modules import amazon_profit
|
| 16 |
+
|
| 17 |
+
router = APIRouter(prefix="/api/v1")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _admin(session):
|
| 21 |
+
if not session.admin:
|
| 22 |
+
raise err(403, "forbidden", "Only an Administrator can refresh Amazon SKU profit.")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _may_edit(session):
|
| 26 |
+
"""Editors and Administrators of the table; a viewer reads only."""
|
| 27 |
+
import core.user_tables as ut
|
| 28 |
+
return bool(session.admin) or ut.may_edit_database(
|
| 29 |
+
amazon_profit.TABLE_KEY, session.uname, is_admin=session.admin, st=session.runtime)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _cost_reader(runtime):
|
| 33 |
+
"""Odoo cost for linked references, bound to this tenant's own read-only connection."""
|
| 34 |
+
def read(codes):
|
| 35 |
+
source = runtime.odoo_source()
|
| 36 |
+
if source is None:
|
| 37 |
+
return {}, [f"{code}: this workspace has no Odoo connection" for code in sorted(codes)]
|
| 38 |
+
try:
|
| 39 |
+
with source._bound():
|
| 40 |
+
return amazon_profit.odoo_costs(codes)
|
| 41 |
+
except amazon_profit.SourceUnavailable:
|
| 42 |
+
raise
|
| 43 |
+
except Exception as exc: # noqa: BLE001
|
| 44 |
+
raise amazon_profit.SourceUnavailable(
|
| 45 |
+
f"The Odoo cost read failed ({type(exc).__name__}). Try again.") from None
|
| 46 |
+
return read
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def _mirror(runtime):
|
| 50 |
+
"""A private read cursor on this tenant's mirror. Amazon readiness is its own table check,
|
| 51 |
+
not the Odoo backfill gate `ro_con` waits on, so this asserts the tenant and opens a cursor."""
|
| 52 |
+
from harness import datastore as DS
|
| 53 |
+
from harness import runtime as R
|
| 54 |
+
from pathlib import Path
|
| 55 |
+
try:
|
| 56 |
+
runtime.assert_datastore_matches()
|
| 57 |
+
if not Path(runtime.datastore_path).is_file():
|
| 58 |
+
raise err(503, "amazon_source_unavailable", "This workspace has no Amazon data yet.")
|
| 59 |
+
return DS.ro_cursor()
|
| 60 |
+
except R.DatastoreMismatch:
|
| 61 |
+
raise err(500, "store_mismatch", "This workspace is reading another workspace's data.") from None
|
| 62 |
+
except RuntimeError:
|
| 63 |
+
raise err(503, "store_not_ready", "Amazon data is still loading. Try again shortly.") from None
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@router.get("/amazon-profit")
|
| 67 |
+
def profit_status(session: Session = Depends(require_session)):
|
| 68 |
+
"""The current period, thin threshold, last refresh and owner setting, with the choices."""
|
| 69 |
+
import core.user_tables as ut
|
| 70 |
+
settings = amazon_profit.read_settings(session.runtime)
|
| 71 |
+
return {"tableKey": amazon_profit.TABLE_KEY,
|
| 72 |
+
"tableExists": ut.get_one(amazon_profit.TABLE_KEY, session.runtime) is not None,
|
| 73 |
+
"period": amazon_profit.period_bounds(settings["period"]),
|
| 74 |
+
"thin": settings["thin"],
|
| 75 |
+
"refreshedAt": settings.get("refreshedAt"),
|
| 76 |
+
"skus": settings.get("skus"),
|
| 77 |
+
"linked": settings.get("linked"),
|
| 78 |
+
"verifiedMonth": settings.get("verifiedMonth"),
|
| 79 |
+
"refusal": settings.get("refusal"),
|
| 80 |
+
"defaultOwner": settings["defaultOwner"],
|
| 81 |
+
"periods": amazon_profit.period_choices(),
|
| 82 |
+
"statuses": list(amazon_profit.STATUSES),
|
| 83 |
+
"matchStates": list(amazon_profit.MATCH_STATES),
|
| 84 |
+
"matchedAt": settings.get("matchedAt"),
|
| 85 |
+
"matches": settings.get("matches"),
|
| 86 |
+
"matchError": settings.get("matchError"),
|
| 87 |
+
"dueDays": amazon_profit.DUE_DAYS,
|
| 88 |
+
"reopenDays": amazon_profit.REOPEN_DAYS,
|
| 89 |
+
"canEdit": _may_edit(session),
|
| 90 |
+
"canAdmin": bool(session.admin)}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def _recompute(session, period=None, thin=None):
|
| 94 |
+
"""Run one refresh through the tenant's mirror and Odoo cost read; 409 on a failed tie."""
|
| 95 |
+
try:
|
| 96 |
+
if period is not None:
|
| 97 |
+
amazon_profit.period_bounds(period)
|
| 98 |
+
if thin is not None:
|
| 99 |
+
amazon_profit.clean_thin(thin)
|
| 100 |
+
except ValueError as exc:
|
| 101 |
+
raise err(400, "invalid_setting", str(exc)) from None
|
| 102 |
+
con = _mirror(session.runtime)
|
| 103 |
+
try:
|
| 104 |
+
report = amazon_profit.refresh(session.runtime, con, period=period, thin=thin,
|
| 105 |
+
by=session.uname, cost_reader=_cost_reader(session.runtime),
|
| 106 |
+
tenant=session.tenant)
|
| 107 |
+
except amazon_profit.SourceUnavailable as exc:
|
| 108 |
+
raise err(503, "amazon_source_unavailable", str(exc)) from None
|
| 109 |
+
except amazon_profit.TableRefused as exc:
|
| 110 |
+
raise err(409, "table_refused", str(exc)) from None
|
| 111 |
+
except ValueError as exc:
|
| 112 |
+
raise err(400, "invalid_setting", str(exc)) from None
|
| 113 |
+
finally:
|
| 114 |
+
try:
|
| 115 |
+
con.close()
|
| 116 |
+
except Exception: # noqa: BLE001
|
| 117 |
+
pass
|
| 118 |
+
if report.get("refusal"):
|
| 119 |
+
raise err(409, "not_verified", report["refusal"])
|
| 120 |
+
return report
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@router.post("/amazon-profit/refresh")
|
| 124 |
+
def profit_refresh(body: Any = Body(default=None), session: Session = Depends(require_session)):
|
| 125 |
+
"""Recompute every SKU for the period (30d, 90d or YYYY-MM) and store the settings."""
|
| 126 |
+
_admin(session)
|
| 127 |
+
body = body if isinstance(body, dict) else {}
|
| 128 |
+
return _recompute(session, body.get("period"), body.get("thin"))
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
@router.post("/amazon-profit/period")
|
| 132 |
+
def profit_period(body: Any = Body(default=None), session: Session = Depends(require_session)):
|
| 133 |
+
"""Switch the table to another period and recompute every row; owned cells stay."""
|
| 134 |
+
if not _may_edit(session):
|
| 135 |
+
raise err(403, "forbidden", "Only an editor of Amazon SKU profit can change its period.")
|
| 136 |
+
body = body if isinstance(body, dict) else {}
|
| 137 |
+
period = body.get("period")
|
| 138 |
+
if not period:
|
| 139 |
+
raise err(400, "invalid_setting", "Choose 30 days, 90 days or a calendar month.")
|
| 140 |
+
return _recompute(session, period)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
@router.put("/amazon-profit/settings")
|
| 144 |
+
def profit_settings(body: Any = Body(default=None), session: Session = Depends(require_session)):
|
| 145 |
+
"""Administrator settings: the default owner of new items and the thin margin."""
|
| 146 |
+
if not session.admin:
|
| 147 |
+
raise err(403, "forbidden", "Only an Administrator can change Amazon SKU profit settings.")
|
| 148 |
+
body = body if isinstance(body, dict) else {}
|
| 149 |
+
try:
|
| 150 |
+
if "defaultOwner" in body:
|
| 151 |
+
amazon_profit.set_default_owner(session.runtime, body.get("defaultOwner"),
|
| 152 |
+
tenant=session.tenant, by=session.uname)
|
| 153 |
+
if "thin" in body:
|
| 154 |
+
amazon_profit.write_settings(session.runtime,
|
| 155 |
+
thin=amazon_profit.clean_thin(body.get("thin")))
|
| 156 |
+
except ValueError as exc:
|
| 157 |
+
raise err(400, "invalid_setting", str(exc)) from None
|
| 158 |
+
settings = amazon_profit.read_settings(session.runtime)
|
| 159 |
+
return {"defaultOwner": settings["defaultOwner"], "thin": settings["thin"]}
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
@router.post("/amazon-profit/matches/{sku:path}/{decision}")
|
| 163 |
+
def profit_match(sku: str, decision: str, session: Session = Depends(require_session)):
|
| 164 |
+
"""Confirm or reject the suggested Odoo product for one SKU; editors only.
|
| 165 |
+
|
| 166 |
+
Confirm copies the suggestion into the link and recomputes the period, so COGS and profit
|
| 167 |
+
fill at once. Reject clears the suggestion, keeps the SKU unlinked and remembers the code."""
|
| 168 |
+
import core.user_tables as ut
|
| 169 |
+
states = {"confirm": amazon_profit.CONFIRMED, "reject": amazon_profit.REJECTED}
|
| 170 |
+
if decision not in states:
|
| 171 |
+
raise err(404, "not_found", "Choose confirm or reject.")
|
| 172 |
+
if not _may_edit(session):
|
| 173 |
+
raise err(403, "forbidden", "Only an editor of Amazon SKU profit can confirm or reject a match.")
|
| 174 |
+
sku = str(sku or "").strip()
|
| 175 |
+
rows = [r for r in amazon_profit.table_rows(session.runtime).values()
|
| 176 |
+
if str(r.get("sku") or "").strip() == sku]
|
| 177 |
+
if ut.get_one(amazon_profit.TABLE_KEY, session.runtime) is None or not rows:
|
| 178 |
+
raise err(404, "not_found", "This SKU is not in Amazon SKU profit.")
|
| 179 |
+
row = rows[0]
|
| 180 |
+
if str(row.get("odoo_product") or "").strip():
|
| 181 |
+
raise err(409, "already_linked", "This SKU already has an Odoo product.")
|
| 182 |
+
if not str(row.get("suggested_product") or "").strip():
|
| 183 |
+
raise err(409, "no_suggestion", "This SKU has no suggested Odoo product.")
|
| 184 |
+
with amazon_profit._LOCK:
|
| 185 |
+
result = amazon_profit.apply_decisions(session.runtime, by=session.uname, sku=sku,
|
| 186 |
+
state=states[decision])
|
| 187 |
+
out = {"sku": sku, "matchState": (result["row"] or {}).get("match_state"),
|
| 188 |
+
"odooProduct": (result["row"] or {}).get("odoo_product"),
|
| 189 |
+
"unitsPerSku": (result["row"] or {}).get("units_per_sku")}
|
| 190 |
+
if decision == "confirm":
|
| 191 |
+
try:
|
| 192 |
+
report = _recompute(session)
|
| 193 |
+
out["refresh"] = {k: report.get(k) for k in ("skus", "linked", "refreshedAt")}
|
| 194 |
+
except HTTPException as exc:
|
| 195 |
+
out["refreshError"] = ((exc.detail or {}).get("error") or {}).get("message")
|
| 196 |
+
current = next((r for r in amazon_profit.table_rows(session.runtime).values()
|
| 197 |
+
if str(r.get("sku") or "").strip() == sku), {})
|
| 198 |
+
out.update(cogsPerUnit=current.get("cogs_per_unit"), netProfit=current.get("net_profit"),
|
| 199 |
+
margin=current.get("margin"), band=current.get("band"))
|
| 200 |
+
return out
|
api/routes_auth.py
CHANGED
|
@@ -98,6 +98,27 @@ def _clear_failures(key):
|
|
| 98 |
raise _auth_store_down("_clear_failures", exc)
|
| 99 |
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def _public_user(user):
|
| 102 |
"""What the client is allowed to know about itself. Never a hash, a salt, or the epoch —
|
| 103 |
the epoch is a server-side revocation handle and a client has no use for it."""
|
|
@@ -271,6 +292,8 @@ def login(request: Request, response: Response, body: dict = Body(default=None))
|
|
| 271 |
# W57-T36 / C12: every STORED account needs its second factor before any cookie is minted.
|
| 272 |
# qa_runner is the sole exception and is double-gated out of live deployments.
|
| 273 |
recovery_code_used = False
|
|
|
|
|
|
|
| 274 |
if not user.get("qa_runner"):
|
| 275 |
try:
|
| 276 |
rec = (users.registry() or {}).get(user["username"])
|
|
@@ -281,15 +304,17 @@ def login(request: Request, response: Response, body: dict = Body(default=None))
|
|
| 281 |
# person out of the unauthenticated enrolment door that repairs their account.
|
| 282 |
raise err(403, "mfa_enrolment_required", "This account needs two step sign in. Set up your "
|
| 283 |
"authenticator app to continue.")
|
| 284 |
-
|
|
|
|
|
|
|
| 285 |
# A missing factor is a prompt, not a failed attempt at guessing one.
|
| 286 |
raise err(401, "mfa_required", "Enter the 6 digit code from your authenticator app.")
|
| 287 |
# TOTP FIRST: redemption consumes a recovery code. Do not spend one when the app's
|
| 288 |
# ordinary code already proves the factor, and never reset the guess budget on password.
|
| 289 |
-
factor_valid = users.totp_valid(rec.get("mfa_secret"), code)
|
| 290 |
if not factor_valid:
|
| 291 |
recovery_code_used = users.consume_recovery_code(user["username"], code)
|
| 292 |
-
if not (factor_valid or recovery_code_used):
|
| 293 |
_note_failure(throttle_key)
|
| 294 |
try:
|
| 295 |
audit.record("auth.mfa_failed", tenant, user["username"])
|
|
@@ -323,16 +348,29 @@ def login(request: Request, response: Response, body: dict = Body(default=None))
|
|
| 323 |
epoch_bumped = False
|
| 324 |
if not user.get("qa_runner") and not freeze_notice.enabled():
|
| 325 |
try:
|
| 326 |
-
bumped = users.bump_epoch(user["username"])
|
| 327 |
if bumped is not None:
|
| 328 |
session_epoch = int(bumped)
|
| 329 |
epoch_bumped = True
|
|
|
|
|
|
|
| 330 |
except Exception as exc:
|
|
|
|
|
|
|
| 331 |
print(f"[aios-api] login for {user['username']!r}: could not bump the session epoch "
|
| 332 |
f"({type(exc).__name__}). This sign-in stands, but the account's previous "
|
| 333 |
f"session was NOT signed out.")
|
|
|
|
|
|
|
| 334 |
value, _claims = aios_session.mint(tenant, user["username"], session_epoch)
|
| 335 |
aios_session.set_cookie(response, request, value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
# Wave 19 (R4): the login stamp. THE RESOLVED username, never the posted identifier — the
|
| 337 |
# person may have typed an email address and `users.verify` resolved it to the registry key;
|
| 338 |
# stamping what was typed would write onto a key that does not exist. `touch_login` is
|
|
@@ -712,3 +750,26 @@ def logout(request: Request):
|
|
| 712 |
@router.get("/me")
|
| 713 |
def me(session: Session = Depends(require_session)):
|
| 714 |
return {"user": _public_user(session.user), "tenant": session.tenant}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
raise _auth_store_down("_clear_failures", exc)
|
| 99 |
|
| 100 |
|
| 101 |
+
def _trusted_browser_policy(tenant):
|
| 102 |
+
"""An unavailable policy permits MFA sign-in, never remembered-browser admission."""
|
| 103 |
+
from harness import runtime
|
| 104 |
+
try:
|
| 105 |
+
rt = runtime.get_runtime(tenant)
|
| 106 |
+
return users.trusted_browser_policy(rt._store(), rt.store_key(users.TRUST_POLICY_KEY))
|
| 107 |
+
except Exception:
|
| 108 |
+
return None
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def _admit_trusted_browser(request, tenant, user, rec, policy):
|
| 112 |
+
if not policy or not policy["enabled"] or not rec or not rec.get("active", True):
|
| 113 |
+
return None
|
| 114 |
+
if (str(rec.get("tenant") or "royal-imports").strip().lower() != tenant
|
| 115 |
+
or int(rec.get("epoch") or 0) != int(user.get("epoch") or 0)):
|
| 116 |
+
return None
|
| 117 |
+
return aios_session.read_trusted_browser(
|
| 118 |
+
request.cookies.get(aios_session.TRUST_COOKIE_NAME), tenant, user["username"],
|
| 119 |
+
int(rec.get("epoch") or 0), policy["generation"])
|
| 120 |
+
|
| 121 |
+
|
| 122 |
def _public_user(user):
|
| 123 |
"""What the client is allowed to know about itself. Never a hash, a salt, or the epoch —
|
| 124 |
the epoch is a server-side revocation handle and a client has no use for it."""
|
|
|
|
| 292 |
# W57-T36 / C12: every STORED account needs its second factor before any cookie is minted.
|
| 293 |
# qa_runner is the sole exception and is double-gated out of live deployments.
|
| 294 |
recovery_code_used = False
|
| 295 |
+
remembered = None
|
| 296 |
+
trust_policy = None
|
| 297 |
if not user.get("qa_runner"):
|
| 298 |
try:
|
| 299 |
rec = (users.registry() or {}).get(user["username"])
|
|
|
|
| 304 |
# person out of the unauthenticated enrolment door that repairs their account.
|
| 305 |
raise err(403, "mfa_enrolment_required", "This account needs two step sign in. Set up your "
|
| 306 |
"authenticator app to continue.")
|
| 307 |
+
trust_policy = _trusted_browser_policy(tenant)
|
| 308 |
+
remembered = _admit_trusted_browser(request, tenant, user, rec, trust_policy) if not code else None
|
| 309 |
+
if not code and not remembered:
|
| 310 |
# A missing factor is a prompt, not a failed attempt at guessing one.
|
| 311 |
raise err(401, "mfa_required", "Enter the 6 digit code from your authenticator app.")
|
| 312 |
# TOTP FIRST: redemption consumes a recovery code. Do not spend one when the app's
|
| 313 |
# ordinary code already proves the factor, and never reset the guess budget on password.
|
| 314 |
+
factor_valid = bool(remembered) or users.totp_valid(rec.get("mfa_secret"), code)
|
| 315 |
if not factor_valid:
|
| 316 |
recovery_code_used = users.consume_recovery_code(user["username"], code)
|
| 317 |
+
if not (factor_valid or recovery_code_used or remembered):
|
| 318 |
_note_failure(throttle_key)
|
| 319 |
try:
|
| 320 |
audit.record("auth.mfa_failed", tenant, user["username"])
|
|
|
|
| 348 |
epoch_bumped = False
|
| 349 |
if not user.get("qa_runner") and not freeze_notice.enabled():
|
| 350 |
try:
|
| 351 |
+
bumped = users.bump_epoch(user["username"], expected_epoch=int(user.get("epoch") or 0))
|
| 352 |
if bumped is not None:
|
| 353 |
session_epoch = int(bumped)
|
| 354 |
epoch_bumped = True
|
| 355 |
+
except users.SessionEpochChanged:
|
| 356 |
+
raise err(401, "mfa_required", "This account changed during sign in. Enter a new authenticator code.") from None
|
| 357 |
except Exception as exc:
|
| 358 |
+
if remembered:
|
| 359 |
+
raise err(503, "auth_state_unavailable", "Sign in protection is temporarily unavailable. Try again later.") from None
|
| 360 |
print(f"[aios-api] login for {user['username']!r}: could not bump the session epoch "
|
| 361 |
f"({type(exc).__name__}). This sign-in stands, but the account's previous "
|
| 362 |
f"session was NOT signed out.")
|
| 363 |
+
if remembered and not epoch_bumped:
|
| 364 |
+
raise err(503, "auth_state_unavailable", "Sign in protection is temporarily unavailable. Try again later.")
|
| 365 |
value, _claims = aios_session.mint(tenant, user["username"], session_epoch)
|
| 366 |
aios_session.set_cookie(response, request, value)
|
| 367 |
+
if (epoch_bumped and trust_policy and trust_policy["enabled"]
|
| 368 |
+
and (remembered or body.get("rememberBrowser") is True)):
|
| 369 |
+
trusted, claims = aios_session.mint_trusted_browser(
|
| 370 |
+
tenant, user["username"], session_epoch, trust_policy["generation"], original=remembered)
|
| 371 |
+
aios_session.set_trusted_browser_cookie(response, request, trusted, claims)
|
| 372 |
+
elif request.cookies.get(aios_session.TRUST_COOKIE_NAME):
|
| 373 |
+
aios_session.clear_trusted_browser_cookie(response, request)
|
| 374 |
# Wave 19 (R4): the login stamp. THE RESOLVED username, never the posted identifier — the
|
| 375 |
# person may have typed an email address and `users.verify` resolved it to the registry key;
|
| 376 |
# stamping what was typed would write onto a key that does not exist. `touch_login` is
|
|
|
|
| 750 |
@router.get("/me")
|
| 751 |
def me(session: Session = Depends(require_session)):
|
| 752 |
return {"user": _public_user(session.user), "tenant": session.tenant}
|
| 753 |
+
|
| 754 |
+
|
| 755 |
+
@router.post("/logout-everywhere", status_code=204)
|
| 756 |
+
def logout_everywhere(request: Request, session: Session = Depends(require_session)):
|
| 757 |
+
"""Revoke sessions and browser trust together before reporting success."""
|
| 758 |
+
if session.user.get("qa_runner"):
|
| 759 |
+
raise err(400, "no_stored_account", "That sign in has no stored sessions to revoke.")
|
| 760 |
+
from core import freeze_notice
|
| 761 |
+
freeze_notice.check_write()
|
| 762 |
+
try:
|
| 763 |
+
bumped = users.bump_epoch(session.uname)
|
| 764 |
+
except Exception:
|
| 765 |
+
raise err(503, "store_unavailable", "Sessions could not be signed out. Try again.") from None
|
| 766 |
+
if bumped is None:
|
| 767 |
+
raise err(503, "store_unavailable", "Sessions could not be signed out. Try again.")
|
| 768 |
+
out = Response(status_code=204)
|
| 769 |
+
aios_session.clear_cookie(out, request)
|
| 770 |
+
aios_session.clear_trusted_browser_cookie(out, request)
|
| 771 |
+
try:
|
| 772 |
+
audit.record("auth.logout", session.tenant, session.uname, {"reason": "everywhere"})
|
| 773 |
+
except audit.AuditRefused as exc:
|
| 774 |
+
print(f"[aios-api] {exc}")
|
| 775 |
+
return out
|
api/routes_automation.py
CHANGED
|
@@ -61,6 +61,29 @@ MODULE = "automation"
|
|
| 61 |
_GATE = module_gate(MODULE)
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def _governed_query_tool(session, database=""):
|
| 65 |
"""Adapt C4 dry-runs to B's mounted C2 Query door without owning Query authority."""
|
| 66 |
def invoke(prompt, context):
|
|
@@ -248,8 +271,11 @@ def list_voice_agents(session: Session = Depends(_GATE)):
|
|
| 248 |
"""
|
| 249 |
rows = voice.list_browser_dtos(session.runtime)
|
| 250 |
options = _voice_options(session) if rows else None
|
|
|
|
| 251 |
for row in rows:
|
| 252 |
row["audienceOptions"] = options
|
|
|
|
|
|
|
| 253 |
return {"voiceAgents": rows}
|
| 254 |
|
| 255 |
|
|
@@ -264,6 +290,7 @@ def create_voice_agent(body: dict = Body(default=None), session: Session = Depen
|
|
| 264 |
saved = voice.save_draft(session.runtime, body or {}, session.uname, tenant_id=session.tenant)
|
| 265 |
row = voice.get_browser_dto(session.runtime, saved["id"])
|
| 266 |
row["audienceOptions"] = _voice_options(session)
|
|
|
|
| 267 |
return {"voiceAgent": row}
|
| 268 |
except voice.VoiceRefusal as exc:
|
| 269 |
raise err(400, "voice_refused", str(exc))
|
|
@@ -272,14 +299,36 @@ def create_voice_agent(body: dict = Body(default=None), session: Session = Depen
|
|
| 272 |
@router.patch("/voice-agents/{voice_id}")
|
| 273 |
def patch_voice_agent(voice_id: str, body: dict = Body(default=None), session: Session = Depends(_GATE)):
|
| 274 |
try:
|
| 275 |
-
voice.
|
|
|
|
|
|
|
| 276 |
row = voice.get_browser_dto(session.runtime, voice_id)
|
| 277 |
row["audienceOptions"] = _voice_options(session)
|
|
|
|
| 278 |
return {"voiceAgent": row}
|
| 279 |
except voice.VoiceRefusal as exc:
|
| 280 |
raise err(400 if "unknown" not in str(exc) else 404, "voice_refused", str(exc))
|
| 281 |
|
| 282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
@router.post("/voice-agents/{voice_id}/harness/dry-run")
|
| 284 |
def voice_dry_run(voice_id: str, body: dict = Body(default=None), session: Session = Depends(_GATE)):
|
| 285 |
body = body or {}
|
|
@@ -485,7 +534,7 @@ def _sync_schedule_or_503(tenant, defn):
|
|
| 485 |
f"Nothing was reported as armed: {exc}")
|
| 486 |
|
| 487 |
|
| 488 |
-
def _wire(defn, tenant, rt=None):
|
| 489 |
"""One automation, as the client reads it. `running` is PROCESS state, never store state —
|
| 490 |
see the engine header on why a persisted 'running' is a permanent lock."""
|
| 491 |
live = engine.running(tenant, defn.get("id"))
|
|
@@ -497,7 +546,7 @@ def _wire(defn, tenant, rt=None):
|
|
| 497 |
status = {**status, "state": "running", "startedAt": live.get("startedAt"),
|
| 498 |
"step": live.get("step")}
|
| 499 |
agent_contract = engine.agent_contract_dto(rt, defn.get("id")) if rt else None
|
| 500 |
-
|
| 501 |
"id": defn.get("id"), "name": defn.get("name"), "kind": defn.get("kind"),
|
| 502 |
"config": defn.get("config") or {}, "schedule": sched, "status": status,
|
| 503 |
"runs": list(defn.get("runs") or [])[:engine.MAX_RUNS],
|
|
@@ -600,6 +649,9 @@ def _wire(defn, tenant, rt=None):
|
|
| 600 |
"This system Agent is configured at its source."}
|
| 601 |
}),
|
| 602 |
}
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
|
| 605 |
#: ⭐⭐ WAVE 34 · CONTRACTS C3 + C4 — WHY A ROW CANNOT BE DELETED, as a slug.
|
|
@@ -701,7 +753,7 @@ def _field_agent_rows(session):
|
|
| 701 |
# — the exact way `awaitingResults` shipped inert for a whole wave. Passing the
|
| 702 |
# synthetic DEFINITION through the same function makes them identical by
|
| 703 |
# construction; only `system` is stamped afterwards, because no stored row has it.
|
| 704 |
-
row = _wire(defn_syn, session.tenant, rt=session.runtime)
|
| 705 |
row["system"] = SYSTEM_FIELD_AGENT
|
| 706 |
out.append(row)
|
| 707 |
return out
|
|
@@ -860,7 +912,7 @@ def _odoo_sync_row(session, detail=False):
|
|
| 860 |
"config": {"connector": "odoo", "every": every,
|
| 861 |
"frozen": frozen}}]},
|
| 862 |
}
|
| 863 |
-
row = _wire(defn, session.tenant, rt=session.runtime)
|
| 864 |
row["system"] = SYSTEM_ODOO_SYNC
|
| 865 |
# ⛔ THE SENTENCE IS OVERRIDDEN, AND IT IS A FIX RATHER THAN A PREFERENCE. `compose_sentence`
|
| 866 |
# speaks the automation vocabulary — it builds "When <cron phrase>, run N actions" out of
|
|
@@ -1319,7 +1371,7 @@ def list_automations(session: Session = Depends(_GATE)):
|
|
| 1319 |
pass
|
| 1320 |
_STATEMENTS_SEEDED.add(session.tenant)
|
| 1321 |
defs = engine.all_definitions(session.runtime)
|
| 1322 |
-
items = [_wire(d, session.tenant, rt=session.runtime) for _, d in
|
| 1323 |
sorted(defs.items(), key=lambda kv: (kv[1].get("name") or "").lower())]
|
| 1324 |
# ⭐⭐ WAVE 34 · CONTRACT C3 (W34-T48) — field agents join the list as SYNTHETIC rows.
|
| 1325 |
# ⚠ MERGED AND RE-SORTED, not appended in a block at the end. R13 asks for a field agent to be
|
|
@@ -2117,7 +2169,7 @@ def agent_author_action(auto_id: str, body: dict = Body(default=None),
|
|
| 2117 |
|
| 2118 |
session.runtime.update(AGENT_ACTIONS_KEY, _set, flush="sync")
|
| 2119 |
_prune_marks(session, auto_id)
|
| 2120 |
-
return {"automation": _wire(updated, session.tenant, rt=session.runtime),
|
| 2121 |
"actionId": action_id, "agent": agent_id, "notes": notes}
|
| 2122 |
|
| 2123 |
|
|
@@ -2188,7 +2240,7 @@ def get_automation(auto_id: str, session: Session = Depends(_GATE)):
|
|
| 2188 |
defn = engine.all_definitions(session.runtime).get(str(auto_id))
|
| 2189 |
if defn is None:
|
| 2190 |
raise err(404, "unknown_automation", "no automation with that id")
|
| 2191 |
-
return {"automation": _wire(defn, session.tenant, rt=session.runtime)}
|
| 2192 |
|
| 2193 |
|
| 2194 |
@router.post("/automations")
|
|
@@ -2213,7 +2265,7 @@ def create_automation(body: dict = Body(default=None), session: Session = Depend
|
|
| 2213 |
engine.ensure_agent_contract(session.runtime, defn.get("id"), session.uname,
|
| 2214 |
defn.get("name") or "")
|
| 2215 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2216 |
-
return {"automation": _wire(defn, session.tenant, rt=session.runtime), "notes": _notes}
|
| 2217 |
|
| 2218 |
|
| 2219 |
@router.patch("/automations/{auto_id}")
|
|
@@ -2244,7 +2296,10 @@ def patch_automation(auto_id: str, body: dict = Body(default=None),
|
|
| 2244 |
# now but still the guard for any other caller of that function.
|
| 2245 |
if not _odoo_connected(session):
|
| 2246 |
raise err(404, "unknown_automation", "no automation with that id")
|
|
|
|
|
|
|
| 2247 |
return _patch_odoo_sync(session, body or {})
|
|
|
|
| 2248 |
# D-75, same channel as `create` above — an EDIT is where this matters most, because the
|
| 2249 |
# person has just typed the thing that gets dropped.
|
| 2250 |
# ⛔⛔ W36-T38 / R9 — THE AGENT-OWNED WALL, AT THE DOOR, BEFORE ANY WRITE. A `disabled`
|
|
@@ -2265,7 +2320,7 @@ def patch_automation(auto_id: str, body: dict = Body(default=None),
|
|
| 2265 |
# and refuses an id nobody can see.
|
| 2266 |
_prune_marks(session, auto_id)
|
| 2267 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2268 |
-
return {"automation": _wire(defn, session.tenant, rt=session.runtime), "notes": _notes}
|
| 2269 |
|
| 2270 |
|
| 2271 |
def _stored_agent_or_404(session, agent_id):
|
|
@@ -2447,6 +2502,7 @@ def delete_automation(auto_id: str, session: Session = Depends(_GATE)):
|
|
| 2447 |
_sys = str(_defn.get("system") or "").strip()
|
| 2448 |
if _sys:
|
| 2449 |
raise err(409, "system_agent", engine.system_agent_refusal(_sys))
|
|
|
|
| 2450 |
engine.remove(session.runtime, auto_id)
|
| 2451 |
# R9's other half: the user MAY delete an agent-authored step, and deleting the whole
|
| 2452 |
# automation takes its annotations with it rather than stranding them in the document.
|
|
@@ -2518,7 +2574,7 @@ def toggle_automation_node(auto_id: str, node_id: str, session: Session = Depend
|
|
| 2518 |
"unknown_automation" if error == "no such automation" else "node_not_toggleable",
|
| 2519 |
error)
|
| 2520 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2521 |
-
return {"automation": _wire(defn, session.tenant, rt=session.runtime)}
|
| 2522 |
|
| 2523 |
|
| 2524 |
@router.post("/automations/{auto_id}/hook/{token}")
|
|
|
|
| 61 |
_GATE = module_gate(MODULE)
|
| 62 |
|
| 63 |
|
| 64 |
+
def _item_rights(session, item):
|
| 65 |
+
"""Use the item's owner for writes, with Administrator authority and system exclusions."""
|
| 66 |
+
if item and item.get("id") == ODOO_SYNC_ID:
|
| 67 |
+
return {"edit": bool(session.admin), "delete": False}
|
| 68 |
+
editable = bool(item and session.uname and not item.get("system") and not item.get("retired")
|
| 69 |
+
and not str(item.get("id") or "").startswith(("field:", "system:"))
|
| 70 |
+
and not item.get("retiredOnceAt") and (session.admin or session.uname ==
|
| 71 |
+
(item.get("createdBy") or item.get("created_by"))))
|
| 72 |
+
return {"edit": editable, "delete": editable}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def _require_item_edit(session, item):
|
| 76 |
+
if not item:
|
| 77 |
+
raise err(404, "unknown_automation", "This item is no longer available.")
|
| 78 |
+
if not _item_rights(session, item)["edit"]:
|
| 79 |
+
raise err(403, "automation_read_only", "Your current rights do not allow changes to this item.")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _voice_item_rights(session, row, record):
|
| 83 |
+
row["capabilities"] = {**row.get("capabilities", {}), **_item_rights(session, record)}
|
| 84 |
+
return row
|
| 85 |
+
|
| 86 |
+
|
| 87 |
def _governed_query_tool(session, database=""):
|
| 88 |
"""Adapt C4 dry-runs to B's mounted C2 Query door without owning Query authority."""
|
| 89 |
def invoke(prompt, context):
|
|
|
|
| 271 |
"""
|
| 272 |
rows = voice.list_browser_dtos(session.runtime)
|
| 273 |
options = _voice_options(session) if rows else None
|
| 274 |
+
records = voice._store(session.runtime)["voice_agent"] if rows else {}
|
| 275 |
for row in rows:
|
| 276 |
row["audienceOptions"] = options
|
| 277 |
+
voice_id = row.get("automationId") or row.get("voiceAgentId")
|
| 278 |
+
_voice_item_rights(session, row, records.get(voice_id))
|
| 279 |
return {"voiceAgents": rows}
|
| 280 |
|
| 281 |
|
|
|
|
| 290 |
saved = voice.save_draft(session.runtime, body or {}, session.uname, tenant_id=session.tenant)
|
| 291 |
row = voice.get_browser_dto(session.runtime, saved["id"])
|
| 292 |
row["audienceOptions"] = _voice_options(session)
|
| 293 |
+
_voice_item_rights(session, row, saved)
|
| 294 |
return {"voiceAgent": row}
|
| 295 |
except voice.VoiceRefusal as exc:
|
| 296 |
raise err(400, "voice_refused", str(exc))
|
|
|
|
| 299 |
@router.patch("/voice-agents/{voice_id}")
|
| 300 |
def patch_voice_agent(voice_id: str, body: dict = Body(default=None), session: Session = Depends(_GATE)):
|
| 301 |
try:
|
| 302 |
+
record = voice._store(session.runtime)["voice_agent"].get(voice_id)
|
| 303 |
+
_require_item_edit(session, record)
|
| 304 |
+
saved = voice.patch_draft(session.runtime, voice_id, body or {}, session.uname)
|
| 305 |
row = voice.get_browser_dto(session.runtime, voice_id)
|
| 306 |
row["audienceOptions"] = _voice_options(session)
|
| 307 |
+
_voice_item_rights(session, row, saved)
|
| 308 |
return {"voiceAgent": row}
|
| 309 |
except voice.VoiceRefusal as exc:
|
| 310 |
raise err(400 if "unknown" not in str(exc) else 404, "voice_refused", str(exc))
|
| 311 |
|
| 312 |
|
| 313 |
+
@router.delete("/voice-agents/{voice_id}")
|
| 314 |
+
def delete_voice_agent(voice_id: str, session: Session = Depends(_GATE)):
|
| 315 |
+
"""Remove a local Voice definition and disable local routes without changing provider resources."""
|
| 316 |
+
def remove(doc):
|
| 317 |
+
_require_item_edit(session, doc["voice_agent"].get(voice_id))
|
| 318 |
+
if any(row.get("voice_agent_id") == voice_id and row.get("state") == "active"
|
| 319 |
+
and row.get("expires_at", 0) > time.time()
|
| 320 |
+
for row in doc["voice_outbound_lease"].values()):
|
| 321 |
+
raise err(409, "voice_running", "Wait for this Voice agent's active work to finish.")
|
| 322 |
+
del doc["voice_agent"][voice_id]
|
| 323 |
+
for route in doc["voice_number_route"].values():
|
| 324 |
+
if route.get("voice_agent_id") == voice_id:
|
| 325 |
+
route["status"] = "inactive"
|
| 326 |
+
return doc
|
| 327 |
+
|
| 328 |
+
voice._update(session.runtime, remove)
|
| 329 |
+
return {"deleted": voice_id}
|
| 330 |
+
|
| 331 |
+
|
| 332 |
@router.post("/voice-agents/{voice_id}/harness/dry-run")
|
| 333 |
def voice_dry_run(voice_id: str, body: dict = Body(default=None), session: Session = Depends(_GATE)):
|
| 334 |
body = body or {}
|
|
|
|
| 534 |
f"Nothing was reported as armed: {exc}")
|
| 535 |
|
| 536 |
|
| 537 |
+
def _wire(defn, tenant, rt=None, session=None):
|
| 538 |
"""One automation, as the client reads it. `running` is PROCESS state, never store state —
|
| 539 |
see the engine header on why a persisted 'running' is a permanent lock."""
|
| 540 |
live = engine.running(tenant, defn.get("id"))
|
|
|
|
| 546 |
status = {**status, "state": "running", "startedAt": live.get("startedAt"),
|
| 547 |
"step": live.get("step")}
|
| 548 |
agent_contract = engine.agent_contract_dto(rt, defn.get("id")) if rt else None
|
| 549 |
+
row = {
|
| 550 |
"id": defn.get("id"), "name": defn.get("name"), "kind": defn.get("kind"),
|
| 551 |
"config": defn.get("config") or {}, "schedule": sched, "status": status,
|
| 552 |
"runs": list(defn.get("runs") or [])[:engine.MAX_RUNS],
|
|
|
|
| 649 |
"This system Agent is configured at its source."}
|
| 650 |
}),
|
| 651 |
}
|
| 652 |
+
row["capabilities"] = {**row.get("capabilities", {}),
|
| 653 |
+
**(_item_rights(session, defn) if session else {"edit": False, "delete": False})}
|
| 654 |
+
return row
|
| 655 |
|
| 656 |
|
| 657 |
#: ⭐⭐ WAVE 34 · CONTRACTS C3 + C4 — WHY A ROW CANNOT BE DELETED, as a slug.
|
|
|
|
| 753 |
# — the exact way `awaitingResults` shipped inert for a whole wave. Passing the
|
| 754 |
# synthetic DEFINITION through the same function makes them identical by
|
| 755 |
# construction; only `system` is stamped afterwards, because no stored row has it.
|
| 756 |
+
row = _wire(defn_syn, session.tenant, rt=session.runtime, session=session)
|
| 757 |
row["system"] = SYSTEM_FIELD_AGENT
|
| 758 |
out.append(row)
|
| 759 |
return out
|
|
|
|
| 912 |
"config": {"connector": "odoo", "every": every,
|
| 913 |
"frozen": frozen}}]},
|
| 914 |
}
|
| 915 |
+
row = _wire(defn, session.tenant, rt=session.runtime, session=session)
|
| 916 |
row["system"] = SYSTEM_ODOO_SYNC
|
| 917 |
# ⛔ THE SENTENCE IS OVERRIDDEN, AND IT IS A FIX RATHER THAN A PREFERENCE. `compose_sentence`
|
| 918 |
# speaks the automation vocabulary — it builds "When <cron phrase>, run N actions" out of
|
|
|
|
| 1371 |
pass
|
| 1372 |
_STATEMENTS_SEEDED.add(session.tenant)
|
| 1373 |
defs = engine.all_definitions(session.runtime)
|
| 1374 |
+
items = [_wire(d, session.tenant, rt=session.runtime, session=session) for _, d in
|
| 1375 |
sorted(defs.items(), key=lambda kv: (kv[1].get("name") or "").lower())]
|
| 1376 |
# ⭐⭐ WAVE 34 · CONTRACT C3 (W34-T48) — field agents join the list as SYNTHETIC rows.
|
| 1377 |
# ⚠ MERGED AND RE-SORTED, not appended in a block at the end. R13 asks for a field agent to be
|
|
|
|
| 2169 |
|
| 2170 |
session.runtime.update(AGENT_ACTIONS_KEY, _set, flush="sync")
|
| 2171 |
_prune_marks(session, auto_id)
|
| 2172 |
+
return {"automation": _wire(updated, session.tenant, rt=session.runtime, session=session),
|
| 2173 |
"actionId": action_id, "agent": agent_id, "notes": notes}
|
| 2174 |
|
| 2175 |
|
|
|
|
| 2240 |
defn = engine.all_definitions(session.runtime).get(str(auto_id))
|
| 2241 |
if defn is None:
|
| 2242 |
raise err(404, "unknown_automation", "no automation with that id")
|
| 2243 |
+
return {"automation": _wire(defn, session.tenant, rt=session.runtime, session=session)}
|
| 2244 |
|
| 2245 |
|
| 2246 |
@router.post("/automations")
|
|
|
|
| 2265 |
engine.ensure_agent_contract(session.runtime, defn.get("id"), session.uname,
|
| 2266 |
defn.get("name") or "")
|
| 2267 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2268 |
+
return {"automation": _wire(defn, session.tenant, rt=session.runtime, session=session), "notes": _notes}
|
| 2269 |
|
| 2270 |
|
| 2271 |
@router.patch("/automations/{auto_id}")
|
|
|
|
| 2296 |
# now but still the guard for any other caller of that function.
|
| 2297 |
if not _odoo_connected(session):
|
| 2298 |
raise err(404, "unknown_automation", "no automation with that id")
|
| 2299 |
+
if not session.admin:
|
| 2300 |
+
raise err(403, "automation_read_only", "Only an Administrator can change the connector schedule.")
|
| 2301 |
return _patch_odoo_sync(session, body or {})
|
| 2302 |
+
_require_item_edit(session, engine.all_definitions(session.runtime).get(str(auto_id)))
|
| 2303 |
# D-75, same channel as `create` above — an EDIT is where this matters most, because the
|
| 2304 |
# person has just typed the thing that gets dropped.
|
| 2305 |
# ⛔⛔ W36-T38 / R9 — THE AGENT-OWNED WALL, AT THE DOOR, BEFORE ANY WRITE. A `disabled`
|
|
|
|
| 2320 |
# and refuses an id nobody can see.
|
| 2321 |
_prune_marks(session, auto_id)
|
| 2322 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2323 |
+
return {"automation": _wire(defn, session.tenant, rt=session.runtime, session=session), "notes": _notes}
|
| 2324 |
|
| 2325 |
|
| 2326 |
def _stored_agent_or_404(session, agent_id):
|
|
|
|
| 2502 |
_sys = str(_defn.get("system") or "").strip()
|
| 2503 |
if _sys:
|
| 2504 |
raise err(409, "system_agent", engine.system_agent_refusal(_sys))
|
| 2505 |
+
_require_item_edit(session, _defn)
|
| 2506 |
engine.remove(session.runtime, auto_id)
|
| 2507 |
# R9's other half: the user MAY delete an agent-authored step, and deleting the whole
|
| 2508 |
# automation takes its annotations with it rather than stranding them in the document.
|
|
|
|
| 2574 |
"unknown_automation" if error == "no such automation" else "node_not_toggleable",
|
| 2575 |
error)
|
| 2576 |
_sync_schedule_or_503(session.tenant, defn)
|
| 2577 |
+
return {"automation": _wire(defn, session.tenant, rt=session.runtime, session=session)}
|
| 2578 |
|
| 2579 |
|
| 2580 |
@router.post("/automations/{auto_id}/hook/{token}")
|
api/routes_competitor_watch.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Competitor watch App routes: watched terms, term detail, settings and owned alerts."""
|
| 2 |
+
from fastapi import APIRouter, Body, Depends, Query
|
| 3 |
+
|
| 4 |
+
from deps import Session, err, require_session
|
| 5 |
+
|
| 6 |
+
router = APIRouter(prefix="/api/v1/competitor-watch", tags=["competitor-watch"])
|
| 7 |
+
SOURCE = "ut_amzn_brand_analytics"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _module():
|
| 11 |
+
from modules import competitor_watch
|
| 12 |
+
return competitor_watch
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _reader(session: Session = Depends(require_session)) -> Session:
|
| 16 |
+
"""Reads need read access to the brand analytics database."""
|
| 17 |
+
from core import perm_scope
|
| 18 |
+
if not perm_scope.may_read(session.user, SOURCE, st=session.runtime):
|
| 19 |
+
raise err(403, "competitor_watch_forbidden", "Your account cannot read Amazon brand analytics.")
|
| 20 |
+
return session
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _writer(session: Session = Depends(_reader)) -> Session:
|
| 24 |
+
"""Writes also need a role that may edit."""
|
| 25 |
+
if str(session.user.get("role") or "user").strip().lower() == "viewer":
|
| 26 |
+
raise err(403, "competitor_watch_read_only", "Your account can view the competitor watch but not change it.")
|
| 27 |
+
return session
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _tenant(session):
|
| 31 |
+
return str(getattr(session.runtime, "key", "") or session.tenant)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _call(fn, *args, **kwargs):
|
| 35 |
+
try:
|
| 36 |
+
return fn(*args, **kwargs)
|
| 37 |
+
except ValueError as exc:
|
| 38 |
+
raise err(400, "competitor_watch_invalid", str(exc)) from exc
|
| 39 |
+
except KeyError as exc:
|
| 40 |
+
raise err(404, "competitor_watch_alert_not_found", "That alert does not exist.") from exc
|
| 41 |
+
except Exception as exc: # noqa: BLE001
|
| 42 |
+
raise err(503, "competitor_watch_unavailable", "The competitor watch could not be read. Try again shortly.") from exc
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# --------------------------------------------------------------------------- S78-T17 App binding
|
| 46 |
+
|
| 47 |
+
TEMPLATE_KIND = "competitor_watch"
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _app(session, app_id):
|
| 51 |
+
"""The Competitor watch App record this request names, readable by the caller (404 otherwise)."""
|
| 52 |
+
import application_registry
|
| 53 |
+
try:
|
| 54 |
+
app = application_registry.load(session.runtime)["applications"].get(str(app_id or ""))
|
| 55 |
+
except Exception as exc: # noqa: BLE001
|
| 56 |
+
raise err(503, "competitor_watch_unavailable", "The App could not be read. Try again shortly.") from exc
|
| 57 |
+
if (not isinstance(app, dict) or app.get("templateKind") != TEMPLATE_KIND or app.get("retired") is True
|
| 58 |
+
or not application_registry.may_read(app, session.uname, is_admin=session.admin)):
|
| 59 |
+
raise err(404, "competitor_watch_app_not_found", "That Competitor watch App does not exist.")
|
| 60 |
+
return app
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _may_edit(session, app):
|
| 64 |
+
import application_registry
|
| 65 |
+
role = str(session.user.get("role") or "user").strip().lower()
|
| 66 |
+
return role != "viewer" and application_registry.may_edit(app, session.uname, is_admin=session.admin)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _app_reader(app: str = Query(..., min_length=1), session: Session = Depends(_reader)) -> Session:
|
| 70 |
+
"""C4: a read names the App, and the caller must be able to read that App."""
|
| 71 |
+
_app(session, app)
|
| 72 |
+
return session
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def _app_writer(app: str = Query(..., min_length=1), session: Session = Depends(_writer)) -> Session:
|
| 76 |
+
"""C4: a write names the App, and only an editor of that App may change it."""
|
| 77 |
+
if not _may_edit(session, _app(session, app)):
|
| 78 |
+
raise err(403, "competitor_watch_read_only", "You can view this App but not change it.")
|
| 79 |
+
return session
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@router.get("/terms")
|
| 83 |
+
def list_terms(session: Session = Depends(_app_reader)):
|
| 84 |
+
cw = _module()
|
| 85 |
+
return _call(cw.terms_summary, _tenant(session), st=session.runtime)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@router.get("/terms/detail")
|
| 89 |
+
def term_detail(term: str = Query(..., min_length=1), session: Session = Depends(_app_reader)):
|
| 90 |
+
cw = _module()
|
| 91 |
+
return _call(cw.term_detail, _tenant(session), term, st=session.runtime)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@router.post("/terms")
|
| 95 |
+
def add_terms(terms: list[str] = Body(..., embed=True), session: Session = Depends(_app_writer)):
|
| 96 |
+
cw = _module()
|
| 97 |
+
return {"terms": _call(cw.add_terms, _tenant(session), terms, st=session.runtime)}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@router.post("/terms/remove")
|
| 101 |
+
def remove_terms(terms: list[str] = Body(..., embed=True), session: Session = Depends(_app_writer)):
|
| 102 |
+
cw = _module()
|
| 103 |
+
return {"terms": _call(cw.remove_terms, _tenant(session), terms, st=session.runtime)}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@router.get("/settings")
|
| 107 |
+
def read_settings(session: Session = Depends(_app_reader)):
|
| 108 |
+
cw = _module()
|
| 109 |
+
return _call(cw.settings, _tenant(session), st=session.runtime)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@router.patch("/settings")
|
| 113 |
+
def write_settings(undercutPct: float | None = Body(None), rankSurgePct: float | None = Body(None),
|
| 114 |
+
defaultOwner: str | None = Body(None), session: Session = Depends(_app_writer)):
|
| 115 |
+
cw = _module()
|
| 116 |
+
return _call(cw.set_settings, _tenant(session), undercut_pct=undercutPct, rank_surge_pct=rankSurgePct,
|
| 117 |
+
default_owner=defaultOwner, st=session.runtime)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@router.get("/alerts")
|
| 121 |
+
def list_alerts(app: str = Query(..., min_length=1), status: str | None = Query(None),
|
| 122 |
+
session: Session = Depends(_app_reader)):
|
| 123 |
+
cw = _module()
|
| 124 |
+
listed = _call(cw.alerts, _tenant(session), status=status, st=session.runtime)
|
| 125 |
+
return {**listed, "canEdit": _may_edit(session, _app(session, app))}
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
@router.patch("/alerts/{alert_id}")
|
| 129 |
+
def update_alert(alert_id: str, owner: str | None = Body(None), status: str | None = Body(None),
|
| 130 |
+
due: str | None = Body(None), session: Session = Depends(_app_writer)):
|
| 131 |
+
cw = _module()
|
| 132 |
+
return _call(cw.update_alert, _tenant(session), alert_id, owner=owner, status=status, due=due,
|
| 133 |
+
user=session.uname or "unknown", st=session.runtime)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def _week_end(start):
|
| 137 |
+
import datetime as dt
|
| 138 |
+
try:
|
| 139 |
+
return (dt.date.fromisoformat(str(start)[:10]) + dt.timedelta(days=6)).isoformat()
|
| 140 |
+
except ValueError:
|
| 141 |
+
return str(start)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def _not_ready_message(cw, periods):
|
| 145 |
+
"""The plain sentence naming when comparison starts (one week held, or none)."""
|
| 146 |
+
import datetime as dt
|
| 147 |
+
if len(periods) == 1:
|
| 148 |
+
try:
|
| 149 |
+
due = dt.date.fromisoformat(str(periods[0])[:10]) + dt.timedelta(days=14)
|
| 150 |
+
return f"Comparison starts after the next weekly pull, about {due.strftime('%b')} {due.day}."
|
| 151 |
+
except ValueError:
|
| 152 |
+
pass
|
| 153 |
+
return cw.NOT_READY
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def screen_payload(cw, detail, terms, term, can_edit):
|
| 157 |
+
"""Project `term_detail` to the screen: shares in percent points, previous values, causes, trend."""
|
| 158 |
+
periods = list(detail.get("periods") or ())
|
| 159 |
+
top3 = list(detail.get("top3") or ())
|
| 160 |
+
shares = [abs(v) for row in top3 for v in (row.get("clickShare"), row.get("conversionShare"))
|
| 161 |
+
if isinstance(v, (int, float))]
|
| 162 |
+
shares += [abs(v) for row in detail.get("trend") or () for v in row.get("clickShare") or ()
|
| 163 |
+
if isinstance(v, (int, float))]
|
| 164 |
+
# Unknown: whether Amazon sends shares as fractions or percent. All values at or under 1 read as fractions.
|
| 165 |
+
scale = 100.0 if shares and max(shares) <= 1 else 1.0
|
| 166 |
+
|
| 167 |
+
def pts(value):
|
| 168 |
+
return None if not isinstance(value, (int, float)) else round(value * scale, 4)
|
| 169 |
+
|
| 170 |
+
def previous(value, delta):
|
| 171 |
+
return None if value is None or not isinstance(delta, (int, float)) else round(value - delta * scale, 4)
|
| 172 |
+
rows = []
|
| 173 |
+
for row in top3:
|
| 174 |
+
change = row.get("change") or {}
|
| 175 |
+
click, conv = pts(row.get("clickShare")), pts(row.get("conversionShare"))
|
| 176 |
+
causes = [str(row[key]) for key in ("priceCause", "rankCause") if row.get(key)]
|
| 177 |
+
rows.append({"rank": row.get("rank"), "asin": row.get("asin"), "ours": row.get("isOurs") is True,
|
| 178 |
+
"clickShare": click, "conversionShare": conv,
|
| 179 |
+
"previousRank": change.get("previousRank"),
|
| 180 |
+
"previousClickShare": previous(click, change.get("clickShareDelta")),
|
| 181 |
+
"previousConversionShare": previous(conv, change.get("conversionShareDelta")),
|
| 182 |
+
"cause": " ".join(causes) or None})
|
| 183 |
+
# One line per ASIN in the newest top 3, plus our ASIN when it fell out (at most four lines).
|
| 184 |
+
shown = [row["asin"] for row in rows]
|
| 185 |
+
ours = [row for row in detail.get("trend") or () if row.get("isOurs") and row.get("asin") not in shown]
|
| 186 |
+
wanted = shown + [row["asin"] for row in ours[:1]]
|
| 187 |
+
by_asin = {row.get("asin"): row for row in detail.get("trend") or ()}
|
| 188 |
+
trend = []
|
| 189 |
+
for asin in wanted:
|
| 190 |
+
row = by_asin.get(asin) or {}
|
| 191 |
+
values = list(row.get("clickShare") or ())
|
| 192 |
+
points = [{"start": period, "clickShare": pts(values[i] if i < len(values) else None)}
|
| 193 |
+
for i, period in enumerate(periods)]
|
| 194 |
+
trend.append({"asin": asin, "ours": row.get("isOurs") is True,
|
| 195 |
+
"points": sorted(points, key=lambda p: p["start"])})
|
| 196 |
+
comparison = detail.get("comparison") or {}
|
| 197 |
+
ready = comparison.get("ready") is True
|
| 198 |
+
return {"canEdit": bool(can_edit), "terms": list(terms), "term": term,
|
| 199 |
+
"week": {"start": periods[0], "end": _week_end(periods[0])} if periods else None,
|
| 200 |
+
"previousWeek": {"start": periods[1], "end": _week_end(periods[1])} if len(periods) > 1 else None,
|
| 201 |
+
"comparisonReady": ready,
|
| 202 |
+
"comparisonMessage": "" if ready else _not_ready_message(cw, periods),
|
| 203 |
+
"top3": rows, "trend": trend, "problems": []}
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
@router.get("/screen")
|
| 207 |
+
def app_screen(app: str = Query(..., min_length=1), term: str | None = Query(None),
|
| 208 |
+
session: Session = Depends(_app_reader)):
|
| 209 |
+
"""S78-T17: the Terms view in one read (watched terms, the picked term's top 3 and trend)."""
|
| 210 |
+
cw = _module()
|
| 211 |
+
tenant = _tenant(session)
|
| 212 |
+
record = _app(session, app)
|
| 213 |
+
terms = [row["term"] for row in _call(cw.terms_summary, tenant, st=session.runtime)["terms"]]
|
| 214 |
+
picked = " ".join(str(term or "").split()).lower()
|
| 215 |
+
picked = picked if picked in terms else (terms[0] if terms else None)
|
| 216 |
+
detail = _call(cw.term_detail, tenant, picked, st=session.runtime) if picked else {}
|
| 217 |
+
return screen_payload(cw, detail, terms, picked, _may_edit(session, record))
|
api/routes_connectors.py
CHANGED
|
@@ -225,6 +225,34 @@ _ODOO_DETAIL_CATALOG_LOCK = threading.Lock()
|
|
| 225 |
_ODOO_DETAIL_CATALOG_CACHE_SIZE = 16
|
| 226 |
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
class _ConnectorDetailReads:
|
| 229 |
"""Request-local copies only; credentials, grants and enablements never outlive a read.
|
| 230 |
|
|
@@ -1939,7 +1967,7 @@ def _odoo_current_stages(stages, last_success):
|
|
| 1939 |
return projected
|
| 1940 |
|
| 1941 |
|
| 1942 |
-
def odoo_health_receipt(rt, connection_id=None, *, now=None):
|
| 1943 |
"""Project the truthful Odoo receipt for one connection and each of its databases."""
|
| 1944 |
cid = _odoo_connection_slot(rt, connection_id)
|
| 1945 |
try:
|
|
@@ -1958,7 +1986,7 @@ def odoo_health_receipt(rt, connection_id=None, *, now=None):
|
|
| 1958 |
if isinstance(value, dict)}
|
| 1959 |
|
| 1960 |
# Durable materialization receipts are the authority for database work and survive restarts.
|
| 1961 |
-
for job in connector_sync.jobs(rt, "odoo", cid):
|
| 1962 |
key = str(job.get("databaseKey") or "")
|
| 1963 |
if not key:
|
| 1964 |
continue
|
|
@@ -2098,13 +2126,14 @@ def _sync_payload(connector_key, connection_id, row=None):
|
|
| 2098 |
if row is None:
|
| 2099 |
return _safe_connector_status(connector_key, {"provider": connector_key, "label": meta["label"],
|
| 2100 |
"connectionId": slot, "recorded": False, "state": None,
|
| 2101 |
-
"at": None, "stateAt": None, "from": None,
|
| 2102 |
"detail": "no sync has been recorded", "errors": [], "errorsTotal": 0,
|
| 2103 |
"databases": [], "retryable": False, "retryHint": meta["retryHint"]})
|
| 2104 |
answer = dict(row)
|
| 2105 |
answer.update({"provider": connector_key, "connectionId": slot,
|
| 2106 |
"label": meta["label"], "recorded": True,
|
| 2107 |
-
"stateAt": row.get("at"), "
|
|
|
|
| 2108 |
return _safe_connector_status(connector_key, answer)
|
| 2109 |
|
| 2110 |
|
|
@@ -2747,18 +2776,45 @@ def _odoo_field_key(rt, database_key, model, raw_name, connection_id=...):
|
|
| 2747 |
"odoo", connection_id, database_key, model, raw_name)
|
| 2748 |
|
| 2749 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2750 |
@router.get("/connectors/odoo/status")
|
| 2751 |
def odoo_status(connection_id: str = None, session: Session = Depends(require_session)):
|
| 2752 |
_odoo_admin(session)
|
| 2753 |
import odoo_relational
|
| 2754 |
-
|
| 2755 |
-
|
| 2756 |
-
|
|
|
|
| 2757 |
errors = stored.get("errors") if isinstance(stored.get("errors"), list) else []
|
| 2758 |
phase = str(stored.get("phase") or "unknown")
|
| 2759 |
-
lifecycle = connector_sync.read(
|
| 2760 |
-
jobs = _connection_jobs(
|
| 2761 |
-
receipt = odoo_health_receipt(
|
|
|
|
| 2762 |
last_success = _odoo_latest_success(receipt.get("lastSuccess"),
|
| 2763 |
*(job.get("at") for job in jobs if job.get("state") == "ready"))
|
| 2764 |
current_jobs = [job for job in jobs if job.get("state") != "failed"
|
|
@@ -2782,7 +2838,7 @@ def odoo_status(connection_id: str = None, session: Session = Depends(require_se
|
|
| 2782 |
"health": "paused" if cfg.get("frozen") else durable["state"],
|
| 2783 |
"lastRun": durable["at"], "frozen": cfg.get("frozen") is True,
|
| 2784 |
**durable, "stateAt": durable["at"], "connectionId": resolved_connection,
|
| 2785 |
-
"receipt": receipt}
|
| 2786 |
# The old keys stay the old default-connection answer. A caller that names a connection gets
|
| 2787 |
# that slot's lifecycle instead of the tenant-wide legacy document that caused the C6 defect.
|
| 2788 |
named = connection_id is not None
|
|
@@ -2815,30 +2871,44 @@ def odoo_status(connection_id: str = None, session: Session = Depends(require_se
|
|
| 2815 |
"errorsTotal": int(errors_total or 0),
|
| 2816 |
"connectionId": (lifecycle or {}).get("connectionId")
|
| 2817 |
or resolved_connection,
|
| 2818 |
-
"receipt": receipt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2819 |
|
| 2820 |
|
| 2821 |
def _daily_completed_sync(rt, connector_key):
|
| 2822 |
"""Read completed mirror markers; never create a mirror or contact a provider."""
|
| 2823 |
if connector_key == "amazon_sp":
|
| 2824 |
-
|
| 2825 |
-
try:
|
| 2826 |
-
states = cx.sync_state(rt, rt.key)
|
| 2827 |
-
except cx.AmazonStatusSafetyError as refused:
|
| 2828 |
-
states = dict(getattr(refused, "safe_status", None) or {})
|
| 2829 |
-
except Exception:
|
| 2830 |
-
return None, False
|
| 2831 |
-
if not isinstance(states, dict):
|
| 2832 |
-
return None, False
|
| 2833 |
-
rows = [row for row in states.values() if isinstance(row, dict) and not row.get("declared")]
|
| 2834 |
-
stamps = [_sync_stamp(row.get("lastSuccess")) for row in rows]
|
| 2835 |
-
stamps = [stamp for stamp in stamps if stamp]
|
| 2836 |
-
if stamps:
|
| 2837 |
-
return max(stamps, key=datetime.fromisoformat), True
|
| 2838 |
-
# This is the existing reader's explicit unreadable-mirror outcome, not a never-run fact.
|
| 2839 |
-
unknown = any(str(row.get("unknownCause") or "").startswith(
|
| 2840 |
-
"This container has no readable sync status.") for row in rows)
|
| 2841 |
-
return None, bool(rows) and not unknown
|
| 2842 |
if connector_key == "meta_ads":
|
| 2843 |
from pathlib import Path
|
| 2844 |
from harness import datastore
|
|
@@ -2865,6 +2935,15 @@ def _daily_completed_sync(rt, connector_key):
|
|
| 2865 |
return None, False
|
| 2866 |
|
| 2867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2868 |
@router.get("/connectors/{connector_key}/sync")
|
| 2869 |
def connector_sync_status(connector_key: str, connection_id: str = None,
|
| 2870 |
session: Session = Depends(require_session)):
|
|
@@ -2872,12 +2951,24 @@ def connector_sync_status(connector_key: str, connection_id: str = None,
|
|
| 2872 |
if connector_key not in registered:
|
| 2873 |
raise err(404, "sync_provider_unknown",
|
| 2874 |
f"{connector_key} is not registered for connector sync")
|
| 2875 |
-
|
| 2876 |
-
|
| 2877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2878 |
if connector_key in ("amazon_sp", "meta_ads"):
|
| 2879 |
-
|
| 2880 |
-
|
|
|
|
|
|
|
|
|
|
| 2881 |
lastCompletedKnown=known, canSync=bool(session.admin))
|
| 2882 |
return _safe_connector_status(connector_key, answer)
|
| 2883 |
|
|
|
|
| 225 |
_ODOO_DETAIL_CATALOG_CACHE_SIZE = 16
|
| 226 |
|
| 227 |
|
| 228 |
+
class _ConnectorStatusReads:
|
| 229 |
+
"""Lend immutable documents from one request, batching fresh snapshots when supported."""
|
| 230 |
+
|
| 231 |
+
def __init__(self, runtime, names=()):
|
| 232 |
+
self.runtime = runtime
|
| 233 |
+
self.documents = {}
|
| 234 |
+
self.snapshots = set()
|
| 235 |
+
reader = getattr(runtime, "get_many", None)
|
| 236 |
+
if names and callable(reader):
|
| 237 |
+
try:
|
| 238 |
+
documents = reader(names)
|
| 239 |
+
except Exception: # noqa: BLE001
|
| 240 |
+
documents = None
|
| 241 |
+
if isinstance(documents, dict):
|
| 242 |
+
self.documents.update(documents)
|
| 243 |
+
self.snapshots.update(documents)
|
| 244 |
+
|
| 245 |
+
def get(self, name, fresh=False):
|
| 246 |
+
if fresh and name not in self.snapshots:
|
| 247 |
+
return self.runtime.get(name, fresh=True)
|
| 248 |
+
if name not in self.documents:
|
| 249 |
+
self.documents[name] = self.runtime.get(name)
|
| 250 |
+
return self.documents[name]
|
| 251 |
+
|
| 252 |
+
def __getattr__(self, name):
|
| 253 |
+
return getattr(self.runtime, name)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
class _ConnectorDetailReads:
|
| 257 |
"""Request-local copies only; credentials, grants and enablements never outlive a read.
|
| 258 |
|
|
|
|
| 1967 |
return projected
|
| 1968 |
|
| 1969 |
|
| 1970 |
+
def odoo_health_receipt(rt, connection_id=None, *, now=None, jobs=None):
|
| 1971 |
"""Project the truthful Odoo receipt for one connection and each of its databases."""
|
| 1972 |
cid = _odoo_connection_slot(rt, connection_id)
|
| 1973 |
try:
|
|
|
|
| 1986 |
if isinstance(value, dict)}
|
| 1987 |
|
| 1988 |
# Durable materialization receipts are the authority for database work and survive restarts.
|
| 1989 |
+
for job in (connector_sync.jobs(rt, "odoo", cid) if jobs is None else jobs):
|
| 1990 |
key = str(job.get("databaseKey") or "")
|
| 1991 |
if not key:
|
| 1992 |
continue
|
|
|
|
| 2126 |
if row is None:
|
| 2127 |
return _safe_connector_status(connector_key, {"provider": connector_key, "label": meta["label"],
|
| 2128 |
"connectionId": slot, "recorded": False, "state": None,
|
| 2129 |
+
"at": None, "stateAt": None, "lastSyncAt": None, "from": None,
|
| 2130 |
"detail": "no sync has been recorded", "errors": [], "errorsTotal": 0,
|
| 2131 |
"databases": [], "retryable": False, "retryHint": meta["retryHint"]})
|
| 2132 |
answer = dict(row)
|
| 2133 |
answer.update({"provider": connector_key, "connectionId": slot,
|
| 2134 |
"label": meta["label"], "recorded": True,
|
| 2135 |
+
"stateAt": row.get("at"), "lastSyncAt": None,
|
| 2136 |
+
"retryHint": meta["retryHint"]})
|
| 2137 |
return _safe_connector_status(connector_key, answer)
|
| 2138 |
|
| 2139 |
|
|
|
|
| 2776 |
"odoo", connection_id, database_key, model, raw_name)
|
| 2777 |
|
| 2778 |
|
| 2779 |
+
def _odoo_last_sync_at(rt, receipt, connection_id=None):
|
| 2780 |
+
"""Select successful markers only for databases the connection's detail roster discloses."""
|
| 2781 |
+
import odoo_relational
|
| 2782 |
+
requested = str(connection_id or "").strip()
|
| 2783 |
+
try:
|
| 2784 |
+
keys = {str(row.get("key") or "") for row in odoo_relational.grid_choices(
|
| 2785 |
+
rt, None if requested in ("", "default") else requested)}
|
| 2786 |
+
except Exception: # noqa: BLE001
|
| 2787 |
+
return None
|
| 2788 |
+
stamps = [_sync_stamp(row.get("lastSuccess"))
|
| 2789 |
+
for row in receipt.get("databases", ())
|
| 2790 |
+
if isinstance(row, dict) and row.get("databaseKey") in keys]
|
| 2791 |
+
return max((stamp for stamp in stamps if stamp), key=datetime.fromisoformat, default=None)
|
| 2792 |
+
|
| 2793 |
+
|
| 2794 |
+
def _odoo_status_reads(rt):
|
| 2795 |
+
"""Capture only connection, roster and receipt documents through the tenant's store seam."""
|
| 2796 |
+
from core import keychain
|
| 2797 |
+
import odoo_relational
|
| 2798 |
+
return _ConnectorStatusReads(rt, (
|
| 2799 |
+
keychain.KEY, keychain.SCOPE_KEY, odoo_relational.CONFIG_KEY,
|
| 2800 |
+
odoo_relational.MODEL_CATALOG_KEY, connector_sync.JOBS_KEY, connector_sync.STATE_KEY,
|
| 2801 |
+
_ODOO_HEALTH_STATUS, _DATABASE_DISPATCH_STATUS, _ODOO_SYNC_STATUS))
|
| 2802 |
+
|
| 2803 |
+
|
| 2804 |
@router.get("/connectors/odoo/status")
|
| 2805 |
def odoo_status(connection_id: str = None, session: Session = Depends(require_session)):
|
| 2806 |
_odoo_admin(session)
|
| 2807 |
import odoo_relational
|
| 2808 |
+
rt = _odoo_status_reads(session.runtime)
|
| 2809 |
+
resolved_connection = _odoo_connection_slot(rt, connection_id)
|
| 2810 |
+
cfg = odoo_relational.read_config(rt)
|
| 2811 |
+
stored = rt.get(_ODOO_SYNC_STATUS) or {}
|
| 2812 |
errors = stored.get("errors") if isinstance(stored.get("errors"), list) else []
|
| 2813 |
phase = str(stored.get("phase") or "unknown")
|
| 2814 |
+
lifecycle = connector_sync.read(rt, "odoo", resolved_connection)
|
| 2815 |
+
jobs = _connection_jobs(rt, "odoo", resolved_connection)
|
| 2816 |
+
receipt = odoo_health_receipt(rt, resolved_connection, jobs=jobs)
|
| 2817 |
+
last_sync_at = _odoo_last_sync_at(rt, receipt, connection_id)
|
| 2818 |
last_success = _odoo_latest_success(receipt.get("lastSuccess"),
|
| 2819 |
*(job.get("at") for job in jobs if job.get("state") == "ready"))
|
| 2820 |
current_jobs = [job for job in jobs if job.get("state") != "failed"
|
|
|
|
| 2838 |
"health": "paused" if cfg.get("frozen") else durable["state"],
|
| 2839 |
"lastRun": durable["at"], "frozen": cfg.get("frozen") is True,
|
| 2840 |
**durable, "stateAt": durable["at"], "connectionId": resolved_connection,
|
| 2841 |
+
"receipt": receipt, "lastSyncAt": last_sync_at}
|
| 2842 |
# The old keys stay the old default-connection answer. A caller that names a connection gets
|
| 2843 |
# that slot's lifecycle instead of the tenant-wide legacy document that caused the C6 defect.
|
| 2844 |
named = connection_id is not None
|
|
|
|
| 2871 |
"errorsTotal": int(errors_total or 0),
|
| 2872 |
"connectionId": (lifecycle or {}).get("connectionId")
|
| 2873 |
or resolved_connection,
|
| 2874 |
+
"receipt": receipt, "lastSyncAt": last_sync_at}
|
| 2875 |
+
|
| 2876 |
+
|
| 2877 |
+
def _amazon_completed_sync(rt):
|
| 2878 |
+
"""Project completion markers without counting business rows or building database detail."""
|
| 2879 |
+
import duckdb
|
| 2880 |
+
from harness import amazon_store as az, datastore
|
| 2881 |
+
|
| 2882 |
+
def read(cursor):
|
| 2883 |
+
rows = cursor.execute("SELECT entity, phase, updated_at FROM _sync_state "
|
| 2884 |
+
"WHERE starts_with(entity, ?)", [az.PREFIX]).fetchall()
|
| 2885 |
+
markers = {str(entity): (phase, _sync_stamp(at)) for entity, phase, at in rows}
|
| 2886 |
+
stamps = []
|
| 2887 |
+
for table in az.TABLES:
|
| 2888 |
+
landed = markers.get(az.entity_key(table), (None, None))
|
| 2889 |
+
watermark = markers.get(az.entity_key(az.WATERMARK_PREFIX + table), (None, None))
|
| 2890 |
+
# A refused pass also closes its tick. Only its saved watermark proves success.
|
| 2891 |
+
candidates = ((watermark,) if landed[0] in ("refused", "failed", "throttled") else
|
| 2892 |
+
(landed, markers.get(az.entity_key("sync_tick_" + table), (None, None)), watermark))
|
| 2893 |
+
stamps.extend(at for phase, at in candidates if phase == "done" and at)
|
| 2894 |
+
return (max(stamps, key=datetime.fromisoformat) if stamps else None), True
|
| 2895 |
+
|
| 2896 |
+
try:
|
| 2897 |
+
path = az._tenant_mirror_path(rt.key, rt=rt)
|
| 2898 |
+
if not path.is_file():
|
| 2899 |
+
return None, False
|
| 2900 |
+
if path.resolve() == Path(datastore.DB_PATH).resolve():
|
| 2901 |
+
return read(datastore.ro_cursor())
|
| 2902 |
+
with duckdb.connect(str(path), read_only=True) as cursor:
|
| 2903 |
+
return read(cursor)
|
| 2904 |
+
except Exception:
|
| 2905 |
+
return None, False
|
| 2906 |
|
| 2907 |
|
| 2908 |
def _daily_completed_sync(rt, connector_key):
|
| 2909 |
"""Read completed mirror markers; never create a mirror or contact a provider."""
|
| 2910 |
if connector_key == "amazon_sp":
|
| 2911 |
+
return _amazon_completed_sync(rt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2912 |
if connector_key == "meta_ads":
|
| 2913 |
from pathlib import Path
|
| 2914 |
from harness import datastore
|
|
|
|
| 2935 |
return None, False
|
| 2936 |
|
| 2937 |
|
| 2938 |
+
@router.get("/connectors/{connector_key}/sync-capability")
|
| 2939 |
+
def connector_sync_capability(connector_key: str, session: Session = Depends(require_session)):
|
| 2940 |
+
"""Declare registry-backed sync support without reading a tenant's database catalogue."""
|
| 2941 |
+
supported = connector_key in connector_sync.providers()
|
| 2942 |
+
return {"provider": connector_key, "supported": supported,
|
| 2943 |
+
"syncNow": ({"provider": connector_key, "path": f"/connectors/{connector_key}/sync/now"}
|
| 2944 |
+
if supported and session.admin else None)}
|
| 2945 |
+
|
| 2946 |
+
|
| 2947 |
@router.get("/connectors/{connector_key}/sync")
|
| 2948 |
def connector_sync_status(connector_key: str, connection_id: str = None,
|
| 2949 |
session: Session = Depends(require_session)):
|
|
|
|
| 2951 |
if connector_key not in registered:
|
| 2952 |
raise err(404, "sync_provider_unknown",
|
| 2953 |
f"{connector_key} is not registered for connector sync")
|
| 2954 |
+
rt = (_odoo_status_reads(session.runtime) if connector_key == "odoo"
|
| 2955 |
+
else _ConnectorStatusReads(session.runtime, (connector_sync.JOBS_KEY, connector_sync.STATE_KEY)
|
| 2956 |
+
if connector_key == "amazon_sp" else ()))
|
| 2957 |
+
resolved_connection = (_odoo_connection_slot(rt, connection_id)
|
| 2958 |
+
if connector_key == "odoo" else connection_id)
|
| 2959 |
+
jobs = _connection_jobs(rt, connector_key, resolved_connection)
|
| 2960 |
+
row = (connector_sync.job_summary(jobs)
|
| 2961 |
+
or connector_sync.read(rt, connector_key, resolved_connection))
|
| 2962 |
+
answer = _sync_payload(connector_key, resolved_connection, row)
|
| 2963 |
+
if connector_key == "odoo":
|
| 2964 |
+
receipt = odoo_health_receipt(rt, resolved_connection, jobs=jobs)
|
| 2965 |
+
answer["lastSyncAt"] = _odoo_last_sync_at(rt, receipt, connection_id)
|
| 2966 |
if connector_key in ("amazon_sp", "meta_ads"):
|
| 2967 |
+
if connector_key == "amazon_sp" and (str(resolved_connection or "").strip() or "default") != "default":
|
| 2968 |
+
completed, known = None, False
|
| 2969 |
+
else:
|
| 2970 |
+
completed, known = _daily_completed_sync(rt, connector_key)
|
| 2971 |
+
answer.update(schedule="daily", lastCompletedAt=completed, lastSyncAt=completed,
|
| 2972 |
lastCompletedKnown=known, canSync=bool(session.admin))
|
| 2973 |
return _safe_connector_status(connector_key, answer)
|
| 2974 |
|
api/routes_external.py
CHANGED
|
@@ -46,7 +46,6 @@ _MCP_FIXES = {
|
|
| 46 |
"query_filter_limit": "split it into separate queries",
|
| 47 |
"query_cursor_stale": "run the same query again without a cursor",
|
| 48 |
}
|
| 49 |
-
_RETIRED_MCP_TOOLS = {"data.describe_database": ("data", "describe_database")}
|
| 50 |
_VIEW_DRAFT_CONFIG_KEYS = frozenset({
|
| 51 |
"filters", "filterConj", "sorts", "groupBy", "visible", "order", "display",
|
| 52 |
"memberPids",
|
|
@@ -80,7 +79,13 @@ def _json_digest(value) -> str:
|
|
| 80 |
return hashlib.sha256(encoded.encode("utf-8")).hexdigest()
|
| 81 |
|
| 82 |
|
| 83 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
try:
|
| 85 |
encoded = json.dumps(value, sort_keys=True, separators=(",", ":"),
|
| 86 |
ensure_ascii=True, allow_nan=False)
|
|
@@ -88,8 +93,7 @@ def _canonical_json(value) -> bytes:
|
|
| 88 |
raise err(400, "tool_arguments_invalid",
|
| 89 |
"The tool arguments contain a value that cannot be represented safely.") from exc
|
| 90 |
raw = encoded.encode("utf-8")
|
| 91 |
-
|
| 92 |
-
if len(raw) > _CONFIRMATION_MAX_BYTES and not code_transport_fits(value, _CONFIRMATION_MAX_BYTES):
|
| 93 |
raise err(413, "confirmation_too_large",
|
| 94 |
"This change is too large for one preview. Split it into smaller changes.")
|
| 95 |
if _contains_credential(encoded):
|
|
@@ -156,10 +160,8 @@ class _PreviewRuntime:
|
|
| 156 |
def _load(self, name):
|
| 157 |
key = str(name)
|
| 158 |
if key not in self._docs:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
except TypeError:
|
| 162 |
-
value = self._real.get(key)
|
| 163 |
value = copy.deepcopy(value)
|
| 164 |
self._initial[key] = copy.deepcopy(value)
|
| 165 |
self._docs[key] = value
|
|
@@ -174,6 +176,9 @@ class _PreviewRuntime:
|
|
| 174 |
def get(self, name, *args, **kwargs):
|
| 175 |
return copy.deepcopy(self._load(name))
|
| 176 |
|
|
|
|
|
|
|
|
|
|
| 177 |
def get_projection(self, name, drop=()):
|
| 178 |
value = self.get(name)
|
| 179 |
if isinstance(value, dict):
|
|
@@ -344,7 +349,9 @@ class _ConfirmationRuntime:
|
|
| 344 |
|
| 345 |
|
| 346 |
def _diff_documents(documents, *, limit=250):
|
|
|
|
| 347 |
changes = []
|
|
|
|
| 348 |
|
| 349 |
def walk(before, after, path):
|
| 350 |
if len(changes) >= limit or before == after:
|
|
@@ -372,6 +379,70 @@ def _diff_documents(documents, *, limit=250):
|
|
| 372 |
return changes, len(changes) >= limit
|
| 373 |
|
| 374 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
def _database_preview_documents(session, before, after):
|
| 376 |
"""Project changed records and fields through the caller's current database permissions."""
|
| 377 |
import routes_tables
|
|
@@ -468,17 +539,34 @@ def _credential_binding(session: Session) -> str:
|
|
| 468 |
return str(claims.get("external_credential_id") or claims.get("external_key_id") or "")
|
| 469 |
|
| 470 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
def _confirmation_token(payload) -> str:
|
| 472 |
-
|
| 473 |
-
if payload.get("tool") == "apps":
|
| 474 |
-
from tool_groups.apps import pending_arguments
|
| 475 |
-
wire_payload["arguments"] = pending_arguments(payload["arguments"], encode=True)
|
| 476 |
-
raw = _canonical_json(wire_payload)
|
| 477 |
signature = aios_session._sign(b"external-confirm:v1:" + raw)
|
| 478 |
return f"{_CONFIRMATION_PREFIX}.{_b64(raw)}.{signature}"
|
| 479 |
|
| 480 |
|
| 481 |
-
def _read_confirmation_token(token) -> dict:
|
| 482 |
if not isinstance(token, str) or len(token) > _CONFIRMATION_MAX_BYTES * 2:
|
| 483 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
| 484 |
try:
|
|
@@ -490,8 +578,35 @@ def _read_confirmation_token(token) -> dict:
|
|
| 490 |
payload = json.loads(raw.decode("utf-8"))
|
| 491 |
except (ValueError, TypeError, UnicodeError, json.JSONDecodeError) as exc:
|
| 492 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.") from exc
|
| 493 |
-
if not isinstance(payload, dict) or payload.get("v")
|
| 494 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
if payload.get("tool") == "apps":
|
| 496 |
from tool_groups.apps import pending_arguments
|
| 497 |
payload["arguments"] = pending_arguments(payload["arguments"], encode=False)
|
|
@@ -726,7 +841,7 @@ def _assert_query_filter_bounds(raw) -> None:
|
|
| 726 |
if "where" in node:
|
| 727 |
stack.append((node["where"], 1))
|
| 728 |
continue
|
| 729 |
-
children = node.get("children")
|
| 730 |
if not isinstance(children, list):
|
| 731 |
continue
|
| 732 |
if len(children) > _QUERY_MAX_FILTER_SIBLINGS:
|
|
@@ -738,7 +853,7 @@ def _assert_query_filter_bounds(raw) -> None:
|
|
| 738 |
|
| 739 |
def _assert_tool_query_filter_bounds(name, arguments):
|
| 740 |
"""Name query limits before the finite tool schema reports a generic shape error."""
|
| 741 |
-
if name
|
| 742 |
name, arguments = "query_database", arguments.get("arguments")
|
| 743 |
if name == "query_database" and isinstance(arguments, dict):
|
| 744 |
_assert_query_filter_bounds(arguments.get("filters"))
|
|
@@ -1029,7 +1144,12 @@ def _assert_write_scope(session, tool_name, arguments=None):
|
|
| 1029 |
_, args = tool_groups.resolve(tool_name, arguments)
|
| 1030 |
_assert_workbook_scope(session, args["workbook_id"])
|
| 1031 |
elif not external_access.may_write(access):
|
| 1032 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
code = _test_environment_refusal(session)
|
| 1034 |
if code:
|
| 1035 |
raise err(403, code, MCP_RESULT_CODES[code])
|
|
@@ -2884,6 +3004,63 @@ def _validate_personal_view_draft(session: Session, database: str, raw: dict) ->
|
|
| 2884 |
session, database, fields=[], filters=None if filters == [] else filters)
|
| 2885 |
|
| 2886 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2887 |
def _view_share_preview(session: Session, args: dict):
|
| 2888 |
"""Build C8's audience/field-inheritance plan without changing a View or any grant."""
|
| 2889 |
import routes_shares
|
|
@@ -2985,6 +3162,8 @@ def _view_event(session, kind, args):
|
|
| 2985 |
if kind == "view_upsert":
|
| 2986 |
# Resolve the ordinary View/database wall before the additional T31 draft validation so
|
| 2987 |
# the established refusal vocabulary and connector record-scope guard stay intact.
|
|
|
|
|
|
|
| 2988 |
_validate_personal_view_draft(session, database, raw)
|
| 2989 |
existing = ctx.table.find_view(view_id, prefer_owner=session.uname)
|
| 2990 |
if (existing and str(existing[0]).strip().lower()
|
|
@@ -3123,12 +3302,12 @@ def _bucket_revision(st, key, fallback):
|
|
| 3123 |
return {"rev": None, "token": _json_digest(fallback)}
|
| 3124 |
|
| 3125 |
|
| 3126 |
-
def _database_value_plan(session, database, record_id, values, fields):
|
| 3127 |
"""Apply the grid's field/value vocabulary without calling its event side effects."""
|
| 3128 |
from core import grid_events, user_tables
|
| 3129 |
import aios_grid
|
| 3130 |
|
| 3131 |
-
if not isinstance(values, dict) or not values:
|
| 3132 |
raise err(400, "tool_arguments_invalid", "Record values must be a non-empty object.")
|
| 3133 |
field_by_key = {str(field.get("key")): field for field in fields if isinstance(field, dict)}
|
| 3134 |
unknown = sorted(str(key) for key in values if str(key) not in field_by_key)
|
|
@@ -3136,8 +3315,7 @@ def _database_value_plan(session, database, record_id, values, fields):
|
|
| 3136 |
raise err(403, "field_unavailable",
|
| 3137 |
"Every changed field must be readable and editable in this database.")
|
| 3138 |
ctx = _view_context(session, database, {"pid": record_id})
|
| 3139 |
-
|
| 3140 |
-
for key, raw in values.items():
|
| 3141 |
key = str(key)
|
| 3142 |
definition = field_by_key[key]
|
| 3143 |
if key in (ctx.hidden_keys or frozenset()) or not grid_events._may_edit_field_value(
|
|
@@ -3146,6 +3324,26 @@ def _database_value_plan(session, database, record_id, values, fields):
|
|
| 3146 |
"Every changed field must be readable and editable in this database.")
|
| 3147 |
if user_tables.is_computed_cell(definition) or definition.get("type") == "automation":
|
| 3148 |
raise err(403, "computed_field", "A computed or automation field is read only.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3149 |
why = user_tables.cell_type_refusal(definition, raw)
|
| 3150 |
if why:
|
| 3151 |
raise err(400, "bad_value", why)
|
|
@@ -3153,8 +3351,15 @@ def _database_value_plan(session, database, record_id, values, fields):
|
|
| 3153 |
bag = definition.get("link") if isinstance(definition.get("link"), dict) else {}
|
| 3154 |
if bag.get("on") or bag.get("inverse"):
|
| 3155 |
raise err(403, "computed_field", "A derived link field is read only.")
|
| 3156 |
-
|
| 3157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3158 |
raw_ids = raw if isinstance(raw, (list, tuple, set)) else str(raw or "").split(",")
|
| 3159 |
selected = []
|
| 3160 |
for item in raw_ids:
|
|
@@ -3163,6 +3368,9 @@ def _database_value_plan(session, database, record_id, values, fields):
|
|
| 3163 |
continue
|
| 3164 |
if candidate not in available or len(selected) >= user_tables.LINK_MAX_IDS:
|
| 3165 |
raise err(400, "bad_value", "A linked record is unavailable.")
|
|
|
|
|
|
|
|
|
|
| 3166 |
selected.append(candidate)
|
| 3167 |
if bag.get("single"):
|
| 3168 |
break
|
|
@@ -3235,9 +3443,8 @@ def _plan_database_write(session, args):
|
|
| 3235 |
rows = table.get("rows") or {}
|
| 3236 |
if action == "record_create":
|
| 3237 |
raw_values = args.get("values")
|
| 3238 |
-
|
| 3239 |
-
|
| 3240 |
-
_database_value_plan(session, database, None, raw_values, g["fields"]))
|
| 3241 |
record_id = str(max((int(key) for key in rows if str(key).isdigit()), default=0) + 1)
|
| 3242 |
before, after = None, values
|
| 3243 |
else:
|
|
@@ -3266,16 +3473,8 @@ def _plan_database_write(session, args):
|
|
| 3266 |
else:
|
| 3267 |
try:
|
| 3268 |
if action == "record_create":
|
| 3269 |
-
# The grid's core insert rules: defaults, server-owned numbers, creation order.
|
| 3270 |
-
after = user_tables._new_row_values(target.get("fields") or [], after)
|
| 3271 |
-
if after is None:
|
| 3272 |
-
raise err(400, "bad_value", "A field default is not valid for its field.")
|
| 3273 |
-
if not after:
|
| 3274 |
-
raise err(400, "tool_arguments_invalid", "Record values must be a non-empty object.")
|
| 3275 |
user_tables.allocate_autonumbers(target, after)
|
| 3276 |
user_tables._stamp_row_creation(target, record_id)
|
| 3277 |
-
user_tables._validate_link_selection_values(
|
| 3278 |
-
target.get("fields") or [], values, bucket_after)
|
| 3279 |
except ValueError as exc:
|
| 3280 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300])
|
| 3281 |
target.setdefault("rows", {})[record_id] = copy.deepcopy(after)
|
|
@@ -3416,7 +3615,7 @@ def _execute_database_object(session, args, *, plan=None):
|
|
| 3416 |
flush="sync", expect_rev=expected_rev)
|
| 3417 |
except (view_audit.ViewRevisionStale, view_audit.RevisionUnverifiable) as exc:
|
| 3418 |
raise err(409, "confirmation_stale",
|
| 3419 |
-
"The database changed after
|
| 3420 |
return {"database": database, **copy.deepcopy(plan.get("planned") or {}),
|
| 3421 |
"affected": copy.deepcopy(plan.get("affected") or {})}
|
| 3422 |
|
|
@@ -3864,7 +4063,24 @@ def _build_purpose(value):
|
|
| 3864 |
return wiki_log.purpose_line(value)
|
| 3865 |
|
| 3866 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3867 |
def _build_about(value):
|
|
|
|
| 3868 |
missing = ["about." + key for key in ("title", "description", "tags")
|
| 3869 |
if not isinstance(value, dict) or key not in value]
|
| 3870 |
example = {"about": {"title": "Weekly sales review", "description": "Review weekly sales before the team meeting.",
|
|
@@ -3905,7 +4121,9 @@ def _build_about(value):
|
|
| 3905 |
else "[unknown]") for key in value if key not in rules]
|
| 3906 |
invalid = invalid or ["about"]
|
| 3907 |
details = {"missing": missing, "invalid": invalid, "example": example}
|
| 3908 |
-
message = ("Send about with title, description and tags." if
|
|
|
|
|
|
|
| 3909 |
else "Fix " + ", ".join(invalid) + ".")
|
| 3910 |
if not isinstance(value, dict) or "about.tags" in invalid or "about.tags" in missing:
|
| 3911 |
details["allowed"] = tags
|
|
@@ -3932,7 +4150,7 @@ def _external_build_kind(name, arguments):
|
|
| 3932 |
return "skill"
|
| 3933 |
return "file"
|
| 3934 |
return {"views": "view", "apps": "app", "sheet": "workbook", "automations": "automation",
|
| 3935 |
-
"schedules": "schedule", "data": "database", "requests": "request"}.get(name, name)
|
| 3936 |
|
| 3937 |
|
| 3938 |
def _brain_build_arguments(session, name, arguments, about):
|
|
@@ -3958,6 +4176,10 @@ _CATALOG_BUILD_RESULT_FIELDS = (
|
|
| 3958 |
"app_id", "catalog_id", "productCount", "pageCount", "pages", "deepLink", "pdfLink", "message")
|
| 3959 |
|
| 3960 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3961 |
def _preview_external_write(session, name, arguments, *, issue_token=True, execution=None, purpose=None, about=None,
|
| 3962 |
intent=None):
|
| 3963 |
from core import write_execution
|
|
@@ -3974,7 +4196,7 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 3974 |
about = _build_about(about)
|
| 3975 |
build_kind = _external_build_kind(name, arguments)
|
| 3976 |
environment = _write_environment()
|
| 3977 |
-
normalised = json.loads(_canonical_json(arguments).decode("utf-8"))
|
| 3978 |
_assert_tool_database_scope(session, name, normalised)
|
| 3979 |
|
| 3980 |
preview_runtime = _PreviewRuntime(session.runtime)
|
|
@@ -3983,12 +4205,18 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 3983 |
claims=copy.deepcopy(session.claims), runtime=preview_runtime)
|
| 3984 |
execution = execution or {'seed': uuid.uuid4().hex, 'timestamp': time.time()}
|
| 3985 |
with write_execution.replay(execution):
|
| 3986 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3987 |
if isinstance(result, dict) and result.get("refused"):
|
| 3988 |
raise err(409, "write_partial_refused",
|
| 3989 |
"Part of this change could not be accepted. Nothing was previewed.")
|
| 3990 |
|
| 3991 |
-
|
|
|
|
| 3992 |
if plan is not None:
|
| 3993 |
diff = copy.deepcopy(plan.get("diff") or [])
|
| 3994 |
warnings = list(plan.get("warnings") or [])
|
|
@@ -3996,13 +4224,13 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 3996 |
current_revision = copy.deepcopy(plan.get("revision") or {})
|
| 3997 |
else:
|
| 3998 |
documents = preview_runtime.changed()
|
| 3999 |
-
if name == "requests" and normalised.get("action") in {"answer", "complete"}:
|
| 4000 |
projected = _request_preview_documents(session, normalised["arguments"]["id"], documents)
|
| 4001 |
else:
|
| 4002 |
projected = _public_preview_documents(session, documents)
|
| 4003 |
-
diff
|
| 4004 |
-
|
| 4005 |
-
|
| 4006 |
affected = _affected_for(name, normalised, diff, result)
|
| 4007 |
current_revision = {
|
| 4008 |
key: revision for key, revision in preview_runtime.base_revisions().items()
|
|
@@ -4022,9 +4250,16 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 4022 |
{"user": row["user"], "role": row["role"]}
|
| 4023 |
for row in result["current_entries"]]
|
| 4024 |
|
| 4025 |
-
_canonical_json(diff)
|
| 4026 |
diff_digest = _json_digest(diff)
|
| 4027 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4028 |
expires_at = int(time.time()) + _CONFIRMATION_TTL_SECONDS
|
| 4029 |
payload = {
|
| 4030 |
"v": 1, "tenant": session.tenant, "actor": session.uname,
|
|
@@ -4042,6 +4277,9 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 4042 |
}
|
| 4043 |
if purpose is not None:
|
| 4044 |
payload.update(purpose=purpose, purpose_digest=_json_digest(purpose))
|
|
|
|
|
|
|
|
|
|
| 4045 |
payload["operation_id"] = hashlib.sha256(payload["nonce"].encode("utf-8")).hexdigest()
|
| 4046 |
if (name == "sheet" and normalised.get("action") == "refresh_scope"
|
| 4047 |
and _key_scope(session)[0] == external_access.ACCESS_REFRESH):
|
|
@@ -4050,17 +4288,25 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu
|
|
| 4050 |
"operation_id": payload["operation_id"], "state": "not_committed",
|
| 4051 |
"preview": True, "mutated": False, "tool": name,
|
| 4052 |
"environment": environment,
|
| 4053 |
-
"diff":
|
| 4054 |
"affected": affected, "warnings": warnings,
|
| 4055 |
"current_revision": current_revision,
|
| 4056 |
"expires_at": expires_at, "requires_confirmation": True,
|
| 4057 |
"strong_confirmation_required": payload["strong"],
|
| 4058 |
"about": copy.deepcopy(about), "buildKind": build_kind,
|
| 4059 |
}
|
|
|
|
|
|
|
| 4060 |
if purpose is not None:
|
| 4061 |
response["purpose"] = purpose
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4062 |
if name == "views" and normalised.get("action") == "share":
|
| 4063 |
response.update({key: copy.deepcopy(result[key]) for key in ("view", "audience")})
|
|
|
|
|
|
|
| 4064 |
if payload["strong"]:
|
| 4065 |
response["confirmation_phrase"] = _CONFIRMATION_PHRASE
|
| 4066 |
if issue_token:
|
|
@@ -4083,15 +4329,49 @@ def _refresh_authority(session):
|
|
| 4083 |
session.claims.get("external_workbooks", [])])
|
| 4084 |
|
| 4085 |
|
| 4086 |
-
def _prune_operations(raw):
|
|
|
|
| 4087 |
doc = raw if isinstance(raw, dict) else {}
|
| 4088 |
now = time.time()
|
| 4089 |
-
|
| 4090 |
-
|
| 4091 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4092 |
return doc
|
| 4093 |
|
| 4094 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4095 |
def _store_operation(session, payload, preview, staged, intent=None):
|
| 4096 |
"""Keep the preview identity outside every affected object's revision."""
|
| 4097 |
operation_id = payload["operation_id"]
|
|
@@ -4101,6 +4381,8 @@ def _store_operation(session, payload, preview, staged, intent=None):
|
|
| 4101 |
"databases": _databases_in_arguments(payload["arguments"]),
|
| 4102 |
"documents": {},
|
| 4103 |
}
|
|
|
|
|
|
|
| 4104 |
if payload["tool"] in {"app_upsert", "app_delete"}:
|
| 4105 |
import application_registry as applications
|
| 4106 |
app_id = payload["arguments"].get("app_id") or next(iter(preview["affected"].get("ids") or []), None)
|
|
@@ -4119,17 +4401,38 @@ def _store_operation(session, payload, preview, staged, intent=None):
|
|
| 4119 |
if isinstance(intent, str):
|
| 4120 |
# Intent stays outside the signed payload, redacted, so confirm_change can reuse it.
|
| 4121 |
item["intent"] = audit.redact_intent(intent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4122 |
def save(raw):
|
| 4123 |
-
|
|
|
|
| 4124 |
if operation_id in doc["claims"]:
|
| 4125 |
-
raise err(409, "confirmation_stale", "
|
| 4126 |
stored_item = copy.deepcopy(item)
|
| 4127 |
-
if
|
| 4128 |
-
|
| 4129 |
-
|
|
|
|
| 4130 |
doc["claims"][operation_id] = stored_item
|
| 4131 |
return doc
|
| 4132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4133 |
|
| 4134 |
|
| 4135 |
def _operation_access(session, item, *, confirming=False):
|
|
@@ -4173,7 +4476,10 @@ def _operation_access(session, item, *, confirming=False):
|
|
| 4173 |
"Ask the owner to restore access before checking again.")
|
| 4174 |
for database in set(access["databases"]):
|
| 4175 |
_assert_database_scope(session, database)
|
| 4176 |
-
|
|
|
|
|
|
|
|
|
|
| 4177 |
if name == "database_object":
|
| 4178 |
import routes_tables
|
| 4179 |
g = routes_tables.ut_write_ctx(session, args["database"])
|
|
@@ -4250,6 +4556,19 @@ def _load_operation(session, operation_id, *, confirming=False):
|
|
| 4250 |
if (not isinstance(item, dict) or "payload" not in item
|
| 4251 |
or item.get("retain_until", 0) < time.time()):
|
| 4252 |
raise err(404, "operation_unavailable", _OPERATION_UNAVAILABLE_MESSAGE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4253 |
if item["payload"]["tool"] == "apps":
|
| 4254 |
from tool_groups.apps import pending_arguments
|
| 4255 |
item = copy.deepcopy(item)
|
|
@@ -4258,6 +4577,20 @@ def _load_operation(session, operation_id, *, confirming=False):
|
|
| 4258 |
|
| 4259 |
|
| 4260 |
def _operation_result(operation_id, item):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4261 |
if item.get("status") == "committed":
|
| 4262 |
return copy.deepcopy(item["result"])
|
| 4263 |
pending = item.get("status") == "claimed"
|
|
@@ -4269,6 +4602,11 @@ def _operation_result(operation_id, item):
|
|
| 4269 |
|
| 4270 |
def _operation_status(session, args):
|
| 4271 |
"""Read a retained result without changing any object or operation revision."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4272 |
_, item = _load_operation(session, args["operation_id"])
|
| 4273 |
return _operation_result(args["operation_id"], item)
|
| 4274 |
|
|
@@ -4305,6 +4643,7 @@ def _confirmation_status(session, operation_id, status, result=None):
|
|
| 4305 |
def _committed_build_evidence(session, writer, arguments, *, build_kind=None):
|
| 4306 |
"""Resolve identities from committed documents and this writer's native inserts."""
|
| 4307 |
import application_registry
|
|
|
|
| 4308 |
import automation_engine
|
| 4309 |
import intelligence_store as files
|
| 4310 |
import wiki_log
|
|
@@ -4343,6 +4682,11 @@ def _committed_build_evidence(session, writer, arguments, *, build_kind=None):
|
|
| 4343 |
for identity in before.keys() | after.keys():
|
| 4344 |
if before.get(identity) != after.get(identity):
|
| 4345 |
add("schedule", identity)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4346 |
elif key in (application_registry._KEY, automation_engine.STORE_KEY):
|
| 4347 |
kind = "app" if key == application_registry._KEY else "automation"
|
| 4348 |
if kind == "app":
|
|
@@ -4361,11 +4705,66 @@ def _committed_build_evidence(session, writer, arguments, *, build_kind=None):
|
|
| 4361 |
return [targets[key] for key in sorted(targets)], sorted(set(native_ids))
|
| 4362 |
|
| 4363 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4364 |
def _confirm_change(session, args):
|
| 4365 |
from core import view_audit, write_execution
|
| 4366 |
|
| 4367 |
external_tools.validate_arguments(args, external_tools.TOOLS["confirm_change"]["inputSchema"])
|
| 4368 |
-
payload = _read_confirmation_token(args.get("confirmation_token"))
|
| 4369 |
if any(payload.get(key) != value for key, value in _operation_bindings(session).items()):
|
| 4370 |
raise err(403, "confirmation_principal_changed",
|
| 4371 |
"This confirmation belongs to another account or connection.")
|
|
@@ -4377,7 +4776,7 @@ def _confirm_change(session, args):
|
|
| 4377 |
return _operation_result(operation_id, stored)
|
| 4378 |
_assert_write_scope(fresh_session, payload.get("tool"), payload.get("arguments"))
|
| 4379 |
if payload.get("environment") != _write_environment():
|
| 4380 |
-
raise err(409, "confirmation_stale", "The environment changed
|
| 4381 |
if int(payload.get("expiry") or 0) <= int(time.time()):
|
| 4382 |
raise err(409, "confirmation_expired",
|
| 4383 |
"This confirmation token expired. Preview the change again.")
|
|
@@ -4390,12 +4789,13 @@ def _confirm_change(session, args):
|
|
| 4390 |
execution = payload.get('execution')
|
| 4391 |
if (not isinstance(execution, dict) or not isinstance(execution.get('seed'), str)
|
| 4392 |
or not execution['seed'] or type(execution.get('timestamp')) not in (int, float)):
|
| 4393 |
-
raise err(409, 'confirmation_stale',
|
| 4394 |
-
'This preview is no longer current. Preview the change again.')
|
| 4395 |
try:
|
| 4396 |
_confirmation_claim(fresh_session, operation_id)
|
| 4397 |
except _OperationAlreadyClaimed:
|
| 4398 |
return _operation_status(fresh_session, {"operation_id": operation_id})
|
|
|
|
|
|
|
| 4399 |
try:
|
| 4400 |
recomputed = _preview_external_write(
|
| 4401 |
fresh_session, tool, arguments, issue_token=False, execution=execution,
|
|
@@ -4405,10 +4805,19 @@ def _confirm_change(session, args):
|
|
| 4405 |
or payload.get("environment") != current.get("environment")
|
| 4406 |
or payload.get("revision_digest") != current.get("revision_digest")
|
| 4407 |
or payload.get("diff_digest") != current.get("diff_digest")
|
|
|
|
|
|
|
| 4408 |
or any((key in payload, payload.get(key)) != (key in current, current.get(key))
|
| 4409 |
for key in ("purpose", "purpose_digest", "about", "about_digest", "buildKind"))):
|
| 4410 |
raise err(409, "confirmation_stale",
|
| 4411 |
-
"The data, permissions, or resulting diff changed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4412 |
if tool == "database_object":
|
| 4413 |
result = _execute_database_object(
|
| 4414 |
fresh_session, arguments, plan=recomputed.get("_plan"))
|
|
@@ -4425,10 +4834,16 @@ def _confirm_change(session, args):
|
|
| 4425 |
raise err(409, "write_partial_refused",
|
| 4426 |
"Part of the confirmed change could not be accepted.")
|
| 4427 |
except (view_audit.ViewRevisionStale, view_audit.RevisionUnverifiable) as exc:
|
|
|
|
|
|
|
|
|
|
| 4428 |
_confirmation_status(fresh_session, operation_id, "failed")
|
| 4429 |
raise err(409, "confirmation_stale",
|
| 4430 |
-
"The data changed while
|
| 4431 |
except Exception:
|
|
|
|
|
|
|
|
|
|
| 4432 |
_confirmation_status(fresh_session, operation_id, "failed")
|
| 4433 |
raise
|
| 4434 |
before_revision = copy.deepcopy(recomputed.get("current_revision") or {})
|
|
@@ -4470,6 +4885,8 @@ def _confirm_change(session, args):
|
|
| 4470 |
if tool == "apps" and arguments.get("action") == "catalog_build":
|
| 4471 |
response.update({key: copy.deepcopy(result[key]) for key in _CATALOG_BUILD_RESULT_FIELDS})
|
| 4472 |
_confirmation_status(fresh_session, operation_id, "committed", response)
|
|
|
|
|
|
|
| 4473 |
if tool != "database_object" and receipt.get("persisted") is True:
|
| 4474 |
try:
|
| 4475 |
targets, native_ids = _committed_build_evidence(fresh_session, guarded_session.runtime, arguments,
|
|
@@ -4559,17 +4976,36 @@ _MCP_CODE_GROUPS = (
|
|
| 4559 |
("Check the View's sharing settings before changing it.", """
|
| 4560 |
view_is_shared view_not_personal share_owner_redundant share_role_unknown share_who_required
|
| 4561 |
"""),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4562 |
)
|
| 4563 |
MCP_RESULT_CODES = {
|
| 4564 |
code: reason for reason, codes in _MCP_CODE_GROUPS for code in codes.split()
|
| 4565 |
}
|
| 4566 |
MCP_RESULT_CODES.update({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4567 |
"app_rule": "Correct the App rule named in the refusal before saving it.",
|
| 4568 |
"audience_not_allowed": "A person must choose sharing with everyone on screen.",
|
| 4569 |
"not_owner": "You cannot administer this item's audience.",
|
| 4570 |
"unknown_people": "Choose active people in this workspace.",
|
| 4571 |
"grant_exceeds_role": "You cannot grant a role above your own access.",
|
| 4572 |
"revoke_not_yours": "You can remove only people you added.",
|
|
|
|
| 4573 |
"key_refresh_only": "This connection can only refresh source cells in its granted workbooks.",
|
| 4574 |
"key_workbook_out_of_scope": "This connection cannot refresh this workbook.",
|
| 4575 |
"external_workbooks_invalid": "Choose up to 100 workbook ids. All workbooks is not a valid choice.",
|
|
@@ -4587,11 +5023,15 @@ MCP_RESULT_CODES.update({
|
|
| 4587 |
"operation_pending": "The change is still being saved. Ask again shortly.",
|
| 4588 |
"operation_not_committed": "This change has not been committed.",
|
| 4589 |
"operation_unavailable": _OPERATION_UNAVAILABLE_MESSAGE,
|
|
|
|
|
|
|
| 4590 |
"credential_in_arguments": "Remove credential values from the arguments.",
|
| 4591 |
-
"build_about_required": "Send about with title, description of 20 to 240 characters, and tags from the guide
|
|
|
|
| 4592 |
"build_about_invalid": ("Fix the fields in invalid. Use a nonempty title, a description of 20 to 240 characters, "
|
| 4593 |
"and tags from allowed, each once: " + ", ".join(external_tools.BUILD_TAGS) + "."),
|
| 4594 |
-
"call_intent_required": "Send intent explaining why you are making this write
|
|
|
|
| 4595 |
"call_intent_invalid": "Send intent as a string of 1 to 500 characters. See example.",
|
| 4596 |
"invalid_external_credential": "The credential is invalid, expired, or revoked.",
|
| 4597 |
"credential_in_output": "The result contained a credential value and was withheld.",
|
|
@@ -4693,10 +5133,10 @@ _MCP_RESULT_OPAQUE = frozenset({
|
|
| 4693 |
"revision_before", "revision_after", "filters", "sorts", "config", "binding", "bindings",
|
| 4694 |
})
|
| 4695 |
_MCP_RESULT_IDENTIFIERS = frozenset({
|
| 4696 |
-
"resourceUri", "deepLink", "pdfLink", "src", "websiteUrl", "fileId", "link", "href",
|
| 4697 |
"id", "ids", "operation_id", "key", "keys", "pid", "pids", "database", "databases", "sourceDatabase",
|
| 4698 |
"compatibleDatabases", "field", "fields", "field_key", "colId", "metric_key", "groupBy",
|
| 4699 |
-
"workbook_id", "worksheet", "tab", "view_id", "app_id", "chart_id", "record_id", "contextId",
|
| 4700 |
"path", "paths", "address", "addresses", "range", "cursor", "next_cursor", "token",
|
| 4701 |
"source_version", "code", "tool", "action", "person", "agent", "owner", "author", "from", "to",
|
| 4702 |
"username", "tenant", "createdBy", "updatedBy", "by", "byName", "reads", "approvers", "tags", "updated",
|
|
@@ -4854,6 +5294,26 @@ def _mcp_protocol_error(rid, code, message):
|
|
| 4854 |
return {"jsonrpc": "2.0", "id": rid, "error": {"code": code, "message": out["reason"], "data": out}}
|
| 4855 |
|
| 4856 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4857 |
def _mcp_protocol_result(rid, data):
|
| 4858 |
out = _shape_mcp_result({"ok": True, "data": data})
|
| 4859 |
return {"jsonrpc": "2.0", "id": rid, "result": {
|
|
@@ -4867,18 +5327,17 @@ def _mcp_protocol_result(rid, data):
|
|
| 4867 |
# `loopable_guide` tool (every client can call a tool). ⛔ Model-facing text is user-facing copy:
|
| 4868 |
# no em or en dashes here.
|
| 4869 |
MCP_GUIDE_TOOL = "loopable_guide"
|
| 4870 |
-
|
| 4871 |
-
"description": "State why you are making this call. Required for every write, including confirmation."}
|
| 4872 |
MCP_INSTRUCTIONS = "\n".join([
|
| 4873 |
-
"1. Loopable is a governed business data workspace: databases, saved Views, Sheet workbooks and "
|
| 4874 |
"Apps. Every call runs as the signed-in user, with that user's own permissions. "
|
| 4875 |
"Call loopable_guide for the group index. Call it with group to read that group's actions and schemas. "
|
| 4876 |
"Read the App build guide with apps.guide before building. "
|
| 4877 |
"A build that breaks a required rule is refused with its fix.",
|
| 4878 |
-
"2. Discover before you query: use
|
| 4879 |
"need. Use only field keys that describe_database returns. Never guess a field name. "
|
| 4880 |
"State the age of data older than one day before giving a number.",
|
| 4881 |
-
"3. Read with
|
| 4882 |
"and follow the cursor for more pages instead of reading everything. Report totals from the "
|
| 4883 |
"counts the tool returns, not from the rows you happened to read. "
|
| 4884 |
"If scheduled results are unread, read schedules.inbox before answering.",
|
|
@@ -4890,12 +5349,18 @@ MCP_INSTRUCTIONS = "\n".join([
|
|
| 4890 |
"intelligence.create_wiki publishes a new wiki/ page at once without preview, confirmation or owner approval. "
|
| 4891 |
"It cannot replace a committed page. Existing wiki edits use intelligence.propose with confirmation and file approval. "
|
| 4892 |
"Send intent beside action and arguments on every write, including confirm_change. "
|
| 4893 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4894 |
"5. A refusal (ok: false) names its code and reason. Tell the user the reason. Do not retry "
|
| 4895 |
"the same call with a wider scope.",
|
| 4896 |
"6. Never ask for, print or save a credential.",
|
| 4897 |
"7. " + external_tools.CELL_FIGURE_RULE,
|
| 4898 |
-
"8. Report a limitation or missing field with
|
| 4899 |
"and changes no workspace data. Give the tool, action, code and your question in plain words. "
|
| 4900 |
"Omit records, cell values, filters and credentials. Refused calls are logged automatically. "
|
| 4901 |
"To include your question with a call, set params._meta['loopable/question'] to at most 500 characters.",
|
|
@@ -4913,7 +5378,8 @@ _MCP_GUIDE_SPEC = {
|
|
| 4913 |
"name": MCP_GUIDE_TOOL,
|
| 4914 |
"description": "Read workspace connections, your skills, databases and the group index, "
|
| 4915 |
"or choose a group for its actions and argument schemas.",
|
| 4916 |
-
"inputSchema": {"type": "object", "properties": {"
|
|
|
|
| 4917 |
"additionalProperties": False},
|
| 4918 |
"annotations": {"title": "Loopable guide", "readOnlyHint": True,
|
| 4919 |
"destructiveHint": False, "openWorldHint": False},
|
|
@@ -5059,8 +5525,79 @@ def _mcp_database_actions(session, rows):
|
|
| 5059 |
return allowed
|
| 5060 |
|
| 5061 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5062 |
def _mcp_capabilities(session, groups, rows):
|
| 5063 |
-
"""Report
|
| 5064 |
import tool_groups
|
| 5065 |
database_actions = _mcp_database_actions(session, rows)
|
| 5066 |
environment_refusal = _test_environment_refusal(session)
|
|
@@ -5074,44 +5611,52 @@ def _mcp_capabilities(session, groups, rows):
|
|
| 5074 |
row = {"group": group, "action": action, "state": "available", "requires": ""}
|
| 5075 |
legacy = entry.get("legacy_name", "")
|
| 5076 |
required = []
|
| 5077 |
-
|
| 5078 |
-
|
| 5079 |
-
|
| 5080 |
-
grants =
|
| 5081 |
-
|
| 5082 |
-
|
| 5083 |
-
|
| 5084 |
-
|
| 5085 |
-
|
| 5086 |
-
|
| 5087 |
-
|
| 5088 |
-
|
| 5089 |
-
|
| 5090 |
-
|
| 5091 |
-
|
| 5092 |
-
|
| 5093 |
-
|
| 5094 |
-
|
| 5095 |
-
|
| 5096 |
-
|
| 5097 |
-
|
| 5098 |
-
|
| 5099 |
-
|
| 5100 |
-
|
| 5101 |
-
|
| 5102 |
-
|
| 5103 |
-
|
| 5104 |
-
|
| 5105 |
-
|
| 5106 |
-
|
| 5107 |
-
|
| 5108 |
-
|
| 5109 |
-
|
| 5110 |
-
|
| 5111 |
-
|
| 5112 |
-
|
| 5113 |
-
|
| 5114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5115 |
result.append(row)
|
| 5116 |
return result
|
| 5117 |
|
|
@@ -5192,8 +5737,16 @@ def _mcp_guide(session: Session, group=None) -> str:
|
|
| 5192 |
"""Return the group index or one group's action schemas without reading records."""
|
| 5193 |
import tool_groups
|
| 5194 |
groups = tool_groups.groups()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5195 |
if group is not None and group not in groups:
|
| 5196 |
-
raise sheets.SheetRefusal("tool_not_found", "
|
|
|
|
| 5197 |
# One rows-free definitions read per guide: a live runtime pays one pool checkout per database.
|
| 5198 |
# The guide reads schemas only, so no reader here needs `rows`. On a failed read, keep the old path.
|
| 5199 |
try:
|
|
@@ -5237,14 +5790,24 @@ def _mcp_guide(session: Session, group=None) -> str:
|
|
| 5237 |
"Previous tool names remain callable for one release, but are absent from tools/list.", ""]
|
| 5238 |
lines += ["Available means this action is supported with your current grants. "
|
| 5239 |
"Choose a database and fields you can access. Workbook, record and personal View permissions "
|
| 5240 |
-
"still apply to each target. Changes still need their stated confirmation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5241 |
if group is not None:
|
| 5242 |
selected = groups[group]
|
| 5243 |
lines += ["## " + _mcp_plain(selected["title"], "Actions"), "",
|
| 5244 |
_mcp_plain(selected["guide"], "Choose an action below."), ""]
|
| 5245 |
-
if
|
|
|
|
|
|
|
| 5246 |
from tool_groups.data import query_guide
|
| 5247 |
lines += [query_guide(rows), ""]
|
|
|
|
|
|
|
|
|
|
| 5248 |
states = {row["action"]: row for row in capabilities if row["group"] == group}
|
| 5249 |
uses_filter_node = False
|
| 5250 |
for action, entry in selected["actions"].items():
|
|
@@ -5279,7 +5842,7 @@ def _mcp_guide(session: Session, group=None) -> str:
|
|
| 5279 |
lines.append(f"- `{name}`: {_mcp_plain(entry['title'], 'Actions')}. "
|
| 5280 |
+ _mcp_plain(entry['guide'], "Read this group's guide for actions."))
|
| 5281 |
lines += ["", "Use confirm_change to apply an agreed preview. Keep its operation_id. "
|
| 5282 |
-
"Repeating a confirmation returns its first result. Call
|
| 5283 |
"and arguments {operation_id: the id} to check whether it committed. Results remain available "
|
| 5284 |
"for at least 24 hours while your access remains valid. A retry result means the change is still being saved.",
|
| 5285 |
"", "## Your databases", "",
|
|
@@ -5300,19 +5863,27 @@ def _mcp_tool_specs() -> list[dict]:
|
|
| 5300 |
import tool_groups
|
| 5301 |
confirm = external_tools.TOOLS["confirm_change"]
|
| 5302 |
specs = [copy.deepcopy(_MCP_GUIDE_SPEC), *tool_groups.tool_specs(), {
|
| 5303 |
-
"name": "confirm_change", "description": confirm["description"],
|
| 5304 |
"inputSchema": copy.deepcopy(confirm["inputSchema"]),
|
| 5305 |
"annotations": {"readOnlyHint": False, "destructiveHint": True, "openWorldHint": True},
|
| 5306 |
}]
|
| 5307 |
-
|
|
|
|
| 5308 |
for spec in specs:
|
| 5309 |
spec["inputSchema"]["properties"]["intent"] = copy.deepcopy(_MCP_INTENT_SCHEMA)
|
|
|
|
| 5310 |
if spec["name"] == "confirm_change":
|
| 5311 |
spec["inputSchema"]["required"] = [*spec["inputSchema"].get("required", []), "intent"]
|
| 5312 |
if spec["name"] in group_names:
|
| 5313 |
import wiki_log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5314 |
spec["inputSchema"]["properties"]["purpose"] = copy.deepcopy(wiki_log.PURPOSE_SCHEMA)
|
| 5315 |
spec["inputSchema"]["properties"]["about"] = copy.deepcopy(external_tools.ABOUT_SCHEMA)
|
|
|
|
| 5316 |
if spec["name"] == "sheet":
|
| 5317 |
from tool_groups.sheet import WORKBOOK_RESOURCE_URI
|
| 5318 |
spec["_meta"] = {"ui": {"resourceUri": WORKBOOK_RESOURCE_URI}}
|
|
@@ -5342,12 +5913,6 @@ def _dispatch_external_tool(ctx, name, arguments, *, about=None):
|
|
| 5342 |
import tool_groups
|
| 5343 |
resolved = tool_groups.resolve(name, arguments)
|
| 5344 |
if resolved is None:
|
| 5345 |
-
if isinstance(name, str) and name in _RETIRED_MCP_TOOLS:
|
| 5346 |
-
current, action = _RETIRED_MCP_TOOLS[name]
|
| 5347 |
-
raise sheets.SheetRefusal(
|
| 5348 |
-
_mcp_public_code(None, 404),
|
| 5349 |
-
f"That flat tool name is retired. Call tool {current} with action "
|
| 5350 |
-
f"{action} and arguments containing database set to a visible database key.")
|
| 5351 |
raise sheets.SheetRefusal("tool_not_found", "The requested tool is not available.")
|
| 5352 |
entry, args = resolved
|
| 5353 |
if call is not None and call["tool"] == name:
|
|
@@ -5430,7 +5995,8 @@ def _handle_mcp(req, session: Session, base_url: str = ""):
|
|
| 5430 |
def _mcp_limitation_address(tool, arguments):
|
| 5431 |
"""Resolve registry identities before validation while preserving audit alias attribution."""
|
| 5432 |
import tool_groups
|
| 5433 |
-
|
|
|
|
| 5434 |
action = arguments.get("action") if isinstance(arguments, dict) else None
|
| 5435 |
if group and isinstance(action, str):
|
| 5436 |
for trusted_action in group["actions"]:
|
|
@@ -5451,22 +6017,27 @@ def _mcp_audit_schema(name, arguments):
|
|
| 5451 |
schema = {"properties": {"intent": _MCP_INTENT_SCHEMA}}
|
| 5452 |
if not isinstance(name, str):
|
| 5453 |
return "unknown", schema
|
|
|
|
|
|
|
| 5454 |
if name == MCP_GUIDE_TOOL:
|
| 5455 |
schema = copy.deepcopy(_MCP_GUIDE_SPEC["inputSchema"])
|
| 5456 |
elif name in external_tools.TOOLS:
|
| 5457 |
schema = copy.deepcopy(external_tools.TOOLS[name]["inputSchema"])
|
| 5458 |
elif name in tool_groups.groups():
|
| 5459 |
actions = tool_groups.groups()[name]["actions"]
|
| 5460 |
-
action =
|
| 5461 |
selected = actions.get(action) if isinstance(action, str) else None
|
| 5462 |
-
|
| 5463 |
-
"
|
|
|
|
|
|
|
|
|
|
| 5464 |
else:
|
| 5465 |
return "unknown", schema
|
| 5466 |
schema["properties"]["intent"] = _MCP_INTENT_SCHEMA
|
| 5467 |
if name not in {MCP_GUIDE_TOOL, "confirm_change"}:
|
| 5468 |
schema["properties"].update(about=external_tools.ABOUT_SCHEMA, purpose={})
|
| 5469 |
-
return
|
| 5470 |
|
| 5471 |
|
| 5472 |
def _mcp_call_intent(arguments):
|
|
@@ -5483,7 +6054,7 @@ def _mcp_call_intent(arguments):
|
|
| 5483 |
def _mcp_saved_intent(session, arguments):
|
| 5484 |
"""Return the intent saved with the preview the signed token names, or None."""
|
| 5485 |
try:
|
| 5486 |
-
payload = _read_confirmation_token(arguments.get("confirmation_token"))
|
| 5487 |
_, stored = _load_operation(session, payload.get("operation_id", ""), confirming=True)
|
| 5488 |
if stored["payload"] != payload:
|
| 5489 |
return None
|
|
@@ -5493,11 +6064,49 @@ def _mcp_saved_intent(session, arguments):
|
|
| 5493 |
return intent if isinstance(intent, str) and intent.strip() else None
|
| 5494 |
|
| 5495 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5496 |
def _mcp_hoist_call_metadata(name, arguments):
|
| 5497 |
"""Return group call arguments with intent, about and purpose lifted out of the inner arguments."""
|
| 5498 |
import tool_groups
|
| 5499 |
keys = ("intent", "about", "purpose")
|
| 5500 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5501 |
or not isinstance(arguments.get("arguments"), dict)
|
| 5502 |
or not any(key in arguments["arguments"] for key in keys)):
|
| 5503 |
return arguments
|
|
@@ -5510,13 +6119,35 @@ def _mcp_hoist_call_metadata(name, arguments):
|
|
| 5510 |
return hoisted
|
| 5511 |
|
| 5512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5513 |
def _mcp_requires_intent(name, arguments):
|
| 5514 |
"""Classify declared writes before argument validation can mask the intent requirement."""
|
|
|
|
|
|
|
| 5515 |
if name == "confirm_change":
|
| 5516 |
return True
|
| 5517 |
if isinstance(name, str) and name in external_tools.TOOLS:
|
| 5518 |
return external_tools.is_write(name, arguments)
|
| 5519 |
-
import tool_groups
|
| 5520 |
group = tool_groups.groups().get(name) if isinstance(name, str) else None
|
| 5521 |
action = arguments.get("action") if isinstance(arguments, dict) else None
|
| 5522 |
entry = group["actions"].get(action) if group and isinstance(action, str) else None
|
|
@@ -5524,6 +6155,27 @@ def _mcp_requires_intent(name, arguments):
|
|
| 5524 |
|
| 5525 |
|
| 5526 |
def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5527 |
"""One HTTP request object; the stdio precedent's JSON-RPC and content envelopes."""
|
| 5528 |
protocol_error = _mcp_protocol_error
|
| 5529 |
|
|
@@ -5541,6 +6193,8 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5541 |
if not isinstance(params, dict):
|
| 5542 |
return protocol_error(rid, -32602, "MCP params must be an object.")
|
| 5543 |
name = params.get("name")
|
|
|
|
|
|
|
| 5544 |
try:
|
| 5545 |
# Same AI apps wall as the read doors, inside the dispatcher so a grant refusal
|
| 5546 |
# remains MCP content. Only the unchanged bearer dependency produces an HTTP 401.
|
|
@@ -5578,6 +6232,10 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5578 |
"_meta": {"ui": {"csp": {"connectDomains": [], "resourceDomains": [], "frameDomains": []}}}}
|
| 5579 |
if method == "resources/list":
|
| 5580 |
return {"jsonrpc": "2.0", "id": rid, "result": {"resources": [resource]}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5581 |
if params.get("uri") != WORKBOOK_RESOURCE_URI:
|
| 5582 |
return protocol_error(rid, -32602, "That resource is not available.")
|
| 5583 |
resource["text"] = (Path(__file__).parent / "side_view" / "workbook.html").read_text(encoding="utf-8")
|
|
@@ -5591,7 +6249,7 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5591 |
if intent is not None:
|
| 5592 |
external_activity.note_saved_intent(intent)
|
| 5593 |
if intent is None and _mcp_requires_intent(name, arguments):
|
| 5594 |
-
raise err(400, "call_intent_required",
|
| 5595 |
{"example": {"intent": "Prepare the weekly sales review"}})
|
| 5596 |
if isinstance(arguments, dict):
|
| 5597 |
arguments = {key: value for key, value in arguments.items() if key != "intent"}
|
|
@@ -5612,7 +6270,24 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5612 |
if "about" in metadata_keys:
|
| 5613 |
about = arguments.pop("about")
|
| 5614 |
_assert_tool_query_filter_bounds(name, arguments)
|
| 5615 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5616 |
confirm = True
|
| 5617 |
scoped_arguments = arguments
|
| 5618 |
if resolved:
|
|
@@ -5676,6 +6351,8 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5676 |
out = _tool_refusal(name, "external_access_unavailable", "The AI API is unavailable right now.", 503)
|
| 5677 |
except sheets.SheetRefusal as exc:
|
| 5678 |
import routes_sheet
|
|
|
|
|
|
|
| 5679 |
translated = routes_sheet._refuse(exc)
|
| 5680 |
info = translated.detail["error"]
|
| 5681 |
out = _tool_refusal(name, info["code"], info["message"], translated.status_code,
|
|
@@ -5693,12 +6370,19 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
|
| 5693 |
if (info.get("code") in {"assistant_forbidden", "key_database_out_of_scope"}
|
| 5694 |
and isinstance(details, dict) and isinstance(details.get("database"), str)):
|
| 5695 |
retry["database"] = details["database"]
|
| 5696 |
-
|
| 5697 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5698 |
except (TypeError, ValueError, KeyError, AttributeError):
|
| 5699 |
out = _tool_refusal(name, "tool_arguments_invalid", "The arguments could not be read. Use the published input schema.")
|
| 5700 |
except Exception:
|
| 5701 |
-
out = _tool_refusal(name, "tool_unavailable",
|
|
|
|
| 5702 |
return _mcp_content(rid, out, session)
|
| 5703 |
|
| 5704 |
|
|
@@ -5709,10 +6393,37 @@ async def external_mcp(request: Request, session: Session = Depends(require_exte
|
|
| 5709 |
req = await request.json()
|
| 5710 |
except (ValueError, UnicodeError):
|
| 5711 |
return _mcp_protocol_error(None, -32700, "The request body must be valid JSON.")
|
| 5712 |
-
reply = await run_in_threadpool(_handle_mcp, req, session,
|
| 5713 |
return Response(status_code=204) if reply is None else reply
|
| 5714 |
|
| 5715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5716 |
# R8 (SDLC 61) revokes the read-only surface. Every write must be named here deliberately.
|
| 5717 |
# A new write in the registry without a matching literal raises at import, before serving.
|
| 5718 |
_WRITE_TOOLS = frozenset({
|
|
|
|
| 46 |
"query_filter_limit": "split it into separate queries",
|
| 47 |
"query_cursor_stale": "run the same query again without a cursor",
|
| 48 |
}
|
|
|
|
| 49 |
_VIEW_DRAFT_CONFIG_KEYS = frozenset({
|
| 50 |
"filters", "filterConj", "sorts", "groupBy", "visible", "order", "display",
|
| 51 |
"memberPids",
|
|
|
|
| 79 |
return hashlib.sha256(encoded.encode("utf-8")).hexdigest()
|
| 80 |
|
| 81 |
|
| 82 |
+
def _fits_token(value, raw) -> bool:
|
| 83 |
+
from tool_groups.apps import code_transport_fits
|
| 84 |
+
return len(raw) <= _CONFIRMATION_MAX_BYTES or code_transport_fits(value, _CONFIRMATION_MAX_BYTES)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def _canonical_json(value, *, capped=True) -> bytes:
|
| 88 |
+
"""Encode a value canonically. The byte cap binds a signed token only, never a write (rule 1)."""
|
| 89 |
try:
|
| 90 |
encoded = json.dumps(value, sort_keys=True, separators=(",", ":"),
|
| 91 |
ensure_ascii=True, allow_nan=False)
|
|
|
|
| 93 |
raise err(400, "tool_arguments_invalid",
|
| 94 |
"The tool arguments contain a value that cannot be represented safely.") from exc
|
| 95 |
raw = encoded.encode("utf-8")
|
| 96 |
+
if capped and not _fits_token(value, raw):
|
|
|
|
| 97 |
raise err(413, "confirmation_too_large",
|
| 98 |
"This change is too large for one preview. Split it into smaller changes.")
|
| 99 |
if _contains_credential(encoded):
|
|
|
|
| 160 |
def _load(self, name):
|
| 161 |
key = str(name)
|
| 162 |
if key not in self._docs:
|
| 163 |
+
import application_registry
|
| 164 |
+
value = application_registry._read_strict(self._real, key)
|
|
|
|
|
|
|
| 165 |
value = copy.deepcopy(value)
|
| 166 |
self._initial[key] = copy.deepcopy(value)
|
| 167 |
self._docs[key] = value
|
|
|
|
| 176 |
def get(self, name, *args, **kwargs):
|
| 177 |
return copy.deepcopy(self._load(name))
|
| 178 |
|
| 179 |
+
def _read_strict(self, name):
|
| 180 |
+
return self.get(name)
|
| 181 |
+
|
| 182 |
def get_projection(self, name, drop=()):
|
| 183 |
value = self.get(name)
|
| 184 |
if isinstance(value, dict):
|
|
|
|
| 349 |
|
| 350 |
|
| 351 |
def _diff_documents(documents, *, limit=250):
|
| 352 |
+
"""List changed leaves. limit=None lists every change."""
|
| 353 |
changes = []
|
| 354 |
+
limit = float("inf") if limit is None else limit
|
| 355 |
|
| 356 |
def walk(before, after, path):
|
| 357 |
if len(changes) >= limit or before == after:
|
|
|
|
| 379 |
return changes, len(changes) >= limit
|
| 380 |
|
| 381 |
|
| 382 |
+
_PREVIEW_DIFF_LIMIT = 250
|
| 383 |
+
_PREVIEW_SAMPLE_CHANGES = 20
|
| 384 |
+
_PREVIEW_SAMPLE_ROW_BYTES = 4096
|
| 385 |
+
_CELL_ADDRESS = re.compile(r"([A-Z]{1,3})([0-9]{1,7})")
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def _column_number(letters):
|
| 389 |
+
number = 0
|
| 390 |
+
for letter in letters:
|
| 391 |
+
number = number * 26 + ord(letter) - 64
|
| 392 |
+
return number
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def _column_letters(number):
|
| 396 |
+
letters = ""
|
| 397 |
+
while number:
|
| 398 |
+
number, rest = divmod(number - 1, 26)
|
| 399 |
+
letters = chr(65 + rest) + letters
|
| 400 |
+
return letters
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def _summarise_diff(diff, documents):
|
| 404 |
+
"""Return the diff a preview shows and its summary. A diff that fits shows whole, with no summary."""
|
| 405 |
+
if (len(diff) < _PREVIEW_DIFF_LIMIT
|
| 406 |
+
and len(_canonical_json(diff, capped=False)) <= _CONFIRMATION_MAX_BYTES):
|
| 407 |
+
return diff, None
|
| 408 |
+
cells, cell_rows, other_rows = {}, [], []
|
| 409 |
+
for row in diff:
|
| 410 |
+
parts = [part.replace("~1", "/").replace("~0", "~") for part in row["path"][1:].split("/")]
|
| 411 |
+
if not (len(parts) >= 6 and parts[0] == "workbooks" and parts[2] == "sheets" and parts[4] == "cells"):
|
| 412 |
+
other_rows.append(row)
|
| 413 |
+
continue
|
| 414 |
+
cell_rows.append(row)
|
| 415 |
+
kind = "changed"
|
| 416 |
+
if len(parts) == 6 and row["before"] is None:
|
| 417 |
+
kind = "added"
|
| 418 |
+
elif len(parts) == 6 and row["after"] is None:
|
| 419 |
+
kind = "removed"
|
| 420 |
+
tab = cells.setdefault((parts[1], parts[3]), {})
|
| 421 |
+
if tab.get(parts[5]) in (None, "changed"):
|
| 422 |
+
tab[parts[5]] = kind
|
| 423 |
+
counts = {"added": 0, "changed": 0, "removed": 0}
|
| 424 |
+
ranges = []
|
| 425 |
+
for (workbook_id, index), addresses in sorted(cells.items()):
|
| 426 |
+
for kind in addresses.values():
|
| 427 |
+
counts[kind] += 1
|
| 428 |
+
pair = documents.get("workbooks/" + workbook_id) or {}
|
| 429 |
+
sheets_list = ((pair.get("after") or pair.get("before") or {}).get("sheets") or [])
|
| 430 |
+
sheet = sheets_list[int(index)] if index.isdigit() and int(index) < len(sheets_list) else {}
|
| 431 |
+
found = [_CELL_ADDRESS.fullmatch(address) for address in addresses]
|
| 432 |
+
found = [(_column_number(match[1]), int(match[2])) for match in found if match]
|
| 433 |
+
span = (f"{_column_letters(min(c for c, _ in found))}{min(r for _, r in found)}:"
|
| 434 |
+
f"{_column_letters(max(c for c, _ in found))}{max(r for _, r in found)}") if found else ""
|
| 435 |
+
ranges.append({"workbook_id": workbook_id, "tab": (sheet or {}).get("name") or (sheet or {}).get("id"),
|
| 436 |
+
"tab_id": (sheet or {}).get("id"), "range": span, "cells": len(addresses)})
|
| 437 |
+
sample = []
|
| 438 |
+
for row in (cell_rows + other_rows)[:_PREVIEW_SAMPLE_CHANGES]:
|
| 439 |
+
size = len(_canonical_json(row, capped=False))
|
| 440 |
+
sample.append(row if size <= _PREVIEW_SAMPLE_ROW_BYTES
|
| 441 |
+
else {"path": row["path"], "omitted": True, "bytes": size})
|
| 442 |
+
return sample, {"changes": len(diff), "cells": {**counts, "total": sum(counts.values())},
|
| 443 |
+
"other_changes": len(other_rows), "ranges": ranges, "sample": len(sample), "complete": False}
|
| 444 |
+
|
| 445 |
+
|
| 446 |
def _database_preview_documents(session, before, after):
|
| 447 |
"""Project changed records and fields through the caller's current database permissions."""
|
| 448 |
import routes_tables
|
|
|
|
| 539 |
return str(claims.get("external_credential_id") or claims.get("external_key_id") or "")
|
| 540 |
|
| 541 |
|
| 542 |
+
def _is_save_code(payload):
|
| 543 |
+
return payload.get("tool") == "apps" and payload.get("arguments", {}).get("action") == "save_code"
|
| 544 |
+
|
| 545 |
+
|
| 546 |
+
def _arguments_outside_token(payload):
|
| 547 |
+
"""True when a payload's arguments are too large for a token, so v3 applies."""
|
| 548 |
+
return not _is_save_code(payload) and not _fits_token(payload, _canonical_json(payload, capped=False))
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
def _token_wire(payload):
|
| 552 |
+
"""Return what the token signs. Arguments too large for a token stay in the stored operation."""
|
| 553 |
+
if _is_save_code(payload):
|
| 554 |
+
return {"v": 2, "operation_id": payload["operation_id"],
|
| 555 |
+
"payload_digest": _json_digest(payload),
|
| 556 |
+
**{key: payload[key] for key in ("tenant", "actor", "credential", "authentication")}}
|
| 557 |
+
if not _arguments_outside_token(payload):
|
| 558 |
+
return copy.deepcopy(payload)
|
| 559 |
+
# v3 signs every field but the arguments. arguments_digest binds them to the stored operation.
|
| 560 |
+
return {**{key: copy.deepcopy(value) for key, value in payload.items() if key != "arguments"}, "v": 3}
|
| 561 |
+
|
| 562 |
+
|
| 563 |
def _confirmation_token(payload) -> str:
|
| 564 |
+
raw = _canonical_json(_token_wire(payload))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
signature = aios_session._sign(b"external-confirm:v1:" + raw)
|
| 566 |
return f"{_CONFIRMATION_PREFIX}.{_b64(raw)}.{signature}"
|
| 567 |
|
| 568 |
|
| 569 |
+
def _read_confirmation_token(token, session=None) -> dict:
|
| 570 |
if not isinstance(token, str) or len(token) > _CONFIRMATION_MAX_BYTES * 2:
|
| 571 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
| 572 |
try:
|
|
|
|
| 578 |
payload = json.loads(raw.decode("utf-8"))
|
| 579 |
except (ValueError, TypeError, UnicodeError, json.JSONDecodeError) as exc:
|
| 580 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.") from exc
|
| 581 |
+
if not isinstance(payload, dict) or payload.get("v") not in (1, 2, 3):
|
| 582 |
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
| 583 |
+
if payload["v"] == 3:
|
| 584 |
+
if ("arguments" in payload or not isinstance(payload.get("operation_id"), str)
|
| 585 |
+
or not re.fullmatch(r"[a-f0-9]{64}", payload["operation_id"])
|
| 586 |
+
or not isinstance(payload.get("arguments_digest"), str)):
|
| 587 |
+
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
| 588 |
+
if session is None:
|
| 589 |
+
raise err(400, "confirmation_session_required", "Read this confirmation through its authenticated connection.")
|
| 590 |
+
if any(payload.get(key) != value for key, value in _operation_bindings(session).items()):
|
| 591 |
+
raise err(403, "confirmation_principal_changed", "This confirmation belongs to another account or connection.")
|
| 592 |
+
_, item = _load_operation(session, payload["operation_id"], confirming=True)
|
| 593 |
+
# The stored arguments return unchecked here; confirm_change refuses them as stale unless their digest matches.
|
| 594 |
+
return {**payload, "v": 1, "arguments": copy.deepcopy(item["payload"].get("arguments"))}
|
| 595 |
+
if payload["v"] == 2:
|
| 596 |
+
if (set(payload) != {"v", "operation_id", "payload_digest", "tenant", "actor", "credential", "authentication"}
|
| 597 |
+
or not isinstance(payload.get("operation_id"), str)
|
| 598 |
+
or not re.fullmatch(r"[a-f0-9]{64}", payload["operation_id"])
|
| 599 |
+
or not isinstance(payload.get("payload_digest"), str)
|
| 600 |
+
or not re.fullmatch(r"[a-f0-9]{64}", payload["payload_digest"])):
|
| 601 |
+
raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.")
|
| 602 |
+
if session is None:
|
| 603 |
+
raise err(400, "confirmation_session_required", "Read this confirmation through its authenticated connection.")
|
| 604 |
+
if any(payload.get(key) != value for key, value in _operation_bindings(session).items()):
|
| 605 |
+
raise err(403, "confirmation_principal_changed", "This confirmation belongs to another account or connection.")
|
| 606 |
+
_, item = _load_operation(session, payload["operation_id"], confirming=True)
|
| 607 |
+
if _json_digest(item["payload"]) != payload["payload_digest"]:
|
| 608 |
+
raise err(400, "confirmation_token_invalid", "The saved preview no longer matches this confirmation. Preview it again.")
|
| 609 |
+
return item["payload"]
|
| 610 |
if payload.get("tool") == "apps":
|
| 611 |
from tool_groups.apps import pending_arguments
|
| 612 |
payload["arguments"] = pending_arguments(payload["arguments"], encode=False)
|
|
|
|
| 841 |
if "where" in node:
|
| 842 |
stack.append((node["where"], 1))
|
| 843 |
continue
|
| 844 |
+
children = node.get("nodes", node.get("children"))
|
| 845 |
if not isinstance(children, list):
|
| 846 |
continue
|
| 847 |
if len(children) > _QUERY_MAX_FILTER_SIBLINGS:
|
|
|
|
| 853 |
|
| 854 |
def _assert_tool_query_filter_bounds(name, arguments):
|
| 855 |
"""Name query limits before the finite tool schema reports a generic shape error."""
|
| 856 |
+
if name in ("table", "data") and isinstance(arguments, dict) and arguments.get("action") == "query_database":
|
| 857 |
name, arguments = "query_database", arguments.get("arguments")
|
| 858 |
if name == "query_database" and isinstance(arguments, dict):
|
| 859 |
_assert_query_filter_bounds(arguments.get("filters"))
|
|
|
|
| 1144 |
_, args = tool_groups.resolve(tool_name, arguments)
|
| 1145 |
_assert_workbook_scope(session, args["workbook_id"])
|
| 1146 |
elif not external_access.may_write(access):
|
| 1147 |
+
import tool_groups
|
| 1148 |
+
# A group tool names the fix. A legacy tool keeps its published sentence.
|
| 1149 |
+
fix = (", so connect with a Read and write key and call again"
|
| 1150 |
+
if isinstance(tool_name, str) and tool_groups.canonical_name(tool_name) in tool_groups.groups()
|
| 1151 |
+
else "")
|
| 1152 |
+
raise err(403, "key_read_only", f"{tool_name} writes, and this credential is read only{fix}.")
|
| 1153 |
code = _test_environment_refusal(session)
|
| 1154 |
if code:
|
| 1155 |
raise err(403, code, MCP_RESULT_CODES[code])
|
|
|
|
| 3004 |
session, database, fields=[], filters=None if filters == [] else filters)
|
| 3005 |
|
| 3006 |
|
| 3007 |
+
def _saved_view_filters(config):
|
| 3008 |
+
"""Convert declared query groups to native View groups and refuse any lossy normalization."""
|
| 3009 |
+
value = config.get("filters", [])
|
| 3010 |
+
_assert_query_filter_bounds([value] if isinstance(value, dict) else value)
|
| 3011 |
+
import aios_grid
|
| 3012 |
+
|
| 3013 |
+
def refuse():
|
| 3014 |
+
raise err(400, "view_filters_unsupported",
|
| 3015 |
+
"These filters cannot be saved without changing their meaning. "
|
| 3016 |
+
"Use colId, op and value leaves, with conj and children for nested groups.")
|
| 3017 |
+
|
| 3018 |
+
def node(raw):
|
| 3019 |
+
if not isinstance(raw, dict):
|
| 3020 |
+
refuse()
|
| 3021 |
+
if "nodes" in raw or "children" in raw:
|
| 3022 |
+
key = "nodes" if "nodes" in raw else "children"
|
| 3023 |
+
if (set(raw) - {"conj", key} or raw.get("conj", "and") not in ("and", "or")
|
| 3024 |
+
or not isinstance(raw[key], list) or not raw[key]):
|
| 3025 |
+
refuse()
|
| 3026 |
+
return {"conj": raw.get("conj", "and"), "children": [node(child) for child in raw[key]]}
|
| 3027 |
+
if (set(raw) - {"colId", "op", "value", "value2", "values", "id", "locked"}
|
| 3028 |
+
or not isinstance(raw.get("colId"), str) or raw.get("op") not in aios_grid.FILTER_OPS):
|
| 3029 |
+
refuse()
|
| 3030 |
+
for key in ("value", "value2"):
|
| 3031 |
+
value = raw.get(key)
|
| 3032 |
+
if isinstance(value, (dict, list)) or value is not None and len(str(value)) > 500:
|
| 3033 |
+
refuse()
|
| 3034 |
+
return copy.deepcopy(raw)
|
| 3035 |
+
|
| 3036 |
+
if isinstance(value, dict):
|
| 3037 |
+
value = [node(value)]
|
| 3038 |
+
elif isinstance(value, list):
|
| 3039 |
+
value = [node(item) for item in value]
|
| 3040 |
+
else:
|
| 3041 |
+
refuse()
|
| 3042 |
+
keys = set(_view_referenced_fields({"filters": value}))
|
| 3043 |
+
cleaned = aios_grid.clean_filter_tree(value, keys)
|
| 3044 |
+
|
| 3045 |
+
def faithful(before, after):
|
| 3046 |
+
if len(before) != len(after):
|
| 3047 |
+
return False
|
| 3048 |
+
for left, right in zip(before, after):
|
| 3049 |
+
if "children" in left:
|
| 3050 |
+
if left["conj"] != right.get("conj") or not faithful(left["children"], right.get("children", [])):
|
| 3051 |
+
return False
|
| 3052 |
+
else:
|
| 3053 |
+
for key, operand in left.items():
|
| 3054 |
+
expected = "" if operand is None else str(operand) if key in {"value", "value2"} else operand
|
| 3055 |
+
if right.get(key) != expected:
|
| 3056 |
+
return False
|
| 3057 |
+
return True
|
| 3058 |
+
|
| 3059 |
+
if not faithful(value, cleaned):
|
| 3060 |
+
refuse()
|
| 3061 |
+
return {**config, "filters": cleaned}
|
| 3062 |
+
|
| 3063 |
+
|
| 3064 |
def _view_share_preview(session: Session, args: dict):
|
| 3065 |
"""Build C8's audience/field-inheritance plan without changing a View or any grant."""
|
| 3066 |
import routes_shares
|
|
|
|
| 3162 |
if kind == "view_upsert":
|
| 3163 |
# Resolve the ordinary View/database wall before the additional T31 draft validation so
|
| 3164 |
# the established refusal vocabulary and connector record-scope guard stay intact.
|
| 3165 |
+
raw = {**raw, "config": _saved_view_filters(raw["config"])}
|
| 3166 |
+
event["view"] = raw
|
| 3167 |
_validate_personal_view_draft(session, database, raw)
|
| 3168 |
existing = ctx.table.find_view(view_id, prefer_owner=session.uname)
|
| 3169 |
if (existing and str(existing[0]).strip().lower()
|
|
|
|
| 3302 |
return {"rev": None, "token": _json_digest(fallback)}
|
| 3303 |
|
| 3304 |
|
| 3305 |
+
def _database_value_plan(session, database, record_id, values, fields, *, apply_defaults=False):
|
| 3306 |
"""Apply the grid's field/value vocabulary without calling its event side effects."""
|
| 3307 |
from core import grid_events, user_tables
|
| 3308 |
import aios_grid
|
| 3309 |
|
| 3310 |
+
if not isinstance(values, dict) or (not values and not apply_defaults):
|
| 3311 |
raise err(400, "tool_arguments_invalid", "Record values must be a non-empty object.")
|
| 3312 |
field_by_key = {str(field.get("key")): field for field in fields if isinstance(field, dict)}
|
| 3313 |
unknown = sorted(str(key) for key in values if str(key) not in field_by_key)
|
|
|
|
| 3315 |
raise err(403, "field_unavailable",
|
| 3316 |
"Every changed field must be readable and editable in this database.")
|
| 3317 |
ctx = _view_context(session, database, {"pid": record_id})
|
| 3318 |
+
for key in values:
|
|
|
|
| 3319 |
key = str(key)
|
| 3320 |
definition = field_by_key[key]
|
| 3321 |
if key in (ctx.hidden_keys or frozenset()) or not grid_events._may_edit_field_value(
|
|
|
|
| 3324 |
"Every changed field must be readable and editable in this database.")
|
| 3325 |
if user_tables.is_computed_cell(definition) or definition.get("type") == "automation":
|
| 3326 |
raise err(403, "computed_field", "A computed or automation field is read only.")
|
| 3327 |
+
if apply_defaults:
|
| 3328 |
+
editable = [definition for key, definition in field_by_key.items()
|
| 3329 |
+
if key not in (ctx.hidden_keys or frozenset())
|
| 3330 |
+
and grid_events._may_edit_field_value(ctx, key, definition)
|
| 3331 |
+
and not user_tables.is_computed_cell(definition)
|
| 3332 |
+
and definition.get("type") != "automation"]
|
| 3333 |
+
try:
|
| 3334 |
+
expanded = user_tables._new_row_values(editable, values)
|
| 3335 |
+
except ValueError as exc:
|
| 3336 |
+
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300]) from exc
|
| 3337 |
+
if expanded is None:
|
| 3338 |
+
raise err(400, "bad_value", "A field default is not valid for its field.")
|
| 3339 |
+
# Supplied arrays and typed cells must reach their normalizers unchanged.
|
| 3340 |
+
values = {**expanded, **values}
|
| 3341 |
+
if not values:
|
| 3342 |
+
raise err(400, "tool_arguments_invalid", "Record values must be a non-empty object.")
|
| 3343 |
+
planned = {}
|
| 3344 |
+
for key, raw in values.items():
|
| 3345 |
+
key = str(key)
|
| 3346 |
+
definition = field_by_key[key]
|
| 3347 |
why = user_tables.cell_type_refusal(definition, raw)
|
| 3348 |
if why:
|
| 3349 |
raise err(400, "bad_value", why)
|
|
|
|
| 3351 |
bag = definition.get("link") if isinstance(definition.get("link"), dict) else {}
|
| 3352 |
if bag.get("on") or bag.get("inverse"):
|
| 3353 |
raise err(403, "computed_field", "A derived link field is read only.")
|
| 3354 |
+
target_database = str(bag.get("table") or "")
|
| 3355 |
+
_assert_database_scope(session, target_database)
|
| 3356 |
+
target = _external_read_scope(session, target_database)
|
| 3357 |
+
target_fields = target["fields"]
|
| 3358 |
+
available = {str(row["pid"]): row for row in target["records"]}
|
| 3359 |
+
if ("selectionFilter" in bag and user_tables.clean_link_selection_filter(
|
| 3360 |
+
bag["selectionFilter"], target_fields) is None):
|
| 3361 |
+
raise err(400, "link_selection_refused",
|
| 3362 |
+
"The link selection condition is invalid or names an unavailable field.")
|
| 3363 |
raw_ids = raw if isinstance(raw, (list, tuple, set)) else str(raw or "").split(",")
|
| 3364 |
selected = []
|
| 3365 |
for item in raw_ids:
|
|
|
|
| 3368 |
continue
|
| 3369 |
if candidate not in available or len(selected) >= user_tables.LINK_MAX_IDS:
|
| 3370 |
raise err(400, "bad_value", "A linked record is unavailable.")
|
| 3371 |
+
if not user_tables.link_selection_permits(definition, available[candidate], target_fields):
|
| 3372 |
+
raise err(400, "link_selection_refused",
|
| 3373 |
+
"That record does not match the link selection condition. Choose an available record.")
|
| 3374 |
selected.append(candidate)
|
| 3375 |
if bag.get("single"):
|
| 3376 |
break
|
|
|
|
| 3443 |
rows = table.get("rows") or {}
|
| 3444 |
if action == "record_create":
|
| 3445 |
raw_values = args.get("values")
|
| 3446 |
+
values = _database_value_plan(
|
| 3447 |
+
session, database, None, raw_values, g["fields"], apply_defaults=True)
|
|
|
|
| 3448 |
record_id = str(max((int(key) for key in rows if str(key).isdigit()), default=0) + 1)
|
| 3449 |
before, after = None, values
|
| 3450 |
else:
|
|
|
|
| 3473 |
else:
|
| 3474 |
try:
|
| 3475 |
if action == "record_create":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3476 |
user_tables.allocate_autonumbers(target, after)
|
| 3477 |
user_tables._stamp_row_creation(target, record_id)
|
|
|
|
|
|
|
| 3478 |
except ValueError as exc:
|
| 3479 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300])
|
| 3480 |
target.setdefault("rows", {})[record_id] = copy.deepcopy(after)
|
|
|
|
| 3615 |
flush="sync", expect_rev=expected_rev)
|
| 3616 |
except (view_audit.ViewRevisionStale, view_audit.RevisionUnverifiable) as exc:
|
| 3617 |
raise err(409, "confirmation_stale",
|
| 3618 |
+
"The database changed after the preview, " + _STALE_FIX) from exc
|
| 3619 |
return {"database": database, **copy.deepcopy(plan.get("planned") or {}),
|
| 3620 |
"affected": copy.deepcopy(plan.get("affected") or {})}
|
| 3621 |
|
|
|
|
| 4063 |
return wiki_log.purpose_line(value)
|
| 4064 |
|
| 4065 |
|
| 4066 |
+
def _build_about_normalised(value):
|
| 4067 |
+
"""Trim and lower-case tags, collapse repeats in first-seen order, and rewrite dashes in title and description."""
|
| 4068 |
+
if not isinstance(value, dict):
|
| 4069 |
+
return value
|
| 4070 |
+
value = dict(value)
|
| 4071 |
+
for key in ("title", "description"):
|
| 4072 |
+
if isinstance(value.get(key), str):
|
| 4073 |
+
# A dash at either end has no clause to join, so it goes before the rewrite.
|
| 4074 |
+
trimmed = value[key].strip().strip(dash_norm._DASH_CHARS).strip()
|
| 4075 |
+
value[key] = dash_norm.plain(trimmed).strip() if trimmed else trimmed
|
| 4076 |
+
if isinstance(value.get("tags"), list):
|
| 4077 |
+
tags = [tag.strip().lower() if isinstance(tag, str) else tag for tag in value["tags"]]
|
| 4078 |
+
value["tags"] = list(dict.fromkeys(tags)) if all(isinstance(tag, str) for tag in tags) else tags
|
| 4079 |
+
return value
|
| 4080 |
+
|
| 4081 |
+
|
| 4082 |
def _build_about(value):
|
| 4083 |
+
value = _build_about_normalised(value)
|
| 4084 |
missing = ["about." + key for key in ("title", "description", "tags")
|
| 4085 |
if not isinstance(value, dict) or key not in value]
|
| 4086 |
example = {"about": {"title": "Weekly sales review", "description": "Review weekly sales before the team meeting.",
|
|
|
|
| 4121 |
else "[unknown]") for key in value if key not in rules]
|
| 4122 |
invalid = invalid or ["about"]
|
| 4123 |
details = {"missing": missing, "invalid": invalid, "example": example}
|
| 4124 |
+
message = ("Send about with title, description and tags." if value is None
|
| 4125 |
+
else "Send about as an object with title, description and tags." if not isinstance(value, dict)
|
| 4126 |
+
else "Add " + ", ".join(missing) + " to about." if missing
|
| 4127 |
else "Fix " + ", ".join(invalid) + ".")
|
| 4128 |
if not isinstance(value, dict) or "about.tags" in invalid or "about.tags" in missing:
|
| 4129 |
details["allowed"] = tags
|
|
|
|
| 4150 |
return "skill"
|
| 4151 |
return "file"
|
| 4152 |
return {"views": "view", "apps": "app", "sheet": "workbook", "automations": "automation",
|
| 4153 |
+
"schedules": "schedule", "table": "database", "data": "database", "requests": "request"}.get(name, name)
|
| 4154 |
|
| 4155 |
|
| 4156 |
def _brain_build_arguments(session, name, arguments, about):
|
|
|
|
| 4176 |
"app_id", "catalog_id", "productCount", "pageCount", "pages", "deepLink", "pdfLink", "message")
|
| 4177 |
|
| 4178 |
|
| 4179 |
+
def _is_website_note(name, arguments):
|
| 4180 |
+
return name == "websites" and isinstance(arguments, dict) and arguments.get("action") == "add_internal_note"
|
| 4181 |
+
|
| 4182 |
+
|
| 4183 |
def _preview_external_write(session, name, arguments, *, issue_token=True, execution=None, purpose=None, about=None,
|
| 4184 |
intent=None):
|
| 4185 |
from core import write_execution
|
|
|
|
| 4196 |
about = _build_about(about)
|
| 4197 |
build_kind = _external_build_kind(name, arguments)
|
| 4198 |
environment = _write_environment()
|
| 4199 |
+
normalised = json.loads(_canonical_json(arguments, capped=False).decode("utf-8"))
|
| 4200 |
_assert_tool_database_scope(session, name, normalised)
|
| 4201 |
|
| 4202 |
preview_runtime = _PreviewRuntime(session.runtime)
|
|
|
|
| 4205 |
claims=copy.deepcopy(session.claims), runtime=preview_runtime)
|
| 4206 |
execution = execution or {'seed': uuid.uuid4().hex, 'timestamp': time.time()}
|
| 4207 |
with write_execution.replay(execution):
|
| 4208 |
+
if _is_website_note(name, normalised):
|
| 4209 |
+
import website_tools
|
| 4210 |
+
result = website_tools.plan_internal_note(_tool_context(preview_session, preview=True), normalised["arguments"])
|
| 4211 |
+
normalised = {"action": "add_internal_note", "arguments": copy.deepcopy(result["arguments"])}
|
| 4212 |
+
else:
|
| 4213 |
+
result = _dispatch_external_tool(_tool_context(preview_session, preview=True), name, normalised, about=about)
|
| 4214 |
if isinstance(result, dict) and result.get("refused"):
|
| 4215 |
raise err(409, "write_partial_refused",
|
| 4216 |
"Part of this change could not be accepted. Nothing was previewed.")
|
| 4217 |
|
| 4218 |
+
remote_note = _is_website_note(name, normalised)
|
| 4219 |
+
plan = result if name == "database_object" or remote_note else None
|
| 4220 |
if plan is not None:
|
| 4221 |
diff = copy.deepcopy(plan.get("diff") or [])
|
| 4222 |
warnings = list(plan.get("warnings") or [])
|
|
|
|
| 4224 |
current_revision = copy.deepcopy(plan.get("revision") or {})
|
| 4225 |
else:
|
| 4226 |
documents = preview_runtime.changed()
|
| 4227 |
+
if name == "requests" and normalised.get("action") in {"answer", "complete", "ask_user"}:
|
| 4228 |
projected = _request_preview_documents(session, normalised["arguments"]["id"], documents)
|
| 4229 |
else:
|
| 4230 |
projected = _public_preview_documents(session, documents)
|
| 4231 |
+
# The whole diff is computed and digested. Only its display is summarised (rule 1).
|
| 4232 |
+
diff, _ = _diff_documents(projected, limit=None)
|
| 4233 |
+
warnings = []
|
| 4234 |
affected = _affected_for(name, normalised, diff, result)
|
| 4235 |
current_revision = {
|
| 4236 |
key: revision for key, revision in preview_runtime.base_revisions().items()
|
|
|
|
| 4250 |
{"user": row["user"], "role": row["role"]}
|
| 4251 |
for row in result["current_entries"]]
|
| 4252 |
|
| 4253 |
+
_canonical_json(diff, capped=False)
|
| 4254 |
diff_digest = _json_digest(diff)
|
| 4255 |
+
shown_diff, diff_summary = (diff, None) if plan is not None else _summarise_diff(diff, projected)
|
| 4256 |
+
if diff_summary is not None:
|
| 4257 |
+
warnings.append(f"The preview shows {diff_summary['sample']} of {diff_summary['changes']} changes. "
|
| 4258 |
+
"confirm_change applies all of them.")
|
| 4259 |
+
if len(affected.get("ranges") or []) > _PREVIEW_SAMPLE_CHANGES:
|
| 4260 |
+
affected = {**affected, "ranges": [
|
| 4261 |
+
{key: row[key] for key in ("workbook_id", "tab", "range")} for row in diff_summary["ranges"]]}
|
| 4262 |
+
revision_digest = plan["source_fingerprint"] if remote_note else _json_digest(current_revision)
|
| 4263 |
expires_at = int(time.time()) + _CONFIRMATION_TTL_SECONDS
|
| 4264 |
payload = {
|
| 4265 |
"v": 1, "tenant": session.tenant, "actor": session.uname,
|
|
|
|
| 4277 |
}
|
| 4278 |
if purpose is not None:
|
| 4279 |
payload.update(purpose=purpose, purpose_digest=_json_digest(purpose))
|
| 4280 |
+
if remote_note:
|
| 4281 |
+
payload.update(remote_plan_digest=plan["plan_digest"], target_digest=plan["target_digest"],
|
| 4282 |
+
source_fingerprint=plan["source_fingerprint"])
|
| 4283 |
payload["operation_id"] = hashlib.sha256(payload["nonce"].encode("utf-8")).hexdigest()
|
| 4284 |
if (name == "sheet" and normalised.get("action") == "refresh_scope"
|
| 4285 |
and _key_scope(session)[0] == external_access.ACCESS_REFRESH):
|
|
|
|
| 4288 |
"operation_id": payload["operation_id"], "state": "not_committed",
|
| 4289 |
"preview": True, "mutated": False, "tool": name,
|
| 4290 |
"environment": environment,
|
| 4291 |
+
"diff": shown_diff, "diff_digest": diff_digest,
|
| 4292 |
"affected": affected, "warnings": warnings,
|
| 4293 |
"current_revision": current_revision,
|
| 4294 |
"expires_at": expires_at, "requires_confirmation": True,
|
| 4295 |
"strong_confirmation_required": payload["strong"],
|
| 4296 |
"about": copy.deepcopy(about), "buildKind": build_kind,
|
| 4297 |
}
|
| 4298 |
+
if diff_summary is not None:
|
| 4299 |
+
response["diff_summary"] = diff_summary
|
| 4300 |
if purpose is not None:
|
| 4301 |
response["purpose"] = purpose
|
| 4302 |
+
if remote_note:
|
| 4303 |
+
response.update(current_revision=None, revision_before=None, revision_after=None,
|
| 4304 |
+
remote_plan_digest=plan["plan_digest"], target_digest=plan["target_digest"],
|
| 4305 |
+
source_fingerprint=plan["source_fingerprint"], retry=False)
|
| 4306 |
if name == "views" and normalised.get("action") == "share":
|
| 4307 |
response.update({key: copy.deepcopy(result[key]) for key in ("view", "audience")})
|
| 4308 |
+
if name == "apps" and normalised.get("action") == "catalog_build":
|
| 4309 |
+
response.update({key: copy.deepcopy(result[key]) for key in _CATALOG_BUILD_RESULT_FIELDS})
|
| 4310 |
if payload["strong"]:
|
| 4311 |
response["confirmation_phrase"] = _CONFIRMATION_PHRASE
|
| 4312 |
if issue_token:
|
|
|
|
| 4329 |
session.claims.get("external_workbooks", [])])
|
| 4330 |
|
| 4331 |
|
| 4332 |
+
def _prune_operations(raw, removed=None):
|
| 4333 |
+
"""Drop expired claims. The argument documents of dropped claims are added to removed."""
|
| 4334 |
doc = raw if isinstance(raw, dict) else {}
|
| 4335 |
now = time.time()
|
| 4336 |
+
kept = {}
|
| 4337 |
+
for key, value in (doc.get("claims") or {}).items():
|
| 4338 |
+
if isinstance(value, dict) and float(value.get("retain_until", value.get("expiry", 0))) >= now:
|
| 4339 |
+
kept[key] = value
|
| 4340 |
+
elif removed is not None and isinstance(value, dict) and isinstance(value.get("arguments_ref"), dict):
|
| 4341 |
+
removed.append(value["arguments_ref"].get("key"))
|
| 4342 |
+
doc["claims"] = kept
|
| 4343 |
return doc
|
| 4344 |
|
| 4345 |
|
| 4346 |
+
_OPERATION_ARGUMENTS_PREFIX = "external_confirmation_arguments_"
|
| 4347 |
+
_OPERATION_ARGUMENT_STUB_BYTES = 4096
|
| 4348 |
+
|
| 4349 |
+
|
| 4350 |
+
def _drop_operation_documents(session, keys):
|
| 4351 |
+
"""Remove argument documents, best effort. Call it after the bucket update, never inside it."""
|
| 4352 |
+
for key in keys:
|
| 4353 |
+
if not isinstance(key, str) or not key.startswith(_OPERATION_ARGUMENTS_PREFIX):
|
| 4354 |
+
continue
|
| 4355 |
+
try:
|
| 4356 |
+
session.runtime.delete_document(key)
|
| 4357 |
+
except Exception:
|
| 4358 |
+
try:
|
| 4359 |
+
session.runtime.put(key, {})
|
| 4360 |
+
except Exception:
|
| 4361 |
+
pass
|
| 4362 |
+
|
| 4363 |
+
|
| 4364 |
+
def _holds_arguments_apart(payload):
|
| 4365 |
+
"""True when a stored operation keeps its arguments in their own document."""
|
| 4366 |
+
return _arguments_outside_token(payload)
|
| 4367 |
+
|
| 4368 |
+
|
| 4369 |
+
def _receipt_arguments(arguments):
|
| 4370 |
+
"""Keep the small top-level arguments a receipt check reads after the full document is removed."""
|
| 4371 |
+
return {key: copy.deepcopy(value) for key, value in arguments.items()
|
| 4372 |
+
if len(_canonical_json(value, capped=False)) <= _OPERATION_ARGUMENT_STUB_BYTES}
|
| 4373 |
+
|
| 4374 |
+
|
| 4375 |
def _store_operation(session, payload, preview, staged, intent=None):
|
| 4376 |
"""Keep the preview identity outside every affected object's revision."""
|
| 4377 |
operation_id = payload["operation_id"]
|
|
|
|
| 4381 |
"databases": _databases_in_arguments(payload["arguments"]),
|
| 4382 |
"documents": {},
|
| 4383 |
}
|
| 4384 |
+
if _is_website_note(payload["tool"], payload["arguments"]):
|
| 4385 |
+
access["databases"].append("ut_odoo_orders")
|
| 4386 |
if payload["tool"] in {"app_upsert", "app_delete"}:
|
| 4387 |
import application_registry as applications
|
| 4388 |
app_id = payload["arguments"].get("app_id") or next(iter(preview["affected"].get("ids") or []), None)
|
|
|
|
| 4401 |
if isinstance(intent, str):
|
| 4402 |
# Intent stays outside the signed payload, redacted, so confirm_change can reuse it.
|
| 4403 |
item["intent"] = audit.redact_intent(intent)
|
| 4404 |
+
stored_arguments = copy.deepcopy(payload["arguments"])
|
| 4405 |
+
if payload["tool"] == "apps":
|
| 4406 |
+
from tool_groups.apps import pending_arguments
|
| 4407 |
+
stored_arguments = pending_arguments(payload["arguments"], encode=True)
|
| 4408 |
+
ref = None
|
| 4409 |
+
if _holds_arguments_apart(payload):
|
| 4410 |
+
# Large arguments get their own document, so every other preview keeps a small bucket.
|
| 4411 |
+
ref = {"key": _OPERATION_ARGUMENTS_PREFIX + operation_id, "digest": _json_digest(stored_arguments)}
|
| 4412 |
+
session.runtime.put(ref["key"], {"operation_id": operation_id, "arguments": stored_arguments})
|
| 4413 |
+
item["arguments_ref"] = ref
|
| 4414 |
+
item["receipt_arguments"] = _receipt_arguments(stored_arguments)
|
| 4415 |
+
removed = []
|
| 4416 |
+
|
| 4417 |
def save(raw):
|
| 4418 |
+
removed.clear()
|
| 4419 |
+
doc = _prune_operations(raw, removed)
|
| 4420 |
if operation_id in doc["claims"]:
|
| 4421 |
+
raise err(409, "confirmation_stale", "This preview was already saved, " + _STALE_FIX)
|
| 4422 |
stored_item = copy.deepcopy(item)
|
| 4423 |
+
if ref is None:
|
| 4424 |
+
stored_item["payload"]["arguments"] = copy.deepcopy(stored_arguments)
|
| 4425 |
+
else:
|
| 4426 |
+
stored_item["payload"].pop("arguments", None)
|
| 4427 |
doc["claims"][operation_id] = stored_item
|
| 4428 |
return doc
|
| 4429 |
+
try:
|
| 4430 |
+
session.runtime.update(_CONFIRMATION_BUCKET, save, flush="sync")
|
| 4431 |
+
except Exception:
|
| 4432 |
+
if ref is not None:
|
| 4433 |
+
_drop_operation_documents(session, [ref["key"]])
|
| 4434 |
+
raise
|
| 4435 |
+
_drop_operation_documents(session, removed)
|
| 4436 |
|
| 4437 |
|
| 4438 |
def _operation_access(session, item, *, confirming=False):
|
|
|
|
| 4476 |
"Ask the owner to restore access before checking again.")
|
| 4477 |
for database in set(access["databases"]):
|
| 4478 |
_assert_database_scope(session, database)
|
| 4479 |
+
if _is_website_note(name, args):
|
| 4480 |
+
_external_read_scope(session, database, fields=[], mode="schema")
|
| 4481 |
+
else:
|
| 4482 |
+
_external_read_scope(session, database, fields=[])
|
| 4483 |
if name == "database_object":
|
| 4484 |
import routes_tables
|
| 4485 |
g = routes_tables.ut_write_ctx(session, args["database"])
|
|
|
|
| 4556 |
if (not isinstance(item, dict) or "payload" not in item
|
| 4557 |
or item.get("retain_until", 0) < time.time()):
|
| 4558 |
raise err(404, "operation_unavailable", _OPERATION_UNAVAILABLE_MESSAGE)
|
| 4559 |
+
if isinstance(item.get("arguments_ref"), dict):
|
| 4560 |
+
item = copy.deepcopy(item)
|
| 4561 |
+
ref = item["arguments_ref"]
|
| 4562 |
+
held = session.runtime.get(ref.get("key"), fresh=True) if str(ref.get("key", "")).startswith(
|
| 4563 |
+
_OPERATION_ARGUMENTS_PREFIX) else None
|
| 4564 |
+
if (isinstance(held, dict) and held.get("operation_id") == operation_id
|
| 4565 |
+
and isinstance(held.get("arguments"), dict) and _json_digest(held["arguments"]) == ref.get("digest")):
|
| 4566 |
+
item["payload"]["arguments"] = held["arguments"]
|
| 4567 |
+
elif item.get("status") != "preview" and isinstance(item.get("receipt_arguments"), dict):
|
| 4568 |
+
# A confirmed operation removed its document. Its receipt reads the small arguments kept in the claim.
|
| 4569 |
+
item["payload"]["arguments"] = item["receipt_arguments"]
|
| 4570 |
+
else:
|
| 4571 |
+
raise err(409, "confirmation_stale", "This preview's saved arguments are gone, " + _STALE_FIX)
|
| 4572 |
if item["payload"]["tool"] == "apps":
|
| 4573 |
from tool_groups.apps import pending_arguments
|
| 4574 |
item = copy.deepcopy(item)
|
|
|
|
| 4577 |
|
| 4578 |
|
| 4579 |
def _operation_result(operation_id, item):
|
| 4580 |
+
payload = item.get("payload") or {}
|
| 4581 |
+
if _is_website_note(payload.get("tool"), payload.get("arguments")):
|
| 4582 |
+
if item.get("status") in {"committed", "failed", "unknown"} and item.get("result"):
|
| 4583 |
+
result = copy.deepcopy(item["result"])
|
| 4584 |
+
result.update(state=item["status"], retry=False, revision_before=None, revision_after=None,
|
| 4585 |
+
applied={"committed": True, "failed": False, "unknown": None}[item["status"]])
|
| 4586 |
+
return result
|
| 4587 |
+
state = {"preview": "not_committed", "failed": "failed"}.get(item.get("status"), "unknown")
|
| 4588 |
+
return {"operation_id": operation_id, "tool": "websites", "state": state,
|
| 4589 |
+
"confirmed": item.get("status") != "preview", "applied": None if state == "unknown" else False,
|
| 4590 |
+
"retry": False, "revision_before": None, "revision_after": None,
|
| 4591 |
+
"affected": copy.deepcopy(item.get("affected") or {}),
|
| 4592 |
+
"reason": "The remote outcome is unknown. Do not retry this note." if state == "unknown"
|
| 4593 |
+
else "This note has not been committed."}
|
| 4594 |
if item.get("status") == "committed":
|
| 4595 |
return copy.deepcopy(item["result"])
|
| 4596 |
pending = item.get("status") == "claimed"
|
|
|
|
| 4602 |
|
| 4603 |
def _operation_status(session, args):
|
| 4604 |
"""Read a retained result without changing any object or operation revision."""
|
| 4605 |
+
import catalog_store
|
| 4606 |
+
# A long Catalog PDF export is an operation too. Only its own caller can read it.
|
| 4607 |
+
export = catalog_store.export_status(session, args["operation_id"])
|
| 4608 |
+
if export is not None:
|
| 4609 |
+
return export
|
| 4610 |
_, item = _load_operation(session, args["operation_id"])
|
| 4611 |
return _operation_result(args["operation_id"], item)
|
| 4612 |
|
|
|
|
| 4643 |
def _committed_build_evidence(session, writer, arguments, *, build_kind=None):
|
| 4644 |
"""Resolve identities from committed documents and this writer's native inserts."""
|
| 4645 |
import application_registry
|
| 4646 |
+
import catalog_store
|
| 4647 |
import automation_engine
|
| 4648 |
import intelligence_store as files
|
| 4649 |
import wiki_log
|
|
|
|
| 4682 |
for identity in before.keys() | after.keys():
|
| 4683 |
if before.get(identity) != after.get(identity):
|
| 4684 |
add("schedule", identity)
|
| 4685 |
+
elif key == catalog_store.STORE_KEY:
|
| 4686 |
+
before, after = before.get("catalogs") or {}, after.get("catalogs") or {}
|
| 4687 |
+
for identity in before.keys() | after.keys():
|
| 4688 |
+
if before.get(identity) != after.get(identity):
|
| 4689 |
+
add("app", (after.get(identity) or before.get(identity) or {}).get("appId"))
|
| 4690 |
elif key in (application_registry._KEY, automation_engine.STORE_KEY):
|
| 4691 |
kind = "app" if key == application_registry._KEY else "automation"
|
| 4692 |
if kind == "app":
|
|
|
|
| 4705 |
return [targets[key] for key in sorted(targets)], sorted(set(native_ids))
|
| 4706 |
|
| 4707 |
|
| 4708 |
+
def _finish_website_note(session, payload, plan, result):
|
| 4709 |
+
"""Retain remote evidence without claiming local revisions or retry authority."""
|
| 4710 |
+
operation_id = payload["operation_id"]
|
| 4711 |
+
state = result.get("state")
|
| 4712 |
+
if (state not in {"committed", "failed", "unknown"}
|
| 4713 |
+
or result.get("applied") is not {"committed": True, "failed": False, "unknown": None}.get(state)):
|
| 4714 |
+
result, state = {"reason": "remote_result_unavailable"}, "unknown"
|
| 4715 |
+
response = {**result, "operation_id": operation_id, "tool": "websites", "state": state,
|
| 4716 |
+
"confirmed": True, "applied": {"committed": True, "failed": False, "unknown": None}[state],
|
| 4717 |
+
"retry": False, "revision_before": None, "revision_after": None,
|
| 4718 |
+
"affected": copy.deepcopy(plan["affected"]), "about": copy.deepcopy(payload["about"]),
|
| 4719 |
+
"buildKind": payload["buildKind"]}
|
| 4720 |
+
try:
|
| 4721 |
+
detail = {**(external_activity.write_activity_detail(session) or {}),
|
| 4722 |
+
"remote_state": state, "about_digest": payload["about_digest"], "buildKind": payload["buildKind"]}
|
| 4723 |
+
if payload.get("purpose") is not None:
|
| 4724 |
+
detail["purpose"] = payload["purpose"]
|
| 4725 |
+
receipt = audit.external_write_receipt(
|
| 4726 |
+
session.tenant, session.uname, tool="websites", auth_type=payload["authentication"],
|
| 4727 |
+
affected_count=payload["affected_count"], diff_digest=payload["diff_digest"],
|
| 4728 |
+
revision_before=None, revision_after=None, confirmation_id=operation_id, activity_detail=detail)
|
| 4729 |
+
if receipt.get("persisted") is not True:
|
| 4730 |
+
raise RuntimeError("Remote audit was not retained")
|
| 4731 |
+
receipt.update(about=copy.deepcopy(payload["about"]), buildKind=payload["buildKind"])
|
| 4732 |
+
external_activity.note_write_receipt(receipt)
|
| 4733 |
+
response["audit_receipt"] = receipt
|
| 4734 |
+
_confirmation_status(session, operation_id, state, response)
|
| 4735 |
+
return response
|
| 4736 |
+
except Exception:
|
| 4737 |
+
response.update(state="unknown", applied=None, retry=False, reason="remote_evidence_not_retained")
|
| 4738 |
+
response.pop("audit_receipt", None)
|
| 4739 |
+
try:
|
| 4740 |
+
_confirmation_status(session, operation_id, "unknown", response)
|
| 4741 |
+
except Exception:
|
| 4742 |
+
pass
|
| 4743 |
+
return response
|
| 4744 |
+
|
| 4745 |
+
|
| 4746 |
+
_STALE_FIX = "so repeat the original call for a new preview and confirm with its new confirmation_token."
|
| 4747 |
+
|
| 4748 |
+
|
| 4749 |
+
def _confirm_fix(args):
|
| 4750 |
+
"""Name what a confirm_change call got wrong and the shape that succeeds."""
|
| 4751 |
+
allowed = ("confirmation_token", "confirmation_phrase", "intent")
|
| 4752 |
+
extra = sorted(str(key) for key in args if key not in allowed) if isinstance(args, dict) else []
|
| 4753 |
+
phrase = args.get("confirmation_phrase", "") if isinstance(args, dict) else ""
|
| 4754 |
+
cause = (extra[0] + " is not a confirm_change field" if len(extra) == 1
|
| 4755 |
+
else ", ".join(extra) + " are not confirm_change fields" if extra
|
| 4756 |
+
else "confirmation_phrase is not text" if isinstance(args, dict)
|
| 4757 |
+
and isinstance(args.get("confirmation_token"), str) and not isinstance(phrase, str)
|
| 4758 |
+
else "confirmation_token is missing or not text")
|
| 4759 |
+
return (cause + ", so send only confirmation_token from the preview, plus confirmation_phrase "
|
| 4760 |
+
"when the preview asks for it.")
|
| 4761 |
+
|
| 4762 |
+
|
| 4763 |
def _confirm_change(session, args):
|
| 4764 |
from core import view_audit, write_execution
|
| 4765 |
|
| 4766 |
external_tools.validate_arguments(args, external_tools.TOOLS["confirm_change"]["inputSchema"])
|
| 4767 |
+
payload = _read_confirmation_token(args.get("confirmation_token"), session=session)
|
| 4768 |
if any(payload.get(key) != value for key, value in _operation_bindings(session).items()):
|
| 4769 |
raise err(403, "confirmation_principal_changed",
|
| 4770 |
"This confirmation belongs to another account or connection.")
|
|
|
|
| 4776 |
return _operation_result(operation_id, stored)
|
| 4777 |
_assert_write_scope(fresh_session, payload.get("tool"), payload.get("arguments"))
|
| 4778 |
if payload.get("environment") != _write_environment():
|
| 4779 |
+
raise err(409, "confirmation_stale", "The environment changed after the preview, " + _STALE_FIX)
|
| 4780 |
if int(payload.get("expiry") or 0) <= int(time.time()):
|
| 4781 |
raise err(409, "confirmation_expired",
|
| 4782 |
"This confirmation token expired. Preview the change again.")
|
|
|
|
| 4789 |
execution = payload.get('execution')
|
| 4790 |
if (not isinstance(execution, dict) or not isinstance(execution.get('seed'), str)
|
| 4791 |
or not execution['seed'] or type(execution.get('timestamp')) not in (int, float)):
|
| 4792 |
+
raise err(409, 'confirmation_stale', 'This preview is no longer current, ' + _STALE_FIX)
|
|
|
|
| 4793 |
try:
|
| 4794 |
_confirmation_claim(fresh_session, operation_id)
|
| 4795 |
except _OperationAlreadyClaimed:
|
| 4796 |
return _operation_status(fresh_session, {"operation_id": operation_id})
|
| 4797 |
+
remote_note = _is_website_note(tool, arguments)
|
| 4798 |
+
capability = None
|
| 4799 |
try:
|
| 4800 |
recomputed = _preview_external_write(
|
| 4801 |
fresh_session, tool, arguments, issue_token=False, execution=execution,
|
|
|
|
| 4805 |
or payload.get("environment") != current.get("environment")
|
| 4806 |
or payload.get("revision_digest") != current.get("revision_digest")
|
| 4807 |
or payload.get("diff_digest") != current.get("diff_digest")
|
| 4808 |
+
or (remote_note and any(payload.get(key) != current.get(key)
|
| 4809 |
+
for key in ("remote_plan_digest", "target_digest", "source_fingerprint")))
|
| 4810 |
or any((key in payload, payload.get(key)) != (key in current, current.get(key))
|
| 4811 |
for key in ("purpose", "purpose_digest", "about", "about_digest", "buildKind"))):
|
| 4812 |
raise err(409, "confirmation_stale",
|
| 4813 |
+
"The data, permissions, or resulting diff changed after the preview, " + _STALE_FIX)
|
| 4814 |
+
if remote_note:
|
| 4815 |
+
import website_tools
|
| 4816 |
+
ctx = _tool_context(fresh_session)
|
| 4817 |
+
capability = website_tools._note_execution_capability(
|
| 4818 |
+
ctx, recomputed["_plan"], confirmation_token=args["confirmation_token"])
|
| 4819 |
+
result = website_tools.execute_confirmed_note(ctx, recomputed["_plan"], capability=capability)
|
| 4820 |
+
return _finish_website_note(fresh_session, payload, recomputed["_plan"], result)
|
| 4821 |
if tool == "database_object":
|
| 4822 |
result = _execute_database_object(
|
| 4823 |
fresh_session, arguments, plan=recomputed.get("_plan"))
|
|
|
|
| 4834 |
raise err(409, "write_partial_refused",
|
| 4835 |
"Part of the confirmed change could not be accepted.")
|
| 4836 |
except (view_audit.ViewRevisionStale, view_audit.RevisionUnverifiable) as exc:
|
| 4837 |
+
if remote_note and capability is not None and capability.transport_started:
|
| 4838 |
+
return _finish_website_note(fresh_session, payload, recomputed["_plan"],
|
| 4839 |
+
{"state": "unknown", "applied": None, "reason": "remote_response_lost"})
|
| 4840 |
_confirmation_status(fresh_session, operation_id, "failed")
|
| 4841 |
raise err(409, "confirmation_stale",
|
| 4842 |
+
"The data changed while this change was being saved, " + _STALE_FIX) from exc
|
| 4843 |
except Exception:
|
| 4844 |
+
if remote_note and capability is not None and capability.transport_started:
|
| 4845 |
+
return _finish_website_note(fresh_session, payload, recomputed["_plan"],
|
| 4846 |
+
{"state": "unknown", "applied": None, "reason": "remote_response_lost"})
|
| 4847 |
_confirmation_status(fresh_session, operation_id, "failed")
|
| 4848 |
raise
|
| 4849 |
before_revision = copy.deepcopy(recomputed.get("current_revision") or {})
|
|
|
|
| 4885 |
if tool == "apps" and arguments.get("action") == "catalog_build":
|
| 4886 |
response.update({key: copy.deepcopy(result[key]) for key in _CATALOG_BUILD_RESULT_FIELDS})
|
| 4887 |
_confirmation_status(fresh_session, operation_id, "committed", response)
|
| 4888 |
+
if isinstance(stored.get("arguments_ref"), dict):
|
| 4889 |
+
_drop_operation_documents(fresh_session, [stored["arguments_ref"].get("key")])
|
| 4890 |
if tool != "database_object" and receipt.get("persisted") is True:
|
| 4891 |
try:
|
| 4892 |
targets, native_ids = _committed_build_evidence(fresh_session, guarded_session.runtime, arguments,
|
|
|
|
| 4976 |
("Check the View's sharing settings before changing it.", """
|
| 4977 |
view_is_shared view_not_personal share_owner_redundant share_role_unknown share_who_required
|
| 4978 |
"""),
|
| 4979 |
+
# Catalog refusals keep their own code. An unpublished code collapsed to tool_refused.
|
| 4980 |
+
("The catalog selection or design is invalid. Correct the field the refusal names and preview again.", """
|
| 4981 |
+
catalog_selection_invalid catalog_design_invalid catalog_view_invalid catalog_skus_invalid
|
| 4982 |
+
catalog_skus_missing catalog_sku_ambiguous catalog_key_ambiguous catalog_no_matches catalog_empty
|
| 4983 |
+
catalog_source_mismatch invalid_catalog invalid_catalog_paper invalid_catalog_cursor invalid_catalog_limit
|
| 4984 |
+
"""),
|
| 4985 |
+
("The Catalog App or its fields are not available to this account.", """
|
| 4986 |
+
catalog_app_unavailable catalog_app_not_found catalog_forbidden catalog_fields_forbidden
|
| 4987 |
+
catalog_source_forbidden catalog_tenant_forbidden
|
| 4988 |
+
"""),
|
| 4989 |
+
("Catalog storage did not finish this request. Read the catalog again, then retry.", """
|
| 4990 |
+
catalog_query_incomplete catalog_id_conflict catalog_migration_invalid catalog_save_failed
|
| 4991 |
+
catalog_store_invalid catalog_export_failed
|
| 4992 |
+
"""),
|
| 4993 |
)
|
| 4994 |
MCP_RESULT_CODES = {
|
| 4995 |
code: reason for reason, codes in _MCP_CODE_GROUPS for code in codes.split()
|
| 4996 |
}
|
| 4997 |
MCP_RESULT_CODES.update({
|
| 4998 |
+
"workbook_name_ambiguous": "More than one readable workbook has this exact name. Use one of the candidate ids.",
|
| 4999 |
+
"view_filters_unsupported": "These filters cannot be saved without changing their meaning. Use colId, op and value leaves, with conj and children groups.",
|
| 5000 |
+
"confirmation_session_required": "Read this confirmation through its authenticated connection.",
|
| 5001 |
+
"link_selection_refused": "Choose a readable linked record that matches the field's selection condition.",
|
| 5002 |
"app_rule": "Correct the App rule named in the refusal before saving it.",
|
| 5003 |
"audience_not_allowed": "A person must choose sharing with everyone on screen.",
|
| 5004 |
"not_owner": "You cannot administer this item's audience.",
|
| 5005 |
"unknown_people": "Choose active people in this workspace.",
|
| 5006 |
"grant_exceeds_role": "You cannot grant a role above your own access.",
|
| 5007 |
"revoke_not_yours": "You can remove only people you added.",
|
| 5008 |
+
"field_not_shared": "A field in this View is private to its creator. Hide it in the View or ask its creator to share it.",
|
| 5009 |
"key_refresh_only": "This connection can only refresh source cells in its granted workbooks.",
|
| 5010 |
"key_workbook_out_of_scope": "This connection cannot refresh this workbook.",
|
| 5011 |
"external_workbooks_invalid": "Choose up to 100 workbook ids. All workbooks is not a valid choice.",
|
|
|
|
| 5023 |
"operation_pending": "The change is still being saved. Ask again shortly.",
|
| 5024 |
"operation_not_committed": "This change has not been committed.",
|
| 5025 |
"operation_unavailable": _OPERATION_UNAVAILABLE_MESSAGE,
|
| 5026 |
+
"catalog_not_found": ("No stored catalog has this catalog_id. Use the catalog_id that apps.catalog_build "
|
| 5027 |
+
"returned, after confirm_change committed that build."),
|
| 5028 |
"credential_in_arguments": "Remove credential values from the arguments.",
|
| 5029 |
+
"build_about_required": ("Send about with title, description of 20 to 240 characters, and tags from the guide, "
|
| 5030 |
+
"beside action and arguments or inside arguments. See missing and example."),
|
| 5031 |
"build_about_invalid": ("Fix the fields in invalid. Use a nonempty title, a description of 20 to 240 characters, "
|
| 5032 |
"and tags from allowed, each once: " + ", ".join(external_tools.BUILD_TAGS) + "."),
|
| 5033 |
+
"call_intent_required": ("Send intent explaining why you are making this write, beside action and arguments "
|
| 5034 |
+
"or inside arguments. See example."),
|
| 5035 |
"call_intent_invalid": "Send intent as a string of 1 to 500 characters. See example.",
|
| 5036 |
"invalid_external_credential": "The credential is invalid, expired, or revoked.",
|
| 5037 |
"credential_in_output": "The result contained a credential value and was withheld.",
|
|
|
|
| 5133 |
"revision_before", "revision_after", "filters", "sorts", "config", "binding", "bindings",
|
| 5134 |
})
|
| 5135 |
_MCP_RESULT_IDENTIFIERS = frozenset({
|
| 5136 |
+
"resourceUri", "deepLink", "pdfLink", "downloadUrl", "src", "websiteUrl", "fileId", "link", "href",
|
| 5137 |
"id", "ids", "operation_id", "key", "keys", "pid", "pids", "database", "databases", "sourceDatabase",
|
| 5138 |
"compatibleDatabases", "field", "fields", "field_key", "colId", "metric_key", "groupBy",
|
| 5139 |
+
"workbook_id", "worksheet", "tab", "view_id", "app_id", "catalog_id", "chart_id", "record_id", "contextId",
|
| 5140 |
"path", "paths", "address", "addresses", "range", "cursor", "next_cursor", "token",
|
| 5141 |
"source_version", "code", "tool", "action", "person", "agent", "owner", "author", "from", "to",
|
| 5142 |
"username", "tenant", "createdBy", "updatedBy", "by", "byName", "reads", "approvers", "tags", "updated",
|
|
|
|
| 5294 |
return {"jsonrpc": "2.0", "id": rid, "error": {"code": code, "message": out["reason"], "data": out}}
|
| 5295 |
|
| 5296 |
|
| 5297 |
+
def _mcp_file_resource(rid, session, uri):
|
| 5298 |
+
"""Answer resources/read for a caller's own file with its bytes as a base64 blob."""
|
| 5299 |
+
import assistant_files
|
| 5300 |
+
from loopy_catalog_tools import FILE_RESOURCE_PREFIX
|
| 5301 |
+
try:
|
| 5302 |
+
item = _external_file(session, uri[len(FILE_RESOURCE_PREFIX):])
|
| 5303 |
+
data = assistant_files.read_bytes(session.runtime, item)
|
| 5304 |
+
except HTTPException as exc:
|
| 5305 |
+
if exc.status_code == 401:
|
| 5306 |
+
_auth_refusal()
|
| 5307 |
+
data = None
|
| 5308 |
+
except Exception:
|
| 5309 |
+
data = None
|
| 5310 |
+
if data is None:
|
| 5311 |
+
return _mcp_protocol_error(rid, -32602, "That resource is not available.")
|
| 5312 |
+
return {"jsonrpc": "2.0", "id": rid, "result": {"contents": [{
|
| 5313 |
+
"uri": uri, "mimeType": item["mime"], "name": item["filename"],
|
| 5314 |
+
"blob": base64.b64encode(data).decode("ascii")}]}}
|
| 5315 |
+
|
| 5316 |
+
|
| 5317 |
def _mcp_protocol_result(rid, data):
|
| 5318 |
out = _shape_mcp_result({"ok": True, "data": data})
|
| 5319 |
return {"jsonrpc": "2.0", "id": rid, "result": {
|
|
|
|
| 5327 |
# `loopable_guide` tool (every client can call a tool). ⛔ Model-facing text is user-facing copy:
|
| 5328 |
# no em or en dashes here.
|
| 5329 |
MCP_GUIDE_TOOL = "loopable_guide"
|
| 5330 |
+
from tool_groups import INTENT_SCHEMA as _MCP_INTENT_SCHEMA # noqa: E402 - one schema for base and served specs
|
|
|
|
| 5331 |
MCP_INSTRUCTIONS = "\n".join([
|
| 5332 |
+
"1. Loopable is a governed business data workspace: Table databases, saved Views, Sheet workbooks and "
|
| 5333 |
"Apps. Every call runs as the signed-in user, with that user's own permissions. "
|
| 5334 |
"Call loopable_guide for the group index. Call it with group to read that group's actions and schemas. "
|
| 5335 |
"Read the App build guide with apps.guide before building. "
|
| 5336 |
"A build that breaks a required rule is refused with its fix.",
|
| 5337 |
+
"2. Discover before you query: use table.list_databases, then table.describe_database for the database you "
|
| 5338 |
"need. Use only field keys that describe_database returns. Never guess a field name. "
|
| 5339 |
"State the age of data older than one day before giving a number.",
|
| 5340 |
+
"3. Read with table.query_database. Filter and sort on the server, ask only for the fields you need, "
|
| 5341 |
"and follow the cursor for more pages instead of reading everything. Report totals from the "
|
| 5342 |
"counts the tool returns, not from the rows you happened to read. "
|
| 5343 |
"If scheduled results are unread, read schedules.inbox before answering.",
|
|
|
|
| 5349 |
"intelligence.create_wiki publishes a new wiki/ page at once without preview, confirmation or owner approval. "
|
| 5350 |
"It cannot replace a committed page. Existing wiki edits use intelligence.propose with confirmation and file approval. "
|
| 5351 |
"Send intent beside action and arguments on every write, including confirm_change. "
|
| 5352 |
+
"If your client sends only action and arguments, put intent and about inside arguments. "
|
| 5353 |
+
"Reads may send intent. Use 1 to 500 characters explaining why you are making this call."
|
| 5354 |
+
" Use requests.list_team for teammate names and last-active times, requests.send_message for questions or change proposals, "
|
| 5355 |
+
"requests.check_inbox for incoming requests, and requests.ask_user for recipient Approve or Deny decisions. "
|
| 5356 |
+
"requests.send_message also skips preview and confirmation and shares requests.send's hourly cap and source-access checks. "
|
| 5357 |
+
"A decision needs recipient human confirmation. Approval authorizes the recipient AI only. "
|
| 5358 |
+
"It executes no target write or access grant.",
|
| 5359 |
"5. A refusal (ok: false) names its code and reason. Tell the user the reason. Do not retry "
|
| 5360 |
"the same call with a wider scope.",
|
| 5361 |
"6. Never ask for, print or save a credential.",
|
| 5362 |
"7. " + external_tools.CELL_FIGURE_RULE,
|
| 5363 |
+
"8. Report a limitation or missing field with table.report_limitation. It needs no confirmation "
|
| 5364 |
"and changes no workspace data. Give the tool, action, code and your question in plain words. "
|
| 5365 |
"Omit records, cell values, filters and credentials. Refused calls are logged automatically. "
|
| 5366 |
"To include your question with a call, set params._meta['loopable/question'] to at most 500 characters.",
|
|
|
|
| 5378 |
"name": MCP_GUIDE_TOOL,
|
| 5379 |
"description": "Read workspace connections, your skills, databases and the group index, "
|
| 5380 |
"or choose a group for its actions and argument schemas.",
|
| 5381 |
+
"inputSchema": {"type": "object", "properties": {"action": {"type": "string", "enum": ["read"]},
|
| 5382 |
+
"group": {"type": "string"}}, "required": [],
|
| 5383 |
"additionalProperties": False},
|
| 5384 |
"annotations": {"title": "Loopable guide", "readOnlyHint": True,
|
| 5385 |
"destructiveHint": False, "openWorldHint": False},
|
|
|
|
| 5525 |
return allowed
|
| 5526 |
|
| 5527 |
|
| 5528 |
+
def _website_runtime_metadata():
|
| 5529 |
+
"""Read configured runtime metadata and existing smoke evidence without starting a process."""
|
| 5530 |
+
import os
|
| 5531 |
+
import website_tools
|
| 5532 |
+
|
| 5533 |
+
status = getattr(deps.running_main(), "WEBSITE_RUNTIME", {})
|
| 5534 |
+
if (not isinstance(status, dict) or status.get("ready") is not True
|
| 5535 |
+
or status.get("state") not in {"prepared", "external"}):
|
| 5536 |
+
return False
|
| 5537 |
+
node = Path(os.environ.get("POMERADO_NODE", ""))
|
| 5538 |
+
package = Path(os.environ.get("POMERADO_PACKAGE_ROOT", ""))
|
| 5539 |
+
cache = os.environ.get("POMERADO_BROWSERS_PATH") or os.environ.get("PLAYWRIGHT_BROWSERS_PATH", "")
|
| 5540 |
+
try:
|
| 5541 |
+
if (not node.is_absolute() or not node.is_file() or not package.is_absolute()
|
| 5542 |
+
or package.name != "pomerado" or package.parent.name != "node_modules"
|
| 5543 |
+
or not cache or not Path(cache).is_absolute() or not Path(cache).is_dir()):
|
| 5544 |
+
return False
|
| 5545 |
+
manifest = json.loads((package / "package.json").read_text(encoding="utf-8"))
|
| 5546 |
+
if manifest.get("version") != website_tools._VERSION or manifest.get("license") != "MIT":
|
| 5547 |
+
return False
|
| 5548 |
+
if status["state"] == "prepared":
|
| 5549 |
+
root = Path(os.environ.get("POMERADO_RUNTIME_DIR", ""))
|
| 5550 |
+
if not root.is_absolute():
|
| 5551 |
+
return False
|
| 5552 |
+
root = root.resolve()
|
| 5553 |
+
if (node != root / "node-v24.21.0-linux-x64/bin/node"
|
| 5554 |
+
or package != root / "sdk/node_modules/pomerado" or Path(cache) != root / "browsers"):
|
| 5555 |
+
return False
|
| 5556 |
+
version = "24.21.0"
|
| 5557 |
+
else:
|
| 5558 |
+
if (status.get("node") != str(node) or status.get("package_root") != str(package)
|
| 5559 |
+
or status.get("browsers_path") != cache or status.get("chromium_ready") is not True
|
| 5560 |
+
or status.get("os_libraries_ready") is not True):
|
| 5561 |
+
return False
|
| 5562 |
+
version = status.get("node_version", "")
|
| 5563 |
+
match = re.fullmatch(r"v?(24)\.(\d+)\.(\d+)", version)
|
| 5564 |
+
return bool(match and tuple(map(int, match.groups())) >= (24, 21, 0))
|
| 5565 |
+
except (OSError, ValueError, TypeError):
|
| 5566 |
+
return False
|
| 5567 |
+
|
| 5568 |
+
|
| 5569 |
+
def _website_requirements(session, *, note=False):
|
| 5570 |
+
"""Mirror website admission using schema, permissions and readiness metadata only."""
|
| 5571 |
+
import os
|
| 5572 |
+
import website_tools
|
| 5573 |
+
|
| 5574 |
+
required = []
|
| 5575 |
+
try:
|
| 5576 |
+
website_tools._admit(_tool_context(session))
|
| 5577 |
+
except Exception:
|
| 5578 |
+
required.append("a Royal Imports Administrator with unrestricted sales-order database access")
|
| 5579 |
+
try:
|
| 5580 |
+
target = urlsplit(os.environ.get("ODOO_WEB_RI_URL", ""))
|
| 5581 |
+
login_ready = (target.scheme == "https" and target.hostname and not target.username and not target.password
|
| 5582 |
+
and all(os.environ.get(key) for key in ("ODOO_WEB_RI_USER", "ODOO_WEB_RI_PASSWORD")))
|
| 5583 |
+
except ValueError:
|
| 5584 |
+
login_ready = False
|
| 5585 |
+
if not login_ready:
|
| 5586 |
+
required.append("configured website login credentials")
|
| 5587 |
+
if not _website_runtime_metadata():
|
| 5588 |
+
required.append("a verified Node 24.21 runtime with pinned MIT Pomerado, Chromium and its OS libraries")
|
| 5589 |
+
if note:
|
| 5590 |
+
runner = website_tools._NOTE_NODE_RUNNER
|
| 5591 |
+
provider_ready = bool(os.environ.get("OPENROUTER_API_KEY")) and all(value in runner for value in (
|
| 5592 |
+
'const route = "https://openrouter.ai/api/v1"', 'useResponses: true',
|
| 5593 |
+
'"gpt-6.1-sol": "openai/gpt-6.1-sol"', '"gpt-6-luna": "openai/gpt-6-luna"'))
|
| 5594 |
+
if not provider_ready:
|
| 5595 |
+
required.append("configured OpenRouter access with the pinned Responses model mapping")
|
| 5596 |
+
return required
|
| 5597 |
+
|
| 5598 |
+
|
| 5599 |
def _mcp_capabilities(session, groups, rows):
|
| 5600 |
+
"""Report implementation support separately from this request's permission requirements."""
|
| 5601 |
import tool_groups
|
| 5602 |
database_actions = _mcp_database_actions(session, rows)
|
| 5603 |
environment_refusal = _test_environment_refusal(session)
|
|
|
|
| 5611 |
row = {"group": group, "action": action, "state": "available", "requires": ""}
|
| 5612 |
legacy = entry.get("legacy_name", "")
|
| 5613 |
required = []
|
| 5614 |
+
supported = _mcp_action_supported(entry)
|
| 5615 |
+
grants = tool_groups.action_grants(entry, group)
|
| 5616 |
+
if legacy == "validated_batch":
|
| 5617 |
+
grants = (*grants, "sheets")
|
| 5618 |
+
for grant in grants:
|
| 5619 |
+
try:
|
| 5620 |
+
if grant == "ai_apps":
|
| 5621 |
+
require_external_analyst(session)
|
| 5622 |
+
else:
|
| 5623 |
+
session.require(grant)
|
| 5624 |
+
except HTTPException:
|
| 5625 |
+
required.append({"ai_apps": "AI apps access", "sheets": "Sheet access",
|
| 5626 |
+
"intelligence": "Brain access"}.get(grant, "Access to " + grant))
|
| 5627 |
+
refresh = (_key_scope(session)[0] == external_access.ACCESS_REFRESH
|
| 5628 |
+
and ((group == "sheet" and action == "refresh_scope") or legacy == "confirm_change"))
|
| 5629 |
+
if not entry["read"] and not external_access.may_write(_key_scope(session)[0]) and not refresh:
|
| 5630 |
+
required.append("a connection with write access")
|
| 5631 |
+
if refresh and not session.claims.get("external_workbooks"):
|
| 5632 |
+
required.append("a workbook granted to this connection")
|
| 5633 |
+
if not entry["read"] and environment_refusal:
|
| 5634 |
+
required.append("a named non-production environment for this test connection")
|
| 5635 |
+
if group == "websites":
|
| 5636 |
+
required.extend(_website_requirements(session, note=action == "add_internal_note"))
|
| 5637 |
+
if legacy in {"describe_database", "query_database", "view_upsert", "view_delete", "view_share_preview", "view_object", "app_upsert", "app_delete"} or (
|
| 5638 |
+
legacy == "database_object" and entry.get("legacy_action") != "list") or legacy == "sheet_set_binding":
|
| 5639 |
+
if not rows:
|
| 5640 |
+
required.append("database access for your account and this connection")
|
| 5641 |
+
if legacy == "database_object" and not entry["read"]:
|
| 5642 |
+
operation = entry["legacy_action"]
|
| 5643 |
+
if not database_actions.get(operation):
|
| 5644 |
+
required.append("permission to " + {
|
| 5645 |
+
"record_create": "add records with editable fields",
|
| 5646 |
+
"record_update": "edit record fields", "record_delete": "delete records",
|
| 5647 |
+
"field_create": "add fields as the database owner or an Administrator",
|
| 5648 |
+
"field_update": "edit a visible field", "field_delete": "delete a visible field",
|
| 5649 |
+
}[operation] + " in a writable database")
|
| 5650 |
+
granted = not required
|
| 5651 |
+
row.update(state=("not supported here" if not supported else
|
| 5652 |
+
"available" if granted else "not available to you"),
|
| 5653 |
+
requires=" and ".join(required))
|
| 5654 |
+
row.update(documented=True, supported=supported, exposed=True,
|
| 5655 |
+
granted=granted,
|
| 5656 |
+
previewable=supported and granted and not entry["read"] and entry.get("confirm", True)
|
| 5657 |
+
and legacy != "confirm_change",
|
| 5658 |
+
confirmation_required=not entry["read"] and entry.get("confirm", True),
|
| 5659 |
+
execution_verified=False)
|
| 5660 |
result.append(row)
|
| 5661 |
return result
|
| 5662 |
|
|
|
|
| 5737 |
"""Return the group index or one group's action schemas without reading records."""
|
| 5738 |
import tool_groups
|
| 5739 |
groups = tool_groups.groups()
|
| 5740 |
+
if isinstance(group, str) and group not in groups and group != "data":
|
| 5741 |
+
# A cased, plural or dotted name (Sheets, sheet.write_cells) reads the one group it names.
|
| 5742 |
+
key = group.strip().lower().partition(".")[0]
|
| 5743 |
+
group = next((name for name in (key, key[:-1] if key.endswith("s") else None)
|
| 5744 |
+
if name in groups or name == "data"), group)
|
| 5745 |
+
alias = group == "data"
|
| 5746 |
+
group = tool_groups.canonical_name(group)
|
| 5747 |
if group is not None and group not in groups:
|
| 5748 |
+
raise sheets.SheetRefusal("tool_not_found", "Set group to one of: " + ", ".join(groups)
|
| 5749 |
+
+ ". Omit group to read the index.")
|
| 5750 |
# One rows-free definitions read per guide: a live runtime pays one pool checkout per database.
|
| 5751 |
# The guide reads schemas only, so no reader here needs `rows`. On a failed read, keep the old path.
|
| 5752 |
try:
|
|
|
|
| 5790 |
"Previous tool names remain callable for one release, but are absent from tools/list.", ""]
|
| 5791 |
lines += ["Available means this action is supported with your current grants. "
|
| 5792 |
"Choose a database and fields you can access. Workbook, record and personal View permissions "
|
| 5793 |
+
"still apply to each target. Changes still need their stated confirmation. "
|
| 5794 |
+
"Exposed means the server lists the tool in tools/list. A client may cache an older list. "
|
| 5795 |
+
"Refresh or reconnect that client if a listed group is missing. "
|
| 5796 |
+
"Discovery does not prove a completed write or delivery. execution_verified is always false "
|
| 5797 |
+
"in this index. Check the returned operation receipt or delivery result for execution proof. "
|
| 5798 |
+
"previewable describes a supported preview with current grants, subject to target checks.", ""]
|
| 5799 |
if group is not None:
|
| 5800 |
selected = groups[group]
|
| 5801 |
lines += ["## " + _mcp_plain(selected["title"], "Actions"), "",
|
| 5802 |
_mcp_plain(selected["guide"], "Choose an action below."), ""]
|
| 5803 |
+
if alias:
|
| 5804 |
+
lines += [tool_groups.TABLE_ALIAS_NOTE, ""]
|
| 5805 |
+
if group == "table":
|
| 5806 |
from tool_groups.data import query_guide
|
| 5807 |
lines += [query_guide(rows), ""]
|
| 5808 |
+
if group == "sheet":
|
| 5809 |
+
from tool_groups.sheet import read_guide
|
| 5810 |
+
lines += [read_guide(), ""]
|
| 5811 |
states = {row["action"]: row for row in capabilities if row["group"] == group}
|
| 5812 |
uses_filter_node = False
|
| 5813 |
for action, entry in selected["actions"].items():
|
|
|
|
| 5842 |
lines.append(f"- `{name}`: {_mcp_plain(entry['title'], 'Actions')}. "
|
| 5843 |
+ _mcp_plain(entry['guide'], "Read this group's guide for actions."))
|
| 5844 |
lines += ["", "Use confirm_change to apply an agreed preview. Keep its operation_id. "
|
| 5845 |
+
"Repeating a confirmation returns its first result. Call table with action operation_status "
|
| 5846 |
"and arguments {operation_id: the id} to check whether it committed. Results remain available "
|
| 5847 |
"for at least 24 hours while your access remains valid. A retry result means the change is still being saved.",
|
| 5848 |
"", "## Your databases", "",
|
|
|
|
| 5863 |
import tool_groups
|
| 5864 |
confirm = external_tools.TOOLS["confirm_change"]
|
| 5865 |
specs = [copy.deepcopy(_MCP_GUIDE_SPEC), *tool_groups.tool_specs(), {
|
| 5866 |
+
"name": "confirm_change", "description": confirm["description"].replace("data.operation_status", "table.operation_status"),
|
| 5867 |
"inputSchema": copy.deepcopy(confirm["inputSchema"]),
|
| 5868 |
"annotations": {"readOnlyHint": False, "destructiveHint": True, "openWorldHint": True},
|
| 5869 |
}]
|
| 5870 |
+
groups = tool_groups.groups()
|
| 5871 |
+
group_names = set(groups)
|
| 5872 |
for spec in specs:
|
| 5873 |
spec["inputSchema"]["properties"]["intent"] = copy.deepcopy(_MCP_INTENT_SCHEMA)
|
| 5874 |
+
# Every write-capable tool requires intent. Groups carry it from tool_groups.tool_specs.
|
| 5875 |
if spec["name"] == "confirm_change":
|
| 5876 |
spec["inputSchema"]["required"] = [*spec["inputSchema"].get("required", []), "intent"]
|
| 5877 |
if spec["name"] in group_names:
|
| 5878 |
import wiki_log
|
| 5879 |
+
spec["description"] = (
|
| 5880 |
+
f"{groups[spec['name']]['title']} module. Read loopable_guide(group='{spec['name']}') "
|
| 5881 |
+
"for supported actions, current permissions, schemas and confirmation rules.")
|
| 5882 |
+
if spec["name"] == "apps":
|
| 5883 |
+
spec["description"] += " Read apps.guide before creating or editing an App."
|
| 5884 |
spec["inputSchema"]["properties"]["purpose"] = copy.deepcopy(wiki_log.PURPOSE_SCHEMA)
|
| 5885 |
spec["inputSchema"]["properties"]["about"] = copy.deepcopy(external_tools.ABOUT_SCHEMA)
|
| 5886 |
+
spec["inputSchema"]["properties"]["action"]["enum"] = list(groups[spec["name"]]["actions"])
|
| 5887 |
if spec["name"] == "sheet":
|
| 5888 |
from tool_groups.sheet import WORKBOOK_RESOURCE_URI
|
| 5889 |
spec["_meta"] = {"ui": {"resourceUri": WORKBOOK_RESOURCE_URI}}
|
|
|
|
| 5913 |
import tool_groups
|
| 5914 |
resolved = tool_groups.resolve(name, arguments)
|
| 5915 |
if resolved is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5916 |
raise sheets.SheetRefusal("tool_not_found", "The requested tool is not available.")
|
| 5917 |
entry, args = resolved
|
| 5918 |
if call is not None and call["tool"] == name:
|
|
|
|
| 5995 |
def _mcp_limitation_address(tool, arguments):
|
| 5996 |
"""Resolve registry identities before validation while preserving audit alias attribution."""
|
| 5997 |
import tool_groups
|
| 5998 |
+
name, arguments = tool_groups.canonical_call(tool, arguments)
|
| 5999 |
+
group = tool_groups.groups().get(name)
|
| 6000 |
action = arguments.get("action") if isinstance(arguments, dict) else None
|
| 6001 |
if group and isinstance(action, str):
|
| 6002 |
for trusted_action in group["actions"]:
|
|
|
|
| 6017 |
schema = {"properties": {"intent": _MCP_INTENT_SCHEMA}}
|
| 6018 |
if not isinstance(name, str):
|
| 6019 |
return "unknown", schema
|
| 6020 |
+
original = name
|
| 6021 |
+
name, grouped_arguments = tool_groups.canonical_call(name, arguments)
|
| 6022 |
if name == MCP_GUIDE_TOOL:
|
| 6023 |
schema = copy.deepcopy(_MCP_GUIDE_SPEC["inputSchema"])
|
| 6024 |
elif name in external_tools.TOOLS:
|
| 6025 |
schema = copy.deepcopy(external_tools.TOOLS[name]["inputSchema"])
|
| 6026 |
elif name in tool_groups.groups():
|
| 6027 |
actions = tool_groups.groups()[name]["actions"]
|
| 6028 |
+
action = grouped_arguments.get("action") if isinstance(grouped_arguments, dict) else None
|
| 6029 |
selected = actions.get(action) if isinstance(action, str) else None
|
| 6030 |
+
if "." in original:
|
| 6031 |
+
schema = copy.deepcopy(selected["schema"]) if selected else {"properties": {}}
|
| 6032 |
+
else:
|
| 6033 |
+
schema = {"properties": {"action": {}, "arguments": selected["schema"] if selected else {},
|
| 6034 |
+
"purpose": {}, "about": external_tools.ABOUT_SCHEMA}}
|
| 6035 |
else:
|
| 6036 |
return "unknown", schema
|
| 6037 |
schema["properties"]["intent"] = _MCP_INTENT_SCHEMA
|
| 6038 |
if name not in {MCP_GUIDE_TOOL, "confirm_change"}:
|
| 6039 |
schema["properties"].update(about=external_tools.ABOUT_SCHEMA, purpose={})
|
| 6040 |
+
return original, schema
|
| 6041 |
|
| 6042 |
|
| 6043 |
def _mcp_call_intent(arguments):
|
|
|
|
| 6054 |
def _mcp_saved_intent(session, arguments):
|
| 6055 |
"""Return the intent saved with the preview the signed token names, or None."""
|
| 6056 |
try:
|
| 6057 |
+
payload = _read_confirmation_token(arguments.get("confirmation_token"), session=session)
|
| 6058 |
_, stored = _load_operation(session, payload.get("operation_id", ""), confirming=True)
|
| 6059 |
if stored["payload"] != payload:
|
| 6060 |
return None
|
|
|
|
| 6064 |
return intent if isinstance(intent, str) and intent.strip() else None
|
| 6065 |
|
| 6066 |
|
| 6067 |
+
def _mcp_decode_about(arguments):
|
| 6068 |
+
"""Return arguments with an about sent as JSON object text decoded, or the same object unchanged."""
|
| 6069 |
+
if not isinstance(arguments.get("about"), str):
|
| 6070 |
+
return arguments
|
| 6071 |
+
try:
|
| 6072 |
+
decoded = json.loads(arguments["about"])
|
| 6073 |
+
except ValueError:
|
| 6074 |
+
return arguments
|
| 6075 |
+
return {**arguments, "about": decoded} if isinstance(decoded, dict) else arguments
|
| 6076 |
+
|
| 6077 |
+
|
| 6078 |
def _mcp_hoist_call_metadata(name, arguments):
|
| 6079 |
"""Return group call arguments with intent, about and purpose lifted out of the inner arguments."""
|
| 6080 |
import tool_groups
|
| 6081 |
keys = ("intent", "about", "purpose")
|
| 6082 |
+
if isinstance(arguments, dict) and isinstance(arguments.get("arguments"), str):
|
| 6083 |
+
# Some clients send arguments as a JSON object string. Only an object decodes.
|
| 6084 |
+
try:
|
| 6085 |
+
decoded = json.loads(arguments["arguments"])
|
| 6086 |
+
except ValueError:
|
| 6087 |
+
decoded = None
|
| 6088 |
+
if isinstance(decoded, dict):
|
| 6089 |
+
arguments = {**arguments, "arguments": decoded}
|
| 6090 |
+
if isinstance(arguments, dict):
|
| 6091 |
+
# about sent as JSON object text decodes the same way, beside action and arguments or inside arguments.
|
| 6092 |
+
arguments = _mcp_decode_about(arguments)
|
| 6093 |
+
if isinstance(arguments.get("arguments"), dict):
|
| 6094 |
+
inner = _mcp_decode_about(arguments["arguments"])
|
| 6095 |
+
if inner is not arguments["arguments"]:
|
| 6096 |
+
arguments = {**arguments, "arguments": inner}
|
| 6097 |
+
canonical = tool_groups.canonical_call(name, arguments)[0] if isinstance(name, str) else None
|
| 6098 |
+
if canonical in {"confirm_change", MCP_GUIDE_TOOL} and isinstance(arguments, dict):
|
| 6099 |
+
# A flat tool called in the group shape unwraps: its fields beside action and arguments win.
|
| 6100 |
+
flat = {key: value for key, value in arguments.items() if key != "arguments"}
|
| 6101 |
+
# "call" and "confirm" name the same step as confirm_change.
|
| 6102 |
+
if canonical == "confirm_change" and flat.get("action") in ("confirm_change", "call", "confirm"):
|
| 6103 |
+
flat.pop("action")
|
| 6104 |
+
if isinstance(arguments.get("arguments"), dict):
|
| 6105 |
+
return {**arguments["arguments"], **flat}
|
| 6106 |
+
return flat if "arguments" not in arguments else arguments
|
| 6107 |
+
# The Table group's `data` alias hoists like `table` (C4).
|
| 6108 |
+
if (canonical not in tool_groups.groups()
|
| 6109 |
+
or not isinstance(arguments, dict)
|
| 6110 |
or not isinstance(arguments.get("arguments"), dict)
|
| 6111 |
or not any(key in arguments["arguments"] for key in keys)):
|
| 6112 |
return arguments
|
|
|
|
| 6119 |
return hoisted
|
| 6120 |
|
| 6121 |
|
| 6122 |
+
def _mcp_intent_fix(name):
|
| 6123 |
+
"""Name the intent field and where this tool takes it."""
|
| 6124 |
+
import tool_groups
|
| 6125 |
+
if name == "confirm_change":
|
| 6126 |
+
return ("Send intent beside confirmation_token: 1 to 500 characters on why you are confirming this change. "
|
| 6127 |
+
"A client that sends only action and arguments may put both inside arguments.")
|
| 6128 |
+
if tool_groups.canonical_call(name, {})[0] in tool_groups.groups():
|
| 6129 |
+
return ("Send intent beside action and arguments: 1 to 500 characters on why you are making this write. "
|
| 6130 |
+
"If your client sends only action and arguments, put intent inside arguments.")
|
| 6131 |
+
return "Send intent beside this tool's other arguments: 1 to 500 characters on why you are making this write."
|
| 6132 |
+
|
| 6133 |
+
|
| 6134 |
+
def _mcp_about_fix(name):
|
| 6135 |
+
"""Name where a group call takes about, or nothing for a flat tool."""
|
| 6136 |
+
import tool_groups
|
| 6137 |
+
if isinstance(name, str) and tool_groups.canonical_call(name, {})[0] in tool_groups.groups():
|
| 6138 |
+
return (" Put about beside action and arguments. "
|
| 6139 |
+
"If your client sends only action and arguments, put about inside arguments.")
|
| 6140 |
+
return ""
|
| 6141 |
+
|
| 6142 |
+
|
| 6143 |
def _mcp_requires_intent(name, arguments):
|
| 6144 |
"""Classify declared writes before argument validation can mask the intent requirement."""
|
| 6145 |
+
import tool_groups
|
| 6146 |
+
name, arguments = tool_groups.canonical_call(name, arguments)
|
| 6147 |
if name == "confirm_change":
|
| 6148 |
return True
|
| 6149 |
if isinstance(name, str) and name in external_tools.TOOLS:
|
| 6150 |
return external_tools.is_write(name, arguments)
|
|
|
|
| 6151 |
group = tool_groups.groups().get(name) if isinstance(name, str) else None
|
| 6152 |
action = arguments.get("action") if isinstance(arguments, dict) else None
|
| 6153 |
entry = group["actions"].get(action) if group and isinstance(action, str) else None
|
|
|
|
| 6155 |
|
| 6156 |
|
| 6157 |
def _dispatch_mcp(req, session: Session, base_url: str = ""):
|
| 6158 |
+
"""Route compatibility names through the same admission and response paths."""
|
| 6159 |
+
import tool_groups
|
| 6160 |
+
alias = False
|
| 6161 |
+
if isinstance(req, dict) and req.get("method") == "tools/call" and isinstance(req.get("params"), dict):
|
| 6162 |
+
params = req["params"]
|
| 6163 |
+
original = params.get("name")
|
| 6164 |
+
raw = params.get("arguments", {})
|
| 6165 |
+
name, arguments = tool_groups.canonical_call(original, raw)
|
| 6166 |
+
alias = isinstance(original, str) and (original == "data" or original.startswith("data.")) and name == "table"
|
| 6167 |
+
if name != original or arguments is not raw:
|
| 6168 |
+
req = {**req, "params": {**params, "name": name, "arguments": arguments}}
|
| 6169 |
+
reply = _dispatch_mcp_call(req, session, base_url)
|
| 6170 |
+
if alias and isinstance(reply, dict) and "content" in reply.get("result", {}):
|
| 6171 |
+
content = reply["result"]["content"][0]
|
| 6172 |
+
out = json.loads(content["text"])
|
| 6173 |
+
out["deprecation"] = tool_groups.TABLE_ALIAS_NOTE
|
| 6174 |
+
content["text"] = json.dumps(out, ensure_ascii=True, allow_nan=False)
|
| 6175 |
+
return reply
|
| 6176 |
+
|
| 6177 |
+
|
| 6178 |
+
def _dispatch_mcp_call(req, session: Session, base_url: str = ""):
|
| 6179 |
"""One HTTP request object; the stdio precedent's JSON-RPC and content envelopes."""
|
| 6180 |
protocol_error = _mcp_protocol_error
|
| 6181 |
|
|
|
|
| 6193 |
if not isinstance(params, dict):
|
| 6194 |
return protocol_error(rid, -32602, "MCP params must be an object.")
|
| 6195 |
name = params.get("name")
|
| 6196 |
+
called = name
|
| 6197 |
+
arguments = params.get("arguments", {})
|
| 6198 |
try:
|
| 6199 |
# Same AI apps wall as the read doors, inside the dispatcher so a grant refusal
|
| 6200 |
# remains MCP content. Only the unchanged bearer dependency produces an HTTP 401.
|
|
|
|
| 6232 |
"_meta": {"ui": {"csp": {"connectDomains": [], "resourceDomains": [], "frameDomains": []}}}}
|
| 6233 |
if method == "resources/list":
|
| 6234 |
return {"jsonrpc": "2.0", "id": rid, "result": {"resources": [resource]}}
|
| 6235 |
+
from loopy_catalog_tools import FILE_RESOURCE_PREFIX
|
| 6236 |
+
uri = params.get("uri")
|
| 6237 |
+
if isinstance(uri, str) and uri.startswith(FILE_RESOURCE_PREFIX):
|
| 6238 |
+
return _mcp_file_resource(rid, session, uri)
|
| 6239 |
if params.get("uri") != WORKBOOK_RESOURCE_URI:
|
| 6240 |
return protocol_error(rid, -32602, "That resource is not available.")
|
| 6241 |
resource["text"] = (Path(__file__).parent / "side_view" / "workbook.html").read_text(encoding="utf-8")
|
|
|
|
| 6249 |
if intent is not None:
|
| 6250 |
external_activity.note_saved_intent(intent)
|
| 6251 |
if intent is None and _mcp_requires_intent(name, arguments):
|
| 6252 |
+
raise err(400, "call_intent_required", _mcp_intent_fix(name),
|
| 6253 |
{"example": {"intent": "Prepare the weekly sales review"}})
|
| 6254 |
if isinstance(arguments, dict):
|
| 6255 |
arguments = {key: value for key, value in arguments.items() if key != "intent"}
|
|
|
|
| 6270 |
if "about" in metadata_keys:
|
| 6271 |
about = arguments.pop("about")
|
| 6272 |
_assert_tool_query_filter_bounds(name, arguments)
|
| 6273 |
+
try:
|
| 6274 |
+
resolved = tool_groups.resolve(name, arguments)
|
| 6275 |
+
except sheets.SheetRefusal as exc:
|
| 6276 |
+
if name == "sheet" and exc.code == "tool_arguments_invalid":
|
| 6277 |
+
from tool_groups.sheet import read_guide
|
| 6278 |
+
raise sheets.SheetRefusal(exc.code, exc.message + " " + read_guide()) from exc
|
| 6279 |
+
if name == "table" and exc.code == "tool_arguments_invalid":
|
| 6280 |
+
action = arguments.get("action") if isinstance(arguments, dict) else None
|
| 6281 |
+
if (not isinstance(action, str)
|
| 6282 |
+
or action not in tool_groups.groups()["table"]["actions"]
|
| 6283 |
+
or action in {"describe_database", "query_database"}):
|
| 6284 |
+
from tool_groups.data import READ_ARGUMENT_HELP
|
| 6285 |
+
raise sheets.SheetRefusal(exc.code, exc.message + " " + READ_ARGUMENT_HELP) from exc
|
| 6286 |
+
if (tool_groups.canonical_name(name) == "views" and exc.code == "tool_arguments_invalid"
|
| 6287 |
+
and isinstance(arguments, dict) and arguments.get("action") == "share"):
|
| 6288 |
+
from tool_groups.views import share_arguments_fix
|
| 6289 |
+
raise sheets.SheetRefusal(exc.code, share_arguments_fix(arguments)) from exc
|
| 6290 |
+
raise
|
| 6291 |
confirm = True
|
| 6292 |
scoped_arguments = arguments
|
| 6293 |
if resolved:
|
|
|
|
| 6351 |
out = _tool_refusal(name, "external_access_unavailable", "The AI API is unavailable right now.", 503)
|
| 6352 |
except sheets.SheetRefusal as exc:
|
| 6353 |
import routes_sheet
|
| 6354 |
+
if exc.code == "tool_arguments_invalid" and name == "confirm_change":
|
| 6355 |
+
exc = sheets.SheetRefusal(exc.code, _confirm_fix(arguments))
|
| 6356 |
translated = routes_sheet._refuse(exc)
|
| 6357 |
info = translated.detail["error"]
|
| 6358 |
out = _tool_refusal(name, info["code"], info["message"], translated.status_code,
|
|
|
|
| 6370 |
if (info.get("code") in {"assistant_forbidden", "key_database_out_of_scope"}
|
| 6371 |
and isinstance(details, dict) and isinstance(details.get("database"), str)):
|
| 6372 |
retry["database"] = details["database"]
|
| 6373 |
+
if info.get("code") == "workbook_missing" and isinstance(details, dict):
|
| 6374 |
+
retry["suggestions"] = details.get("suggestions", [])
|
| 6375 |
+
if info.get("code") == "workbook_name_ambiguous" and isinstance(details, dict):
|
| 6376 |
+
retry["candidates"] = details.get("candidates", [])
|
| 6377 |
+
message = info.get("message", "The app refused this operation.")
|
| 6378 |
+
if info.get("code") == "build_about_required" and isinstance(message, str):
|
| 6379 |
+
message += _mcp_about_fix(called)
|
| 6380 |
+
out = _tool_refusal(name, info.get("code", "tool_refused"), message, exc.status_code, **retry)
|
| 6381 |
except (TypeError, ValueError, KeyError, AttributeError):
|
| 6382 |
out = _tool_refusal(name, "tool_arguments_invalid", "The arguments could not be read. Use the published input schema.")
|
| 6383 |
except Exception:
|
| 6384 |
+
out = _tool_refusal(name, "tool_unavailable",
|
| 6385 |
+
"The tool could not complete this operation, so try the same call again shortly.", 503)
|
| 6386 |
return _mcp_content(rid, out, session)
|
| 6387 |
|
| 6388 |
|
|
|
|
| 6393 |
req = await request.json()
|
| 6394 |
except (ValueError, UnicodeError):
|
| 6395 |
return _mcp_protocol_error(None, -32700, "The request body must be valid JSON.")
|
| 6396 |
+
reply = await run_in_threadpool(_handle_mcp, req, session, _mcp_public_origin(request))
|
| 6397 |
return Response(status_code=204) if reply is None else reply
|
| 6398 |
|
| 6399 |
|
| 6400 |
+
def _mcp_public_origin(request: Request) -> str:
|
| 6401 |
+
"""C3: MCP links use the deployment's public base; only an unconfigured local run uses the request."""
|
| 6402 |
+
import loopy_links
|
| 6403 |
+
return loopy_links.public_base() or str(request.base_url)
|
| 6404 |
+
|
| 6405 |
+
|
| 6406 |
+
def _external_file(session: Session, file_id):
|
| 6407 |
+
"""Return this caller's own file metadata when every source is inside the credential scope."""
|
| 6408 |
+
import assistant_files
|
| 6409 |
+
require_external_analyst(session)
|
| 6410 |
+
item = assistant_files.owned_metadata(session.runtime, session.uname, file_id)
|
| 6411 |
+
sources = (item or {}).get("sources")
|
| 6412 |
+
if item is None or session.tenant != item["tenant"] or not isinstance(sources, list) or not sources:
|
| 6413 |
+
raise err(404, "unknown_file", "That file does not exist.")
|
| 6414 |
+
for database in sources:
|
| 6415 |
+
_assert_database_scope(session, database)
|
| 6416 |
+
_external_read_scope(session, database, fields=[], mode="schema")
|
| 6417 |
+
return item
|
| 6418 |
+
|
| 6419 |
+
|
| 6420 |
+
@router.get("/files/{file_id}")
|
| 6421 |
+
def external_file(file_id: str, session: Session = Depends(require_external_session)):
|
| 6422 |
+
"""Download a file this credential's own tool call produced, with the same bearer credential."""
|
| 6423 |
+
import routes_files
|
| 6424 |
+
return routes_files._response(session.runtime, _external_file(session, file_id))
|
| 6425 |
+
|
| 6426 |
+
|
| 6427 |
# R8 (SDLC 61) revokes the read-only surface. Every write must be named here deliberately.
|
| 6428 |
# A new write in the registry without a matching literal raises at import, before serving.
|
| 6429 |
_WRITE_TOOLS = frozenset({
|
api/routes_forms.py
CHANGED
|
@@ -410,11 +410,12 @@ def _clean_values(fields: list, raw: dict):
|
|
| 410 |
def _public_base() -> str:
|
| 411 |
"""The origin a share link is built on. Empty = the client builds it from its own location.
|
| 412 |
|
| 413 |
-
`
|
| 414 |
-
|
| 415 |
a link with the wrong origin looks right, gets pasted into an email and 404s for the reader.
|
| 416 |
"""
|
| 417 |
-
|
|
|
|
| 418 |
|
| 419 |
|
| 420 |
def _link_of(token: str) -> str:
|
|
|
|
| 410 |
def _public_base() -> str:
|
| 411 |
"""The origin a share link is built on. Empty = the client builds it from its own location.
|
| 412 |
|
| 413 |
+
`loopy_links.public_base()` reads `AIOS_PUBLIC_BASE` like the OAuth callback, and live never
|
| 414 |
+
answers *.hf.space (C3). With no base it returns a PATH rather than guessing a hostname:
|
| 415 |
a link with the wrong origin looks right, gets pasted into an email and 404s for the reader.
|
| 416 |
"""
|
| 417 |
+
import loopy_links
|
| 418 |
+
return loopy_links.public_base() or ""
|
| 419 |
|
| 420 |
|
| 421 |
def _link_of(token: str) -> str:
|
api/routes_geo.py
CHANGED
|
@@ -327,10 +327,40 @@ def _zone_context(session, raw_table_key):
|
|
| 327 |
return table_key, fields, set(grid.get("pids") or ()), list(grid.get("rows_src") or ())
|
| 328 |
|
| 329 |
|
| 330 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
from core import route_system
|
| 332 |
zone = next((item for item in route_system.list_zones(
|
| 333 |
-
session.runtime, table_key=table_key)
|
| 334 |
if str(item.get("id") or "") == str(zone_id)), None)
|
| 335 |
if not isinstance(zone, dict):
|
| 336 |
raise err(404, "unknown_zone", "no Zone with that id")
|
|
@@ -573,11 +603,13 @@ def geo_providers(session: Session = Depends(require_session)):
|
|
| 573 |
|
| 574 |
|
| 575 |
@router.get("/geo/zones")
|
| 576 |
-
def geo_zones(tableKey: str = Query(default=""), session: Session = Depends(require_session)
|
|
|
|
| 577 |
"""The canonical table-scoped Zone DTOs a Map may draw or filter by."""
|
| 578 |
from core import route_system
|
|
|
|
| 579 |
table_key, _fields, pids, rows = _zone_context(session, tableKey)
|
| 580 |
-
zones = route_system.list_zones(session.runtime, rows, table_key=table_key)
|
| 581 |
# A hidden Zone remains recoverable to its creator/admin, but cannot become visible through a
|
| 582 |
# second table simply because row ids happen to collide there.
|
| 583 |
visible = [zone for zone in zones if zone.get("visible", True)
|
|
@@ -588,10 +620,14 @@ def geo_zones(tableKey: str = Query(default=""), session: Session = Depends(requ
|
|
| 588 |
|
| 589 |
|
| 590 |
@router.post("/geo/zones")
|
| 591 |
-
def geo_zone_create(body: dict = Body(default=None), session: Session = Depends(require_session)
|
|
|
|
| 592 |
"""Persist one Zone in the existing table-scoped Zone store and field/filter contract."""
|
| 593 |
from core import route_system
|
| 594 |
body = body if isinstance(body, dict) else {}
|
|
|
|
|
|
|
|
|
|
| 595 |
table_key, field_keys, pids, rows = _zone_context(session, body.get("tableKey"))
|
| 596 |
try:
|
| 597 |
if any(key in body for key in ("memberships", "memberPids", "member_pids")):
|
|
@@ -601,7 +637,7 @@ def geo_zone_create(body: dict = Body(default=None), session: Session = Depends(
|
|
| 601 |
"geometryGeoJson": body.get("geometryGeoJson"), "zipCodes": body.get("zipCodes"),
|
| 602 |
"visible": body.get("visible"), "geographicField": body.get("geographicField")
|
| 603 |
}, rows=rows, tenant_id=session.tenant, created_by=session.uname,
|
| 604 |
-
table_key=table_key)
|
| 605 |
if zone is None:
|
| 606 |
raise ValueError("the Zone does not match this database's fields and records")
|
| 607 |
except ValueError as exc:
|
|
@@ -613,12 +649,15 @@ def geo_zone_create(body: dict = Body(default=None), session: Session = Depends(
|
|
| 613 |
|
| 614 |
@router.patch("/geo/zones/{zone_id}")
|
| 615 |
def geo_zone_update(zone_id: str, body: dict = Body(default=None),
|
| 616 |
-
session: Session = Depends(require_session)):
|
| 617 |
"""Edit a table-scoped Zone while retaining creator, geometry and membership invariants."""
|
| 618 |
from core import route_system
|
| 619 |
body = body if isinstance(body, dict) else {}
|
|
|
|
|
|
|
|
|
|
| 620 |
table_key, field_keys, pids, rows = _zone_context(session, body.get("tableKey"))
|
| 621 |
-
previous = _zone_subject(session, table_key, zone_id)
|
| 622 |
try:
|
| 623 |
if any(key in body for key in ("memberships", "memberPids", "member_pids")):
|
| 624 |
raise ValueError("Zone membership is derived from current permitted rows")
|
|
@@ -630,7 +669,7 @@ def geo_zone_update(zone_id: str, body: dict = Body(default=None),
|
|
| 630 |
"visible": body.get("visible", previous.get("visible")),
|
| 631 |
"geographicField": body.get("geographicField", previous.get("geographicField"))
|
| 632 |
}, rows=rows, tenant_id=session.tenant, created_by=session.uname,
|
| 633 |
-
table_key=table_key)
|
| 634 |
if zone is None:
|
| 635 |
raise ValueError("the Zone does not match this database's fields and records")
|
| 636 |
except ValueError as exc:
|
|
@@ -640,13 +679,16 @@ def geo_zone_update(zone_id: str, body: dict = Body(default=None),
|
|
| 640 |
|
| 641 |
@router.delete("/geo/zones/{zone_id}")
|
| 642 |
def geo_zone_delete(zone_id: str, body: dict = Body(default=None),
|
| 643 |
-
session: Session = Depends(require_session)):
|
| 644 |
"""Delete exactly one table-scoped Zone; record values are never mutated here."""
|
| 645 |
body = body if isinstance(body, dict) else {}
|
|
|
|
|
|
|
|
|
|
| 646 |
table_key, _fields, _pids, _rows = _zone_context(session, body.get("tableKey"))
|
| 647 |
-
_zone_subject(session, table_key, zone_id)
|
| 648 |
from core import route_system
|
| 649 |
-
if not route_system.delete_zone(session.runtime, zone_id, table_key=table_key):
|
| 650 |
raise err(404, "unknown_zone", "no Zone with that id")
|
| 651 |
return {"deleted": str(zone_id)}
|
| 652 |
|
|
|
|
| 327 |
return table_key, fields, set(grid.get("pids") or ()), list(grid.get("rows_src") or ())
|
| 328 |
|
| 329 |
|
| 330 |
+
def _zone_application(session, table_key, application, *, write=False):
|
| 331 |
+
"""Authorize an explicit App and its bound database before reading its Zones."""
|
| 332 |
+
if application is None:
|
| 333 |
+
return None
|
| 334 |
+
if not isinstance(application, str) or not application.strip() or len(application) > 120:
|
| 335 |
+
raise err(400, "invalid_route_application", "choose a Routing App id")
|
| 336 |
+
application = application.strip()
|
| 337 |
+
table_key = str(table_key or "").strip()
|
| 338 |
+
import application_registry as registry
|
| 339 |
+
from core import route_system
|
| 340 |
+
try:
|
| 341 |
+
doc = registry._read_strict(session.runtime, registry._KEY) or {}
|
| 342 |
+
if not isinstance(doc, dict) or not isinstance(doc.get("applications", {}), dict):
|
| 343 |
+
raise ValueError("invalid Application registry")
|
| 344 |
+
except Exception:
|
| 345 |
+
raise err(503, "application_store_unavailable", "the Application registry is unavailable")
|
| 346 |
+
app = doc.get("applications", {}).get(application)
|
| 347 |
+
if app is None and session.admin and not write and route_system.list_zones(
|
| 348 |
+
session.runtime, table_key=table_key, application=application):
|
| 349 |
+
return application
|
| 350 |
+
if not isinstance(app, dict) or app.get("tenant") != session.tenant:
|
| 351 |
+
raise err(404, "route_application_not_found", "that Routing App does not exist")
|
| 352 |
+
if not registry.may_read(app, session.uname, is_admin=session.admin):
|
| 353 |
+
raise err(403, "route_application_forbidden", "your account cannot open this Routing App")
|
| 354 |
+
source = app.get("sourceDatabase")
|
| 355 |
+
if (source != table_key if source else table_key not in (app.get("compatibleDatabases") or ())):
|
| 356 |
+
raise err(409, "route_application_source", "this App does not use the selected database")
|
| 357 |
+
return application
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def _zone_subject(session, table_key, zone_id, application=None):
|
| 361 |
from core import route_system
|
| 362 |
zone = next((item for item in route_system.list_zones(
|
| 363 |
+
session.runtime, table_key=table_key, application=application)
|
| 364 |
if str(item.get("id") or "") == str(zone_id)), None)
|
| 365 |
if not isinstance(zone, dict):
|
| 366 |
raise err(404, "unknown_zone", "no Zone with that id")
|
|
|
|
| 603 |
|
| 604 |
|
| 605 |
@router.get("/geo/zones")
|
| 606 |
+
def geo_zones(tableKey: str = Query(default=""), session: Session = Depends(require_session),
|
| 607 |
+
application: str = None):
|
| 608 |
"""The canonical table-scoped Zone DTOs a Map may draw or filter by."""
|
| 609 |
from core import route_system
|
| 610 |
+
application = _zone_application(session, tableKey, application)
|
| 611 |
table_key, _fields, pids, rows = _zone_context(session, tableKey)
|
| 612 |
+
zones = route_system.list_zones(session.runtime, rows, table_key=table_key, application=application)
|
| 613 |
# A hidden Zone remains recoverable to its creator/admin, but cannot become visible through a
|
| 614 |
# second table simply because row ids happen to collide there.
|
| 615 |
visible = [zone for zone in zones if zone.get("visible", True)
|
|
|
|
| 620 |
|
| 621 |
|
| 622 |
@router.post("/geo/zones")
|
| 623 |
+
def geo_zone_create(body: dict = Body(default=None), session: Session = Depends(require_session),
|
| 624 |
+
application: str = None):
|
| 625 |
"""Persist one Zone in the existing table-scoped Zone store and field/filter contract."""
|
| 626 |
from core import route_system
|
| 627 |
body = body if isinstance(body, dict) else {}
|
| 628 |
+
from routes_customers import _route_application_body
|
| 629 |
+
_route_application_body(body, application)
|
| 630 |
+
application = _zone_application(session, body.get("tableKey"), application, write=True)
|
| 631 |
table_key, field_keys, pids, rows = _zone_context(session, body.get("tableKey"))
|
| 632 |
try:
|
| 633 |
if any(key in body for key in ("memberships", "memberPids", "member_pids")):
|
|
|
|
| 637 |
"geometryGeoJson": body.get("geometryGeoJson"), "zipCodes": body.get("zipCodes"),
|
| 638 |
"visible": body.get("visible"), "geographicField": body.get("geographicField")
|
| 639 |
}, rows=rows, tenant_id=session.tenant, created_by=session.uname,
|
| 640 |
+
table_key=table_key, application=application)
|
| 641 |
if zone is None:
|
| 642 |
raise ValueError("the Zone does not match this database's fields and records")
|
| 643 |
except ValueError as exc:
|
|
|
|
| 649 |
|
| 650 |
@router.patch("/geo/zones/{zone_id}")
|
| 651 |
def geo_zone_update(zone_id: str, body: dict = Body(default=None),
|
| 652 |
+
session: Session = Depends(require_session), application: str = None):
|
| 653 |
"""Edit a table-scoped Zone while retaining creator, geometry and membership invariants."""
|
| 654 |
from core import route_system
|
| 655 |
body = body if isinstance(body, dict) else {}
|
| 656 |
+
from routes_customers import _route_application_body
|
| 657 |
+
_route_application_body(body, application)
|
| 658 |
+
application = _zone_application(session, body.get("tableKey"), application, write=True)
|
| 659 |
table_key, field_keys, pids, rows = _zone_context(session, body.get("tableKey"))
|
| 660 |
+
previous = _zone_subject(session, table_key, zone_id, application)
|
| 661 |
try:
|
| 662 |
if any(key in body for key in ("memberships", "memberPids", "member_pids")):
|
| 663 |
raise ValueError("Zone membership is derived from current permitted rows")
|
|
|
|
| 669 |
"visible": body.get("visible", previous.get("visible")),
|
| 670 |
"geographicField": body.get("geographicField", previous.get("geographicField"))
|
| 671 |
}, rows=rows, tenant_id=session.tenant, created_by=session.uname,
|
| 672 |
+
table_key=table_key, application=application)
|
| 673 |
if zone is None:
|
| 674 |
raise ValueError("the Zone does not match this database's fields and records")
|
| 675 |
except ValueError as exc:
|
|
|
|
| 679 |
|
| 680 |
@router.delete("/geo/zones/{zone_id}")
|
| 681 |
def geo_zone_delete(zone_id: str, body: dict = Body(default=None),
|
| 682 |
+
session: Session = Depends(require_session), application: str = None):
|
| 683 |
"""Delete exactly one table-scoped Zone; record values are never mutated here."""
|
| 684 |
body = body if isinstance(body, dict) else {}
|
| 685 |
+
from routes_customers import _route_application_body
|
| 686 |
+
_route_application_body(body, application)
|
| 687 |
+
application = _zone_application(session, body.get("tableKey"), application, write=True)
|
| 688 |
table_key, _fields, _pids, _rows = _zone_context(session, body.get("tableKey"))
|
| 689 |
+
_zone_subject(session, table_key, zone_id, application)
|
| 690 |
from core import route_system
|
| 691 |
+
if not route_system.delete_zone(session.runtime, zone_id, table_key=table_key, application=application):
|
| 692 |
raise err(404, "unknown_zone", "no Zone with that id")
|
| 693 |
return {"deleted": str(zone_id)}
|
| 694 |
|
api/routes_grid.py
CHANGED
|
@@ -1816,7 +1816,7 @@ def _materialise_recurrence_cells(session, scope, events, results, fields):
|
|
| 1816 |
# admin route over the CALLING TENANT, and a scheduler must call it once per tenant.
|
| 1817 |
|
| 1818 |
|
| 1819 |
-
def refresh_recurrence_columns(st, scope, today=None, defn=None):
|
| 1820 |
"""R2's roll-forward for ONE database: `{field_key: rows_written}`; `{}` when nothing moved.
|
| 1821 |
|
| 1822 |
Callable WITHOUT an HTTP request — `st` is a store handle (`session.runtime`) and `scope` a
|
|
@@ -1880,7 +1880,8 @@ def refresh_recurrence_columns(st, scope, today=None, defn=None):
|
|
| 1880 |
counts[key] = counts.get(key, 0) + 1
|
| 1881 |
if not updates:
|
| 1882 |
return {}
|
| 1883 |
-
|
|
|
|
| 1884 |
if not written:
|
| 1885 |
return {}
|
| 1886 |
if written != len(updates):
|
|
@@ -1891,7 +1892,7 @@ def refresh_recurrence_columns(st, scope, today=None, defn=None):
|
|
| 1891 |
return counts
|
| 1892 |
|
| 1893 |
|
| 1894 |
-
def refresh_recurrence_tenant(st, today=None):
|
| 1895 |
"""Every user database in ONE tenant: `{table_key: {field_key: rows_written}}`.
|
| 1896 |
|
| 1897 |
ONE `all_tables` read for the whole walk, with each definition LENT onward — see
|
|
@@ -1903,7 +1904,7 @@ def refresh_recurrence_tenant(st, today=None):
|
|
| 1903 |
today = str(today or time.strftime("%Y-%m-%d"))
|
| 1904 |
out = {}
|
| 1905 |
for scope, defn in (user_tables.all_tables(st=st) or {}).items():
|
| 1906 |
-
counts = refresh_recurrence_columns(st, scope, today=today, defn=defn)
|
| 1907 |
if counts:
|
| 1908 |
out[str(scope)] = counts
|
| 1909 |
return out
|
|
@@ -1929,7 +1930,7 @@ def recurrence_refresh(session: Session = Depends(require_session)):
|
|
| 1929 |
raise err(503, "store_unavailable",
|
| 1930 |
"the tenant store is unavailable. Nothing was refreshed")
|
| 1931 |
today = time.strftime("%Y-%m-%d")
|
| 1932 |
-
tables = refresh_recurrence_tenant(session.runtime, today=today)
|
| 1933 |
return {"today": today, "tables": tables,
|
| 1934 |
"cells": sum(sum(counts.values()) for counts in tables.values()),
|
| 1935 |
"columns": sum(len(counts) for counts in tables.values())}
|
|
|
|
| 1816 |
# admin route over the CALLING TENANT, and a scheduler must call it once per tenant.
|
| 1817 |
|
| 1818 |
|
| 1819 |
+
def refresh_recurrence_columns(st, scope, today=None, defn=None, by=None):
|
| 1820 |
"""R2's roll-forward for ONE database: `{field_key: rows_written}`; `{}` when nothing moved.
|
| 1821 |
|
| 1822 |
Callable WITHOUT an HTTP request — `st` is a store handle (`session.runtime`) and `scope` a
|
|
|
|
| 1880 |
counts[key] = counts.get(key, 0) + 1
|
| 1881 |
if not updates:
|
| 1882 |
return {}
|
| 1883 |
+
# S76-T32: `by` is the signed-in admin who asked; a scheduler passes None and stamps no person.
|
| 1884 |
+
written = user_tables.patch_many_cells(scope, updates, st=st, by=by)
|
| 1885 |
if not written:
|
| 1886 |
return {}
|
| 1887 |
if written != len(updates):
|
|
|
|
| 1892 |
return counts
|
| 1893 |
|
| 1894 |
|
| 1895 |
+
def refresh_recurrence_tenant(st, today=None, by=None):
|
| 1896 |
"""Every user database in ONE tenant: `{table_key: {field_key: rows_written}}`.
|
| 1897 |
|
| 1898 |
ONE `all_tables` read for the whole walk, with each definition LENT onward — see
|
|
|
|
| 1904 |
today = str(today or time.strftime("%Y-%m-%d"))
|
| 1905 |
out = {}
|
| 1906 |
for scope, defn in (user_tables.all_tables(st=st) or {}).items():
|
| 1907 |
+
counts = refresh_recurrence_columns(st, scope, today=today, defn=defn, by=by)
|
| 1908 |
if counts:
|
| 1909 |
out[str(scope)] = counts
|
| 1910 |
return out
|
|
|
|
| 1930 |
raise err(503, "store_unavailable",
|
| 1931 |
"the tenant store is unavailable. Nothing was refreshed")
|
| 1932 |
today = time.strftime("%Y-%m-%d")
|
| 1933 |
+
tables = refresh_recurrence_tenant(session.runtime, today=today, by=getattr(session, "uname", None))
|
| 1934 |
return {"today": today, "tables": tables,
|
| 1935 |
"cells": sum(sum(counts.values()) for counts in tables.values()),
|
| 1936 |
"columns": sum(len(counts) for counts in tables.values())}
|
api/routes_nav.py
CHANGED
|
@@ -28,6 +28,7 @@ router = APIRouter(prefix="/api/v1")
|
|
| 28 |
_NAV_PREFS_KEY = "nav_prefs"
|
| 29 |
_MAX_NAV_FOLDERS = 16
|
| 30 |
|
|
|
|
| 31 |
#: WAVE 19 (R8 / contract C1): the database's NAME and ICON overrides.
|
| 32 |
#:
|
| 33 |
#: ⚠ TENANT-WIDE, which is the whole difference from `nav_prefs` above and the reason it is a
|
|
@@ -724,6 +725,8 @@ def nav(session: Session = Depends(require_session)):
|
|
| 724 |
# marked as such. `degraded` is that mark; the shell renders it in the affected slot.
|
| 725 |
_degraded.append("databases")
|
| 726 |
pass
|
|
|
|
|
|
|
| 727 |
# S73-T64: every viewer is admitted to the activity log, which then gates each event. Alone it
|
| 728 |
# would turn "no access" and "no databases yet" into a rail holding one always-empty database,
|
| 729 |
# so it rides only beside another page this session may open.
|
|
@@ -1478,7 +1481,7 @@ def update_schema_map_setup(key: str, body: dict = Body(default=None),
|
|
| 1478 |
@router.get("/nav/application-templates")
|
| 1479 |
def application_templates(session: Session = Depends(require_session)):
|
| 1480 |
"""The trusted template contracts the App creation flow may offer."""
|
| 1481 |
-
return {"templates": application_registry.template_contracts()}
|
| 1482 |
|
| 1483 |
|
| 1484 |
@router.post("/nav/applications", status_code=201)
|
|
@@ -1492,7 +1495,7 @@ def create_nav_application(body: dict = Body(default=None),
|
|
| 1492 |
"""
|
| 1493 |
payload = body if isinstance(body, dict) else {}
|
| 1494 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
| 1495 |
-
wizard_kinds = {row["kind"] for row in application_registry.template_contracts()}
|
| 1496 |
if template_kind not in wizard_kinds:
|
| 1497 |
raise err(400, "bad_application", "application template kind is not recognized")
|
| 1498 |
source = str(payload.get("sourceDatabase") or "").strip()
|
|
|
|
| 28 |
_NAV_PREFS_KEY = "nav_prefs"
|
| 29 |
_MAX_NAV_FOLDERS = 16
|
| 30 |
|
| 31 |
+
|
| 32 |
#: WAVE 19 (R8 / contract C1): the database's NAME and ICON overrides.
|
| 33 |
#:
|
| 34 |
#: ⚠ TENANT-WIDE, which is the whole difference from `nav_prefs` above and the reason it is a
|
|
|
|
| 725 |
# marked as such. `degraded` is that mark; the shell renders it in the affected slot.
|
| 726 |
_degraded.append("databases")
|
| 727 |
pass
|
| 728 |
+
import external_freshness
|
| 729 |
+
external_freshness.amazon_nav(session, pages)
|
| 730 |
# S73-T64: every viewer is admitted to the activity log, which then gates each event. Alone it
|
| 731 |
# would turn "no access" and "no databases yet" into a rail holding one always-empty database,
|
| 732 |
# so it rides only beside another page this session may open.
|
|
|
|
| 1481 |
@router.get("/nav/application-templates")
|
| 1482 |
def application_templates(session: Session = Depends(require_session)):
|
| 1483 |
"""The trusted template contracts the App creation flow may offer."""
|
| 1484 |
+
return {"templates": application_registry.template_contracts(session=session)}
|
| 1485 |
|
| 1486 |
|
| 1487 |
@router.post("/nav/applications", status_code=201)
|
|
|
|
| 1495 |
"""
|
| 1496 |
payload = body if isinstance(body, dict) else {}
|
| 1497 |
template_kind = str(payload.get("templateKind") or "").strip().lower()
|
| 1498 |
+
wizard_kinds = {row["kind"] for row in application_registry.template_contracts(session=session)}
|
| 1499 |
if template_kind not in wizard_kinds:
|
| 1500 |
raise err(400, "bad_application", "application template kind is not recognized")
|
| 1501 |
source = str(payload.get("sourceDatabase") or "").strip()
|
api/routes_odoo_attachments.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Contract C5 (S78-T14): the delivery photo list and the photo bytes, behind the delivery read wall.
|
| 2 |
+
|
| 3 |
+
The work lives in `odoo_attachments.py`. This router decides who may ask and shapes the response.
|
| 4 |
+
Mounted by `main.py` through `_mount_router("routes_odoo_attachments")`.
|
| 5 |
+
"""
|
| 6 |
+
from fastapi import APIRouter, Depends, Path
|
| 7 |
+
from fastapi.responses import Response
|
| 8 |
+
|
| 9 |
+
import odoo_attachments
|
| 10 |
+
from deps import Session, require_session
|
| 11 |
+
|
| 12 |
+
router = APIRouter(prefix="/api/v1")
|
| 13 |
+
|
| 14 |
+
_MAX_ID = 2 ** 31 - 1
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@router.get("/odoo/deliveries/{picking_id}/photos")
|
| 18 |
+
def delivery_photos(picking_id: int = Path(..., ge=1, le=_MAX_ID), session: Session = Depends(require_session)):
|
| 19 |
+
"""`[{id, name, mimetype, url}]`: every photo attachment of one delivery."""
|
| 20 |
+
return odoo_attachments.list_photos(session, picking_id)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@router.get("/odoo/attachments/{attachment_id}")
|
| 24 |
+
def delivery_photo(attachment_id: int = Path(..., ge=1, le=_MAX_ID), session: Session = Depends(require_session)):
|
| 25 |
+
"""The photo bytes, streamed through and never stored."""
|
| 26 |
+
body, mimetype, _name = odoo_attachments.read_photo(session, attachment_id)
|
| 27 |
+
return Response(content=body, media_type=mimetype, headers={
|
| 28 |
+
"Cache-Control": "private, max-age=300",
|
| 29 |
+
"Content-Disposition": "inline",
|
| 30 |
+
"X-Content-Type-Options": "nosniff",
|
| 31 |
+
})
|
api/routes_odoo_tables.py
CHANGED
|
@@ -587,21 +587,21 @@ _SYNC_STAMP_FORMATS = ("%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%dT%H
|
|
| 587 |
|
| 588 |
|
| 589 |
def _parse_sync_stamp(text):
|
| 590 |
-
"""
|
| 591 |
-
anything at all, and an unparseable stamp must produce an UNKNOWN verdict rather than a
|
| 592 |
-
confident one."""
|
| 593 |
import datetime as _dt
|
| 594 |
raw = str(text or "").strip()
|
| 595 |
if not raw:
|
| 596 |
return None
|
| 597 |
for fmt in _SYNC_STAMP_FORMATS:
|
| 598 |
try:
|
| 599 |
-
return _dt.datetime.strptime(raw, fmt)
|
| 600 |
except ValueError:
|
| 601 |
continue
|
| 602 |
try:
|
| 603 |
-
|
| 604 |
-
|
|
|
|
|
|
|
| 605 |
return None
|
| 606 |
|
| 607 |
|
|
@@ -627,7 +627,9 @@ def stuck_verdict(phase, updated_at, cadence_seconds, now=None):
|
|
| 627 |
if when is None:
|
| 628 |
return None, ("this phase is running and its last progress stamp could not be read, so "
|
| 629 |
"whether it is advancing is unknown")
|
| 630 |
-
now = now or _dt.datetime.
|
|
|
|
|
|
|
| 631 |
limit = max(int(cadence_seconds or 0), 60) * 2
|
| 632 |
if (now - when).total_seconds() > limit:
|
| 633 |
return True, (f"this phase has stayed running with no progress since {when}, which is "
|
|
@@ -4537,6 +4539,12 @@ def mirror_stamp(spec, cur=None, table_key="", rt=None, defn=None):
|
|
| 4537 |
return ""
|
| 4538 |
except Exception: # noqa: BLE001
|
| 4539 |
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4540 |
ident = str(spec.get("id") or "")
|
| 4541 |
alias = ident.split(".", 1)[0] if "." in ident else ""
|
| 4542 |
table = str((spec.get("tables") or {}).get(alias) or spec.get("table")
|
|
|
|
| 587 |
|
| 588 |
|
| 589 |
def _parse_sync_stamp(text):
|
| 590 |
+
"""Parse a sync stamp as UTC, treating naive stamps as UTC, or return None."""
|
|
|
|
|
|
|
| 591 |
import datetime as _dt
|
| 592 |
raw = str(text or "").strip()
|
| 593 |
if not raw:
|
| 594 |
return None
|
| 595 |
for fmt in _SYNC_STAMP_FORMATS:
|
| 596 |
try:
|
| 597 |
+
return _dt.datetime.strptime(raw, fmt).replace(tzinfo=_dt.timezone.utc)
|
| 598 |
except ValueError:
|
| 599 |
continue
|
| 600 |
try:
|
| 601 |
+
parsed = _dt.datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
| 602 |
+
return (parsed.replace(tzinfo=_dt.timezone.utc) if parsed.tzinfo is None
|
| 603 |
+
else parsed.astimezone(_dt.timezone.utc))
|
| 604 |
+
except (ValueError, OverflowError):
|
| 605 |
return None
|
| 606 |
|
| 607 |
|
|
|
|
| 627 |
if when is None:
|
| 628 |
return None, ("this phase is running and its last progress stamp could not be read, so "
|
| 629 |
"whether it is advancing is unknown")
|
| 630 |
+
now = now or _dt.datetime.now(_dt.timezone.utc)
|
| 631 |
+
now = (now.replace(tzinfo=_dt.timezone.utc) if now.tzinfo is None
|
| 632 |
+
else now.astimezone(_dt.timezone.utc))
|
| 633 |
limit = max(int(cadence_seconds or 0), 60) * 2
|
| 634 |
if (now - when).total_seconds() > limit:
|
| 635 |
return True, (f"this phase has stayed running with no progress since {when}, which is "
|
|
|
|
| 4539 |
return ""
|
| 4540 |
except Exception: # noqa: BLE001
|
| 4541 |
return ""
|
| 4542 |
+
if str(table_key).startswith("ut_amzn_"):
|
| 4543 |
+
try:
|
| 4544 |
+
from external_freshness import amazon_sync_stamp
|
| 4545 |
+
return str(amazon_sync_stamp(cur, table_key) or "")[:10]
|
| 4546 |
+
except Exception:
|
| 4547 |
+
return ""
|
| 4548 |
ident = str(spec.get("id") or "")
|
| 4549 |
alias = ident.split(".", 1)[0] if "." in ident else ""
|
| 4550 |
table = str((spec.get("tables") or {}).get(alias) or spec.get("table")
|
api/routes_publish.py
CHANGED
|
@@ -186,7 +186,9 @@ def _equalise_pw_cost(entry) -> None:
|
|
| 186 |
|
| 187 |
|
| 188 |
def _public_base() -> str:
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
def _link_of(token: str) -> str:
|
|
|
|
| 186 |
|
| 187 |
|
| 188 |
def _public_base() -> str:
|
| 189 |
+
"""The origin a published link is built on; live never links *.hf.space (C3)."""
|
| 190 |
+
import loopy_links
|
| 191 |
+
return loopy_links.public_base() or ""
|
| 192 |
|
| 193 |
|
| 194 |
def _link_of(token: str) -> str:
|
api/routes_query.py
CHANGED
|
@@ -61,7 +61,7 @@ class _C2Route(APIRoute):
|
|
| 61 |
|
| 62 |
return door
|
| 63 |
|
| 64 |
-
from deps import (Session, assistant_read_scope, assistant_source_status, err,
|
| 65 |
module_gate, require_session)
|
| 66 |
|
| 67 |
# ⛔⛔ GATED AS A ROUTER, BECAUSE UNTIL 2026-08-18 THESE ROUTES HAD NO MODULE GATE AT
|
|
@@ -133,9 +133,24 @@ _CHAT_WORDS = (
|
|
| 133 |
"try talk chat explain database table field data workspace loopable assistant loopy app question answer"
|
| 134 |
)
|
| 135 |
CONVERSATION_PROMPT = (
|
| 136 |
-
"\
|
| 137 |
-
"
|
|
|
|
| 138 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
QUERY_DRAFT_ADMIN = "This database has no approved fields yet. Pick the ones this answer should use and they will be approved for everyone."
|
| 140 |
QUERY_DRAFT_MEMBER = "This database has no approved fields yet. Pick the ones you need and your administrators will be asked to approve them."
|
| 141 |
QUERY_DRAFT_SENT = "Sent to your administrators. Ask again once one of them approves."
|
|
@@ -1048,11 +1063,9 @@ def _call_model(question, snapshot, model=MODEL_AUTO, chat=None, history=None, s
|
|
| 1048 |
if turn.get("context"):
|
| 1049 |
prompt += "\n\n" + turn["context"]
|
| 1050 |
if turn.get("conversation"):
|
| 1051 |
-
|
| 1052 |
-
|
| 1053 |
-
|
| 1054 |
-
f" The databases {who} can open: " + "; ".join(names) + "."
|
| 1055 |
-
if names else f" {who.capitalize()} can open no database yet.")
|
| 1056 |
if turn.get("toolShareRequests"):
|
| 1057 |
prompt += ("\nThis turn concerns a pending tool-share approval only. Use the prior conversation to identify "
|
| 1058 |
"the request. Ask which request when the user's approval is ambiguous. "
|
|
@@ -2098,7 +2111,7 @@ def _submit_active(body, session, question, selected_model, context_hint, dock,
|
|
| 2098 |
turn["memoryOnly"] = True
|
| 2099 |
if conversation:
|
| 2100 |
turn["conversation"] = True
|
| 2101 |
-
turn["conversationSources"] =
|
| 2102 |
turn["memoryCompanyRequested"] = assistant_memory.company_requested(question)
|
| 2103 |
if snapshot is not None:
|
| 2104 |
turn["memorySources"] = [{"database": snapshot["database"],
|
|
@@ -2417,7 +2430,48 @@ def _recorded_artifacts(session):
|
|
| 2417 |
yield from assistant_artifacts._rows(session.runtime, h, index)
|
| 2418 |
|
| 2419 |
|
| 2420 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2421 |
actions = ["run"]
|
| 2422 |
if item["kind"] in ("sheet", "view", "query") and item.get("ref"):
|
| 2423 |
actions.insert(0, "open")
|
|
@@ -2425,6 +2479,15 @@ def _artifact_actions(item):
|
|
| 2425 |
actions.extend(["resume" if item.get("status") == "paused" else "pause", "delete"])
|
| 2426 |
elif item["kind"] in ("view", "query") and item.get("ref"):
|
| 2427 |
actions.append("delete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2428 |
return actions
|
| 2429 |
|
| 2430 |
|
|
@@ -2466,7 +2529,7 @@ def _artifact_rows(session, views_only=False):
|
|
| 2466 |
"database": view["source"]["database"], "question": view["question"]}}})
|
| 2467 |
for row in rows:
|
| 2468 |
row["title"] = _no_dashes(row["title"])[:200]
|
| 2469 |
-
row["actions"] = _artifact_actions(row)
|
| 2470 |
return sorted(rows, key=lambda row: (row["createdAt"], row["id"]), reverse=True)
|
| 2471 |
|
| 2472 |
|
|
@@ -2504,7 +2567,7 @@ def _artifact_item(session, item_id):
|
|
| 2504 |
if stored and stored["status"] != "deleted":
|
| 2505 |
if stored["kind"] == "query":
|
| 2506 |
_artifact_or_404(session, stored["ref"])
|
| 2507 |
-
stored["actions"] = _artifact_actions(stored)
|
| 2508 |
return stored
|
| 2509 |
item = next((row for row in _artifact_rows(session) if row["id"] == item_id), None)
|
| 2510 |
if item is None:
|
|
@@ -2622,18 +2685,23 @@ def resume_artifact(id: str, session: Session = Depends(require_session)):
|
|
| 2622 |
def delete_artifact(id: str, session: Session = Depends(require_session)):
|
| 2623 |
import agent_runtime
|
| 2624 |
item = _artifact_item(session, id)
|
|
|
|
|
|
|
| 2625 |
if item["kind"] == "schedule":
|
| 2626 |
name, args = "schedule_delete", {"id": item["ref"]}
|
| 2627 |
elif item["kind"] == "view":
|
| 2628 |
-
name, args = "
|
| 2629 |
elif item["kind"] == "query":
|
| 2630 |
name, args = "query_view_delete", {"qid": item["ref"]}
|
|
|
|
|
|
|
| 2631 |
else:
|
| 2632 |
raise err(400, "not_deletable", "This artifact cannot be deleted here.")
|
| 2633 |
pending = agent_runtime._pending(session, "app", name, args)
|
| 2634 |
-
if item["kind"] in ("query", "schedule"):
|
| 2635 |
# The artifact carries the name the reader knows; a schedule id or a Query id is not one.
|
| 2636 |
-
noun = "Query answer" if item["kind"] == "query" else "
|
|
|
|
| 2637 |
pending["summary"] = f"Remove {noun} {_no_dashes(item['title']).rstrip('. ')}."
|
| 2638 |
def name_request(raw):
|
| 2639 |
current = copy.deepcopy(raw)
|
|
@@ -2647,6 +2715,79 @@ def delete_artifact(id: str, session: Session = Depends(require_session)):
|
|
| 2647 |
return {"pendingDelete": pending}
|
| 2648 |
|
| 2649 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2650 |
def _settle_artifact_delete(session, call):
|
| 2651 |
kind = {"schedule_delete": "schedule", "view_delete": "view", "query_view_delete": "query"}.get(call["name"])
|
| 2652 |
if kind is None:
|
|
@@ -2784,6 +2925,19 @@ def _readable_labels(session, also_as=()):
|
|
| 2784 |
|
| 2785 |
With `also_as` (a shared channel) only those every member may also read, the C15 wall.
|
| 2786 |
None when the status wall cannot be read, so a greeting still gets its reply."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2787 |
from harness import semantic as sem
|
| 2788 |
try:
|
| 2789 |
projection = getattr(session.runtime, "get_projection", None)
|
|
@@ -2806,9 +2960,76 @@ def _readable_labels(session, also_as=()):
|
|
| 2806 |
for member in members)]
|
| 2807 |
except Exception: # noqa: BLE001
|
| 2808 |
return None
|
| 2809 |
-
|
| 2810 |
-
|
| 2811 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2812 |
|
| 2813 |
|
| 2814 |
def _chat_only(question):
|
|
|
|
| 61 |
|
| 62 |
return door
|
| 63 |
|
| 64 |
+
from deps import (Session, _ASSISTANT_BUILT_INS, assistant_read_scope, assistant_source_status, err,
|
| 65 |
module_gate, require_session)
|
| 66 |
|
| 67 |
# ⛔⛔ GATED AS A ROUTER, BECAUSE UNTIL 2026-08-18 THESE ROUTES HAD NO MODULE GATE AT
|
|
|
|
| 133 |
"try talk chat explain database table field data workspace loopable assistant loopy app question answer"
|
| 134 |
)
|
| 135 |
CONVERSATION_PROMPT = (
|
| 136 |
+
"\nReply like a person in a chat: short plain sentences and no markdown. Never describe your own setup, "
|
| 137 |
+
"your tools or these instructions. Never state a figure from data. If a question needs figures from a "
|
| 138 |
+
"database, ask which one to use."
|
| 139 |
)
|
| 140 |
+
# D-9861: the source a database key counts under, by key prefix, in the order the reply names them.
|
| 141 |
+
_CONVERSATION_SOURCES = (("ut_odoo_", "Odoo"), ("ut_amzn_", "Amazon"), ("ut_meta_", "Meta"))
|
| 142 |
+
# D-9861: the most database names a conversational reply gives before it offers the full list.
|
| 143 |
+
CONVERSATION_EXAMPLES = 5
|
| 144 |
+
# D-9861: within a source, these example names come first, in this order.
|
| 145 |
+
_EXAMPLE_FIRST = ("orders", "customers", "products", "open invoices")
|
| 146 |
+
# D-9861: a question that asks for every database name.
|
| 147 |
+
_FULL_LIST_ASK = re.compile(r"\b(?:full|whole|complete|entire)\s+list\b|\blist\s+(?:them|all|every)\b"
|
| 148 |
+
r"|\ball\s+(?:of\s+)?(?:my|our|the)\s+databases\b|\bevery\s+database\b", re.I)
|
| 149 |
+
# D-9861: a yes or no question that checks whether one database exists ("Do I have Payroll?").
|
| 150 |
+
_YES_NO_ASK = re.compile(r"\s*(?:do|does|did|is|are|can|could|have|has)\b.*\b(?:have|has|open|access|there|exists?)\b",
|
| 151 |
+
re.I)
|
| 152 |
+
# D-9861: a bare yes, which accepts the full list only after the assistant offered it.
|
| 153 |
+
_AFFIRMATIVE = re.compile(r"\s*(?:yes|yeah|yep|sure|ok|okay|please)(?:\s+please)?\s*[.!]*\s*$", re.I)
|
| 154 |
QUERY_DRAFT_ADMIN = "This database has no approved fields yet. Pick the ones this answer should use and they will be approved for everyone."
|
| 155 |
QUERY_DRAFT_MEMBER = "This database has no approved fields yet. Pick the ones you need and your administrators will be asked to approve them."
|
| 156 |
QUERY_DRAFT_SENT = "Sent to your administrators. Ask again once one of them approves."
|
|
|
|
| 1063 |
if turn.get("context"):
|
| 1064 |
prompt += "\n\n" + turn["context"]
|
| 1065 |
if turn.get("conversation"):
|
| 1066 |
+
prompt += CONVERSATION_PROMPT + _conversation_databases(turn.get("conversationSources"),
|
| 1067 |
+
bool(turn.get("also_as")),
|
| 1068 |
+
_asks_full_list(question, history))
|
|
|
|
|
|
|
| 1069 |
if turn.get("toolShareRequests"):
|
| 1070 |
prompt += ("\nThis turn concerns a pending tool-share approval only. Use the prior conversation to identify "
|
| 1071 |
"the request. Ask which request when the user's approval is ambiguous. "
|
|
|
|
| 2111 |
turn["memoryOnly"] = True
|
| 2112 |
if conversation:
|
| 2113 |
turn["conversation"] = True
|
| 2114 |
+
turn["conversationSources"] = _readable_sources(session, also_as)
|
| 2115 |
turn["memoryCompanyRequested"] = assistant_memory.company_requested(question)
|
| 2116 |
if snapshot is not None:
|
| 2117 |
turn["memorySources"] = [{"database": snapshot["database"],
|
|
|
|
| 2430 |
yield from assistant_artifacts._rows(session.runtime, h, index)
|
| 2431 |
|
| 2432 |
|
| 2433 |
+
def _artifact_can_edit(session, item, *, query_views=None):
|
| 2434 |
+
"""Resolve current item authority before offering or executing an Artifact mutation."""
|
| 2435 |
+
try:
|
| 2436 |
+
session.require("analyst")
|
| 2437 |
+
if not session.uname or item.get("status") == "deleted":
|
| 2438 |
+
return False
|
| 2439 |
+
kind, ref = item["kind"], item.get("ref")
|
| 2440 |
+
owner = assistant_log._hash(session.runtime, session.uname)
|
| 2441 |
+
legacy_query = kind == "query" and item["id"] == "query:" + str(ref)
|
| 2442 |
+
# Every caller already resolved the row through this principal's catalog or Query namespace.
|
| 2443 |
+
if not legacy_query and assistant_artifacts._seq(owner, item["id"]) is None:
|
| 2444 |
+
return False
|
| 2445 |
+
if kind == "query":
|
| 2446 |
+
if query_views is not None:
|
| 2447 |
+
# The list already admitted these sources in its current permission batch.
|
| 2448 |
+
return ref in query_views
|
| 2449 |
+
_artifact_or_404(session, ref)
|
| 2450 |
+
return True
|
| 2451 |
+
if kind == "view":
|
| 2452 |
+
from routes_starred import _find_view
|
| 2453 |
+
from routes_starred import _home_view_row
|
| 2454 |
+
found = _find_view(session, ref, item["toolCall"]["args"].get("database") or "")
|
| 2455 |
+
return bool(found and _home_view_row(session, found)["capabilities"]["edit"])
|
| 2456 |
+
if kind == "sheet":
|
| 2457 |
+
from core import sheets
|
| 2458 |
+
import routes_sheet
|
| 2459 |
+
book = sheets.get_workbook(session.runtime, ref)
|
| 2460 |
+
routes_sheet._require_reader(session, book)
|
| 2461 |
+
return routes_sheet._role_on_workbook(session, book) in ("owner", "edit")
|
| 2462 |
+
if kind == "schedule":
|
| 2463 |
+
import schedules
|
| 2464 |
+
schedule = schedules.get(session.runtime, ref)
|
| 2465 |
+
return bool(schedule and schedule.get("owner") == session.uname and not schedule.get("system"))
|
| 2466 |
+
if kind == "snapshot":
|
| 2467 |
+
# The owner's own frozen copy; its content stays behind the serve route's source check.
|
| 2468 |
+
return item.get("status") == "active"
|
| 2469 |
+
return kind in ("report", "run", "pdf", "xlsx", "image")
|
| 2470 |
+
except Exception:
|
| 2471 |
+
return False
|
| 2472 |
+
|
| 2473 |
+
|
| 2474 |
+
def _artifact_actions(item, session=None, *, query_views=None):
|
| 2475 |
actions = ["run"]
|
| 2476 |
if item["kind"] in ("sheet", "view", "query") and item.get("ref"):
|
| 2477 |
actions.insert(0, "open")
|
|
|
|
| 2479 |
actions.extend(["resume" if item.get("status") == "paused" else "pause", "delete"])
|
| 2480 |
elif item["kind"] in ("view", "query") and item.get("ref"):
|
| 2481 |
actions.append("delete")
|
| 2482 |
+
if session is not None:
|
| 2483 |
+
# Extracted projections without an item authority resolver keep mutation actions closed.
|
| 2484 |
+
can_edit = globals().get("_artifact_can_edit")
|
| 2485 |
+
if callable(can_edit) and can_edit(session, item, query_views=query_views):
|
| 2486 |
+
actions.append("rename")
|
| 2487 |
+
if "delete" not in actions:
|
| 2488 |
+
actions.append("delete")
|
| 2489 |
+
else:
|
| 2490 |
+
actions = [action for action in actions if action not in ("delete", "pause", "resume")]
|
| 2491 |
return actions
|
| 2492 |
|
| 2493 |
|
|
|
|
| 2529 |
"database": view["source"]["database"], "question": view["question"]}}})
|
| 2530 |
for row in rows:
|
| 2531 |
row["title"] = _no_dashes(row["title"])[:200]
|
| 2532 |
+
row["actions"] = _artifact_actions(row, session, query_views=by_ref)
|
| 2533 |
return sorted(rows, key=lambda row: (row["createdAt"], row["id"]), reverse=True)
|
| 2534 |
|
| 2535 |
|
|
|
|
| 2567 |
if stored and stored["status"] != "deleted":
|
| 2568 |
if stored["kind"] == "query":
|
| 2569 |
_artifact_or_404(session, stored["ref"])
|
| 2570 |
+
stored["actions"] = _artifact_actions(stored, session)
|
| 2571 |
return stored
|
| 2572 |
item = next((row for row in _artifact_rows(session) if row["id"] == item_id), None)
|
| 2573 |
if item is None:
|
|
|
|
| 2685 |
def delete_artifact(id: str, session: Session = Depends(require_session)):
|
| 2686 |
import agent_runtime
|
| 2687 |
item = _artifact_item(session, id)
|
| 2688 |
+
if not _artifact_can_edit(session, item):
|
| 2689 |
+
raise err(403, "artifact_read_only", "Your current rights do not allow deleting this artifact.")
|
| 2690 |
if item["kind"] == "schedule":
|
| 2691 |
name, args = "schedule_delete", {"id": item["ref"]}
|
| 2692 |
elif item["kind"] == "view":
|
| 2693 |
+
name, args = "artifact_entry_delete", {"id": item["id"]}
|
| 2694 |
elif item["kind"] == "query":
|
| 2695 |
name, args = "query_view_delete", {"qid": item["ref"]}
|
| 2696 |
+
elif item["kind"] in ("sheet", "report", "run", "pdf", "xlsx", "image", "snapshot"):
|
| 2697 |
+
name, args = "artifact_entry_delete", {"id": item["id"]}
|
| 2698 |
else:
|
| 2699 |
raise err(400, "not_deletable", "This artifact cannot be deleted here.")
|
| 2700 |
pending = agent_runtime._pending(session, "app", name, args)
|
| 2701 |
+
if item["kind"] in ("query", "schedule", "view", "sheet", "report", "run", "pdf", "xlsx", "image", "snapshot"):
|
| 2702 |
# The artifact carries the name the reader knows; a schedule id or a Query id is not one.
|
| 2703 |
+
noun = ("Query answer" if item["kind"] == "query" else item["kind"]
|
| 2704 |
+
if item["kind"] in ("schedule", "view") else "artifact")
|
| 2705 |
pending["summary"] = f"Remove {noun} {_no_dashes(item['title']).rstrip('. ')}."
|
| 2706 |
def name_request(raw):
|
| 2707 |
current = copy.deepcopy(raw)
|
|
|
|
| 2715 |
return {"pendingDelete": pending}
|
| 2716 |
|
| 2717 |
|
| 2718 |
+
def _update_artifact_entry(session, item_id, patch):
|
| 2719 |
+
"""Update only this principal's catalog entry while preserving its backing object."""
|
| 2720 |
+
owner = assistant_log._hash(session.runtime, session.uname)
|
| 2721 |
+
seq = assistant_artifacts._seq(owner, item_id)
|
| 2722 |
+
if seq is None:
|
| 2723 |
+
raise err(404, "unknown_artifact", "That artifact does not exist.")
|
| 2724 |
+
saved = {}
|
| 2725 |
+
|
| 2726 |
+
def update(raw):
|
| 2727 |
+
saved.clear()
|
| 2728 |
+
doc = copy.deepcopy(raw or {})
|
| 2729 |
+
row = next((row for row in doc.get("items", []) if row.get("id") == item_id), None)
|
| 2730 |
+
if row is None or row.get("status") == "deleted":
|
| 2731 |
+
raise err(404, "unknown_artifact", "That artifact does not exist.")
|
| 2732 |
+
if not _artifact_can_edit(session, row):
|
| 2733 |
+
raise err(403, "artifact_read_only", "Your current rights do not allow changing this artifact.")
|
| 2734 |
+
row.update(patch)
|
| 2735 |
+
saved.update(row)
|
| 2736 |
+
return doc
|
| 2737 |
+
|
| 2738 |
+
session.runtime.update(assistant_artifacts._chunk_key(owner, seq // assistant_artifacts.CHUNK_ITEMS),
|
| 2739 |
+
update, flush="sync")
|
| 2740 |
+
if not saved:
|
| 2741 |
+
raise err(503, "store_unavailable", "The artifact change could not be confirmed. Reload and try again.")
|
| 2742 |
+
saved["actions"] = _artifact_actions(saved, session)
|
| 2743 |
+
return saved
|
| 2744 |
+
|
| 2745 |
+
|
| 2746 |
+
@router.patch("/artifacts/{id}")
|
| 2747 |
+
def rename_artifact(id: str, body: dict = Body(default=None), session: Session = Depends(require_session)):
|
| 2748 |
+
"""Rename an authorized Artifact's label without changing its generated content."""
|
| 2749 |
+
title = (body or {}).get("title")
|
| 2750 |
+
if not isinstance(title, str) or not title.strip() or len(title.strip()) > 200:
|
| 2751 |
+
raise err(400, "bad_title", "Use a title of 1 to 200 characters.")
|
| 2752 |
+
item = _artifact_item(session, id)
|
| 2753 |
+
if not _artifact_can_edit(session, item):
|
| 2754 |
+
raise err(403, "artifact_read_only", "Your current rights do not allow renaming this artifact.")
|
| 2755 |
+
title = title.strip()
|
| 2756 |
+
if item["kind"] == "query":
|
| 2757 |
+
if len(title) > 60 or title != " ".join(title.split()):
|
| 2758 |
+
raise err(400, "bad_title", "Use a title of at most 60 characters with single spaces.")
|
| 2759 |
+
title = rename_query(item["ref"], {"name": title}, session)["name"]
|
| 2760 |
+
if id.startswith("query:"):
|
| 2761 |
+
return {"artifact": _artifact_item(session, id)}
|
| 2762 |
+
return {"artifact": _update_artifact_entry(session, id, {"title": title})}
|
| 2763 |
+
|
| 2764 |
+
|
| 2765 |
+
def _delete_artifact_entry(session, args):
|
| 2766 |
+
item = _artifact_item(session, args["id"])
|
| 2767 |
+
if item["kind"] == "view":
|
| 2768 |
+
if not _artifact_can_edit(session, item):
|
| 2769 |
+
raise err(403, "artifact_read_only", "Your current rights do not allow deleting this artifact.")
|
| 2770 |
+
from routes_starred import delete_home_view
|
| 2771 |
+
delete_home_view(item["toolCall"]["args"]["database"], item["ref"], None, session)
|
| 2772 |
+
assistant_artifacts.set_status(session.runtime, session.uname, item["id"], "deleted")
|
| 2773 |
+
return {"deleted": item["id"]}
|
| 2774 |
+
if item["kind"] not in ("sheet", "report", "run", "pdf", "xlsx", "image", "snapshot"):
|
| 2775 |
+
raise err(400, "not_deletable", "This artifact cannot be deleted through this action.")
|
| 2776 |
+
_update_artifact_entry(session, item["id"], {"status": "deleted"})
|
| 2777 |
+
return {"deleted": item["id"]}
|
| 2778 |
+
|
| 2779 |
+
|
| 2780 |
+
def _register_artifact_entry_delete():
|
| 2781 |
+
import agent_tools
|
| 2782 |
+
agent_tools.register("artifact_entry_delete", {
|
| 2783 |
+
"type": "object", "description": "Remove your Artifact entry after confirmation while retaining its backing data.",
|
| 2784 |
+
"properties": {"id": {"type": "string"}}, "required": ["id"], "additionalProperties": False,
|
| 2785 |
+
}, _delete_artifact_entry, effect="delete")
|
| 2786 |
+
|
| 2787 |
+
|
| 2788 |
+
_register_artifact_entry_delete()
|
| 2789 |
+
|
| 2790 |
+
|
| 2791 |
def _settle_artifact_delete(session, call):
|
| 2792 |
kind = {"schedule_delete": "schedule", "view_delete": "view", "query_view_delete": "query"}.get(call["name"])
|
| 2793 |
if kind is None:
|
|
|
|
| 2925 |
|
| 2926 |
With `also_as` (a shared channel) only those every member may also read, the C15 wall.
|
| 2927 |
None when the status wall cannot be read, so a greeting still gets its reply."""
|
| 2928 |
+
sources = _readable_sources(session, also_as)
|
| 2929 |
+
return None if sources is None else sorted(sources)
|
| 2930 |
+
|
| 2931 |
+
|
| 2932 |
+
def _database_source(database):
|
| 2933 |
+
"""The source a database key counts under: Odoo, Amazon, Meta, or "" for any other."""
|
| 2934 |
+
if database in _ASSISTANT_BUILT_INS:
|
| 2935 |
+
return "Odoo"
|
| 2936 |
+
return next((name for prefix, name in _CONVERSATION_SOURCES if database.startswith(prefix)), "")
|
| 2937 |
+
|
| 2938 |
+
|
| 2939 |
+
def _readable_sources(session, also_as=()):
|
| 2940 |
+
"""`_readable_labels` as {label: source}: None when the wall cannot be read, empty when nothing is open."""
|
| 2941 |
from harness import semantic as sem
|
| 2942 |
try:
|
| 2943 |
projection = getattr(session.runtime, "get_projection", None)
|
|
|
|
| 2960 |
for member in members)]
|
| 2961 |
except Exception: # noqa: BLE001
|
| 2962 |
return None
|
| 2963 |
+
sources = {}
|
| 2964 |
+
# A label two keys share keeps the source of the first key in sorted order.
|
| 2965 |
+
for database in sorted(permitted):
|
| 2966 |
+
label = (_said(statuses[database].get("label") or (definitions.get(database) or {}).get("label")
|
| 2967 |
+
or topics.get(database) or database.removeprefix("ut_").replace("_", " ")) or database)
|
| 2968 |
+
sources.setdefault(label, _database_source(database))
|
| 2969 |
+
return sources
|
| 2970 |
+
|
| 2971 |
+
|
| 2972 |
+
def _conversation_databases(sources, channel, full=False):
|
| 2973 |
+
"""The database facts a conversation prompt carries: exact counts, one ready answer, the list if asked."""
|
| 2974 |
+
if sources is None:
|
| 2975 |
+
return ""
|
| 2976 |
+
who = "Everyone in this channel" if channel else "This user"
|
| 2977 |
+
if not sources:
|
| 2978 |
+
return f" {who} can open no database yet."
|
| 2979 |
+
|
| 2980 |
+
def stripped(label, name):
|
| 2981 |
+
return label[len(name) + 1:] if name and label.startswith(name + " ") else label
|
| 2982 |
+
|
| 2983 |
+
def rank(label, name):
|
| 2984 |
+
short = stripped(label, name)
|
| 2985 |
+
first = short.lower()
|
| 2986 |
+
return (_EXAMPLE_FIRST.index(first) if first in _EXAMPLE_FIRST else len(_EXAMPLE_FIRST), len(short), short)
|
| 2987 |
+
|
| 2988 |
+
def count(number, noun):
|
| 2989 |
+
return f"{number} {noun}{'' if number == 1 else 's'}"
|
| 2990 |
+
|
| 2991 |
+
order = [name for _, name in _CONVERSATION_SOURCES] + [""]
|
| 2992 |
+
groups = [(name, sorted((label for label, source in sources.items() if source == name),
|
| 2993 |
+
key=lambda label, name=name: rank(label, name))) for name in order]
|
| 2994 |
+
groups = [(name, labels) for name, labels in groups if labels]
|
| 2995 |
+
n = len(sources)
|
| 2996 |
+
picked = {name: [] for name, _ in groups}
|
| 2997 |
+
for depth in range(CONVERSATION_EXAMPLES):
|
| 2998 |
+
for name, labels in groups:
|
| 2999 |
+
if depth < len(labels) and sum(map(len, picked.values())) < CONVERSATION_EXAMPLES:
|
| 3000 |
+
picked[name].append(stripped(labels[depth], name))
|
| 3001 |
+
only_other = [name for name, _ in groups] == [""]
|
| 3002 |
+
|
| 3003 |
+
def join(parts):
|
| 3004 |
+
return parts[0] if len(parts) == 1 else ", ".join(parts[:-1]) + " and " + parts[-1]
|
| 3005 |
+
|
| 3006 |
+
def part(name, labels, examples=False):
|
| 3007 |
+
head = (count(len(labels), "database") if only_other else
|
| 3008 |
+
f"{len(labels)} from {name}" if name else count(len(labels), "other"))
|
| 3009 |
+
return head + (f" ({', '.join(picked[name])})" if examples and picked[name] else "")
|
| 3010 |
+
|
| 3011 |
+
split = "" if only_other else ": " + join([part(name, labels) for name, labels in groups])
|
| 3012 |
+
phrase = join([part(name, labels, True) for name, labels in groups])
|
| 3013 |
+
subject = "Everyone in this channel" if channel else "You"
|
| 3014 |
+
answer = f"{subject} can open {phrase}." if only_other else f"{subject} can open {count(n, 'database')}: {phrase}."
|
| 3015 |
+
return (f" {who} can open exactly {count(n, 'database')}{split}."
|
| 3016 |
+
f' Asked what databases or data they have, reply with this and nothing more: "{answer} Want the full list?"'
|
| 3017 |
+
" Never list every database unless the user asks for the full list."
|
| 3018 |
+
+ (" The full list, for this question only: " + "; ".join(sorted(sources)) + "." if full else ""))
|
| 3019 |
+
|
| 3020 |
+
|
| 3021 |
+
def _asks_full_list(question, history):
|
| 3022 |
+
"""True when the question asks for every database name, checks for one, or says yes to the full list offer."""
|
| 3023 |
+
text = str(question or "").strip()
|
| 3024 |
+
if _FULL_LIST_ASK.search(text):
|
| 3025 |
+
return True
|
| 3026 |
+
if _YES_NO_ASK.match(text) and not _chat_only(text):
|
| 3027 |
+
return True
|
| 3028 |
+
if not _AFFIRMATIVE.match(text):
|
| 3029 |
+
return False
|
| 3030 |
+
said = next((str(turn.get("content") or "") for turn in reversed(history or ())
|
| 3031 |
+
if isinstance(turn, dict) and turn.get("role") == "assistant"), "")
|
| 3032 |
+
return "full list" in said.lower()
|
| 3033 |
|
| 3034 |
|
| 3035 |
def _chat_only(question):
|
api/routes_schedules.py
CHANGED
|
@@ -21,7 +21,8 @@ async def schedule_tick(request: Request):
|
|
| 21 |
def list_schedules(all: int = Query(0), session: Session = Depends(require_session)):
|
| 22 |
items = schedules.list_for(session.runtime, None if session.admin and all == 1 else session.uname)
|
| 23 |
return {"schedules": [item for item in items if item.get("owner") == session.uname
|
| 24 |
-
or (item.get("action") or {}).get("kind") != "query"]
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
@router.post("/schedules")
|
|
@@ -30,7 +31,7 @@ def create_schedule(body: dict = Body(...), session: Session = Depends(require_s
|
|
| 30 |
schedule_id = schedules.create(
|
| 31 |
session.runtime, session.uname, cron=body.get("cron"), tz=body.get("tz"),
|
| 32 |
action=body.get("action"), label=body.get("label", ""), once_at=body.get("once_at"),
|
| 33 |
-
trigger=body.get("trigger"))
|
| 34 |
except ValueError as exc:
|
| 35 |
raise err(400, "invalid_schedule", str(exc))
|
| 36 |
return {"id": schedule_id, "schedule": schedules.get(session.runtime, schedule_id)}
|
|
@@ -57,6 +58,51 @@ def _change_wall(session, schedule_id):
|
|
| 57 |
raise err(404, "no_schedule", "There is no schedule with that ID.")
|
| 58 |
if item.get("system") and not session.admin:
|
| 59 |
raise err(403, "system_schedule", "Only an administrator can change this schedule.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
| 62 |
@router.post("/schedules/{schedule_id}/pause")
|
|
|
|
| 21 |
def list_schedules(all: int = Query(0), session: Session = Depends(require_session)):
|
| 22 |
items = schedules.list_for(session.runtime, None if session.admin and all == 1 else session.uname)
|
| 23 |
return {"schedules": [item for item in items if item.get("owner") == session.uname
|
| 24 |
+
or (item.get("action") or {}).get("kind") != "query"],
|
| 25 |
+
"rules": schedules.RUN_RULES}
|
| 26 |
|
| 27 |
|
| 28 |
@router.post("/schedules")
|
|
|
|
| 31 |
schedule_id = schedules.create(
|
| 32 |
session.runtime, session.uname, cron=body.get("cron"), tz=body.get("tz"),
|
| 33 |
action=body.get("action"), label=body.get("label", ""), once_at=body.get("once_at"),
|
| 34 |
+
trigger=body.get("trigger"), rule=body.get("rule", "notify"))
|
| 35 |
except ValueError as exc:
|
| 36 |
raise err(400, "invalid_schedule", str(exc))
|
| 37 |
return {"id": schedule_id, "schedule": schedules.get(session.runtime, schedule_id)}
|
|
|
|
| 58 |
raise err(404, "no_schedule", "There is no schedule with that ID.")
|
| 59 |
if item.get("system") and not session.admin:
|
| 60 |
raise err(403, "system_schedule", "Only an administrator can change this schedule.")
|
| 61 |
+
return item
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _query_wall(session, schedule_id):
|
| 65 |
+
item = _change_wall(session, schedule_id)
|
| 66 |
+
if item.get("owner") != session.uname or item["action"]["kind"] != "query":
|
| 67 |
+
raise err(404, "no_schedule", "There is no schedule with that ID.")
|
| 68 |
+
return item
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
@router.get("/schedules/{schedule_id}/reviews")
|
| 72 |
+
def schedule_reviews(schedule_id: str, session: Session = Depends(require_session)):
|
| 73 |
+
import schedule_envelope
|
| 74 |
+
|
| 75 |
+
item = _query_wall(session, schedule_id)
|
| 76 |
+
try:
|
| 77 |
+
return {"reviews": schedule_envelope.reviews(session.runtime, item)}
|
| 78 |
+
except ValueError as exc:
|
| 79 |
+
raise err(400, "schedule_review_refused", str(exc)) from None
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@router.post("/schedules/{schedule_id}/reviews/{result_id}")
|
| 83 |
+
def decide_schedule_review(schedule_id: str, result_id: str, body: dict = Body(...),
|
| 84 |
+
session: Session = Depends(require_session)):
|
| 85 |
+
import schedule_envelope
|
| 86 |
+
|
| 87 |
+
item = _query_wall(session, schedule_id)
|
| 88 |
+
try:
|
| 89 |
+
return {"review": schedule_envelope.decide(session.runtime, item, result_id,
|
| 90 |
+
decision=body.get("decision"), digest=body.get("digest"))}
|
| 91 |
+
except ValueError as exc:
|
| 92 |
+
raise err(409, "schedule_review_refused", str(exc)) from None
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@router.post("/schedules/{schedule_id}/results/{result_id}/correction")
|
| 96 |
+
def correct_schedule_result(schedule_id: str, result_id: str, body: dict = Body(...),
|
| 97 |
+
session: Session = Depends(require_session)):
|
| 98 |
+
import schedule_envelope
|
| 99 |
+
|
| 100 |
+
item = _query_wall(session, schedule_id)
|
| 101 |
+
try:
|
| 102 |
+
correction = schedule_envelope.correct(session.runtime, item, result_id, body.get("change"))
|
| 103 |
+
return {"correction": correction}
|
| 104 |
+
except ValueError as exc:
|
| 105 |
+
raise err(400, "schedule_correction_refused", str(exc)) from None
|
| 106 |
|
| 107 |
|
| 108 |
@router.post("/schedules/{schedule_id}/pause")
|
api/routes_security.py
CHANGED
|
@@ -17,13 +17,13 @@ import re
|
|
| 17 |
import time
|
| 18 |
from typing import Callable
|
| 19 |
|
| 20 |
-
from fastapi import APIRouter, Depends, Query, Request, Response
|
| 21 |
from starlette.routing import Mount
|
| 22 |
from starlette.staticfiles import StaticFiles
|
| 23 |
|
| 24 |
import aios_session
|
| 25 |
from deps import Session, err
|
| 26 |
-
from core import audit
|
| 27 |
from routes_admin import admin_gate
|
| 28 |
|
| 29 |
router = APIRouter(prefix="/api/v1/security")
|
|
@@ -236,6 +236,37 @@ def security_posture(request: Request, session: Session = Depends(admin_gate)):
|
|
| 236 |
return build_posture(request, session.tenant)
|
| 237 |
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
@router.get("/events")
|
| 240 |
def security_log(response: Response, session: Session = Depends(admin_gate),
|
| 241 |
limit: int = Query(200, ge=1, le=audit.MAX_EVENTS)):
|
|
@@ -319,17 +350,20 @@ def _authentication_control():
|
|
| 319 |
refusals = {ast.unparse(node.test): node for node in ast.walk(function)
|
| 320 |
if isinstance(node, ast.If)}
|
| 321 |
for guard, code in (("not users.mfa_enrolled(rec)", "mfa_enrolment_required"),
|
| 322 |
-
("not code", "mfa_required"),
|
| 323 |
-
("not (factor_valid or recovery_code_used)", "mfa_invalid")):
|
| 324 |
block = refusals.get(guard)
|
| 325 |
covered = covered and block is not None and any(
|
| 326 |
isinstance(node, ast.Raise) and any(isinstance(value, ast.Constant) and value.value == code
|
| 327 |
for value in ast.walk(node))
|
| 328 |
for node in ast.walk(block))
|
|
|
|
|
|
|
|
|
|
| 329 |
met = (policy == (12, 10, 1, 90) and covered and
|
| 330 |
rules >= {"length", "uppercase", "lowercase", "digit", "symbol", "identifier"})
|
| 331 |
return _answer("met" if met else "not_met", (
|
| 332 |
-
"Runtime policy reads 12 characters, ten prior passwords and 1/90 day age limits;
|
| 333 |
if met else "The runtime password policy or inspected login factor block does not match the authentication baseline."))
|
| 334 |
|
| 335 |
|
|
|
|
| 17 |
import time
|
| 18 |
from typing import Callable
|
| 19 |
|
| 20 |
+
from fastapi import APIRouter, Body, Depends, Query, Request, Response
|
| 21 |
from starlette.routing import Mount
|
| 22 |
from starlette.staticfiles import StaticFiles
|
| 23 |
|
| 24 |
import aios_session
|
| 25 |
from deps import Session, err
|
| 26 |
+
from core import audit, users
|
| 27 |
from routes_admin import admin_gate
|
| 28 |
|
| 29 |
router = APIRouter(prefix="/api/v1/security")
|
|
|
|
| 236 |
return build_posture(request, session.tenant)
|
| 237 |
|
| 238 |
|
| 239 |
+
@router.get("/trusted-browser")
|
| 240 |
+
def trusted_browser_policy(response: Response, session: Session = Depends(admin_gate)):
|
| 241 |
+
response.headers["Cache-Control"] = "no-store"
|
| 242 |
+
try:
|
| 243 |
+
policy = users.trusted_browser_policy(
|
| 244 |
+
session.runtime._store(), session.runtime.store_key(users.TRUST_POLICY_KEY))
|
| 245 |
+
except Exception:
|
| 246 |
+
raise err(503, "security_policy_unavailable", "Browser sign in settings could not be loaded. Try again.") from None
|
| 247 |
+
return {"enabled": policy["enabled"]}
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
@router.put("/trusted-browser")
|
| 251 |
+
def set_trusted_browser_policy(response: Response, body: dict = Body(default=None),
|
| 252 |
+
session: Session = Depends(admin_gate)):
|
| 253 |
+
response.headers["Cache-Control"] = "no-store"
|
| 254 |
+
enabled = (body or {}).get("enabled")
|
| 255 |
+
if type(enabled) is not bool:
|
| 256 |
+
raise err(400, "bad_security_policy", "Choose whether browsers can be remembered.")
|
| 257 |
+
try:
|
| 258 |
+
policy = users.set_trusted_browser_policy(
|
| 259 |
+
session.runtime._store(), session.runtime.store_key(users.TRUST_POLICY_KEY), enabled)
|
| 260 |
+
except Exception:
|
| 261 |
+
raise err(503, "security_policy_unavailable", "Browser sign in settings could not be saved. Try again.") from None
|
| 262 |
+
try:
|
| 263 |
+
audit.record("config.changed", session.tenant, session.uname,
|
| 264 |
+
{"setting": "trusted_browser", "enabled": policy["enabled"]})
|
| 265 |
+
except audit.AuditRefused as exc:
|
| 266 |
+
print(f"[aios-api] {exc}")
|
| 267 |
+
return {"enabled": policy["enabled"]}
|
| 268 |
+
|
| 269 |
+
|
| 270 |
@router.get("/events")
|
| 271 |
def security_log(response: Response, session: Session = Depends(admin_gate),
|
| 272 |
limit: int = Query(200, ge=1, le=audit.MAX_EVENTS)):
|
|
|
|
| 350 |
refusals = {ast.unparse(node.test): node for node in ast.walk(function)
|
| 351 |
if isinstance(node, ast.If)}
|
| 352 |
for guard, code in (("not users.mfa_enrolled(rec)", "mfa_enrolment_required"),
|
| 353 |
+
("not code and (not remembered)", "mfa_required"),
|
| 354 |
+
("not (factor_valid or recovery_code_used or remembered)", "mfa_invalid")):
|
| 355 |
block = refusals.get(guard)
|
| 356 |
covered = covered and block is not None and any(
|
| 357 |
isinstance(node, ast.Raise) and any(isinstance(value, ast.Constant) and value.value == code
|
| 358 |
for value in ast.walk(node))
|
| 359 |
for node in ast.walk(block))
|
| 360 |
+
trust_calls = [node for node in ast.walk(function) if isinstance(node, ast.Call)
|
| 361 |
+
and ast.unparse(node.func) == "_admit_trusted_browser"]
|
| 362 |
+
covered = covered and len(trust_calls) == 1
|
| 363 |
met = (policy == (12, 10, 1, 90) and covered and
|
| 364 |
rules >= {"length", "uppercase", "lowercase", "digit", "symbol", "identifier"})
|
| 365 |
return _answer("met" if met else "not_met", (
|
| 366 |
+
"Runtime policy reads 12 characters, ten prior passwords and 1/90 day age limits; stored accounts require MFA enrollment and a factor or signed seven day browser proof, with a QA identity exception."
|
| 367 |
if met else "The runtime password policy or inspected login factor block does not match the authentication baseline."))
|
| 368 |
|
| 369 |
|
api/routes_sheet.py
CHANGED
|
@@ -1412,7 +1412,7 @@ _MISSING_CODES = ("workbook_missing", "folder_missing", "tab_missing", "unknown_
|
|
| 1412 |
#: it, which is what 403 means and is the only status a client can act on.
|
| 1413 |
#:
|
| 1414 |
#: `workbook_read_only` is a VIEWER at a write door; `workbook_owner_only` is a non-owner at a
|
| 1415 |
-
#: door C5 reserves for the owner (the two share doors
|
| 1416 |
#: because they send the reader to two different places: one asks for edit access, the other
|
| 1417 |
#: cannot be granted at all.
|
| 1418 |
_FORBIDDEN_CODES = ("workbook_read_only", "workbook_owner_only", "tenant_mismatch", "sheet_actor_required")
|
|
@@ -2980,25 +2980,10 @@ def read_sheet_catch_up(workbook_id: str, session: Session = Depends(require_ses
|
|
| 2980 |
status = _sheet_catch_up(session)
|
| 2981 |
sources = status.get("sources") if isinstance(status.get("sources"), dict) else {}
|
| 2982 |
behind = {name for name, entry in sources.items() if isinstance(entry, dict) and entry.get("behind")}
|
| 2983 |
-
held, bound
|
| 2984 |
if status.get("active") is True or behind:
|
| 2985 |
-
|
| 2986 |
-
|
| 2987 |
-
before_normalize=lambda doc: _require_reader(session, doc))
|
| 2988 |
-
except sheets.SheetRefusal as exc:
|
| 2989 |
-
raise _refuse(exc)
|
| 2990 |
-
for binding in book["bindings"]:
|
| 2991 |
-
if not binding.get("database"):
|
| 2992 |
-
continue
|
| 2993 |
-
# The page polls every 2 s, so each distinct database is checked once per read.
|
| 2994 |
-
if binding["database"] not in readable:
|
| 2995 |
-
try:
|
| 2996 |
-
_require_database(session, binding["database"])
|
| 2997 |
-
readable[binding["database"]] = _database_source(session, binding["database"])
|
| 2998 |
-
except HTTPException:
|
| 2999 |
-
readable[binding["database"]] = HTTPException
|
| 3000 |
-
if readable[binding["database"]] is HTTPException:
|
| 3001 |
-
continue
|
| 3002 |
try:
|
| 3003 |
bound.add(readable[binding["database"]])
|
| 3004 |
if status.get("active") is True and _binding_catching_up(session, binding):
|
|
@@ -3023,13 +3008,94 @@ def read_sheet_catch_up(workbook_id: str, session: Session = Depends(require_ses
|
|
| 3023 |
return {"catchUp": {"active": False, "behind": lines} if lines else {"active": False}}
|
| 3024 |
|
| 3025 |
|
| 3026 |
-
|
| 3027 |
-
|
| 3028 |
from core import sheets
|
|
|
|
| 3029 |
try:
|
| 3030 |
-
|
|
|
|
| 3031 |
except sheets.SheetRefusal as exc:
|
| 3032 |
raise _refuse(exc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3033 |
|
| 3034 |
|
| 3035 |
@router.post("/sheets/{workbook_id}/refresh-history/{refresh_id}/retry")
|
|
@@ -3443,18 +3509,10 @@ def duplicate_sheet_tab(workbook_id: str, tab: str, body: dict = Body(default=No
|
|
| 3443 |
|
| 3444 |
@router.delete("/sheets/{workbook_id}")
|
| 3445 |
def drop_sheet(workbook_id: str, session: Session = Depends(require_session)):
|
| 3446 |
-
"""Delete a workbook
|
| 3447 |
-
|
| 3448 |
-
⛔ AN `edit` GRANT IS PERMISSION TO CHANGE THE CONTENT, NOT TO DESTROY THE OBJECT, and the
|
| 3449 |
-
fail-closed reading is the one that ships: an editor who deletes the owner's workbook has
|
| 3450 |
-
taken it from everybody it was shared with, and nothing in this system can put it back. The
|
| 3451 |
-
refusal is `workbook_owner_only` rather than `workbook_read_only` because the sentences send
|
| 3452 |
-
the reader to two different places, and telling an editor their workbook is read only would be
|
| 3453 |
-
flatly false.
|
| 3454 |
-
"""
|
| 3455 |
from core import sheets
|
| 3456 |
try:
|
| 3457 |
-
|
| 3458 |
removed = sheets.delete_workbook(_store(session), workbook_id)
|
| 3459 |
except sheets.SheetRefusal as exc:
|
| 3460 |
raise _refuse(exc)
|
|
@@ -5896,7 +5954,7 @@ def _drill_draft_binding(cell, binding, draft_filter):
|
|
| 5896 |
return binding
|
| 5897 |
if (binding.get("scope") or {}).get("view_id"):
|
| 5898 |
raise err(409, "drill_draft_view",
|
| 5899 |
-
"This cell reads a saved view, so its filter is changed in
|
| 5900 |
try:
|
| 5901 |
tree = json.loads(draft_filter)
|
| 5902 |
except ValueError:
|
|
|
|
| 1412 |
#: it, which is what 403 means and is the only status a client can act on.
|
| 1413 |
#:
|
| 1414 |
#: `workbook_read_only` is a VIEWER at a write door; `workbook_owner_only` is a non-owner at a
|
| 1415 |
+
#: door C5 reserves for the owner (the two share doors). Two codes rather than one
|
| 1416 |
#: because they send the reader to two different places: one asks for edit access, the other
|
| 1417 |
#: cannot be granted at all.
|
| 1418 |
_FORBIDDEN_CODES = ("workbook_read_only", "workbook_owner_only", "tenant_mismatch", "sheet_actor_required")
|
|
|
|
| 2980 |
status = _sheet_catch_up(session)
|
| 2981 |
sources = status.get("sources") if isinstance(status.get("sources"), dict) else {}
|
| 2982 |
behind = {name for name, entry in sources.items() if isinstance(entry, dict) and entry.get("behind")}
|
| 2983 |
+
held, bound = set(), set()
|
| 2984 |
if status.get("active") is True or behind:
|
| 2985 |
+
for binding, readable in _workbook_binding_sources(
|
| 2986 |
+
session, workbook_id, expected_rev=book.get("rev")):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2987 |
try:
|
| 2988 |
bound.add(readable[binding["database"]])
|
| 2989 |
if status.get("active") is True and _binding_catching_up(session, binding):
|
|
|
|
| 3008 |
return {"catchUp": {"active": False, "behind": lines} if lines else {"active": False}}
|
| 3009 |
|
| 3010 |
|
| 3011 |
+
def _workbook_binding_sources(session, workbook_id, *, expected_rev=None, include_related=False):
|
| 3012 |
+
"""Walk every admitted tab's bindings with one permission snapshot and one check per database."""
|
| 3013 |
from core import sheets
|
| 3014 |
+
from modules import sheet as sheet_mod
|
| 3015 |
try:
|
| 3016 |
+
book = sheets.get_workbook_bindings(_store(session), workbook_id, expected_rev=expected_rev,
|
| 3017 |
+
before_normalize=lambda doc: _require_reader(session, doc))
|
| 3018 |
except sheets.SheetRefusal as exc:
|
| 3019 |
raise _refuse(exc)
|
| 3020 |
+
if not book["bindings"]:
|
| 3021 |
+
return
|
| 3022 |
+
readable, relations, views = {}, {}, {}
|
| 3023 |
+
_capture_sheet_permissions(session)
|
| 3024 |
+
|
| 3025 |
+
def admit(database):
|
| 3026 |
+
if database not in readable:
|
| 3027 |
+
try:
|
| 3028 |
+
_require_database(session, database)
|
| 3029 |
+
readable[database] = _database_source(session, database)
|
| 3030 |
+
except HTTPException:
|
| 3031 |
+
readable[database] = HTTPException
|
| 3032 |
+
return readable[database] is not HTTPException
|
| 3033 |
+
|
| 3034 |
+
for binding in book["bindings"]:
|
| 3035 |
+
database = binding.get("database")
|
| 3036 |
+
if not database or not admit(database):
|
| 3037 |
+
continue
|
| 3038 |
+
sources = {database: readable[database]}
|
| 3039 |
+
if include_related:
|
| 3040 |
+
try:
|
| 3041 |
+
nodes, _ = _binding_filter_nodes(session, database, binding, memo=views)
|
| 3042 |
+
except HTTPException:
|
| 3043 |
+
nodes = []
|
| 3044 |
+
for via in sheet_mod.related_vias(nodes):
|
| 3045 |
+
slot = (database, via)
|
| 3046 |
+
if slot not in relations:
|
| 3047 |
+
try:
|
| 3048 |
+
relations[slot] = _binding_related_entry(session, database, via)["target"]
|
| 3049 |
+
except HTTPException:
|
| 3050 |
+
relations[slot] = None
|
| 3051 |
+
target = relations[slot]
|
| 3052 |
+
if target and admit(target):
|
| 3053 |
+
sources[target] = readable[target]
|
| 3054 |
+
yield binding, sources
|
| 3055 |
+
|
| 3056 |
+
|
| 3057 |
+
def _sheet_refresh_sources(session, workbook_id):
|
| 3058 |
+
"""Describe readable bound databases using connector status, never lifecycle time as sync time."""
|
| 3059 |
+
import odoo_relational as rel
|
| 3060 |
+
import routes_connectors as connectors
|
| 3061 |
+
from core import user_tables
|
| 3062 |
+
databases = {}
|
| 3063 |
+
for _binding, sources in _workbook_binding_sources(session, workbook_id, include_related=True):
|
| 3064 |
+
databases.update(sources)
|
| 3065 |
+
if not databases:
|
| 3066 |
+
return []
|
| 3067 |
+
definitions = user_tables.all_defs(st=_sheet_permission_reader(session))
|
| 3068 |
+
statuses, result = {}, []
|
| 3069 |
+
for database, source in databases.items():
|
| 3070 |
+
provider = {"odoo": "odoo", "amazon": "amazon_sp", "meta": "meta_ads"}.get(source)
|
| 3071 |
+
connection = rel.connection_for_table_key(database, session.runtime) if source == "odoo" else None
|
| 3072 |
+
slot = (provider, connection)
|
| 3073 |
+
if provider and slot not in statuses:
|
| 3074 |
+
statuses[slot] = connectors.connector_sync_status(
|
| 3075 |
+
provider, connection_id=connection, session=session)
|
| 3076 |
+
status = statuses.get(slot) or {}
|
| 3077 |
+
schedule = status.get("schedule") or status.get("syncEvery")
|
| 3078 |
+
if source == "odoo" and not schedule:
|
| 3079 |
+
schedule = rel.read_config(session.runtime)["syncEvery"]
|
| 3080 |
+
stamp = (status.get("lastSyncAt") if "lastSyncAt" in status else
|
| 3081 |
+
status.get("lastCompletedAt") or status.get("lastSuccess"))
|
| 3082 |
+
result.append({"database": database,
|
| 3083 |
+
"label": _related_filter_database_label(session, definitions, database),
|
| 3084 |
+
"connector": _SOURCE_LABELS.get(source),
|
| 3085 |
+
"cadence": {"daily": "Daily", "manual": "Manual"}.get(schedule),
|
| 3086 |
+
"lastSyncAt": connectors._sync_stamp(stamp)})
|
| 3087 |
+
return result
|
| 3088 |
+
|
| 3089 |
+
|
| 3090 |
+
@router.get("/sheets/{workbook_id}/refresh-history")
|
| 3091 |
+
def read_sheet_refresh_history(workbook_id: str, session: Session = Depends(require_session)):
|
| 3092 |
+
from core import sheets
|
| 3093 |
+
with _sheet_request_scope(session):
|
| 3094 |
+
try:
|
| 3095 |
+
history = sheets.refresh_history(_store(session), workbook_id, session)
|
| 3096 |
+
except sheets.SheetRefusal as exc:
|
| 3097 |
+
raise _refuse(exc)
|
| 3098 |
+
return {**history, "sources": _sheet_refresh_sources(session, workbook_id)}
|
| 3099 |
|
| 3100 |
|
| 3101 |
@router.post("/sheets/{workbook_id}/refresh-history/{refresh_id}/retry")
|
|
|
|
| 3509 |
|
| 3510 |
@router.delete("/sheets/{workbook_id}")
|
| 3511 |
def drop_sheet(workbook_id: str, session: Session = Depends(require_session)):
|
| 3512 |
+
"""Delete a workbook through its current edit right under R26."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3513 |
from core import sheets
|
| 3514 |
try:
|
| 3515 |
+
_require_editor(session, sheets.get_workbook(_store(session), workbook_id))
|
| 3516 |
removed = sheets.delete_workbook(_store(session), workbook_id)
|
| 3517 |
except sheets.SheetRefusal as exc:
|
| 3518 |
raise _refuse(exc)
|
|
|
|
| 5954 |
return binding
|
| 5955 |
if (binding.get("scope") or {}).get("view_id"):
|
| 5956 |
raise err(409, "drill_draft_view",
|
| 5957 |
+
"This cell reads a saved view, so its filter is changed in Table.")
|
| 5958 |
try:
|
| 5959 |
tree = json.loads(draft_filter)
|
| 5960 |
except ValueError:
|
api/routes_slack.py
CHANGED
|
@@ -394,6 +394,96 @@ def permission_safe_automation_digest(rt, context, *, module="", fields=None, ro
|
|
| 394 |
return "\n".join(lines)[:3200]
|
| 395 |
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
def deliver_channel_reply(rt, channel, text, *, principal=None, automation_id=None,
|
| 398 |
provenance=None, post=None, team_id=None, blocks=None):
|
| 399 |
"""Send only after one channel/principal binding and an optional automation grant validate."""
|
|
@@ -415,14 +505,13 @@ def deliver_channel_reply(rt, channel, text, *, principal=None, automation_id=No
|
|
| 415 |
if post is None:
|
| 416 |
import requests
|
| 417 |
post = requests.post
|
| 418 |
-
response = post("https://slack.com/api/chat.postMessage", json=payload,
|
| 419 |
-
headers={"Authorization": "Bearer " + creds["bot_token"]},
|
| 420 |
-
timeout=10, allow_redirects=False)
|
| 421 |
try:
|
| 422 |
-
|
|
|
|
|
|
|
| 423 |
except Exception: # noqa: BLE001
|
| 424 |
-
sent
|
| 425 |
-
return {
|
| 426 |
context = channel_answer_context(rt, channel, principal=principal, automation_id=automation_id)
|
| 427 |
creds = slack_creds(rt, team_id) if team_id else slack_creds(rt)
|
| 428 |
if context is None:
|
|
@@ -434,14 +523,12 @@ def deliver_channel_reply(rt, channel, text, *, principal=None, automation_id=No
|
|
| 434 |
if post is None:
|
| 435 |
import requests
|
| 436 |
post = requests.post
|
| 437 |
-
response = post("https://slack.com/api/chat.postMessage", json=payload,
|
| 438 |
-
headers={"Authorization": "Bearer " + str(creds["bot_token"])}, timeout=10)
|
| 439 |
-
# Slack may return HTTP 200 with `{ok:false}`. A transport success is not a delivered reply.
|
| 440 |
try:
|
| 441 |
-
|
|
|
|
| 442 |
except Exception: # noqa: BLE001
|
| 443 |
-
|
| 444 |
-
return {
|
| 445 |
"writeApproval": context["writeApproval"], "provenance": approved}
|
| 446 |
|
| 447 |
# ── the credential ───────────────────────────────────────────────────────────────────────────
|
|
|
|
| 394 |
return "\n".join(lines)[:3200]
|
| 395 |
|
| 396 |
|
| 397 |
+
def _slack_digest_label(value, limit):
|
| 398 |
+
"""Escape a label without splitting entities or allowing Slack mentions and formatting."""
|
| 399 |
+
import unicodedata
|
| 400 |
+
raw = dash_norm.plain(str(value if value is not None else ""))
|
| 401 |
+
raw = " ".join("".join(" " if unicodedata.category(char).startswith("C") else char
|
| 402 |
+
for char in raw).split())
|
| 403 |
+
escapes = {"&": "&", "<": "<", ">": ">", "|": "¦", "@": "@",
|
| 404 |
+
"*": "∗", "_": "_", "~": "∼", "`": "'"}
|
| 405 |
+
tokens = [escapes.get(char, char) for char in raw]
|
| 406 |
+
if sum(map(len, tokens)) <= limit:
|
| 407 |
+
return "".join(tokens)
|
| 408 |
+
kept, size = [], 0
|
| 409 |
+
for token in tokens:
|
| 410 |
+
if size + len(token) > max(0, limit - 3):
|
| 411 |
+
break
|
| 412 |
+
kept.append(token)
|
| 413 |
+
size += len(token)
|
| 414 |
+
return "".join(kept) + "..." if kept else ""
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def permission_safe_view_entry_digest(rt, context, *, module, view, view_id, fields, rows,
|
| 418 |
+
provenance):
|
| 419 |
+
"""Compose one complete cohort only when every record and its identity field are readable."""
|
| 420 |
+
from urllib.parse import urlencode, urlsplit
|
| 421 |
+
agent = _agents(rt).get(str(context.get("agentId") or ""))
|
| 422 |
+
if not isinstance(agent, dict) or not agent_may_read(agent, module):
|
| 423 |
+
return None
|
| 424 |
+
identity = next((field for field in fields if field.get("pinned")), fields[0] if fields else {})
|
| 425 |
+
title_key = identity.get("key")
|
| 426 |
+
visible = agent_visible_fields(agent, fields, module, st=rt)
|
| 427 |
+
if not title_key or title_key not in {field.get("key") for field in visible}:
|
| 428 |
+
return None
|
| 429 |
+
safe_rows = agent_rows(agent, rows, module, fields, st=rt)
|
| 430 |
+
if not rows or len(safe_rows) != len(rows):
|
| 431 |
+
return None
|
| 432 |
+
# Reject the whole cohort before formatting titles or exposing a denied record count.
|
| 433 |
+
if any(not isinstance(row.get(title_key), (str, int, float))
|
| 434 |
+
or not _slack_digest_label(row.get(title_key), 120) for row in safe_rows):
|
| 435 |
+
return None
|
| 436 |
+
base = (os.environ.get("AIOS_PUBLIC_BASE") or os.environ.get("APP_BASE_URL") or "").rstrip("/")
|
| 437 |
+
try:
|
| 438 |
+
parsed = urlsplit(base)
|
| 439 |
+
if parsed.scheme not in ("https", "http") or not parsed.netloc or parsed.username \
|
| 440 |
+
or parsed.password or parsed.query or parsed.fragment \
|
| 441 |
+
or any(char.isspace() or char in "<>|" or ord(char) < 32 for char in base):
|
| 442 |
+
return None
|
| 443 |
+
except ValueError:
|
| 444 |
+
return None
|
| 445 |
+
view_name = _slack_digest_label(view.get("name"), 120)
|
| 446 |
+
if not view_name:
|
| 447 |
+
return None
|
| 448 |
+
count = len(safe_rows)
|
| 449 |
+
heading = f"{count} new record{'' if count == 1 else 's'} in {view_name}"
|
| 450 |
+
view_url = base + "/#/data?" + urlencode({"database": module, "view": view_id})
|
| 451 |
+
urls = [base + "/#/data?" + urlencode({"database": module, "record": str(row["pid"])})
|
| 452 |
+
for row in safe_rows[:20]]
|
| 453 |
+
overflow = f"\n<{view_url}|and {count - 20} more>" if count > 20 else ""
|
| 454 |
+
# Reserve every URL, delimiter, overflow and source footer before allocating title space.
|
| 455 |
+
budget = (4000 - len(source_footer(provenance)) - 2 - len(heading) - len(overflow)
|
| 456 |
+
- sum(len(url) + 4 for url in urls))
|
| 457 |
+
per_title = min(120, budget // len(urls))
|
| 458 |
+
if per_title < 8:
|
| 459 |
+
return None
|
| 460 |
+
titles = [_slack_digest_label(row[title_key], per_title) for row in safe_rows[:20]]
|
| 461 |
+
text = heading + "".join(f"\n<{url}|{title}>" for url, title in zip(urls, titles)) + overflow
|
| 462 |
+
return text
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def _slack_delivery_response(response):
|
| 466 |
+
"""Keep useful Slack refusal codes while excluding response bodies and credentials."""
|
| 467 |
+
try:
|
| 468 |
+
body = response.json()
|
| 469 |
+
except Exception: # noqa: BLE001
|
| 470 |
+
return {"sent": False, "reason": "malformed_response"}
|
| 471 |
+
if not isinstance(body, dict) or type(body.get("ok")) is not bool:
|
| 472 |
+
return {"sent": False, "reason": "malformed_response"}
|
| 473 |
+
if bool(getattr(response, "ok", False)) and body["ok"] is True:
|
| 474 |
+
return {"sent": True}
|
| 475 |
+
safe_codes = {"not_in_channel", "channel_not_found", "missing_scope", "invalid_auth",
|
| 476 |
+
"token_revoked", "account_inactive", "is_archived", "restricted_action",
|
| 477 |
+
"no_permission", "ratelimited", "rate_limited", "msg_too_long",
|
| 478 |
+
"invalid_blocks", "invalid_arguments", "internal_error", "fatal_error",
|
| 479 |
+
"request_timeout", "team_access_not_granted", "org_login_required",
|
| 480 |
+
"ekm_access_denied", "access_denied", "not_authed"}
|
| 481 |
+
error = body.get("error")
|
| 482 |
+
code = error if isinstance(error, str) and error in safe_codes else (
|
| 483 |
+
"http_error" if not getattr(response, "ok", False) else "slack_error")
|
| 484 |
+
return {"sent": False, "reason": code}
|
| 485 |
+
|
| 486 |
+
|
| 487 |
def deliver_channel_reply(rt, channel, text, *, principal=None, automation_id=None,
|
| 488 |
provenance=None, post=None, team_id=None, blocks=None):
|
| 489 |
"""Send only after one channel/principal binding and an optional automation grant validate."""
|
|
|
|
| 505 |
if post is None:
|
| 506 |
import requests
|
| 507 |
post = requests.post
|
|
|
|
|
|
|
|
|
|
| 508 |
try:
|
| 509 |
+
response = post("https://slack.com/api/chat.postMessage", json=payload,
|
| 510 |
+
headers={"Authorization": "Bearer " + creds["bot_token"]},
|
| 511 |
+
timeout=10, allow_redirects=False)
|
| 512 |
except Exception: # noqa: BLE001
|
| 513 |
+
return {"sent": False, "reason": "transport_error"}
|
| 514 |
+
return {**_slack_delivery_response(response), "channel": str(channel), "principal": principal}
|
| 515 |
context = channel_answer_context(rt, channel, principal=principal, automation_id=automation_id)
|
| 516 |
creds = slack_creds(rt, team_id) if team_id else slack_creds(rt)
|
| 517 |
if context is None:
|
|
|
|
| 523 |
if post is None:
|
| 524 |
import requests
|
| 525 |
post = requests.post
|
|
|
|
|
|
|
|
|
|
| 526 |
try:
|
| 527 |
+
response = post("https://slack.com/api/chat.postMessage", json=payload,
|
| 528 |
+
headers={"Authorization": "Bearer " + str(creds["bot_token"])}, timeout=10)
|
| 529 |
except Exception: # noqa: BLE001
|
| 530 |
+
return {"sent": False, "reason": "transport_error"}
|
| 531 |
+
return {**_slack_delivery_response(response), "channel": context["channel"], "principal": context["principal"],
|
| 532 |
"writeApproval": context["writeApproval"], "provenance": approved}
|
| 533 |
|
| 534 |
# ── the credential ───────────────────────────────────────────────────────────────────────────
|
api/routes_starred.py
CHANGED
|
@@ -34,7 +34,7 @@ import json
|
|
| 34 |
import time
|
| 35 |
from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
|
| 36 |
|
| 37 |
-
from fastapi import APIRouter, Body, Depends
|
| 38 |
|
| 39 |
import routes_nav
|
| 40 |
from deps import Session, err, require_session
|
|
@@ -304,7 +304,7 @@ def _refresh_one(session, cache, key):
|
|
| 304 |
return (order, docs, unread)
|
| 305 |
|
| 306 |
|
| 307 |
-
def _scan_starred_views(session, keys=None, cache=None, include=None):
|
| 308 |
"""`(rows, unread)` — every view this caller has starred, newest-worked database first.
|
| 309 |
|
| 310 |
A row is `{"id", "database", "name", "config"}`. ⚠ `config` is carried so the COUNTS route does
|
|
@@ -367,11 +367,12 @@ def _scan_starred_views(session, keys=None, cache=None, include=None):
|
|
| 367 |
doc = session.runtime.get_projection(ws_key, drop=("overlays", "fields")) or {}
|
| 368 |
docs[key] = doc
|
| 369 |
|
| 370 |
-
found = {}
|
| 371 |
own = doc.get(session.uname)
|
| 372 |
for vid, view in ((own.get("views") or {}) if isinstance(own, dict) else {}).items():
|
| 373 |
if isinstance(view, dict):
|
| 374 |
found[str(vid)] = view
|
|
|
|
| 375 |
|
| 376 |
for vid in pending:
|
| 377 |
owner = granted.get(vid)
|
|
@@ -391,12 +392,17 @@ def _scan_starred_views(session, keys=None, cache=None, include=None):
|
|
| 391 |
break
|
| 392 |
if isinstance(view, dict):
|
| 393 |
found.setdefault(vid, view)
|
|
|
|
| 394 |
resolved.add(vid)
|
| 395 |
for vid, view in found.items():
|
| 396 |
cfg = view.get("config") or {}
|
| 397 |
if (include(view) if include else cfg.get("important") is True):
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
return out, unread
|
| 401 |
|
| 402 |
|
|
@@ -516,15 +522,82 @@ def _payload(session, keys=None, cache=None):
|
|
| 516 |
|
| 517 |
@router.get("/nav/views")
|
| 518 |
def nav_views(session: Session = Depends(require_session)):
|
| 519 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
""
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
|
| 530 |
@router.get("/starred")
|
|
|
|
| 34 |
import time
|
| 35 |
from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
|
| 36 |
|
| 37 |
+
from fastapi import APIRouter, Body, Depends, Request
|
| 38 |
|
| 39 |
import routes_nav
|
| 40 |
from deps import Session, err, require_session
|
|
|
|
| 304 |
return (order, docs, unread)
|
| 305 |
|
| 306 |
|
| 307 |
+
def _scan_starred_views(session, keys=None, cache=None, include=None, with_rights=False):
|
| 308 |
"""`(rows, unread)` — every view this caller has starred, newest-worked database first.
|
| 309 |
|
| 310 |
A row is `{"id", "database", "name", "config"}`. ⚠ `config` is carried so the COUNTS route does
|
|
|
|
| 367 |
doc = session.runtime.get_projection(ws_key, drop=("overlays", "fields")) or {}
|
| 368 |
docs[key] = doc
|
| 369 |
|
| 370 |
+
found, owners = {}, {}
|
| 371 |
own = doc.get(session.uname)
|
| 372 |
for vid, view in ((own.get("views") or {}) if isinstance(own, dict) else {}).items():
|
| 373 |
if isinstance(view, dict):
|
| 374 |
found[str(vid)] = view
|
| 375 |
+
owners[str(vid)] = session.uname
|
| 376 |
|
| 377 |
for vid in pending:
|
| 378 |
owner = granted.get(vid)
|
|
|
|
| 392 |
break
|
| 393 |
if isinstance(view, dict):
|
| 394 |
found.setdefault(vid, view)
|
| 395 |
+
owners.setdefault(vid, owner if owner else candidate_owner)
|
| 396 |
resolved.add(vid)
|
| 397 |
for vid, view in found.items():
|
| 398 |
cfg = view.get("config") or {}
|
| 399 |
if (include(view) if include else cfg.get("important") is True):
|
| 400 |
+
row = {"id": vid, "database": key,
|
| 401 |
+
"name": str(view.get("name") or vid)[:120], "config": cfg}
|
| 402 |
+
if with_rights:
|
| 403 |
+
row.update(_home_view_row(
|
| 404 |
+
session, (key, ws_key, owners[vid], {**view, "id": vid}, None)))
|
| 405 |
+
out.append(row)
|
| 406 |
return out, unread
|
| 407 |
|
| 408 |
|
|
|
|
| 522 |
|
| 523 |
@router.get("/nav/views")
|
| 524 |
def nav_views(session: Session = Depends(require_session)):
|
| 525 |
+
"""Project readable saved View names and their current item rights."""
|
| 526 |
+
rows, unread = _scan_starred_views(session, include=lambda view: True, with_rights=True)
|
| 527 |
+
return {"views": [{key: row[key] for key in ("database", "id", "name", "capabilities")}
|
| 528 |
+
for row in rows], "unread": unread}
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
# S76-T17: the Home Views section's rename and delete live beside its GET, never in routes_nav (R7).
|
| 532 |
+
|
| 533 |
+
def _home_view_row(session, found):
|
| 534 |
+
"""Project only a saved View's identity, name and current edit right."""
|
| 535 |
+
database, _, owner, view, _ = found
|
| 536 |
+
editable = (view.get("id") != SYSTEM_VIEW_ID and view.get("kind") != "system"
|
| 537 |
+
and _may_star_view(session, owner, view))
|
| 538 |
+
return {"database": database, "id": view["id"], "name": str(view.get("name") or view["id"])[:120],
|
| 539 |
+
"capabilities": {"edit": bool(editable), "delete": bool(editable)}}
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
def _editable_home_view(session, database, view_id):
|
| 543 |
+
found = _find_view(session, view_id, database)
|
| 544 |
+
if found is None:
|
| 545 |
+
raise err(404, "view_not_found", "That saved View is no longer available.")
|
| 546 |
+
if not _home_view_row(session, found)["capabilities"]["edit"]:
|
| 547 |
+
raise err(403, "view_read_only", "Your current rights do not allow changes to this View.")
|
| 548 |
+
return found
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
@router.patch("/nav/views/{database}/{view_id}")
|
| 552 |
+
def rename_home_view(database: str, view_id: str, request: Request, body: dict = Body(default=None),
|
| 553 |
+
session: Session = Depends(require_session)):
|
| 554 |
+
"""Rename the canonical saved View without reading or returning database records."""
|
| 555 |
+
from core import table_store, view_audit, write_execution
|
| 556 |
+
from routes_tables import _audit_origin
|
| 557 |
+
|
| 558 |
+
name = (body or {}).get("name")
|
| 559 |
+
if not isinstance(name, str) or not name.strip() or len(name.strip()) > 120:
|
| 560 |
+
raise err(400, "bad_name", "Use a View name of 1 to 120 characters.")
|
| 561 |
+
found = _editable_home_view(session, database, view_id)
|
| 562 |
+
key, workspace, owner, _, cache = found
|
| 563 |
+
views = table_store.make(workspace, st=session.runtime)
|
| 564 |
+
saved, changes = {}, {}
|
| 565 |
+
|
| 566 |
+
def rename(data, audit):
|
| 567 |
+
saved.clear()
|
| 568 |
+
current = ((data.get(owner) or {}).get("views") or {}).get(view_id)
|
| 569 |
+
if not isinstance(current, dict):
|
| 570 |
+
raise err(404, "view_not_found", "That saved View is no longer available.")
|
| 571 |
+
if not _may_star_view(session, owner, current):
|
| 572 |
+
raise err(403, "view_read_only", "Your current rights do not allow changes to this View.")
|
| 573 |
+
names = [candidate.get("name") for who, personal in data.items()
|
| 574 |
+
if who not in table_store.RESERVED_MEMBERS and isinstance(personal, dict)
|
| 575 |
+
for candidate_id, candidate in (personal.get("views") or {}).items()
|
| 576 |
+
if candidate_id != view_id and isinstance(candidate, dict)]
|
| 577 |
+
updated = {**current, "name": table_store._unique_name(name.strip(), names),
|
| 578 |
+
"updatedAt": write_execution.utcnow().isoformat()} # S76-T37
|
| 579 |
+
changes.update(before=view_audit.summary(current), after=view_audit.summary(updated))
|
| 580 |
+
data[owner]["views"][view_id] = updated
|
| 581 |
+
audit(action=view_audit.derive_action(current, updated), owner=owner, view_id=view_id,
|
| 582 |
+
actor=session.uname, origin=_audit_origin(request), **changes)
|
| 583 |
+
saved.update(updated)
|
| 584 |
+
return data
|
| 585 |
|
| 586 |
+
views._audited_update(rename)
|
| 587 |
+
views.log_view_activity(view_id, "saved", by=session.uname, via=_audit_origin(request), **changes)
|
| 588 |
+
return {"view": _home_view_row(session, (key, workspace, owner, saved, cache))}
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
@router.delete("/nav/views/{database}/{view_id}")
|
| 592 |
+
def delete_home_view(database: str, view_id: str, request: Request, session: Session = Depends(require_session)):
|
| 593 |
+
"""Move the canonical saved View to trash through its own edit right."""
|
| 594 |
+
from core import table_store
|
| 595 |
+
from routes_tables import _audit_origin
|
| 596 |
+
|
| 597 |
+
_, workspace, owner, _, _ = _editable_home_view(session, database, view_id)
|
| 598 |
+
table_store.make(workspace, st=session.runtime).delete_view(
|
| 599 |
+
owner, view_id, actor=session.uname, origin=_audit_origin(request))
|
| 600 |
+
return {"deleted": view_id}
|
| 601 |
|
| 602 |
|
| 603 |
@router.get("/starred")
|
api/routes_tables.py
CHANGED
|
@@ -2957,9 +2957,13 @@ def add_row(table_key: str, body: dict = Body(default=None),
|
|
| 2957 |
if ut.ZONE_FIELD_KEY in values or "zone" in values:
|
| 2958 |
raise err(400, "zone_membership_read_only",
|
| 2959 |
"Zone membership is worked out from the record's current location")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2960 |
try:
|
| 2961 |
rid = ut.add_row(table_key, values, session.uname, st=session.runtime,
|
| 2962 |
-
rid=(body or {}).get("rid"), via=via)
|
| 2963 |
except ValueError as exc:
|
| 2964 |
# A core value rule (negative policy, link condition, server-owned cell) names its reason.
|
| 2965 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300] or "the row was refused")
|
|
@@ -2982,7 +2986,23 @@ def add_row(table_key: str, body: dict = Body(default=None),
|
|
| 2982 |
_refresh_relations(session)
|
| 2983 |
from routes_alerts import after_write
|
| 2984 |
after_write(session, table_key)
|
| 2985 |
-
return {"rid": rid, "pid": int(rid)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2986 |
|
| 2987 |
|
| 2988 |
@router.post("/tables/{table_key}/rows/import", status_code=201)
|
|
@@ -3041,6 +3061,10 @@ def import_rows(table_key: str, body: dict = Body(default=None),
|
|
| 3041 |
raise err(400, "bad_value", f"row {index + 1}: {why}. Nothing was imported")
|
| 3042 |
try:
|
| 3043 |
made = ut.add_rows(table_key, rows_in, session.uname, st=session.runtime)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3044 |
except Exception:
|
| 3045 |
raise err(503, "store_unavailable", "nothing was imported. The store refused")
|
| 3046 |
if made is None:
|
|
@@ -3127,6 +3151,33 @@ def _admit_if_sources(session, table_key, body, definition, fkey=''):
|
|
| 3127 |
raise err(403 if exc.code == 'if_source_forbidden' else 400, exc.code, str(exc))
|
| 3128 |
|
| 3129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3130 |
@router.post("/tables/{table_key}/fields", status_code=201)
|
| 3131 |
@_c8_write
|
| 3132 |
def add_field(table_key: str, body: dict = Body(default=None),
|
|
@@ -3134,6 +3185,7 @@ def add_field(table_key: str, body: dict = Body(default=None),
|
|
| 3134 |
_managed_or_refuse(session, table_key, mutation=True)
|
| 3135 |
definition = _field_or_refuse(session, table_key)
|
| 3136 |
_admit_if_sources(session, table_key, body, definition)
|
|
|
|
| 3137 |
ut = _ut()
|
| 3138 |
from routes_grid import govern_date_recurrence
|
| 3139 |
# Only recurrence admission needs the merged workspace. Plain creates need no row audience.
|
|
@@ -3269,6 +3321,7 @@ def patch_field(table_key: str, fkey: str, body: dict = Body(default=None),
|
|
| 3269 |
_managed_or_refuse(session, table_key, mutation=True)
|
| 3270 |
definition = _field_or_refuse(session, table_key, fkey)
|
| 3271 |
_admit_if_sources(session, table_key, body, definition, fkey)
|
|
|
|
| 3272 |
ut = _ut()
|
| 3273 |
body = body or {}
|
| 3274 |
from routes_grid import govern_date_recurrence
|
|
@@ -3602,6 +3655,12 @@ def patch_row(table_key: str, pid: int, body: dict = Body(default=None),
|
|
| 3602 |
except ValueError as exc:
|
| 3603 |
# A core value rule refused the cell before anything was stored. Name its reason (C4).
|
| 3604 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300] or "the edit was refused")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3605 |
# ⚠ THE READ-BACK IS THE DEFINITION ROW, AND ON A `ut_` SCOPE THAT IS THE WHOLE OF IT.
|
| 3606 |
#
|
| 3607 |
# ⛔ CORRECTED, wave-29 T22 (owner item 2a): this note used to say "THE READ-BACK SPANS BOTH
|
|
|
|
| 2957 |
if ut.ZONE_FIELD_KEY in values or "zone" in values:
|
| 2958 |
raise err(400, "zone_membership_read_only",
|
| 2959 |
"Zone membership is worked out from the record's current location")
|
| 2960 |
+
# S76-T32: an undo restores a deleted record's retained server facts only when it says so.
|
| 2961 |
+
restore = (body or {}).get("restore", False)
|
| 2962 |
+
if not isinstance(restore, bool):
|
| 2963 |
+
raise err(400, "bad_restore", "restore must be true or false")
|
| 2964 |
try:
|
| 2965 |
rid = ut.add_row(table_key, values, session.uname, st=session.runtime,
|
| 2966 |
+
rid=(body or {}).get("rid"), restore=restore, via=via)
|
| 2967 |
except ValueError as exc:
|
| 2968 |
# A core value rule (negative policy, link condition, server-owned cell) names its reason.
|
| 2969 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300] or "the row was refused")
|
|
|
|
| 2986 |
_refresh_relations(session)
|
| 2987 |
from routes_alerts import after_write
|
| 2988 |
after_write(session, table_key)
|
| 2989 |
+
return {"rid": rid, "pid": int(rid), "values": _added_row_values(session, table_key, rid)}
|
| 2990 |
+
|
| 2991 |
+
|
| 2992 |
+
def _added_row_values(session, table_key, rid):
|
| 2993 |
+
"""The stored row as this session may read it: generated cells included, hidden fields and
|
| 2994 |
+
server-only keys (`_rowAudit`, `_created`) never."""
|
| 2995 |
+
import core.perm_scope as perm_scope
|
| 2996 |
+
try:
|
| 2997 |
+
entry = _table_entry(session.runtime, table_key) or {}
|
| 2998 |
+
except Exception:
|
| 2999 |
+
return {} # the row is saved; a failed read-back must not report a failed write
|
| 3000 |
+
row = (entry.get("rows") or {}).get(str(rid))
|
| 3001 |
+
if not isinstance(row, dict):
|
| 3002 |
+
return {}
|
| 3003 |
+
hidden = _ut_hidden(session, table_key, entry.get("fields") or [], st=session.runtime)
|
| 3004 |
+
return perm_scope.strip_row({key: value for key, value in row.items()
|
| 3005 |
+
if not str(key).startswith("_")}, hidden)
|
| 3006 |
|
| 3007 |
|
| 3008 |
@router.post("/tables/{table_key}/rows/import", status_code=201)
|
|
|
|
| 3061 |
raise err(400, "bad_value", f"row {index + 1}: {why}. Nothing was imported")
|
| 3062 |
try:
|
| 3063 |
made = ut.add_rows(table_key, rows_in, session.uname, st=session.runtime)
|
| 3064 |
+
except ValueError as exc:
|
| 3065 |
+
# S76-T33: a Link condition or value rule refuses the batch with its own code and reason.
|
| 3066 |
+
raise err(400, getattr(exc, "code", "bad_value"),
|
| 3067 |
+
(str(exc)[:280].rstrip(". ") or "a value was refused") + ". Nothing was imported")
|
| 3068 |
except Exception:
|
| 3069 |
raise err(503, "store_unavailable", "nothing was imported. The store refused")
|
| 3070 |
if made is None:
|
|
|
|
| 3151 |
raise err(403 if exc.code == 'if_source_forbidden' else 400, exc.code, str(exc))
|
| 3152 |
|
| 3153 |
|
| 3154 |
+
def _admit_link_selection(session, body):
|
| 3155 |
+
"""S76-T33: a Link condition may name only target fields this session can read."""
|
| 3156 |
+
raw = body if isinstance(body, dict) else {}
|
| 3157 |
+
bag = raw.get("link")
|
| 3158 |
+
if not isinstance(bag, dict) or not isinstance(bag.get("selectionFilter"), dict):
|
| 3159 |
+
return
|
| 3160 |
+
target = str(bag.get("table") or "")
|
| 3161 |
+
if not target.startswith(_ut().KEY_PREFIX):
|
| 3162 |
+
return # a registry target is checked in core, which fails closed
|
| 3163 |
+
import core.perm_scope as perm_scope
|
| 3164 |
+
if not perm_scope.may_read(session.user, target, st=session.runtime):
|
| 3165 |
+
raise err(403, "link_selection_forbidden",
|
| 3166 |
+
"You cannot read the linked database, so its records cannot set a condition.")
|
| 3167 |
+
named, stack = set(), list(bag["selectionFilter"].get("nodes") or [])
|
| 3168 |
+
while stack:
|
| 3169 |
+
node = stack.pop()
|
| 3170 |
+
if not isinstance(node, dict):
|
| 3171 |
+
continue
|
| 3172 |
+
stack.extend(node.get("children") or [])
|
| 3173 |
+
rhs = node.get("rhs") if isinstance(node.get("rhs"), dict) else {}
|
| 3174 |
+
named.update(ref for ref in (node.get("colId"), rhs.get("colId")) if isinstance(ref, str) and ref)
|
| 3175 |
+
fields = (_table_entry(session.runtime, target) or {}).get("fields") or []
|
| 3176 |
+
if named & set(_ut_hidden(session, target, fields, st=session.runtime) or ()):
|
| 3177 |
+
raise err(403, "link_selection_forbidden",
|
| 3178 |
+
"The link condition names a field you cannot read in the linked database.")
|
| 3179 |
+
|
| 3180 |
+
|
| 3181 |
@router.post("/tables/{table_key}/fields", status_code=201)
|
| 3182 |
@_c8_write
|
| 3183 |
def add_field(table_key: str, body: dict = Body(default=None),
|
|
|
|
| 3185 |
_managed_or_refuse(session, table_key, mutation=True)
|
| 3186 |
definition = _field_or_refuse(session, table_key)
|
| 3187 |
_admit_if_sources(session, table_key, body, definition)
|
| 3188 |
+
_admit_link_selection(session, body)
|
| 3189 |
ut = _ut()
|
| 3190 |
from routes_grid import govern_date_recurrence
|
| 3191 |
# Only recurrence admission needs the merged workspace. Plain creates need no row audience.
|
|
|
|
| 3321 |
_managed_or_refuse(session, table_key, mutation=True)
|
| 3322 |
definition = _field_or_refuse(session, table_key, fkey)
|
| 3323 |
_admit_if_sources(session, table_key, body, definition, fkey)
|
| 3324 |
+
_admit_link_selection(session, body)
|
| 3325 |
ut = _ut()
|
| 3326 |
body = body or {}
|
| 3327 |
from routes_grid import govern_date_recurrence
|
|
|
|
| 3655 |
except ValueError as exc:
|
| 3656 |
# A core value rule refused the cell before anything was stored. Name its reason (C4).
|
| 3657 |
raise err(400, getattr(exc, "code", "bad_value"), str(exc)[:300] or "the edit was refused")
|
| 3658 |
+
# S76-T29: `overlay_patch` records a value rule's refusal instead of raising; answer it the same way.
|
| 3659 |
+
for _refusal in getattr(ctx.out, "refusals", None) or ():
|
| 3660 |
+
if _refusal.get("event") == "overlay_patch" and _refusal.get("code") not in (
|
| 3661 |
+
"cell_write_refused", "row_outside_audience", "bad_row_id"):
|
| 3662 |
+
raise err(400, _refusal.get("code") or "bad_value",
|
| 3663 |
+
_refusal.get("message") or "the edit was refused")
|
| 3664 |
# ⚠ THE READ-BACK IS THE DEFINITION ROW, AND ON A `ut_` SCOPE THAT IS THE WHOLE OF IT.
|
| 3665 |
#
|
| 3666 |
# ⛔ CORRECTED, wave-29 T22 (owner item 2a): this note used to say "THE READ-BACK SPANS BOTH
|
api/routes_templates.py
CHANGED
|
@@ -35,10 +35,11 @@ from deps import Session, err, perms, require_session
|
|
| 35 |
router = APIRouter(prefix="/api/v1")
|
| 36 |
|
| 37 |
_TEMPLATE_CATEGORIES = {
|
| 38 |
-
"sheet": "Sheet", "data": "
|
| 39 |
-
"route": "Operations", "catalog": "Sales",
|
| 40 |
}
|
| 41 |
-
_TEMPLATE_PREVIEW_LABELS = {**_TEMPLATE_CATEGORIES, "route": "Routing", "catalog": "Catalog"
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
@router.get("/templates/previews/{module}.svg")
|
|
@@ -74,6 +75,16 @@ M92 224h88 M240 224h38 M354 224h54"/>
|
|
| 74 |
<g class="node"><circle cx="212" cy="116" r="10"/>
|
| 75 |
<circle cx="276" cy="180" r="10"/><circle cx="352" cy="228" r="10"/></g>
|
| 76 |
<path class="line" d="M308 116h96 M308 132h64 M212 228h72"/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
</g>'''
|
| 78 |
elif module == "catalog":
|
| 79 |
drawing = '''<g id="catalog-collection">
|
|
@@ -172,7 +183,7 @@ def list_templates(table: str = "", session: Session = Depends(require_session),
|
|
| 172 |
"""
|
| 173 |
if module:
|
| 174 |
if module not in ("sheet", "data", "app", "agents"):
|
| 175 |
-
raise err(400, "unknown_module", "Choose Sheet,
|
| 176 |
return _module_templates(session)
|
| 177 |
fields, source = _target_or_refuse(session, table)
|
| 178 |
vt = _templates()
|
|
@@ -349,6 +360,8 @@ def _catalog_row(key, name, description, requires, *, module="sheet", creation_m
|
|
| 349 |
blocked = next((row for row in requires if row["state"] == "disconnected"), None)
|
| 350 |
blocked = blocked or next((row for row in requires if row["state"] != "ready"), None)
|
| 351 |
preview = key if module == "app" and key in ("route", "catalog") else module
|
|
|
|
|
|
|
| 352 |
return {"key": key, "name": dash_norm.plain(str(name)),
|
| 353 |
"description": dash_norm.plain(str(description)),
|
| 354 |
"image": f"/api/v1/templates/previews/{preview}.svg",
|
|
@@ -608,7 +621,9 @@ def create_module_template(module: str, key: str, body: dict = Body(default=None
|
|
| 608 |
if module == "agents":
|
| 609 |
# A direct call to create_automation does not run its FastAPI dependency.
|
| 610 |
session.require("automation")
|
| 611 |
-
if module == "app" and key
|
|
|
|
|
|
|
| 612 |
raise err(404, "unknown_template", "That template does not exist.")
|
| 613 |
section = next((row for row in _module_templates(session)["sections"] if row["module"] == module), None)
|
| 614 |
template = next((row for row in section["templates"] if row["key"] == key), None) if section else None
|
|
|
|
| 35 |
router = APIRouter(prefix="/api/v1")
|
| 36 |
|
| 37 |
_TEMPLATE_CATEGORIES = {
|
| 38 |
+
"sheet": "Sheet", "data": "Table", "app": "App", "agents": "Automation", "saved": "Saved",
|
| 39 |
+
"route": "Operations", "catalog": "Sales", "competitor_watch": "Sales",
|
| 40 |
}
|
| 41 |
+
_TEMPLATE_PREVIEW_LABELS = {**_TEMPLATE_CATEGORIES, "route": "Routing", "catalog": "Catalog",
|
| 42 |
+
"competitor_watch": "Competitor watch"}
|
| 43 |
|
| 44 |
|
| 45 |
@router.get("/templates/previews/{module}.svg")
|
|
|
|
| 75 |
<g class="node"><circle cx="212" cy="116" r="10"/>
|
| 76 |
<circle cx="276" cy="180" r="10"/><circle cx="352" cy="228" r="10"/></g>
|
| 77 |
<path class="line" d="M308 116h96 M308 132h64 M212 228h72"/>
|
| 78 |
+
</g>'''
|
| 79 |
+
elif module == "competitor_watch":
|
| 80 |
+
# S78-T17: a ranked list beside a trend line, the App's own two panels.
|
| 81 |
+
drawing = '''<g id="competitor-watch">
|
| 82 |
+
<rect class="tint" x="44" y="92" width="184" height="32" rx="8"/>
|
| 83 |
+
<path class="line" d="M44 164h184 M44 204h184 M252 244h184 M252 92v152"/>
|
| 84 |
+
<path class="ink" d="M60 108h16 M92 108h96 M60 148h16 M92 148h72 M60 188h16 M92 188h88 M60 228h16 M92 228h56"/>
|
| 85 |
+
<path class="ink" d="M268 212l40-24 40 8 40-40 40-16"/>
|
| 86 |
+
<g class="node"><circle cx="268" cy="212" r="6"/><circle cx="308" cy="188" r="6"/>
|
| 87 |
+
<circle cx="348" cy="196" r="6"/><circle cx="388" cy="156" r="6"/><circle cx="428" cy="140" r="6"/></g>
|
| 88 |
</g>'''
|
| 89 |
elif module == "catalog":
|
| 90 |
drawing = '''<g id="catalog-collection">
|
|
|
|
| 183 |
"""
|
| 184 |
if module:
|
| 185 |
if module not in ("sheet", "data", "app", "agents"):
|
| 186 |
+
raise err(400, "unknown_module", "Choose Sheet, Table, App or Automation.")
|
| 187 |
return _module_templates(session)
|
| 188 |
fields, source = _target_or_refuse(session, table)
|
| 189 |
vt = _templates()
|
|
|
|
| 360 |
blocked = next((row for row in requires if row["state"] == "disconnected"), None)
|
| 361 |
blocked = blocked or next((row for row in requires if row["state"] != "ready"), None)
|
| 362 |
preview = key if module == "app" and key in ("route", "catalog") else module
|
| 363 |
+
if module == "app" and key == "competitor_watch":
|
| 364 |
+
preview = key # S78-T17: the gated template draws its own preview.
|
| 365 |
return {"key": key, "name": dash_norm.plain(str(name)),
|
| 366 |
"description": dash_norm.plain(str(description)),
|
| 367 |
"image": f"/api/v1/templates/previews/{preview}.svg",
|
|
|
|
| 621 |
if module == "agents":
|
| 622 |
# A direct call to create_automation does not run its FastAPI dependency.
|
| 623 |
session.require("automation")
|
| 624 |
+
if module == "app" and key == "competitor_watch":
|
| 625 |
+
pass # S78-T17: gated by application_registry.template_contracts(session=...).
|
| 626 |
+
elif module == "app" and key not in {"route", "catalog"}:
|
| 627 |
raise err(404, "unknown_template", "That template does not exist.")
|
| 628 |
section = next((row for row in _module_templates(session)["sections"] if row["module"] == module), None)
|
| 629 |
template = next((row for row in section["templates"] if row["key"] == key), None) if section else None
|
api/routes_tickets.py
CHANGED
|
@@ -71,6 +71,16 @@ def send_request(body: dict = Body(...), session: Session = Depends(require_sess
|
|
| 71 |
body.get("kind"), body.get("title"), body.get("body"), body.get("source"))}
|
| 72 |
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
@router.post("/requests/{request_id}/state")
|
| 75 |
def set_request_state(request_id: str, body: dict = Body(...), session: Session = Depends(require_session)):
|
| 76 |
return {"request": _call(session, tickets.set_request_state, request_id, session.uname,
|
|
|
|
| 71 |
body.get("kind"), body.get("title"), body.get("body"), body.get("source"))}
|
| 72 |
|
| 73 |
|
| 74 |
+
@router.post("/requests/close-stale-system-assignments")
|
| 75 |
+
def close_stale_system_assignments(body: dict = Body(...), session: Session = Depends(require_session)):
|
| 76 |
+
try:
|
| 77 |
+
return tickets.close_stale_system_assignments(session.runtime, session.uname, body.get("before"))
|
| 78 |
+
except PermissionError:
|
| 79 |
+
raise err(403, "administrator_required", "Only an Administrator can close stale system requests.")
|
| 80 |
+
except ValueError as exc:
|
| 81 |
+
raise err(400, "invalid_request_cutoff", str(exc))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
@router.post("/requests/{request_id}/state")
|
| 85 |
def set_request_state(request_id: str, body: dict = Body(...), session: Session = Depends(require_session)):
|
| 86 |
return {"request": _call(session, tickets.set_request_state, request_id, session.uname,
|
api/schedule_envelope.py
CHANGED
|
@@ -263,12 +263,18 @@ def render(envelope):
|
|
| 263 |
f"{envelope['rowCount']} rows." + (" " + ", ".join(totals) + "." if totals else ""),
|
| 264 |
f"{len(envelope['entered'])} entered, {len(envelope['left'])} left.",
|
| 265 |
f"{len(table['rows'])} of {table['total']} shown" + (" (groups)." if table["unit"] == "groups" else "."),
|
| 266 |
-
"Open in
|
| 267 |
for name in ("entered", "left"):
|
| 268 |
if envelope[name]:
|
| 269 |
text.insert(-1, name.title() + ": " + ", ".join(
|
| 270 |
_words(item["name"]) for item in envelope[name][:TOP_ROWS])
|
| 271 |
+ (f" and {len(envelope[name]) - TOP_ROWS} more" if len(envelope[name]) > TOP_ROWS else "") + ".")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
return _words("\n".join(text))
|
| 273 |
|
| 274 |
|
|
@@ -283,6 +289,239 @@ def saved(rt, username, rid):
|
|
| 283 |
return copy.deepcopy(doc) if doc.get("owner") == username else None
|
| 284 |
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
def publish(rt, username, outcome):
|
| 287 |
"""Recover one envelope, membership snapshot and conversation turn after a retry."""
|
| 288 |
import assistant_log
|
|
|
|
| 263 |
f"{envelope['rowCount']} rows." + (" " + ", ".join(totals) + "." if totals else ""),
|
| 264 |
f"{len(envelope['entered'])} entered, {len(envelope['left'])} left.",
|
| 265 |
f"{len(table['rows'])} of {table['total']} shown" + (" (groups)." if table["unit"] == "groups" else "."),
|
| 266 |
+
"Open in Table: " + envelope["dataLink"]]
|
| 267 |
for name in ("entered", "left"):
|
| 268 |
if envelope[name]:
|
| 269 |
text.insert(-1, name.title() + ": " + ", ".join(
|
| 270 |
_words(item["name"]) for item in envelope[name][:TOP_ROWS])
|
| 271 |
+ (f" and {len(envelope[name]) - TOP_ROWS} more" if len(envelope[name]) > TOP_ROWS else "") + ".")
|
| 272 |
+
rule = envelope.get("rule") or {}
|
| 273 |
+
if rule.get("rule") not in (None, "notify"):
|
| 274 |
+
status = {"draft": "Draft saved", "pending": "Waiting for your review", "applied": "Change applied",
|
| 275 |
+
"rejected": "Change rejected", "unchanged": "No change needed"}.get(rule.get("state"))
|
| 276 |
+
if status:
|
| 277 |
+
text.insert(1, status + ". Open Scheduled runs in Automation.")
|
| 278 |
return _words("\n".join(text))
|
| 279 |
|
| 280 |
|
|
|
|
| 289 |
return copy.deepcopy(doc) if doc.get("owner") == username else None
|
| 290 |
|
| 291 |
|
| 292 |
+
def _rule_key(rt, schedule):
|
| 293 |
+
return "schedule_reviews_" + _digest(rt.key, schedule["owner"], schedule["id"])
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def _rule_session(rt, schedule):
|
| 297 |
+
from core import external_access
|
| 298 |
+
from deps import Session
|
| 299 |
+
import routes_external as native
|
| 300 |
+
|
| 301 |
+
try:
|
| 302 |
+
user = external_access.resolve_active_user(schedule["owner"], rt.key)
|
| 303 |
+
except external_access.ExternalAccessError as exc:
|
| 304 |
+
raise ValueError("The schedule owner is not available.") from exc
|
| 305 |
+
scope = schedule["action"]["scope"]
|
| 306 |
+
if scope is not None and (not isinstance(scope, list) or any(
|
| 307 |
+
not isinstance(key, str) or not key.strip() or key.strip().lower() == "all" for key in scope)):
|
| 308 |
+
raise ValueError("This schedule's database scope is not valid.")
|
| 309 |
+
session = Session(rt.key, user, {"external_access": external_access.ACCESS_READWRITE,
|
| 310 |
+
**({"external_databases": copy.deepcopy(scope)} if scope is not None else {})}, rt)
|
| 311 |
+
native.require_external_analyst(session)
|
| 312 |
+
native._assert_database_scope(session, schedule["action"]["database"])
|
| 313 |
+
return session
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def _rule_item(rt, schedule, rid):
|
| 317 |
+
_result_key(rt, schedule["owner"], rid)
|
| 318 |
+
return copy.deepcopy(((rt.get(_rule_key(rt, schedule)) or {}).get("items") or {}).get(rid))
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def _rule_save(rt, schedule, rid, item, *, expected=None):
|
| 322 |
+
def change(raw):
|
| 323 |
+
raw = raw or {"version": 1, "items": {}}
|
| 324 |
+
current = raw["items"].get(rid)
|
| 325 |
+
if expected is None:
|
| 326 |
+
raw["items"].setdefault(rid, copy.deepcopy(item))
|
| 327 |
+
elif not current or current.get("state") != expected:
|
| 328 |
+
raise ValueError("This review has already been decided.")
|
| 329 |
+
else:
|
| 330 |
+
raw["items"][rid] = copy.deepcopy(item)
|
| 331 |
+
return raw
|
| 332 |
+
|
| 333 |
+
rt.update(_rule_key(rt, schedule), change, flush="sync")
|
| 334 |
+
return _rule_item(rt, schedule, rid)
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def _public_rule(item):
|
| 338 |
+
return {key: copy.deepcopy(item[key]) for key in
|
| 339 |
+
("id", "rule", "state", "change", "digest", "diff", "at", "receipt", "message")
|
| 340 |
+
if key in item}
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def _rule_outcome(item):
|
| 344 |
+
outcome = copy.deepcopy(item["outcome"])
|
| 345 |
+
outcome["envelope"]["rule"] = _public_rule(item)
|
| 346 |
+
if item["state"] == "failed":
|
| 347 |
+
outcome.update(ok=False, detail=item["message"])
|
| 348 |
+
outcome["envelope"].update(ok=False, message=item["message"])
|
| 349 |
+
return outcome
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def _prepare_rule(rt, schedule, outcome, change):
|
| 353 |
+
import routes_external as native
|
| 354 |
+
from schedules import scheduled_change
|
| 355 |
+
|
| 356 |
+
change = scheduled_change(change)
|
| 357 |
+
if str(change["record_id"]) not in {str(row["pid"]) for row in outcome["members"].values()}:
|
| 358 |
+
raise ValueError("The proposed record is outside this query's current result.")
|
| 359 |
+
session = _rule_session(rt, schedule)
|
| 360 |
+
arguments = {"action": "record_update", "database": schedule["action"]["database"], **change}
|
| 361 |
+
try:
|
| 362 |
+
preview = native._preview_external_write(session, "database_object", arguments, about={
|
| 363 |
+
"title": "Scheduled cell update", "description": "Apply the owner's saved scheduled cell change.",
|
| 364 |
+
"tags": ["operations"]})
|
| 365 |
+
except native.HTTPException as exc:
|
| 366 |
+
error = exc.detail.get("error", {}) if isinstance(exc.detail, dict) else {}
|
| 367 |
+
if error.get("code") == "write_not_changed":
|
| 368 |
+
return {"change": change, "digest": _digest(arguments), "diff": [], "unchanged": True}
|
| 369 |
+
raise
|
| 370 |
+
if preview.get("strong_confirmation_required"):
|
| 371 |
+
raise ValueError("This change needs a separate manual review.")
|
| 372 |
+
return {"change": change, "digest": _digest(arguments, preview["diff_digest"]),
|
| 373 |
+
"diff": preview["diff"], "expiresAt": preview["expires_at"],
|
| 374 |
+
"token": preview["confirmation_token"], "operationId": preview["operation_id"],
|
| 375 |
+
"revisionDigest": preview["_confirmation"]["revision_digest"],
|
| 376 |
+
"diffDigest": preview["diff_digest"], "execution": preview["_confirmation"]["execution"],
|
| 377 |
+
"about": preview["_confirmation"]["about"]}
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def _execute_rule(rt, schedule, item):
|
| 381 |
+
import routes_external as native
|
| 382 |
+
import time
|
| 383 |
+
|
| 384 |
+
# The native claim survives a crash after commit and returns its original audit receipt.
|
| 385 |
+
session = _rule_session(rt, schedule)
|
| 386 |
+
if item["rule"] == "act" and item["expiresAt"] <= time.time():
|
| 387 |
+
status = native._operation_status(session, {"operation_id": item["operationId"]})
|
| 388 |
+
if status.get("state") == "committed":
|
| 389 |
+
return status
|
| 390 |
+
if status.get("retry"):
|
| 391 |
+
raise ValueError("This change is still being saved. Review its status before retrying.")
|
| 392 |
+
needs_preview = ((item["rule"] == "ask" and not item.get("reviewPrepared"))
|
| 393 |
+
or (item["rule"] == "act" and item["expiresAt"] <= time.time()))
|
| 394 |
+
if needs_preview:
|
| 395 |
+
current = _current_rule_result(rt, schedule)
|
| 396 |
+
if item["change"]["record_id"] not in {str(row["pid"]) for row in current["members"].values()}:
|
| 397 |
+
raise ValueError("The proposed record no longer matches this saved query.")
|
| 398 |
+
arguments = {"action": "record_update", "database": schedule["action"]["database"], **item["change"]}
|
| 399 |
+
preview = native._preview_external_write(session, "database_object", arguments,
|
| 400 |
+
execution=copy.deepcopy(item["execution"]), about=copy.deepcopy(item["about"]))
|
| 401 |
+
if (preview["diff_digest"] != item["diffDigest"]
|
| 402 |
+
or preview["_confirmation"]["revision_digest"] != item["revisionDigest"]
|
| 403 |
+
or preview.get("strong_confirmation_required")):
|
| 404 |
+
raise ValueError("The data changed after this proposal. Review a fresh result.")
|
| 405 |
+
item = _rule_save(rt, schedule, item["id"], {**item, "reviewPrepared": True,
|
| 406 |
+
"token": preview["confirmation_token"], "operationId": preview["operation_id"],
|
| 407 |
+
"expiresAt": preview["expires_at"]}, expected="approving" if item["rule"] == "ask" else "ready")
|
| 408 |
+
try:
|
| 409 |
+
receipt = native._confirm_change(session, {"confirmation_token": item["token"]})
|
| 410 |
+
except Exception:
|
| 411 |
+
receipt = native._operation_status(session, {"operation_id": item["operationId"]})
|
| 412 |
+
if receipt.get("state") != "committed":
|
| 413 |
+
raise
|
| 414 |
+
if receipt.get("state") != "committed" or receipt.get("applied") is not True:
|
| 415 |
+
raise ValueError("This change has not been saved. Review its status before retrying.")
|
| 416 |
+
return receipt
|
| 417 |
+
|
| 418 |
+
|
| 419 |
+
def _current_rule_result(rt, schedule):
|
| 420 |
+
from routes_external import query_as
|
| 421 |
+
from schedules import _utc
|
| 422 |
+
|
| 423 |
+
action = schedule["action"]
|
| 424 |
+
query = lambda payload: query_as(rt, rt.key, schedule["owner"], action["database"], payload,
|
| 425 |
+
scope=copy.deepcopy(action["scope"]))
|
| 426 |
+
return build(rt, schedule, _utc(), query)
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
def run_rule(rt, schedule, now, query):
|
| 430 |
+
"""Run a saved query and one deterministic rule through the native write door."""
|
| 431 |
+
import assistant_memory
|
| 432 |
+
from schedules import _iso
|
| 433 |
+
|
| 434 |
+
rule = schedule.get("rule", "notify")
|
| 435 |
+
rid = result_id(rt, schedule, now)
|
| 436 |
+
item = _rule_item(rt, schedule, rid) if rule != "notify" else None
|
| 437 |
+
if item is None:
|
| 438 |
+
outcome = build(rt, schedule, now, query)
|
| 439 |
+
if rule == "notify":
|
| 440 |
+
outcome["envelope"]["rule"] = {"rule": "notify", "state": "reported"}
|
| 441 |
+
return outcome
|
| 442 |
+
correction = assistant_memory.schedule_correction(rt, schedule["owner"], schedule)
|
| 443 |
+
change = correction["change"] if correction else schedule["action"]["change"]
|
| 444 |
+
item = {"id": rid, "rule": rule, "at": _iso(now), "state": "preparing", "outcome": outcome}
|
| 445 |
+
item.update(_prepare_rule(rt, schedule, outcome, change))
|
| 446 |
+
item["state"] = ("unchanged" if item.get("unchanged") else
|
| 447 |
+
{"draft": "draft", "ask": "pending", "act": "ready"}[rule])
|
| 448 |
+
item = _rule_save(rt, schedule, rid, item)
|
| 449 |
+
if rule == "act" and item["state"] == "ready":
|
| 450 |
+
receipt = _execute_rule(rt, schedule, item)
|
| 451 |
+
item = _rule_item(rt, schedule, rid)
|
| 452 |
+
updated = {**item, "state": "applied", "receipt": receipt}
|
| 453 |
+
item = _rule_save(rt, schedule, rid, updated, expected="ready")
|
| 454 |
+
return _rule_outcome(item)
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
def reviews(rt, schedule):
|
| 458 |
+
"""Project this owner's proposals through today's native permissions."""
|
| 459 |
+
session = _rule_session(rt, schedule)
|
| 460 |
+
items = (rt.get(_rule_key(rt, schedule)) or {}).get("items") or {}
|
| 461 |
+
result = []
|
| 462 |
+
for item in items.values():
|
| 463 |
+
try:
|
| 464 |
+
_read_rule(session, schedule, item)
|
| 465 |
+
if item["state"] == "approving" and item.get("reviewPrepared"):
|
| 466 |
+
import routes_external as native
|
| 467 |
+
|
| 468 |
+
status = native._operation_status(session, {"operation_id": item["operationId"]})
|
| 469 |
+
if status.get("state") == "committed":
|
| 470 |
+
item = _rule_save(rt, schedule, item["id"], {**item, "state": "applied", "receipt": status},
|
| 471 |
+
expected="approving")
|
| 472 |
+
except Exception:
|
| 473 |
+
continue
|
| 474 |
+
result.append(_public_rule(item))
|
| 475 |
+
return sorted(result, key=lambda item: (item["at"], item["id"]), reverse=True)
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
def _read_rule(session, schedule, item):
|
| 479 |
+
import routes_tables
|
| 480 |
+
|
| 481 |
+
scoped = routes_tables.ut_write_ctx(session, schedule["action"]["database"])
|
| 482 |
+
if (int(item["change"]["record_id"]) not in scoped["pids"]
|
| 483 |
+
or not set(item["change"]["values"]) <= {field["key"] for field in scoped["fields"]}):
|
| 484 |
+
raise ValueError("This proposed change is outside your current access.")
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def decide(rt, schedule, rid, *, decision, digest):
|
| 488 |
+
"""Claim an exact owner review once before applying or rejecting its proposed change."""
|
| 489 |
+
item = _rule_item(rt, schedule, rid)
|
| 490 |
+
if (not item or item["rule"] != "ask" or item["state"] != "pending"
|
| 491 |
+
or not isinstance(digest, str) or digest != item["digest"]):
|
| 492 |
+
raise ValueError("This review is unavailable or has already been decided.")
|
| 493 |
+
if decision not in ("approve", "reject"):
|
| 494 |
+
raise ValueError("Choose Approve or Reject.")
|
| 495 |
+
session = _rule_session(rt, schedule)
|
| 496 |
+
_read_rule(session, schedule, item)
|
| 497 |
+
state = "approving" if decision == "approve" else "rejected"
|
| 498 |
+
item = _rule_save(rt, schedule, rid, {**item, "state": state}, expected="pending")
|
| 499 |
+
if decision == "approve":
|
| 500 |
+
try:
|
| 501 |
+
receipt = _execute_rule(rt, schedule, item)
|
| 502 |
+
item = _rule_item(rt, schedule, rid)
|
| 503 |
+
updated = {**item, "state": "applied", "receipt": receipt}
|
| 504 |
+
except Exception:
|
| 505 |
+
item = _rule_item(rt, schedule, rid)
|
| 506 |
+
_rule_save(rt, schedule, rid, {**item, "state": "failed",
|
| 507 |
+
"message": "The change could not be approved. Create a fresh proposal."}, expected="approving")
|
| 508 |
+
raise
|
| 509 |
+
item = _rule_save(rt, schedule, rid, updated, expected="approving")
|
| 510 |
+
return _public_rule(item)
|
| 511 |
+
|
| 512 |
+
|
| 513 |
+
def correct(rt, schedule, rid, change):
|
| 514 |
+
"""Validate a future replacement without changing the database or an existing review."""
|
| 515 |
+
import assistant_memory
|
| 516 |
+
|
| 517 |
+
prior = _rule_item(rt, schedule, rid)
|
| 518 |
+
if not prior or prior["state"] not in {"draft", "pending", "rejected", "applied", "failed", "unchanged"}:
|
| 519 |
+
raise ValueError("Choose a completed scheduled result to correct.")
|
| 520 |
+
outcome = _current_rule_result(rt, schedule)
|
| 521 |
+
_prepare_rule(rt, schedule, outcome, change)
|
| 522 |
+
return assistant_memory.remember_schedule_correction(rt, schedule["owner"], schedule, rid, change)
|
| 523 |
+
|
| 524 |
+
|
| 525 |
def publish(rt, username, outcome):
|
| 526 |
"""Recover one envelope, membership snapshot and conversation turn after a retry."""
|
| 527 |
import assistant_log
|
api/schedules.py
CHANGED
|
@@ -30,6 +30,8 @@ _CLAIM_LEASE = dt.timedelta(minutes=10)
|
|
| 30 |
_CLAIM_RENEW_SECONDS = 60
|
| 31 |
_LOG = logging.getLogger(__name__)
|
| 32 |
_QUERY_KEYS = {"fields", "filters", "sorts", "limit", "cursor", "aggregates", "groupBy"}
|
|
|
|
|
|
|
| 33 |
_SYNC_CONNECTORS = {"odoo": "odoo", "amazon": "amazon", "amazon_sp": "amazon",
|
| 34 |
"meta": "meta", "meta_ads": "meta", "meta ads": "meta"}
|
| 35 |
|
|
@@ -93,6 +95,21 @@ def next_run(cron, tz, after):
|
|
| 93 |
return None
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def _action(action):
|
| 97 |
if not isinstance(action, dict):
|
| 98 |
raise ValueError("Choose a prompt, check, reminder or query action.")
|
|
@@ -110,7 +127,8 @@ def _action(action):
|
|
| 110 |
not isinstance(key, str) or not key.strip() for key in scope))):
|
| 111 |
raise ValueError("A saved query needs its credential's database scope.")
|
| 112 |
return {"kind": "query", "database": database, "payload": copy.deepcopy(payload),
|
| 113 |
-
"words": words, "scope": copy.deepcopy(scope)
|
|
|
|
| 114 |
if action.get("kind") == "prompt":
|
| 115 |
prompt = action.get("prompt")
|
| 116 |
if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 4000:
|
|
@@ -191,10 +209,15 @@ def _sync_binding(rt, database, now, health):
|
|
| 191 |
"Every hour because no connector fills this database.")}}
|
| 192 |
|
| 193 |
|
| 194 |
-
def create(rt, owner, *, cron=None, tz, action, label, system=False, once_at=None, trigger=None
|
|
|
|
| 195 |
"""Create immediately in this tenant's store and return the schedule ID."""
|
| 196 |
zone = _zone(tz)
|
| 197 |
action = _action(action)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
once = once_at is not None
|
| 199 |
if trigger is not None:
|
| 200 |
if trigger != "sync" or action["kind"] != "query":
|
|
@@ -224,8 +247,8 @@ def create(rt, owner, *, cron=None, tz, action, label, system=False, once_at=Non
|
|
| 224 |
item = {"id": schedule_id, "owner": owner, "label": label,
|
| 225 |
"cron": None if once or trigger else " ".join(cron.split()),
|
| 226 |
"tz": tz, "action": action, "paused": False, "nextRunAt": next_stamp,
|
| 227 |
-
"createdAt": _iso(now), "lastFiredMinute": None, "lastFiredAt": None,
|
| 228 |
-
"lastOutcome": None, "runs": [], "system": bool(system)}
|
| 229 |
if once:
|
| 230 |
item.update(once=True, onceAt=next_stamp)
|
| 231 |
if trigger:
|
|
@@ -241,10 +264,17 @@ def create(rt, owner, *, cron=None, tz, action, label, system=False, once_at=Non
|
|
| 241 |
return schedule_id
|
| 242 |
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
def get(rt, schedule_id):
|
| 245 |
doc = rt.get(STORE_KEY) or {}
|
| 246 |
item = (doc.get("items") or {}).get(schedule_id)
|
| 247 |
-
return
|
| 248 |
|
| 249 |
|
| 250 |
def _set_paused(rt, schedule_id, paused):
|
|
@@ -261,12 +291,13 @@ def _set_paused(rt, schedule_id, paused):
|
|
| 261 |
or _utc(instant) <= now):
|
| 262 |
raise ValueError("That reminder already ran.")
|
| 263 |
item["paused"] = paused
|
|
|
|
| 264 |
if not paused and item.get("trigger") == "sync" and item.get("syncFallback"):
|
| 265 |
item["nextRunAt"] = _iso(now + dt.timedelta(hours=1))
|
| 266 |
if not paused and not item.get("once") and item.get("trigger") != "sync":
|
| 267 |
next_at = next_run(item["cron"], item["tz"], now)
|
| 268 |
item["nextRunAt"] = _iso(next_at) if next_at else None
|
| 269 |
-
found.append(
|
| 270 |
return doc
|
| 271 |
|
| 272 |
rt.update(STORE_KEY, change, flush="sync")
|
|
@@ -295,7 +326,7 @@ def delete(rt, schedule_id):
|
|
| 295 |
|
| 296 |
def list_for(rt, owner):
|
| 297 |
doc = rt.get(STORE_KEY) or {}
|
| 298 |
-
items = [
|
| 299 |
if isinstance(item, dict) and (owner is None or item.get("owner") == owner)]
|
| 300 |
return sorted(items, key=lambda item: (item.get("createdAt") or "", item.get("id") or ""),
|
| 301 |
reverse=True)
|
|
@@ -564,6 +595,8 @@ def _dispatch(rt, tenant, schedule, now):
|
|
| 564 |
|
| 565 |
if owner not in {item["username"] for item in assignable_identities(tenant)}:
|
| 566 |
return refused("The schedule owner is not an active user.")
|
|
|
|
|
|
|
| 567 |
if action["kind"] == "query":
|
| 568 |
try:
|
| 569 |
from routes_external import query_as
|
|
@@ -574,7 +607,7 @@ def _dispatch(rt, tenant, schedule, now):
|
|
| 574 |
return query_as(rt, tenant, owner, action["database"],
|
| 575 |
copy.deepcopy(payload), scope=copy.deepcopy(action["scope"]))
|
| 576 |
|
| 577 |
-
return schedule_envelope.
|
| 578 |
except ValueError as exc:
|
| 579 |
return refused(str(exc))
|
| 580 |
except Exception as exc:
|
|
@@ -596,22 +629,7 @@ def _dispatch(rt, tenant, schedule, now):
|
|
| 596 |
reminders = importlib.import_module("loopy_reminders")
|
| 597 |
return reminders.deliver(rt, tenant, owner, action["text"], now,
|
| 598 |
schedule_id=schedule["id"])
|
| 599 |
-
|
| 600 |
-
agent_runtime = importlib.import_module("agent_runtime")
|
| 601 |
-
except ImportError:
|
| 602 |
-
return {"ok": False, "detail": "The assistant is not available yet."}
|
| 603 |
-
result = agent_runtime.run_turn(
|
| 604 |
-
rt, {"tenant": tenant, "username": owner, "also_as": []}, "wake", action["prompt"])
|
| 605 |
-
try:
|
| 606 |
-
assistant_log = importlib.import_module("assistant_log")
|
| 607 |
-
except ImportError:
|
| 608 |
-
assistant_log = None
|
| 609 |
-
if assistant_log is not None:
|
| 610 |
-
assistant_log.append(rt, owner, {"role": "assistant", "door": "wake", "kind": "text",
|
| 611 |
-
"text": result.get("reply") or "",
|
| 612 |
-
"payload": {"scheduleId": schedule["id"]}, "at": _iso(now)})
|
| 613 |
-
return {"ok": not bool(result.get("refused")),
|
| 614 |
-
"detail": "The assistant declined." if result.get("refused") else "The assistant replied."}
|
| 615 |
|
| 616 |
|
| 617 |
def fire(rt, tenant, schedule, *, now):
|
|
@@ -679,6 +697,7 @@ def fire(rt, tenant, schedule, *, now):
|
|
| 679 |
outcome = (schedule_envelope.failure(rt, schedule, now, "The saved query could not run.")
|
| 680 |
if enveloped else {"ok": False, "detail": type(exc).__name__})
|
| 681 |
unchanged_sync = (enveloped and not cached and schedule.get("trigger") == "sync"
|
|
|
|
| 682 |
and outcome.get("ok") is True
|
| 683 |
and not outcome["envelope"].get("entered")
|
| 684 |
and not outcome["envelope"].get("left"))
|
|
|
|
| 30 |
_CLAIM_RENEW_SECONDS = 60
|
| 31 |
_LOG = logging.getLogger(__name__)
|
| 32 |
_QUERY_KEYS = {"fields", "filters", "sorts", "limit", "cursor", "aggregates", "groupBy"}
|
| 33 |
+
RUN_RULES = ({"key": "notify", "label": "Notify"}, {"key": "draft", "label": "Draft"},
|
| 34 |
+
{"key": "ask", "label": "Ask me"}, {"key": "act", "label": "Act"})
|
| 35 |
_SYNC_CONNECTORS = {"odoo": "odoo", "amazon": "amazon", "amazon_sp": "amazon",
|
| 36 |
"meta": "meta", "meta_ads": "meta", "meta ads": "meta"}
|
| 37 |
|
|
|
|
| 95 |
return None
|
| 96 |
|
| 97 |
|
| 98 |
+
def scheduled_change(change):
|
| 99 |
+
"""Accept one constant cell update, with no tool, schema or delivery verbs."""
|
| 100 |
+
if not isinstance(change, dict) or set(change) != {"record_id", "values"}:
|
| 101 |
+
raise ValueError("Name one record and one field value to change.")
|
| 102 |
+
rid, values = change["record_id"], change["values"]
|
| 103 |
+
if (not isinstance(rid, str) or not rid.isascii() or not rid.isdigit() or int(rid) < 1
|
| 104 |
+
or not isinstance(values, dict) or len(values) != 1):
|
| 105 |
+
raise ValueError("Name one record and one field value to change.")
|
| 106 |
+
field, value = next(iter(values.items()))
|
| 107 |
+
if (not isinstance(field, str) or not field.strip() or field == "pid"
|
| 108 |
+
or not isinstance(value, str) or len(value) > 10000):
|
| 109 |
+
raise ValueError("Choose one field and a text value of at most 10,000 characters.")
|
| 110 |
+
return {"record_id": str(int(rid)), "values": {field: value}}
|
| 111 |
+
|
| 112 |
+
|
| 113 |
def _action(action):
|
| 114 |
if not isinstance(action, dict):
|
| 115 |
raise ValueError("Choose a prompt, check, reminder or query action.")
|
|
|
|
| 127 |
not isinstance(key, str) or not key.strip() for key in scope))):
|
| 128 |
raise ValueError("A saved query needs its credential's database scope.")
|
| 129 |
return {"kind": "query", "database": database, "payload": copy.deepcopy(payload),
|
| 130 |
+
"words": words, "scope": copy.deepcopy(scope),
|
| 131 |
+
**({"change": scheduled_change(action["change"])} if "change" in action else {})}
|
| 132 |
if action.get("kind") == "prompt":
|
| 133 |
prompt = action.get("prompt")
|
| 134 |
if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 4000:
|
|
|
|
| 209 |
"Every hour because no connector fills this database.")}}
|
| 210 |
|
| 211 |
|
| 212 |
+
def create(rt, owner, *, cron=None, tz, action, label, system=False, once_at=None, trigger=None,
|
| 213 |
+
rule="notify"):
|
| 214 |
"""Create immediately in this tenant's store and return the schedule ID."""
|
| 215 |
zone = _zone(tz)
|
| 216 |
action = _action(action)
|
| 217 |
+
if not isinstance(rule, str) or rule not in {row["key"] for row in RUN_RULES}:
|
| 218 |
+
raise ValueError("Choose Notify, Draft, Ask me or Act.")
|
| 219 |
+
if rule != "notify" and (action["kind"] != "query" or not action.get("change")):
|
| 220 |
+
raise ValueError("This rule needs a saved query and one proposed cell change.")
|
| 221 |
once = once_at is not None
|
| 222 |
if trigger is not None:
|
| 223 |
if trigger != "sync" or action["kind"] != "query":
|
|
|
|
| 247 |
item = {"id": schedule_id, "owner": owner, "label": label,
|
| 248 |
"cron": None if once or trigger else " ".join(cron.split()),
|
| 249 |
"tz": tz, "action": action, "paused": False, "nextRunAt": next_stamp,
|
| 250 |
+
"createdAt": _iso(now), "updatedAt": _iso(now), "lastFiredMinute": None, "lastFiredAt": None,
|
| 251 |
+
"lastOutcome": None, "runs": [], "system": bool(system), "rule": rule}
|
| 252 |
if once:
|
| 253 |
item.update(once=True, onceAt=next_stamp)
|
| 254 |
if trigger:
|
|
|
|
| 264 |
return schedule_id
|
| 265 |
|
| 266 |
|
| 267 |
+
def _public_schedule(item):
|
| 268 |
+
"""Project legacy Notify without changing the authored schedule."""
|
| 269 |
+
result = copy.deepcopy(item)
|
| 270 |
+
result.setdefault("rule", "notify")
|
| 271 |
+
return result
|
| 272 |
+
|
| 273 |
+
|
| 274 |
def get(rt, schedule_id):
|
| 275 |
doc = rt.get(STORE_KEY) or {}
|
| 276 |
item = (doc.get("items") or {}).get(schedule_id)
|
| 277 |
+
return _public_schedule(item) if isinstance(item, dict) else None
|
| 278 |
|
| 279 |
|
| 280 |
def _set_paused(rt, schedule_id, paused):
|
|
|
|
| 291 |
or _utc(instant) <= now):
|
| 292 |
raise ValueError("That reminder already ran.")
|
| 293 |
item["paused"] = paused
|
| 294 |
+
item["updatedAt"] = _iso(now) # S76-T37: pausing or resuming is an update
|
| 295 |
if not paused and item.get("trigger") == "sync" and item.get("syncFallback"):
|
| 296 |
item["nextRunAt"] = _iso(now + dt.timedelta(hours=1))
|
| 297 |
if not paused and not item.get("once") and item.get("trigger") != "sync":
|
| 298 |
next_at = next_run(item["cron"], item["tz"], now)
|
| 299 |
item["nextRunAt"] = _iso(next_at) if next_at else None
|
| 300 |
+
found.append(_public_schedule(item))
|
| 301 |
return doc
|
| 302 |
|
| 303 |
rt.update(STORE_KEY, change, flush="sync")
|
|
|
|
| 326 |
|
| 327 |
def list_for(rt, owner):
|
| 328 |
doc = rt.get(STORE_KEY) or {}
|
| 329 |
+
items = [_public_schedule(item) for item in (doc.get("items") or {}).values()
|
| 330 |
if isinstance(item, dict) and (owner is None or item.get("owner") == owner)]
|
| 331 |
return sorted(items, key=lambda item: (item.get("createdAt") or "", item.get("id") or ""),
|
| 332 |
reverse=True)
|
|
|
|
| 595 |
|
| 596 |
if owner not in {item["username"] for item in assignable_identities(tenant)}:
|
| 597 |
return refused("The schedule owner is not an active user.")
|
| 598 |
+
if action["kind"] == "prompt":
|
| 599 |
+
return refused("Scheduled prompts no longer run. Replace this schedule with a saved query, named check or reminder.")
|
| 600 |
if action["kind"] == "query":
|
| 601 |
try:
|
| 602 |
from routes_external import query_as
|
|
|
|
| 607 |
return query_as(rt, tenant, owner, action["database"],
|
| 608 |
copy.deepcopy(payload), scope=copy.deepcopy(action["scope"]))
|
| 609 |
|
| 610 |
+
return schedule_envelope.run_rule(rt, schedule, now, query)
|
| 611 |
except ValueError as exc:
|
| 612 |
return refused(str(exc))
|
| 613 |
except Exception as exc:
|
|
|
|
| 629 |
reminders = importlib.import_module("loopy_reminders")
|
| 630 |
return reminders.deliver(rt, tenant, owner, action["text"], now,
|
| 631 |
schedule_id=schedule["id"])
|
| 632 |
+
return refused("This schedule action is not supported. Choose a saved query, named check or reminder.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
|
| 635 |
def fire(rt, tenant, schedule, *, now):
|
|
|
|
| 697 |
outcome = (schedule_envelope.failure(rt, schedule, now, "The saved query could not run.")
|
| 698 |
if enveloped else {"ok": False, "detail": type(exc).__name__})
|
| 699 |
unchanged_sync = (enveloped and not cached and schedule.get("trigger") == "sync"
|
| 700 |
+
and schedule.get("rule", "notify") == "notify"
|
| 701 |
and outcome.get("ok") is True
|
| 702 |
and not outcome["envelope"].get("entered")
|
| 703 |
and not outcome["envelope"].get("left"))
|
api/sheet_warm.py
CHANGED
|
@@ -162,20 +162,33 @@ def _conflict_code(exc):
|
|
| 162 |
|
| 163 |
|
| 164 |
def warm_reader(session, wid):
|
| 165 |
-
"""
|
| 166 |
# Two scopes, like the open GET and its POST: the refresh never reuses the read's memo.
|
| 167 |
with routes_sheet._sheet_request_scope(session):
|
| 168 |
book = sheets.get_workbook(session.runtime, wid)
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
if not needs:
|
| 171 |
-
return 0, False
|
| 172 |
-
with
|
|
|
|
| 173 |
out = routes_sheet._refresh_sheet_scoped(wid, session, only=needs, force=False)
|
| 174 |
report = (out or {}).get("binding_refresh") or {}
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
-
def warm_workbook(rt, tenant, wid) -> dict:
|
| 179 |
"""Warm every reader of one workbook. A 409 skips the rest of this workbook for this pass."""
|
| 180 |
book = sheets.get_workbook(rt, wid)
|
| 181 |
bound = bound_cells(book)
|
|
@@ -183,15 +196,18 @@ def warm_workbook(rt, tenant, wid) -> dict:
|
|
| 183 |
if not bound:
|
| 184 |
return result
|
| 185 |
rec = shares.grants("workbook", book.get("id") or wid, st=rt)
|
|
|
|
| 186 |
for uname in reader_names(book, rec, tenant):
|
| 187 |
session = reader_session(rt, tenant, uname, book, rec)
|
| 188 |
if session is None:
|
| 189 |
continue
|
|
|
|
| 190 |
result["readers"] += 1
|
| 191 |
started = perf_counter()
|
| 192 |
try:
|
| 193 |
-
resolved, called = warm_reader(session, wid)
|
| 194 |
except Exception as exc: # noqa: BLE001
|
|
|
|
| 195 |
ms = (perf_counter() - started) * 1000.0
|
| 196 |
code = _conflict_code(exc)
|
| 197 |
if code:
|
|
@@ -200,15 +216,23 @@ def warm_workbook(rt, tenant, wid) -> dict:
|
|
| 200 |
f"this workbook during the warm, so its remaining readers wait for the next "
|
| 201 |
f"pass", flush=True)
|
| 202 |
result["conflict"] = True
|
| 203 |
-
|
|
|
|
| 204 |
print(f"[sheet-warm] wb={wid} reader={uname} failed {type(exc).__name__}: {exc}",
|
| 205 |
flush=True)
|
|
|
|
| 206 |
continue
|
|
|
|
|
|
|
|
|
|
| 207 |
ms = (perf_counter() - started) * 1000.0
|
| 208 |
print(format_reader_line(wid, uname, resolved, max(0, len(bound) - resolved), ms),
|
| 209 |
flush=True)
|
| 210 |
if called and GAP_SECONDS > 0:
|
| 211 |
time.sleep(GAP_SECONDS)
|
|
|
|
|
|
|
|
|
|
| 212 |
return result
|
| 213 |
|
| 214 |
|
|
@@ -236,7 +260,7 @@ def warm_tenant(tenant, reason="sync") -> dict:
|
|
| 236 |
_limit(reason, tenant, ids[done:], "shutdown")
|
| 237 |
break
|
| 238 |
try:
|
| 239 |
-
part = warm_workbook(rt, tenant, wid)
|
| 240 |
except Exception as exc: # noqa: BLE001
|
| 241 |
print(f"[sheet-warm] wb={wid} failed {type(exc).__name__}: {exc}", flush=True)
|
| 242 |
else:
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
def warm_reader(session, wid):
|
| 165 |
+
"""Return resolved count, call status and the admitted reader's completed refresh outcome."""
|
| 166 |
# Two scopes, like the open GET and its POST: the refresh never reuses the read's memo.
|
| 167 |
with routes_sheet._sheet_request_scope(session):
|
| 168 |
book = sheets.get_workbook(session.runtime, wid)
|
| 169 |
+
view = routes_sheet._read_for(session, book)
|
| 170 |
+
needs = cells_needing_warm(view)
|
| 171 |
+
read_failure = next(((cell.get("refusal") or {}).get("code") or "sheet_unresolved"
|
| 172 |
+
for tab in view.get("sheets") or [] for cell in (tab.get("cells") or {}).values()
|
| 173 |
+
if _is_bound(cell) and cell.get("state") == "refused"
|
| 174 |
+
and (cell.get("refusal") or {}).get("code") != "sheet_refresh_required"), None)
|
| 175 |
if not needs:
|
| 176 |
+
return 0, False, {"changedCells": 0, "held": bool(read_failure), "reason": read_failure}
|
| 177 |
+
with sheets.capture_automatic_refresh(session.runtime, wid) as attempts, \
|
| 178 |
+
routes_sheet._sheet_request_scope(session):
|
| 179 |
out = routes_sheet._refresh_sheet_scoped(wid, session, only=needs, force=False)
|
| 180 |
report = (out or {}).get("binding_refresh") or {}
|
| 181 |
+
rows = list(attempts.values())
|
| 182 |
+
held = next((row for row in rows if row.get("state") != "committed"), None)
|
| 183 |
+
outcome = {"changedCells": sum(int(row.get("changedCells", 0)) for row in rows
|
| 184 |
+
if row.get("state") == "committed"),
|
| 185 |
+
"held": bool(read_failure) or held is not None or not rows}
|
| 186 |
+
if outcome["held"]:
|
| 187 |
+
outcome["reason"] = read_failure or (held or {}).get("reason") or "sheet_unresolved"
|
| 188 |
+
return int(report.get("resolved", len(needs))), True, outcome
|
| 189 |
|
| 190 |
|
| 191 |
+
def warm_workbook(rt, tenant, wid, reason="sync") -> dict:
|
| 192 |
"""Warm every reader of one workbook. A 409 skips the rest of this workbook for this pass."""
|
| 193 |
book = sheets.get_workbook(rt, wid)
|
| 194 |
bound = bound_cells(book)
|
|
|
|
| 196 |
if not bound:
|
| 197 |
return result
|
| 198 |
rec = shares.grants("workbook", book.get("id") or wid, st=rt)
|
| 199 |
+
actor, changed, held, failure = None, 0, False, None
|
| 200 |
for uname in reader_names(book, rec, tenant):
|
| 201 |
session = reader_session(rt, tenant, uname, book, rec)
|
| 202 |
if session is None:
|
| 203 |
continue
|
| 204 |
+
actor = actor or session
|
| 205 |
result["readers"] += 1
|
| 206 |
started = perf_counter()
|
| 207 |
try:
|
| 208 |
+
resolved, called, outcome = warm_reader(session, wid)
|
| 209 |
except Exception as exc: # noqa: BLE001
|
| 210 |
+
held = True
|
| 211 |
ms = (perf_counter() - started) * 1000.0
|
| 212 |
code = _conflict_code(exc)
|
| 213 |
if code:
|
|
|
|
| 216 |
f"this workbook during the warm, so its remaining readers wait for the next "
|
| 217 |
f"pass", flush=True)
|
| 218 |
result["conflict"] = True
|
| 219 |
+
failure = code
|
| 220 |
+
break
|
| 221 |
print(f"[sheet-warm] wb={wid} reader={uname} failed {type(exc).__name__}: {exc}",
|
| 222 |
flush=True)
|
| 223 |
+
failure = failure or "sheet_unresolved"
|
| 224 |
continue
|
| 225 |
+
changed += outcome["changedCells"]
|
| 226 |
+
held = held or outcome["held"]
|
| 227 |
+
failure = failure or outcome.get("reason")
|
| 228 |
ms = (perf_counter() - started) * 1000.0
|
| 229 |
print(format_reader_line(wid, uname, resolved, max(0, len(bound) - resolved), ms),
|
| 230 |
flush=True)
|
| 231 |
if called and GAP_SECONDS > 0:
|
| 232 |
time.sleep(GAP_SECONDS)
|
| 233 |
+
if actor is not None:
|
| 234 |
+
sheets.record_automatic_refresh(rt, wid, actor, reason=reason, changed_cells=changed,
|
| 235 |
+
held=held, failure_reason=failure)
|
| 236 |
return result
|
| 237 |
|
| 238 |
|
|
|
|
| 260 |
_limit(reason, tenant, ids[done:], "shutdown")
|
| 261 |
break
|
| 262 |
try:
|
| 263 |
+
part = warm_workbook(rt, tenant, wid, reason=reason)
|
| 264 |
except Exception as exc: # noqa: BLE001
|
| 265 |
print(f"[sheet-warm] wb={wid} failed {type(exc).__name__}: {exc}", flush=True)
|
| 266 |
else:
|
api/test_website_runtime.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Exercise runtime preparation failures and an optional real data-free browser launch."""
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
+
import tempfile
|
| 10 |
+
import unittest
|
| 11 |
+
from unittest.mock import patch
|
| 12 |
+
|
| 13 |
+
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "platform"))
|
| 14 |
+
import website_tools
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class WebsiteRuntimeTests(unittest.TestCase):
|
| 18 |
+
def setUp(self):
|
| 19 |
+
self.temporary = tempfile.TemporaryDirectory(prefix="website-runtime-fixture-")
|
| 20 |
+
self.addCleanup(self.temporary.cleanup)
|
| 21 |
+
self.root = Path(self.temporary.name)
|
| 22 |
+
self.node = self.root / "node-v24.21.0-linux-x64/bin/node"
|
| 23 |
+
self.node.parent.mkdir(parents=True)
|
| 24 |
+
self.node.touch()
|
| 25 |
+
self.package = self.root / "sdk/node_modules/pomerado"
|
| 26 |
+
self.package.mkdir(parents=True)
|
| 27 |
+
(self.package / "package.json").write_text(
|
| 28 |
+
json.dumps({"version": website_tools._VERSION, "license": "MIT"}), encoding="utf-8")
|
| 29 |
+
self.calls = []
|
| 30 |
+
self.failure = None
|
| 31 |
+
self.proof = b"aios-website-runtime-ok\n"
|
| 32 |
+
|
| 33 |
+
def command(self, argv, **kwargs):
|
| 34 |
+
self.calls.append((argv, kwargs))
|
| 35 |
+
browser = "--input-type=module" in argv
|
| 36 |
+
if browser and self.failure:
|
| 37 |
+
return subprocess.CompletedProcess(argv, 127, b"", self.failure)
|
| 38 |
+
return subprocess.CompletedProcess(argv, 0, self.proof if browser else b"", b"")
|
| 39 |
+
|
| 40 |
+
def prepare(self):
|
| 41 |
+
with patch.object(website_tools.host_platform, "system", return_value="Linux"), \
|
| 42 |
+
patch.object(website_tools.host_platform, "machine", return_value="x86_64"), \
|
| 43 |
+
patch.object(website_tools, "urlopen", side_effect=AssertionError("Unexpected download")), \
|
| 44 |
+
patch.object(website_tools.subprocess, "run", side_effect=self.command):
|
| 45 |
+
return website_tools.prepare_runtime(self.root)
|
| 46 |
+
|
| 47 |
+
def test_missing_library_refuses_until_browser_page_check_succeeds(self):
|
| 48 |
+
self.failure = b"libglib-2.0.so.0: cannot open shared object file. CANARY-private-payload"
|
| 49 |
+
with self.assertLogs("website_tools", level="WARNING") as logs:
|
| 50 |
+
with self.assertRaises(website_tools.deps.HTTPException) as failed:
|
| 51 |
+
self.prepare()
|
| 52 |
+
self.assertEqual(failed.exception.status_code, 503)
|
| 53 |
+
self.assertEqual(failed.exception.detail["error"]["code"], "website_runtime_unavailable")
|
| 54 |
+
self.assertIn("Chromium launch and page check", failed.exception.detail["error"]["message"])
|
| 55 |
+
self.assertNotIn("CANARY-private-payload", str(failed.exception.detail) + str(logs.output))
|
| 56 |
+
self.failure = None
|
| 57 |
+
result = self.prepare()
|
| 58 |
+
self.assertEqual(result["POMERADO_NODE"], str(self.node))
|
| 59 |
+
self.assertEqual(result["POMERADO_PACKAGE_ROOT"], str(self.package))
|
| 60 |
+
|
| 61 |
+
def test_process_success_without_browser_page_proof_is_refused(self):
|
| 62 |
+
self.proof = b""
|
| 63 |
+
with self.assertRaises(website_tools.deps.HTTPException):
|
| 64 |
+
self.prepare()
|
| 65 |
+
|
| 66 |
+
def test_existing_unreviewed_package_is_refused_before_browser(self):
|
| 67 |
+
(self.package / "package.json").write_text(
|
| 68 |
+
json.dumps({"version": "0.0.0", "license": "MIT"}), encoding="utf-8")
|
| 69 |
+
with self.assertRaises(website_tools.deps.HTTPException) as failed:
|
| 70 |
+
self.prepare()
|
| 71 |
+
self.assertIn("Pomerado manifest check", failed.exception.detail["error"]["message"])
|
| 72 |
+
self.assertFalse(any("install" in argv or "--input-type=module" in argv for argv, _ in self.calls))
|
| 73 |
+
|
| 74 |
+
def test_setup_child_cannot_receive_product_credentials_or_global_browser_cache(self):
|
| 75 |
+
with patch.dict(os.environ, {"ODOO_WEB_RI_PASSWORD": "CANARY-password", "HF_TOKEN": "CANARY-token",
|
| 76 |
+
"PLAYWRIGHT_BROWSERS_PATH": "/other/browser-cache",
|
| 77 |
+
"OPENROUTER_API_KEY": "CANARY-provider"}):
|
| 78 |
+
result = self.prepare()
|
| 79 |
+
self.assertEqual(result["POMERADO_BROWSERS_PATH"], str(self.root / "browsers"))
|
| 80 |
+
for _argv, options in self.calls:
|
| 81 |
+
self.assertEqual(options["env"]["PLAYWRIGHT_BROWSERS_PATH"], str(self.root / "browsers"))
|
| 82 |
+
self.assertFalse(any("CANARY-" in value for value in options["env"].values()))
|
| 83 |
+
|
| 84 |
+
def test_browser_install_timeout_is_named_without_subprocess_output(self):
|
| 85 |
+
with patch.object(website_tools.host_platform, "system", return_value="Linux"), \
|
| 86 |
+
patch.object(website_tools.host_platform, "machine", return_value="x86_64"), \
|
| 87 |
+
patch.object(website_tools.subprocess, "run", side_effect=[
|
| 88 |
+
subprocess.CompletedProcess([], 0, b"", b""),
|
| 89 |
+
subprocess.TimeoutExpired("private-command", 1, output=b"CANARY-private-output")]):
|
| 90 |
+
with self.assertRaises(website_tools.deps.HTTPException) as failed:
|
| 91 |
+
website_tools.prepare_runtime(self.root)
|
| 92 |
+
self.assertIn("Chromium installation", failed.exception.detail["error"]["message"])
|
| 93 |
+
self.assertNotIn("CANARY-private-output", str(failed.exception.detail))
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 98 |
+
parser.add_argument("--runtime-dir", type=Path, help="Also prepare the reviewed runtime and launch a data-free browser")
|
| 99 |
+
args, remaining = parser.parse_known_args()
|
| 100 |
+
result = unittest.main(argv=[sys.argv[0], *remaining], exit=False)
|
| 101 |
+
if not result.result.wasSuccessful():
|
| 102 |
+
raise SystemExit(1)
|
| 103 |
+
if args.runtime_dir:
|
| 104 |
+
website_tools.prepare_runtime(args.runtime_dir)
|
| 105 |
+
print("Data-free Chromium page check passed.")
|
api/ticket_tools.py
CHANGED
|
@@ -57,9 +57,12 @@ def _source_check(session):
|
|
| 57 |
return lambda source: requests._source_scope(ctx, source, databases)
|
| 58 |
|
| 59 |
|
| 60 |
-
def _read_item(session, row, *, fields=None, source_check=None):
|
|
|
|
|
|
|
| 61 |
attribution = tickets.request_attribution(
|
| 62 |
-
session.runtime, row, _username(session), source_check=source_check
|
|
|
|
| 63 |
return _item(row, session.runtime, attribution=attribution, fields=fields)
|
| 64 |
|
| 65 |
|
|
@@ -84,19 +87,19 @@ def ticket_read(session, args):
|
|
| 84 |
def ticket_assign(session, args):
|
| 85 |
source_check = _source_check(session)
|
| 86 |
return _read_item(session, tickets.assign(session.runtime, args["id"], _username(session), args["to"]),
|
| 87 |
-
source_check=source_check)
|
| 88 |
|
| 89 |
|
| 90 |
def ticket_snooze(session, args):
|
| 91 |
source_check = _source_check(session)
|
| 92 |
return _read_item(session, tickets.snooze(session.runtime, args["id"], _username(session), args["until"]),
|
| 93 |
-
source_check=source_check)
|
| 94 |
|
| 95 |
|
| 96 |
def ticket_resolve(session, args):
|
| 97 |
source_check = _source_check(session)
|
| 98 |
return _read_item(session, tickets.resolve(session.runtime, args["id"], _username(session)),
|
| 99 |
-
source_check=source_check)
|
| 100 |
|
| 101 |
|
| 102 |
def _schema(description, properties, required=()):
|
|
|
|
| 57 |
return lambda source: requests._source_scope(ctx, source, databases)
|
| 58 |
|
| 59 |
|
| 60 |
+
def _read_item(session, row, *, fields=None, source_check=None, condition_audience=False):
|
| 61 |
+
if condition_audience:
|
| 62 |
+
_key, row = tickets._ticket(session.runtime, row["id"])
|
| 63 |
attribution = tickets.request_attribution(
|
| 64 |
+
session.runtime, row, _username(session), source_check=source_check,
|
| 65 |
+
condition_audience=condition_audience)
|
| 66 |
return _item(row, session.runtime, attribution=attribution, fields=fields)
|
| 67 |
|
| 68 |
|
|
|
|
| 87 |
def ticket_assign(session, args):
|
| 88 |
source_check = _source_check(session)
|
| 89 |
return _read_item(session, tickets.assign(session.runtime, args["id"], _username(session), args["to"]),
|
| 90 |
+
source_check=source_check, condition_audience=True)
|
| 91 |
|
| 92 |
|
| 93 |
def ticket_snooze(session, args):
|
| 94 |
source_check = _source_check(session)
|
| 95 |
return _read_item(session, tickets.snooze(session.runtime, args["id"], _username(session), args["until"]),
|
| 96 |
+
source_check=source_check, condition_audience=True)
|
| 97 |
|
| 98 |
|
| 99 |
def ticket_resolve(session, args):
|
| 100 |
source_check = _source_check(session)
|
| 101 |
return _read_item(session, tickets.resolve(session.runtime, args["id"], _username(session)),
|
| 102 |
+
source_check=source_check, condition_audience=True)
|
| 103 |
|
| 104 |
|
| 105 |
def _schema(description, properties, required=()):
|
api/tickets.py
CHANGED
|
@@ -72,6 +72,22 @@ def _stored_audience(row):
|
|
| 72 |
return _names(value) if isinstance(value, str) else set()
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def _keys(value):
|
| 76 |
parts = value.split(",") if isinstance(value, str) else value or ()
|
| 77 |
return sorted({str(part).strip() for part in parts if str(part).strip()})
|
|
@@ -87,8 +103,8 @@ def request_source(value):
|
|
| 87 |
"record": str(value.get("record", value.get("record_id")) or "")}
|
| 88 |
|
| 89 |
|
| 90 |
-
def project_request(row):
|
| 91 |
-
"""Project
|
| 92 |
kind = row.get("kind") or row.get("type") or "assignment"
|
| 93 |
if kind not in REQUEST_KINDS:
|
| 94 |
raise ValueError("Unknown request kind.")
|
|
@@ -100,6 +116,9 @@ def project_request(row):
|
|
| 100 |
"sent": "submitted",
|
| 101 |
"pending": "input-required" if kind in ("access_request", "approval") else "submitted"}.get(legacy)
|
| 102 |
state = state or ("working" if _names(row.get("assigned_to")) or row.get("owner") else "submitted")
|
|
|
|
|
|
|
|
|
|
| 103 |
rid = str(row.get("id") or row.get("request_id") or "")
|
| 104 |
source = row.get("source")
|
| 105 |
if source is None and row.get("source_database"):
|
|
@@ -117,7 +136,7 @@ def project_request(row):
|
|
| 117 |
metadata["reason"] = row["state_reason"]
|
| 118 |
return {"id": rid, "contextId": row.get("contextId") or subject, "kind": kind, "state": state,
|
| 119 |
"from": {"person": person, "agent": row.get("from_agent", SYSTEM_USER)},
|
| 120 |
-
"to": sorted(
|
| 121 |
"title": row.get("title") or row.get("text") or "",
|
| 122 |
"body": row.get("body", row.get("text", row.get("reason"))) or "",
|
| 123 |
"source": source, "createdAt": row.get("created_at") or row.get("raised_at") or "",
|
|
@@ -152,21 +171,30 @@ def _principal(rt, who):
|
|
| 152 |
return name, users._public(name, rec)
|
| 153 |
|
| 154 |
|
| 155 |
-
def _request_reader(rt, row, by, *, sender=True):
|
| 156 |
name, _user = _principal(rt, by)
|
| 157 |
task = project_request(row)
|
| 158 |
-
|
|
|
|
| 159 |
raise PermissionError("There is no request with that ID.")
|
| 160 |
-
if task["source"]
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
return name
|
| 163 |
|
| 164 |
|
| 165 |
-
def _transition(rt, row, by, state, reason=None):
|
| 166 |
"""Check transition authority without granting access to any data or object."""
|
| 167 |
if state not in REQUEST_STATES:
|
| 168 |
raise ValueError("Choose a supported request state.")
|
| 169 |
-
task = project_request(row)
|
|
|
|
| 170 |
system = by is SYSTEM_ACTOR
|
| 171 |
name = None if system else _principal(rt, by)[0]
|
| 172 |
if state == "failed":
|
|
@@ -178,9 +206,9 @@ def _transition(rt, row, by, state, reason=None):
|
|
| 178 |
if name != task["from"]["person"]:
|
| 179 |
raise PermissionError("Only the sender can withdraw a request.")
|
| 180 |
elif state == "auth-required":
|
| 181 |
-
if not system and name not in
|
| 182 |
raise PermissionError("Only the recipient or system can report an expired connection.")
|
| 183 |
-
elif system or name not in
|
| 184 |
raise PermissionError("Only a recipient can change this request's state.")
|
| 185 |
if task["state"] in ("completed", "rejected", "canceled", "failed") and state != task["state"]:
|
| 186 |
raise ValueError("This request has already ended.")
|
|
@@ -607,18 +635,23 @@ def close_cleared(rt, check_id, live_cond_keys):
|
|
| 607 |
return _sync(rt, str(check_id), [], live_keys=live_keys)["closed"]
|
| 608 |
|
| 609 |
|
| 610 |
-
def
|
| 611 |
-
|
| 612 |
-
row = ((table or {}).get("rows") or {}).get(str(ticket_id))
|
| 613 |
-
if not isinstance(row, dict):
|
| 614 |
-
raise PermissionError("There is no request with that ID.")
|
| 615 |
source_row = (table.get("ticketSourceRows") or {}).get(str(ticket_id))
|
| 616 |
database = str(row.get("source_database") or "")
|
| 617 |
if database.startswith(user_tables.KEY_PREFIX) and user_tables.records_mutable(database, st=rt):
|
| 618 |
# Assignment checks use the current editable source, including a deletion, rather
|
| 619 |
# than the older row captured when the condition was raised.
|
| 620 |
source_row = None
|
| 621 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
|
| 624 |
def _member(row, by):
|
|
@@ -629,7 +662,7 @@ def _member(row, by):
|
|
| 629 |
def _change(rt, ticket_id, by, change):
|
| 630 |
key, ticket = _ticket(rt, ticket_id)
|
| 631 |
_member(ticket, by)
|
| 632 |
-
_request_reader(rt, ticket, by, sender=False)
|
| 633 |
result = {}
|
| 634 |
|
| 635 |
def write(data):
|
|
@@ -641,6 +674,7 @@ def _change(rt, ticket_id, by, change):
|
|
| 641 |
row["updated_at"] = _utc().isoformat()
|
| 642 |
result.clear()
|
| 643 |
result.update({**row, **project_request({**row, "id": str(ticket_id)})})
|
|
|
|
| 644 |
return data
|
| 645 |
|
| 646 |
rt.update(user_tables.STORE_KEY, write, flush="sync")
|
|
@@ -656,7 +690,7 @@ def assign(rt, ticket_id, by, to):
|
|
| 656 |
raise ValueError(f"{_text(to)} cannot see the data this request needs, so it cannot be assigned to them.")
|
| 657 |
|
| 658 |
def change(row):
|
| 659 |
-
if project_request(row)["state"] in ("completed", "rejected", "canceled", "failed"):
|
| 660 |
raise ValueError("This request has already ended.")
|
| 661 |
try:
|
| 662 |
assignments = json.loads(row.get("assignments") or "{}")
|
|
@@ -669,6 +703,8 @@ def assign(rt, ticket_id, by, to):
|
|
| 669 |
"assigned_to": _joined(_names(row.get("assigned_to")) | {to}),
|
| 670 |
"assigned_by": _joined(_names(row.get("assigned_by")) | {by}),
|
| 671 |
"assignments": json.dumps(assignments, sort_keys=True), "owner": to})
|
|
|
|
|
|
|
| 672 |
if row.get("state") in REQUEST_STATES:
|
| 673 |
row["state"] = "working"
|
| 674 |
|
|
@@ -694,7 +730,7 @@ def snooze(rt, ticket_id, by, until):
|
|
| 694 |
until = _date(until)
|
| 695 |
|
| 696 |
def change(row):
|
| 697 |
-
if project_request(row)["state"] in ("completed", "rejected", "canceled", "failed"):
|
| 698 |
raise ValueError("This request has already ended.")
|
| 699 |
row.update({"status": "Snoozed", "snoozed_until": until})
|
| 700 |
|
|
@@ -703,7 +739,7 @@ def snooze(rt, ticket_id, by, until):
|
|
| 703 |
|
| 704 |
def resolve(rt, ticket_id, by):
|
| 705 |
def change(row):
|
| 706 |
-
changes = _transition(rt, row, by, "completed")
|
| 707 |
row.update({**changes, "status": "Resolved", "closed_at": _utc().isoformat()})
|
| 708 |
return _change(rt, ticket_id, by, change)[1]
|
| 709 |
|
|
@@ -719,7 +755,7 @@ def list_for(rt, username, *, status=None):
|
|
| 719 |
continue
|
| 720 |
try:
|
| 721 |
_key, ticket = _ticket(rt, rid)
|
| 722 |
-
_request_reader(rt, ticket, username, sender=False)
|
| 723 |
except PermissionError:
|
| 724 |
continue
|
| 725 |
rows.append({**row, **project_request({**row, "id": str(rid)})})
|
|
@@ -729,11 +765,60 @@ def list_for(rt, username, *, status=None):
|
|
| 729 |
def assignable(rt, ticket_id, by):
|
| 730 |
_key, ticket = _ticket(rt, ticket_id)
|
| 731 |
_member(ticket, by)
|
| 732 |
-
_request_reader(rt, ticket, by, sender=False)
|
| 733 |
return audience(rt, ticket)
|
| 734 |
|
| 735 |
|
| 736 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 737 |
if source is None:
|
| 738 |
return {"source_database": "", "source_record": "", "required_fields": ""}
|
| 739 |
database, record = source["database"], source["record"]
|
|
@@ -747,15 +832,21 @@ def _prepare_request_source(rt, by, source):
|
|
| 747 |
if not perm_scope.may_read(principal, database, st=rt):
|
| 748 |
raise ValueError("You cannot see the source database.")
|
| 749 |
try:
|
| 750 |
-
fields, rows = perm_scope._read(database, principal, rt,
|
|
|
|
| 751 |
except Exception as exc:
|
| 752 |
raise ValueError("The source data could not be read.") from exc
|
| 753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
if record and source_row is None:
|
| 755 |
raise ValueError("The source record could not be found.")
|
| 756 |
-
#
|
| 757 |
return {"source_database": database, "source_record": record,
|
| 758 |
-
"required_fields": ",".join(
|
| 759 |
"source_row": source_row}
|
| 760 |
|
| 761 |
|
|
@@ -969,11 +1060,12 @@ def _notify_app_share(rt, request_id):
|
|
| 969 |
_LOG.warning("App share request %s notice failed (%s).", request_id, type(exc).__name__)
|
| 970 |
|
| 971 |
|
| 972 |
-
def request_attribution(rt, row, by, *, source_check=None):
|
| 973 |
"""Keep participant attribution while hiding all source-derived text after access changes."""
|
| 974 |
name, _principal_user = _principal(rt, by)
|
| 975 |
task = project_request(row)
|
| 976 |
-
|
|
|
|
| 977 |
raise PermissionError("There is no request with that ID.")
|
| 978 |
visible = True
|
| 979 |
try:
|
|
@@ -984,7 +1076,9 @@ def request_attribution(rt, row, by, *, source_check=None):
|
|
| 984 |
# An object grant alone cannot prove access to the data quoted in its free text.
|
| 985 |
visible = False
|
| 986 |
if task["source"]:
|
| 987 |
-
prepared = _prepare_request_source(
|
|
|
|
|
|
|
| 988 |
if name not in audience(rt, prepared):
|
| 989 |
visible = False
|
| 990 |
if source_check is not None:
|
|
@@ -1022,8 +1116,8 @@ def _recipient_request_rows(rt, by):
|
|
| 1022 |
import agent_inbox
|
| 1023 |
name, _user = _principal(rt, by)
|
| 1024 |
_key, table = _table(rt)
|
| 1025 |
-
rows = [
|
| 1026 |
-
if name in
|
| 1027 |
rows += agent_inbox.recipient_records(rt, name)
|
| 1028 |
|
| 1029 |
def order(row):
|
|
@@ -1069,7 +1163,21 @@ def list_requests(rt, by, *, state=None):
|
|
| 1069 |
if state is not None and state not in REQUEST_STATES:
|
| 1070 |
raise ValueError("Choose a supported request state.")
|
| 1071 |
import agent_inbox
|
| 1072 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1073 |
return sorted((row for row in rows if state is None or row["state"] == state),
|
| 1074 |
key=lambda row: (row["createdAt"], row["id"]), reverse=True)
|
| 1075 |
|
|
|
|
| 72 |
return _names(value) if isinstance(value, str) else set()
|
| 73 |
|
| 74 |
|
| 75 |
+
def _legacy_condition(row):
|
| 76 |
+
"""Recognize condition alerts without treating a person's explicit request as system work."""
|
| 77 |
+
sender = (row.get("sender") or {}).get("username")
|
| 78 |
+
return bool(row.get("check_id") and row.get("cond_key")
|
| 79 |
+
and not (row.get("kind") or row.get("type"))
|
| 80 |
+
and row.get("from_person") in (None, "", SYSTEM_USER)
|
| 81 |
+
and sender in (None, "", SYSTEM_USER))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def _request_recipients(row):
|
| 85 |
+
"""A condition's eligible alert audience is separate from its explicit assignees."""
|
| 86 |
+
if _legacy_condition(row):
|
| 87 |
+
return _names(row.get("assigned_to")) | _names(row.get("owner"))
|
| 88 |
+
return _names(row.get("recipients", row.get("audience")))
|
| 89 |
+
|
| 90 |
+
|
| 91 |
def _keys(value):
|
| 92 |
parts = value.split(",") if isinstance(value, str) else value or ()
|
| 93 |
return sorted({str(part).strip() for part in parts if str(part).strip()})
|
|
|
|
| 103 |
"record": str(value.get("record", value.get("record_id")) or "")}
|
| 104 |
|
| 105 |
|
| 106 |
+
def project_request(row, *, condition_audience=False):
|
| 107 |
+
"""Project C4 after access checks, separating phantom closure from condition lifecycle authority."""
|
| 108 |
kind = row.get("kind") or row.get("type") or "assignment"
|
| 109 |
if kind not in REQUEST_KINDS:
|
| 110 |
raise ValueError("Unknown request kind.")
|
|
|
|
| 116 |
"sent": "submitted",
|
| 117 |
"pending": "input-required" if kind in ("access_request", "approval") else "submitted"}.get(legacy)
|
| 118 |
state = state or ("working" if _names(row.get("assigned_to")) or row.get("owner") else "submitted")
|
| 119 |
+
if (not condition_audience and _legacy_condition(row) and row.get("legacy_assignment_closed_at")
|
| 120 |
+
and not _request_recipients(row)):
|
| 121 |
+
state = "completed"
|
| 122 |
rid = str(row.get("id") or row.get("request_id") or "")
|
| 123 |
source = row.get("source")
|
| 124 |
if source is None and row.get("source_database"):
|
|
|
|
| 136 |
metadata["reason"] = row["state_reason"]
|
| 137 |
return {"id": rid, "contextId": row.get("contextId") or subject, "kind": kind, "state": state,
|
| 138 |
"from": {"person": person, "agent": row.get("from_agent", SYSTEM_USER)},
|
| 139 |
+
"to": sorted(_request_recipients(row)),
|
| 140 |
"title": row.get("title") or row.get("text") or "",
|
| 141 |
"body": row.get("body", row.get("text", row.get("reason"))) or "",
|
| 142 |
"source": source, "createdAt": row.get("created_at") or row.get("raised_at") or "",
|
|
|
|
| 171 |
return name, users._public(name, rec)
|
| 172 |
|
| 173 |
|
| 174 |
+
def _request_reader(rt, row, by, *, sender=True, condition_audience=False):
|
| 175 |
name, _user = _principal(rt, by)
|
| 176 |
task = project_request(row)
|
| 177 |
+
recipients = _stored_audience(row) if condition_audience and _legacy_condition(row) else task["to"]
|
| 178 |
+
if name not in recipients and not (sender and name == task["from"]["person"]):
|
| 179 |
raise PermissionError("There is no request with that ID.")
|
| 180 |
+
if task["source"]:
|
| 181 |
+
try:
|
| 182 |
+
prepared = row if condition_audience and _legacy_condition(row) else _prepare_request_source(
|
| 183 |
+
rt, name, task["source"], required_fields=row.get("required_fields"),
|
| 184 |
+
source_row=row.get("source_row") if _legacy_condition(row) else None)
|
| 185 |
+
if name not in audience(rt, prepared):
|
| 186 |
+
raise PermissionError("You can no longer see the data this request needs.")
|
| 187 |
+
except Exception as exc:
|
| 188 |
+
raise PermissionError("You can no longer see the data this request needs.") from exc
|
| 189 |
return name
|
| 190 |
|
| 191 |
|
| 192 |
+
def _transition(rt, row, by, state, reason=None, *, condition_audience=False):
|
| 193 |
"""Check transition authority without granting access to any data or object."""
|
| 194 |
if state not in REQUEST_STATES:
|
| 195 |
raise ValueError("Choose a supported request state.")
|
| 196 |
+
task = project_request(row, condition_audience=condition_audience)
|
| 197 |
+
recipients = _stored_audience(row) if condition_audience and _legacy_condition(row) else task["to"]
|
| 198 |
system = by is SYSTEM_ACTOR
|
| 199 |
name = None if system else _principal(rt, by)[0]
|
| 200 |
if state == "failed":
|
|
|
|
| 206 |
if name != task["from"]["person"]:
|
| 207 |
raise PermissionError("Only the sender can withdraw a request.")
|
| 208 |
elif state == "auth-required":
|
| 209 |
+
if not system and name not in recipients:
|
| 210 |
raise PermissionError("Only the recipient or system can report an expired connection.")
|
| 211 |
+
elif system or name not in recipients:
|
| 212 |
raise PermissionError("Only a recipient can change this request's state.")
|
| 213 |
if task["state"] in ("completed", "rejected", "canceled", "failed") and state != task["state"]:
|
| 214 |
raise ValueError("This request has already ended.")
|
|
|
|
| 635 |
return _sync(rt, str(check_id), [], live_keys=live_keys)["closed"]
|
| 636 |
|
| 637 |
|
| 638 |
+
def _ticket_row(rt, table, ticket_id, row):
|
| 639 |
+
"""Attach condition evidence without lending stale editable records to source admission."""
|
|
|
|
|
|
|
|
|
|
| 640 |
source_row = (table.get("ticketSourceRows") or {}).get(str(ticket_id))
|
| 641 |
database = str(row.get("source_database") or "")
|
| 642 |
if database.startswith(user_tables.KEY_PREFIX) and user_tables.records_mutable(database, st=rt):
|
| 643 |
# Assignment checks use the current editable source, including a deletion, rather
|
| 644 |
# than the older row captured when the condition was raised.
|
| 645 |
source_row = None
|
| 646 |
+
return {**row, "id": str(ticket_id), "source_row": source_row}
|
| 647 |
+
|
| 648 |
+
|
| 649 |
+
def _ticket(rt, ticket_id):
|
| 650 |
+
key, table = _table(rt)
|
| 651 |
+
row = ((table or {}).get("rows") or {}).get(str(ticket_id))
|
| 652 |
+
if not isinstance(row, dict):
|
| 653 |
+
raise PermissionError("There is no request with that ID.")
|
| 654 |
+
return key, _ticket_row(rt, table, ticket_id, row)
|
| 655 |
|
| 656 |
|
| 657 |
def _member(row, by):
|
|
|
|
| 662 |
def _change(rt, ticket_id, by, change):
|
| 663 |
key, ticket = _ticket(rt, ticket_id)
|
| 664 |
_member(ticket, by)
|
| 665 |
+
_request_reader(rt, ticket, by, sender=False, condition_audience=True)
|
| 666 |
result = {}
|
| 667 |
|
| 668 |
def write(data):
|
|
|
|
| 674 |
row["updated_at"] = _utc().isoformat()
|
| 675 |
result.clear()
|
| 676 |
result.update({**row, **project_request({**row, "id": str(ticket_id)})})
|
| 677 |
+
result.pop("source_row", None)
|
| 678 |
return data
|
| 679 |
|
| 680 |
rt.update(user_tables.STORE_KEY, write, flush="sync")
|
|
|
|
| 690 |
raise ValueError(f"{_text(to)} cannot see the data this request needs, so it cannot be assigned to them.")
|
| 691 |
|
| 692 |
def change(row):
|
| 693 |
+
if project_request(row, condition_audience=True)["state"] in ("completed", "rejected", "canceled", "failed"):
|
| 694 |
raise ValueError("This request has already ended.")
|
| 695 |
try:
|
| 696 |
assignments = json.loads(row.get("assignments") or "{}")
|
|
|
|
| 703 |
"assigned_to": _joined(_names(row.get("assigned_to")) | {to}),
|
| 704 |
"assigned_by": _joined(_names(row.get("assigned_by")) | {by}),
|
| 705 |
"assignments": json.dumps(assignments, sort_keys=True), "owner": to})
|
| 706 |
+
if _legacy_condition(row):
|
| 707 |
+
row.pop("legacy_assignment_closed_at", None)
|
| 708 |
if row.get("state") in REQUEST_STATES:
|
| 709 |
row["state"] = "working"
|
| 710 |
|
|
|
|
| 730 |
until = _date(until)
|
| 731 |
|
| 732 |
def change(row):
|
| 733 |
+
if project_request(row, condition_audience=True)["state"] in ("completed", "rejected", "canceled", "failed"):
|
| 734 |
raise ValueError("This request has already ended.")
|
| 735 |
row.update({"status": "Snoozed", "snoozed_until": until})
|
| 736 |
|
|
|
|
| 739 |
|
| 740 |
def resolve(rt, ticket_id, by):
|
| 741 |
def change(row):
|
| 742 |
+
changes = _transition(rt, row, by, "completed", condition_audience=True)
|
| 743 |
row.update({**changes, "status": "Resolved", "closed_at": _utc().isoformat()})
|
| 744 |
return _change(rt, ticket_id, by, change)[1]
|
| 745 |
|
|
|
|
| 755 |
continue
|
| 756 |
try:
|
| 757 |
_key, ticket = _ticket(rt, rid)
|
| 758 |
+
_request_reader(rt, ticket, username, sender=False, condition_audience=True)
|
| 759 |
except PermissionError:
|
| 760 |
continue
|
| 761 |
rows.append({**row, **project_request({**row, "id": str(rid)})})
|
|
|
|
| 765 |
def assignable(rt, ticket_id, by):
|
| 766 |
_key, ticket = _ticket(rt, ticket_id)
|
| 767 |
_member(ticket, by)
|
| 768 |
+
_request_reader(rt, ticket, by, sender=False, condition_audience=True)
|
| 769 |
return audience(rt, ticket)
|
| 770 |
|
| 771 |
|
| 772 |
+
def close_stale_system_assignments(rt, by, before):
|
| 773 |
+
"""Close stale legacy assignment projections without ending their business conditions."""
|
| 774 |
+
def administrator():
|
| 775 |
+
name, principal = _principal(rt, by)
|
| 776 |
+
if not perms.is_admin(principal):
|
| 777 |
+
raise PermissionError("Only an Administrator can close stale system requests.")
|
| 778 |
+
return name
|
| 779 |
+
|
| 780 |
+
administrator()
|
| 781 |
+
now = _utc()
|
| 782 |
+
try:
|
| 783 |
+
cutoff = _utc(before) if isinstance(before, str) and before.strip() else None
|
| 784 |
+
except (TypeError, ValueError, OverflowError) as exc:
|
| 785 |
+
raise ValueError("Choose a past cutoff timestamp for stale system requests.") from exc
|
| 786 |
+
if cutoff is None or cutoff > now:
|
| 787 |
+
raise ValueError("Choose a past cutoff timestamp for stale system requests.")
|
| 788 |
+
key, _table_value = _table(rt)
|
| 789 |
+
result = {"closed": 0, "before": cutoff.isoformat()}
|
| 790 |
+
if key is None:
|
| 791 |
+
return result
|
| 792 |
+
|
| 793 |
+
def write(data):
|
| 794 |
+
administrator()
|
| 795 |
+
result["closed"] = 0
|
| 796 |
+
table = data.get(key) or {}
|
| 797 |
+
if table.get("systemKind") != "tickets":
|
| 798 |
+
return data
|
| 799 |
+
for row in (table.get("rows") or {}).values():
|
| 800 |
+
if (not _legacy_condition(row) or _request_recipients(row) or row.get("assigned_by")
|
| 801 |
+
or row.get("legacy_assignment_closed_at")
|
| 802 |
+
or row.get("status") not in STATUSES[:2]
|
| 803 |
+
or row.get("state") in ("completed", "rejected", "canceled", "failed")
|
| 804 |
+
or not row.get("source_database") or row.get("target") or row.get("approvalAction")):
|
| 805 |
+
continue
|
| 806 |
+
try:
|
| 807 |
+
provenance = json.loads(row.get("assignments") or "{}")
|
| 808 |
+
created = row.get("created_at") or row.get("raised_at")
|
| 809 |
+
if provenance != {} or not created or _utc(created) >= cutoff:
|
| 810 |
+
continue
|
| 811 |
+
except (TypeError, ValueError, OverflowError):
|
| 812 |
+
continue
|
| 813 |
+
row.update(legacy_assignment_closed_at=now.isoformat(), updated_at=now.isoformat())
|
| 814 |
+
result["closed"] += 1
|
| 815 |
+
return data
|
| 816 |
+
|
| 817 |
+
rt.update(user_tables.STORE_KEY, write, flush="sync")
|
| 818 |
+
return result
|
| 819 |
+
|
| 820 |
+
|
| 821 |
+
def _prepare_request_source(rt, by, source, *, required_fields=None, source_row=None):
|
| 822 |
if source is None:
|
| 823 |
return {"source_database": "", "source_record": "", "required_fields": ""}
|
| 824 |
database, record = source["database"], source["record"]
|
|
|
|
| 832 |
if not perm_scope.may_read(principal, database, st=rt):
|
| 833 |
raise ValueError("You cannot see the source database.")
|
| 834 |
try:
|
| 835 |
+
fields, rows = perm_scope._read(database, principal, rt,
|
| 836 |
+
want_rows=bool(record) and not isinstance(source_row, dict))
|
| 837 |
except Exception as exc:
|
| 838 |
raise ValueError("The source data could not be read.") from exc
|
| 839 |
+
field_keys = _keys([f["key"] for f in fields if f.get("key")])
|
| 840 |
+
required = _keys(required_fields) or field_keys
|
| 841 |
+
if not field_keys or not set(required).issubset(field_keys):
|
| 842 |
+
raise ValueError("The required source fields could not be read.")
|
| 843 |
+
if not isinstance(source_row, dict):
|
| 844 |
+
source_row = next((r for r in rows if str(r.get("id", r.get("pid", ""))) == record), None) if record else None
|
| 845 |
if record and source_row is None:
|
| 846 |
raise ValueError("The source record could not be found.")
|
| 847 |
+
# Declared condition fields cannot prove which values its free text quotes.
|
| 848 |
return {"source_database": database, "source_record": record,
|
| 849 |
+
"required_fields": ",".join(field_keys),
|
| 850 |
"source_row": source_row}
|
| 851 |
|
| 852 |
|
|
|
|
| 1060 |
_LOG.warning("App share request %s notice failed (%s).", request_id, type(exc).__name__)
|
| 1061 |
|
| 1062 |
|
| 1063 |
+
def request_attribution(rt, row, by, *, source_check=None, condition_audience=False):
|
| 1064 |
"""Keep participant attribution while hiding all source-derived text after access changes."""
|
| 1065 |
name, _principal_user = _principal(rt, by)
|
| 1066 |
task = project_request(row)
|
| 1067 |
+
recipients = _stored_audience(row) if condition_audience and _legacy_condition(row) else task["to"]
|
| 1068 |
+
if name not in recipients and name != task["from"]["person"]:
|
| 1069 |
raise PermissionError("There is no request with that ID.")
|
| 1070 |
visible = True
|
| 1071 |
try:
|
|
|
|
| 1076 |
# An object grant alone cannot prove access to the data quoted in its free text.
|
| 1077 |
visible = False
|
| 1078 |
if task["source"]:
|
| 1079 |
+
prepared = _prepare_request_source(
|
| 1080 |
+
rt, name, task["source"], required_fields=row.get("required_fields"),
|
| 1081 |
+
source_row=row.get("source_row") if _legacy_condition(row) else None)
|
| 1082 |
if name not in audience(rt, prepared):
|
| 1083 |
visible = False
|
| 1084 |
if source_check is not None:
|
|
|
|
| 1116 |
import agent_inbox
|
| 1117 |
name, _user = _principal(rt, by)
|
| 1118 |
_key, table = _table(rt)
|
| 1119 |
+
rows = [_ticket_row(rt, table, rid, row) for rid, row in ((table or {}).get("rows") or {}).items()
|
| 1120 |
+
if name in _request_recipients(row)]
|
| 1121 |
rows += agent_inbox.recipient_records(rt, name)
|
| 1122 |
|
| 1123 |
def order(row):
|
|
|
|
| 1163 |
if state is not None and state not in REQUEST_STATES:
|
| 1164 |
raise ValueError("Choose a supported request state.")
|
| 1165 |
import agent_inbox
|
| 1166 |
+
try:
|
| 1167 |
+
candidates = _recipient_request_rows(rt, by)
|
| 1168 |
+
except PermissionError:
|
| 1169 |
+
return []
|
| 1170 |
+
rows = []
|
| 1171 |
+
for row in candidates:
|
| 1172 |
+
try:
|
| 1173 |
+
if row.get("request_id"):
|
| 1174 |
+
task = agent_inbox.get_request(rt, row["request_id"], by)
|
| 1175 |
+
else:
|
| 1176 |
+
_request_reader(rt, row, by, sender=False)
|
| 1177 |
+
task = project_request(row)
|
| 1178 |
+
rows.append(task)
|
| 1179 |
+
except PermissionError:
|
| 1180 |
+
continue
|
| 1181 |
return sorted((row for row in rows if state is None or row["state"] == state),
|
| 1182 |
key=lambda row: (row["createdAt"], row["id"]), reverse=True)
|
| 1183 |
|