Repository navigation
Commit be16feb
feat(verify): flows.run takes the system principal, starting a flow as its own trigger does (#23551)
Fixes #23255
Clause-②: yes
## What changes
`@objectstack/verify`'s `flows.run` gains a second overload,
`flows.run(name, params, { system: true })`. It starts a flow as the
system principal, the way the flow's own trigger starts it when it
fires: the booted `automation` service's `execute(name, { params,
callerParamKeys: [] })`, in process. That is the call
`AutomationEngine.activateFlowTrigger` hands every trigger as its
callback, with the context the schedule tick builds (seeds as `params`,
no caller-supplied keys).
- The `{ as }` overload is unchanged. It still drives `POST
/automation/:name/trigger` through the runtime's `HttpDispatcher`.
- No HTTP door starts a flow as the system principal. No runtime file
changes: no route, no admission change (`flow-start-admission.ts` is
untouched).
- The answer is the same `FlowRun` shape (`AutomationResult & { flowName
}`), so a paused run goes straight back to `flows.resume`.
- A call naming both `as` and `system: true` is refused with
`INVALID_REQUEST` / `400` before anything runs, through the same
`namesSystem` guard `hooks.run` uses.
Landing spot: `packages/verify/src/handle.ts` only, inside the claimed
file surface. The in-process service is reached through
`kernel.getServiceAsync('automation')`, the same accessor
`automation.evaluateCondition` uses, so no runtime change was needed.
## Mechanism assumptions, measured
- `flows.run` was declared at `handle.ts:371` with `opts: AsUser`, and
dispatched the trigger route with the bearer only. Confirmed.
- `hooks.run`'s system arm is an overload pair (`opts: AsUser` / `opts:
AsSystem`) over one implementation that branches on `namesSystem(opts,
door)`. It is not a boolean on `AsUser`. Mirrored exactly.
- The schedule tick
(`packages/triggers/trigger-schedule/src/schedule-trigger.ts`) builds `{
event: 'schedule', tenantId?, params: { jobId, flowName, schedule },
callerParamKeys: [] }`, and the engine's trigger callback is
`this.execute(flowName, ctx)`. The tick context carries no
`executionContext`. The admission predicate
(`refusesElevatedSelfTriggeredStart`) is asked only at the three doors
that start a flow by name, never inside `execute`. So the in-process
`execute` call is the tick's path, and it needs no system execution
context.
## Pins (`packages/verify/src/handle.flows-system-start.test.ts`, real
booted stack)
| Case | Expected |
|---|---|
| `schedule`, `autolaunched` and `record_change` flows declared `runAs:
'system'`, started with `{ system: true }` | `success: true`, `flowName`
set, 1 elevated ledger row, 1 run-log entry |
| The same three, the same call `as` a member, and `as` the platform
admin (negative controls) | `VerifyRefusal`, `PERMISSION_DENIED` /
`403`, 0 rows, 0 runs |
| A member's direct ledger create (fixture control) |
`PERMISSION_DENIED` / `403`, 0 rows |
| `params` reach the run as seeds (`{ tag }` → the row's name) | 1 row
named by the seed |
| A run that fails (its write is refused) | resolves `success: false`,
`status: 'failed'`, 0 rows, 1 run |
| An unknown flow name | resolves `success: false`, no `code`, the name
in `error`, 0 runs |
| A paused (screen) run, resumed `as` admin through `flows.resume` |
`status: 'paused'` with `runId`, then the resumed write lands |
| `{ as, system: true }` together | `INVALID_REQUEST` / `400`, 0 rows, 0
runs |
Pin sweep: the error code (`PERMISSION_DENIED`) and the refusal message
("This caller may not start this flow") were grepped across the repo. No
pin flips: the trigger door's refusal is unchanged, and every existing
pin of it keeps its assertion verbatim
(`automation-trigger-elevated-door.test.ts` and the runtime door-side
pins).
## Ablation (one-time proof, not a permanent test)
The fix was committed first. Then `scripts/ablation-replace.mjs` mutated
the system branch to be skipped (`if (namesSystem(opts, 'flows.run')) {`
→ `if (false as boolean && namesSystem(opts, 'flows.run')) {`). Anchor 1
→ 0, blob `b18cec017dd6` → `684aab878ddc`. The suite imports
`./handle.js` from source, so no rebuild was needed for the mutation to
reach the run.
- Mutated: `Tests 8 failed | 7 passed (15)`. All 8 system-arm cases
failed. The 7 controls stayed green (6 member/admin 403 rows and the
direct-write control).
- Restored: blob after restore == HEAD (`b18cec017dd6`), `git diff HEAD`
empty.
## Acceptance notes
- **Failure answer.** On `success: false` the system arm resolves, as
the engine answered. It does not reject. This follows the handle's own
design rule ("returns what that door returned"), and `execute` returns a
failed or unknown-flow result instead of throwing. Two consequences are
documented in the TSDoc, README and changeset: a test should assert
`success` before it asserts side effects, and the `{ as }` overload
rejects for the same situations (through the route's
`classifyFlowRefusal` table). HotCRM's interim helper rejects instead.
The report carries this as an open question with options. Rejecting with
the route's exact `code` + `status` would need `classifyFlowRefusal`
exported from `@objectstack/runtime`, which is outside this card's
surface.
- **What the context leaves out.** No `event`: the run history records
the engine's default trigger type, not `schedule`. No organization: a
schedule binding that declares an acting organization runs as it on a
real tick, and this arm does not read the binding. Both are stated in
the TSDoc.
- **Known blind spot.** `automation-trigger-elevated-door.test.ts`'s
`system` row still drives the HTTP dispatcher's `handleAutomation` with
a hand-set `{ isSystem: true }` execution context. That row measures the
route's own system arm, which is a different claim, so it is left as is.
## Local verification
- Commands: `pnpm --filter '@objectstack/verify^...' build` (exit 0),
`pnpm --filter @objectstack/verify build` (exit 0), and `pnpm --filter
@objectstack/verify typecheck` (exit 0, test layer included).
- Targeted run, three files (the new one,
`automation-trigger-elevated-door.test.ts`, `handle.test.ts`): 55/55
passed.
- Full `@objectstack/verify` suite (`pnpm --filter @objectstack/verify
exec vitest run --maxWorkers=2`, at `ba08362c27`): `Test Files 41 passed
(41)`, `Tests 362 passed (362)`, exit 0.
- Derived gate families (`node scripts/pm/dispatch-gates.mjs --commands
--repo objectstack-ai/objectstack`, tree at the pre-merge head
`40e6510ac1`): 63 derived, 62 exit 0, 1 NOT MEASURED. The unmeasured one
is `pnpm check:dual-build-cjs-loads`, exit 3 `PREREQUISITE NOT MET`
(unbuilt packages outside this diff); it belongs to CI. `--ran`
reconciliation: 63 accounted, 0 unrun. The merge of `origin/main`
brought no file in this diff.
- `@objectstack/spec` `test:repo` (after `pnpm --filter
@objectstack/spec build`, at `ba08362c27`): `Test Files 61 passed (61)`,
`Tests 1033 passed (1033)`, exit 0.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01D6F9axfoPzR7t8G1J7Ytzz)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 1d79556 commit be16feb
4 files changed
Lines changed: 400 additions & 6 deletions
File tree
- .changeset
- packages/verify
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
301 | 309 | | |
302 | 310 | | |
303 | 311 | | |
| |||
338 | 346 | | |
339 | 347 | | |
340 | 348 | | |
341 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
342 | 353 | | |
343 | 354 | | |
344 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
0 commit comments