Skip to content

Commit 878aa2e

Browse files
os-steveclaude
andauthored
fix(runtime,objectql): /api/v1/ready drains only on the PRIMARY datasource's failure; a secondary is reported, not drained (#13763)
* wip(runtime): partition /ready driver health into primary vs secondary (#13408) * test(objectql): pin system-object-unbound to the reachable zero-driver state (#13408) * chore: changeset for #13408 * docs(permissions): re-anchor the system-context census after the engine.ts line shift (#13408) * fix(test): type the driver double and narrow the dispatch result so the hidden test layer type-checks (#13408) --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent b05ce97 commit 878aa2e

9 files changed

Lines changed: 914 additions & 56 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
"@objectstack/objectql": minor
3+
"@objectstack/runtime": minor
4+
---
5+
6+
fix(runtime,objectql): `/api/v1/ready` drains only on the PRIMARY datasource's failure; a secondary/tenant datasource is reported, not drained (#13408)
7+
8+
On a multi-datasource deployment, one datasource whose driver could not start
9+
pinned `/api/v1/ready` to 503 on **every replica** — so a readiness-checked load
10+
balancer drained every upstream and took the whole deployment offline, while
11+
Postgres and the app itself were healthy (`/api/v1/health` 200, direct reads
12+
working). One tenant's misconfiguration = total outage. Observed on a live
13+
3-replica EE deployment and recovered only by restarting every process.
14+
15+
Ruled 2026-08-31 (第 6 场总监席决裁批 #12, maintainer verbatim 「同意」), Option B:
16+
17+
> `/api/v1/ready` 只在**主/默认数据源**不健康时摘流量;次要/租户数据源的故障照常
18+
> **上报**(`/ready` 响应 body、日志、告警)但不 drain 节点。
19+
20+
**What changed.** When a driver reports itself unhealthy, `/ready` now asks
21+
which datasource is the deployment's primary one before choosing a status:
22+
23+
- primary unhealthy, or the criterion unresolvable ⇒ **503**, unchanged envelope;
24+
- primary healthy, only a secondary down ⇒ **200**, with the failed drivers named
25+
in a new `degraded` block: `{ status, state, degraded: { drivers, primaryDatasource } }`.
26+
27+
The failed driver is never hidden — the rejected alternative of filtering it out
28+
of the response stays rejected. `degraded` appears **only** on that branch; an
29+
all-healthy 200 body is byte-identical to before.
30+
31+
**The criterion is a readable fact, not a heuristic.** `ObjectQL.resolvePrimaryDatasource()`
32+
(new, exported with `PrimaryDatasourceVerdict` / `PrimaryDatasourceUnresolvedReason`)
33+
answers *where this deployment's platform system objects actually live*, resolved
34+
through the same five-step routing order every query uses — never registration
35+
order, never the driver flagged default at registration. The ADR-0057 §3.6 system
36+
ledgers (`audit` / `telemetry` / `event`) are excluded because they are
37+
deliberately routed off the primary. Disagreement, silence, or a name with no
38+
driver behind it return an **unresolved verdict**, never a guess.
39+
40+
**Fail toward draining.** Every way of not knowing — an engine that predates the
41+
probe, a probe that throws, a malformed verdict, a genuinely split deployment —
42+
falls back to the old whole-node 503. Staying in rotation requires a *positive*
43+
reading; the absence of a negative one is not permission. Pinned in both
44+
directions, including an inversion ablation.
45+
46+
**framework#3756 is not overturned.** Its quantified reason — "a replica that
47+
would fail 100% of its requests" — still holds in the single-datasource
48+
deployment it was measured on, where the primary *is* the only source; that
49+
deployment's answer is unchanged down to the response body. What #3756's
50+
reasoning never covered is the multi-datasource shape, and that is the only
51+
branch this carves out.
52+
53+
**Grade: `minor` for both, proposed rather than assumed** — this changes when a
54+
published operational probe drains a node, so an operator whose alerting keys on
55+
`/ready` returning 503 for *any* driver failure will see 200 + `degraded`
56+
instead, and `degraded` is a new response field. Nothing is removed or renamed,
57+
no declared contract key is added (the ruling explicitly declines that: 「⛔ 本裁不
58+
加契约键」), single-datasource behaviour is bit-identical, and no migration is
59+
required — so `major` overstates it and `patch` understates a deliberate change
60+
to an availability control surface.
61+
62+
⚠️ Out of scope, tracked separately as **#13578**: `DELETE` of a datasource still
63+
does not evict the stuck driver from the in-memory engine registry, so the
64+
datasource keeps appearing in `/ready`'s report until the process restarts. That
65+
half is a defect under either answer to this card and is queued independently.

content/docs/permissions/system-context.mdx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ that silently does not happen.
109109

110110
| # | Behaviour when `isSystem` | Package | What you get / what you lose | Anchor |
111111
|:--|:---|:---|:---|:---|
112-
| 18 | **`readonly` strip bypassed — UPDATE, single row** | objectql | Get: a `readonly` field CAN be written. Lose: the protection that stops a caller seeding e.g. `approval_status` | `objectql/src/engine.ts:10616` |
113-
| 19 | **`readonly` strip bypassed — UPDATE, bulk/predicate** | objectql | Same, on the multi-row path | `objectql/src/engine.ts:10778` |
114-
| 20 | **`readonly` strip bypassed — INSERT (engine pass)** | objectql | Same, on create | `objectql/src/engine.ts:9509` |
112+
| 18 | **`readonly` strip bypassed — UPDATE, single row** | objectql | Get: a `readonly` field CAN be written. Lose: the protection that stops a caller seeding e.g. `approval_status` | `objectql/src/engine.ts:10712` |
113+
| 19 | **`readonly` strip bypassed — UPDATE, bulk/predicate** | objectql | Same, on the multi-row path | `objectql/src/engine.ts:10874` |
114+
| 20 | **`readonly` strip bypassed — INSERT (engine pass)** | objectql | Same, on create | `objectql/src/engine.ts:9605` |
115115
| 21 | **`readonly` strip bypassed — INSERT (protocol ingress)** | metadata-protocol | `isSystem` is the **only** exemption here. `preserveAudit` is deliberately not read on this path (#6640) — a non-system historical import is still stripped on create | `metadata-protocol/src/protocol.ts:1576` |
116-
| 22 | Strict-drop refusal never fires | objectql | Lose: a caller that opted into loud refusal gets **silence** — strict refuses exactly what the strip would have taken, and the strip took nothing | `objectql/src/engine.ts:9546`, `readonly-strict-errors.ts:66` |
117-
| 23 | **Referential-integrity check skipped** | objectql | Get: writes proceed against unreachable/unresolvable targets. Lose: an `isSystem` caller can write a **dangling reference** | `objectql/src/engine.ts:5638` |
118-
| 24 | Tenant-audit warning silenced; `bypassTenantAudit` threaded to the driver | objectql | Get: unscoped system writes stop warning. Lose: the signal that would flag a genuine user-path scoping bug | `objectql/src/engine.ts:3573`, `:3583`, `:3610` |
116+
| 22 | Strict-drop refusal never fires | objectql | Lose: a caller that opted into loud refusal gets **silence** — strict refuses exactly what the strip would have taken, and the strip took nothing | `objectql/src/engine.ts:9642`, `readonly-strict-errors.ts:66` |
117+
| 23 | **Referential-integrity check skipped** | objectql | Get: writes proceed against unreachable/unresolvable targets. Lose: an `isSystem` caller can write a **dangling reference** | `objectql/src/engine.ts:5639` |
118+
| 24 | Tenant-audit warning silenced; `bypassTenantAudit` threaded to the driver | objectql | Get: unscoped system writes stop warning. Lose: the signal that would flag a genuine user-path scoping bug | `objectql/src/engine.ts:3574`, `:3584`, `:3611` |
119119
| 25 | Engine-owned / append-only write guard bypassed | plugin-security | Get: generic writes to `managedBy` engine-owned objects | `system-write-guard.ts:96`, `:120` |
120120
| 26 | Identity write guard bypassed (ADR-0092) | plugin-auth | Get: direct writes to identity tables through the generic data path | `identity-write-guard.ts:98` |
121-
| 27 | Search-companion column **kept** in a read's rows when it was explicitly requested | objectql | Get: the internal companion column is readable. Lose: nothing for app code — this is the engine reading its own index | `objectql/src/engine.ts:6336` |
122-
| 28 | Dependent-count disclosure on a blocked delete | objectql | Get: the count of blocking children. Nothing was elevated past the caller, so nothing is withheld | `objectql/src/engine.ts:11364` |
123-
| 29 | Reference-cleanup log attributes the write to `'system'` | objectql | Get: an honest actor label instead of `anonymous` when the context carries neither `userId` nor `actor` | `objectql/src/engine.ts:11293` |
121+
| 27 | Search-companion column **kept** in a read's rows when it was explicitly requested | objectql | Get: the internal companion column is readable. Lose: nothing for app code — this is the engine reading its own index | `objectql/src/engine.ts:6337` |
122+
| 28 | Dependent-count disclosure on a blocked delete | objectql | Get: the count of blocking children. Nothing was elevated past the caller, so nothing is withheld | `objectql/src/engine.ts:11460` |
123+
| 29 | Reference-cleanup log attributes the write to `'system'` | objectql | Get: an honest actor label instead of `anonymous` when the context carries neither `userId` nor `actor` | `objectql/src/engine.ts:11389` |
124124

125125
### 3. Sharing (`plugin-sharing`)
126126

@@ -179,8 +179,8 @@ a reader tracing where elevation travels needs them.
179179

180180
| # | Site | Package | What it does |
181181
|:--|:---|:---|:---|
182-
| 62 | `objectql/src/engine.ts:3405` | objectql | Propagates `isSystem` into the hook session so hooks can tell engine self-writes from user writes |
183-
| 63 | `objectql/src/engine.ts:13705` | objectql | `ScopedContext.isSystem` getter — re-exposes the underlying execution context's flag |
182+
| 62 | `objectql/src/engine.ts:3406` | objectql | Propagates `isSystem` into the hook session so hooks can tell engine self-writes from user writes |
183+
| 63 | `objectql/src/engine.ts:13801` | objectql | `ScopedContext.isSystem` getter — re-exposes the underlying execution context's flag |
184184
| 64 | `plugin-reports/src/report-service.ts:556` | plugin-reports | Threads the flag into the engine call that runs a report |
185185
| 65 | `body-runner.ts:279` | runtime | Rebuilds an `ExecutionContext` from a hook session, carrying the flag across |
186186

@@ -195,7 +195,7 @@ assuming `isSystem` covers it is a documented source of bugs.
195195
|:---|:---|:---|
196196
| "It suppresses triggers / record-change automation" | **No.** Only `skipTriggers` does. A bare `{ isSystem: true }` on a seed write re-fired automation on freshly seeded rows and wedged first boot | `metadata-protocol/src/seed-loader.ts:1909` (rationale at `:1819``1821`, #3760), `flow.zod.ts:685` |
197197
| "It skips the state machine" | **No.** That is `skipStateMachine`, carried by seed replay and by `treatAsHistorical` imports | `objectql/src/engine.ts` FSM gate; see [State Machine](/docs/protocol/objectql/state-machine) |
198-
| "It skips validation rules" | **No.** Field shape, `format`, `script` and the rest still run. The `readonly` strip runs *before* validation precisely so a discarded value is not judged | `objectql/src/engine.ts:9492``9509` |
198+
| "It skips validation rules" | **No.** Field shape, `format`, `script` and the rest still run. The `readonly` strip runs *before* validation precisely so a discarded value is not judged | `objectql/src/engine.ts:9588``9605` |
199199
| "It preserves a supplied `updated_at` / `updated_by`" | **No.** That is `preserveAudit`, a separate opt-in — and an UPDATE-path exemption only | `field.zod.ts:1451` (#3493 / #6640) |
200200
| "It stamps `created_by`" | **No.** Audit stamping reads `userId` from the context. A user-less system write stamps nothing — that is today's behaviour, not an error | `runtime-identity.ts:280``281` |
201201
| "It bypasses every guard" | **No.** The last-admin guard applies to **every** context, `isSystem` included — the deprovision path that actually locks an org out is the system one | `last-admin-guard.ts:286` |

packages/objectql/src/driver-connect-errors.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,54 @@ export interface DriverHealth {
1919
skipped?: boolean;
2020
}
2121

22+
/**
23+
* Why {@link PrimaryDatasourceVerdict} could not name a primary datasource.
24+
*
25+
* Every member means the SAME thing to a readiness probe — *we cannot tell* —
26+
* and the ruling on #13408 fixes what a probe must do with that answer: fall
27+
* back to the old whole-node 503 (fail toward draining), never to "don't
28+
* drain". They are distinguished only so the reason can be logged and reported;
29+
* ⛔ a caller must not branch on them to keep a replica in rotation.
30+
*/
31+
export type PrimaryDatasourceUnresolvedReason =
32+
/** No platform system object is registered here — nothing to read the fact off. */
33+
| 'no-system-objects-registered'
34+
/** The platform's system objects are split across more than one datasource. */
35+
| 'system-objects-split'
36+
/** A registered system object routes nowhere: no binding and no default driver. */
37+
| 'system-object-unbound'
38+
/** The name resolved, but no driver is registered under it, so nothing probes it. */
39+
| 'no-driver-registered';
40+
41+
/**
42+
* WHICH datasource carries this deployment's platform system objects — the
43+
* machine-readable "primary/default datasource" fact the #13408 ruling requires
44+
* (2026-08-31, 第 6 场总监席决裁批 #12), stated as a verdict rather than a
45+
* `string | undefined` so "we could not tell" can never be mistaken for a name.
46+
*
47+
* ⛔ The ruling forbids deriving this from a heuristic such as "the first
48+
* datasource registered". The one implementation is
49+
* `ObjectQL.resolvePrimaryDatasource()`.
50+
*/
51+
export type PrimaryDatasourceVerdict =
52+
| {
53+
resolved: true;
54+
/** The datasource name, which is also a registered driver's name. */
55+
datasource: string;
56+
/**
57+
* How many registered platform system objects agreed on it. Never 0 — a
58+
* verdict read off nothing is `no-system-objects-registered`, not a name,
59+
* so a caller cannot act on a vacuous agreement.
60+
*/
61+
witnesses: number;
62+
}
63+
| {
64+
resolved: false;
65+
reason: PrimaryDatasourceUnresolvedReason;
66+
/** The distinct datasource names seen, when more than one disagreed. */
67+
candidates?: readonly string[];
68+
};
69+
2270
/** `error.message` when it is an Error, its string form otherwise. */
2371
function failureMessage(error: unknown): string {
2472
if (error instanceof Error) return error.message || error.name;

0 commit comments

Comments
 (0)