@capybara-hq/kernel 0.1.0-rc.143 → 0.1.0-rc.144
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin/admin-agents.controller.d.ts +3 -5
- package/dist/admin/admin-agents.controller.js +1 -1
- package/dist/admin/admin-connectors.controller.d.ts +3 -5
- package/dist/admin/admin-connectors.controller.js +1 -1
- package/dist/admin/admin-items.schemas.d.ts +6 -6
- package/dist/admin/admin-items.schemas.js +5 -9
- package/dist/admin/admin.controller.d.ts +18 -12
- package/dist/admin/admin.controller.js +29 -15
- package/dist/admin/admin.schemas.d.ts +12 -3
- package/dist/admin/admin.schemas.js +17 -6
- package/dist/agents/admin-agents.d.ts +10 -8
- package/dist/agents/admin-agents.js +13 -9
- package/dist/automations/automations.controller.d.ts +9 -5
- package/dist/automations/automations.controller.js +21 -4
- package/dist/automations/automations.schemas.d.ts +19 -0
- package/dist/automations/automations.schemas.js +9 -0
- package/dist/automations/automations.service.d.ts +25 -8
- package/dist/automations/automations.service.js +40 -34
- package/dist/automations/store.d.ts +17 -0
- package/dist/automations/store.js +35 -0
- package/dist/catalog/admin-catalog.controller.d.ts +64 -2
- package/dist/catalog/admin-catalog.controller.js +17 -0
- package/dist/catalog/cli.js +2 -2
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/plugin-package.js +2 -1
- package/dist/client/client-access.service.js +29 -1
- package/dist/config/schema.d.ts +9 -0
- package/dist/config/schema.js +26 -0
- package/dist/ctx/create-ctx.d.ts +2 -19
- package/dist/ctx/create-ctx.js +3 -93
- package/dist/ctx/durable-relay.d.ts +2 -5
- package/dist/ctx/durable-relay.js +56 -29
- package/dist/db/extension-handoff-schema.js +5 -0
- package/dist/db/kernel-schema.d.ts +74 -0
- package/dist/erasure/account-batches.js +2 -0
- package/dist/erasure/account-residual.js +1 -0
- package/dist/erasure/org-catalog.js +8 -3
- package/dist/erasure/org-rules.js +1 -0
- package/dist/evals/evals.schemas.d.ts +15 -5
- package/dist/evals/evals.schemas.js +17 -10
- package/dist/evals/runs.controller.d.ts +4 -12
- package/dist/evals/runs.controller.js +14 -11
- package/dist/evals/runs.service.d.ts +29 -25
- package/dist/evals/runs.service.js +73 -61
- package/dist/evals/trajectory.controller.d.ts +10 -6
- package/dist/evals/trajectory.controller.js +11 -4
- package/dist/evals/trajectory.service.d.ts +17 -9
- package/dist/evals/trajectory.service.js +52 -35
- package/dist/extensions/admin-ops.js +4 -3
- package/dist/extensions/blobs.d.ts +2 -0
- package/dist/extensions/blobs.js +11 -0
- package/dist/extensions/enabled.d.ts +5 -0
- package/dist/extensions/grant-epoch.d.ts +108 -0
- package/dist/extensions/grant-epoch.js +214 -0
- package/dist/extensions/host.d.ts +29 -9
- package/dist/extensions/host.js +148 -42
- package/dist/extensions/instance.d.ts +3 -3
- package/dist/extensions/instance.js +1 -1
- package/dist/extensions/isolated/authorization.d.ts +54 -2
- package/dist/extensions/isolated/authorization.js +148 -35
- package/dist/extensions/isolated/child-session.js +0 -1
- package/dist/extensions/isolated/revision.js +1 -1
- package/dist/extensions/package-digest.job.d.ts +34 -0
- package/dist/extensions/package-digest.job.js +121 -0
- package/dist/extensions/package.js +10 -6
- package/dist/extensions/permission-set.d.ts +14 -78
- package/dist/extensions/permission-set.js +17 -143
- package/dist/extensions/permission-view.d.ts +13 -1
- package/dist/extensions/permission-view.js +20 -0
- package/dist/extensions/registry.js +4 -1
- package/dist/extensions/resolve.d.ts +7 -8
- package/dist/extensions/resolve.js +7 -8
- package/dist/extensions/revision-approvals.d.ts +167 -0
- package/dist/extensions/revision-approvals.js +353 -0
- package/dist/extensions/revision-grants.d.ts +197 -0
- package/dist/extensions/revision-grants.js +584 -0
- package/dist/extensions/state.module.js +12 -1
- package/dist/identity/accounts.d.ts +10 -4
- package/dist/identity/accounts.js +18 -5
- package/dist/infra/database/extension-effect-key-lock.js +7 -0
- package/dist/infra/paging/paged-read.d.ts +34 -4
- package/dist/infra/paging/paged-read.js +38 -1
- package/dist/orgs/invitations.controller.d.ts +4 -7
- package/dist/orgs/invitations.controller.js +8 -5
- package/dist/orgs/invitations.service.d.ts +17 -3
- package/dist/orgs/invitations.service.js +35 -13
- package/dist/orgs/members.service.d.ts +35 -0
- package/dist/orgs/members.service.js +52 -3
- package/dist/orgs/orgs.controller.d.ts +11 -4
- package/dist/orgs/orgs.controller.js +25 -5
- package/dist/orgs/orgs.schemas.d.ts +14 -0
- package/dist/orgs/orgs.schemas.js +19 -2
- package/dist/orgs/orgs.service.d.ts +21 -1
- package/dist/orgs/orgs.service.js +40 -1
- package/dist/orgs/security-settings.service.d.ts +14 -6
- package/dist/orgs/security-settings.service.js +18 -13
- package/dist/orgs/settings.controller.d.ts +7 -4
- package/dist/platform/exclusions.js +15 -0
- package/dist/platform/ops/accounts.ops.js +7 -16
- package/dist/platform/ops/admin-items.ops.js +7 -7
- package/dist/platform/ops/automations.ops.js +6 -4
- package/dist/platform/ops/evals.ops.js +30 -15
- package/dist/platform/ops/org-settings.ops.js +2 -2
- package/dist/platform/ops/orgs.ops.js +44 -27
- package/dist/roles/worker.module.js +3 -0
- package/dist/runtime/step-log.d.ts +1 -1
- package/dist/runtime/step-log.js +5 -4
- package/dist/setup/kernel-checks.d.ts +3 -3
- package/dist/setup/kernel-checks.js +4 -4
- package/dist/setup/setup-check-registry.d.ts +16 -5
- package/dist/setup/setup-check-registry.js +11 -3
- package/dist/setup/setup-checks.service.d.ts +1 -2
- package/dist/setup/setup-checks.service.js +7 -6
- package/dist/tools/admin-connectors.d.ts +10 -8
- package/dist/tools/admin-connectors.js +12 -10
- package/dist/tools/builtins/automations.d.ts +1 -1
- package/dist/tools/builtins/automations.js +1 -1
- package/package.json +3 -3
- package/src/migrations/0234_extension_grant_epoch.sql +141 -0
- package/src/migrations/0235_extension_revision_grants.sql +166 -0
- package/src/migrations/0236_paged_table_idx.sql +33 -0
- package/src/migrations/0237_extension_handoff_health.sql +18 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { SDK_VERSION } from "@capybara-hq/plugin-sdk";
|
|
12
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
14
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
17
|
+
import { canonicalJson } from "../tools/canonical-json.js";
|
|
18
|
+
import { ExtensionApprovals } from "./approvals.js";
|
|
19
|
+
import { ExtensionBlobs } from "./blobs.js";
|
|
20
|
+
import { permissionSetOfPackage, readExtensionPackage } from "./package.js";
|
|
21
|
+
import { blockingKeys, diffPermissions, permissionKeys, WARNINGS_KEY_PREFIX, } from "./permission-set.js";
|
|
22
|
+
import { reviewPermissionRows } from "./permission-view.js";
|
|
23
|
+
import { continuityProblem, ExtensionRevisionGrants, effectiveKeys, RevisionGrantError, } from "./revision-grants.js";
|
|
24
|
+
/**
|
|
25
|
+
* The kernel-written purpose of a migration warnings digest key. The key carries the digest, so a
|
|
26
|
+
* revision whose risky migration statements changed has a key its predecessor's grant lacks, and
|
|
27
|
+
* continuity refuses it: the change goes to review.
|
|
28
|
+
*/
|
|
29
|
+
export function warningsPurpose(key) {
|
|
30
|
+
const digest = key.slice(WARNINGS_KEY_PREFIX.length);
|
|
31
|
+
return {
|
|
32
|
+
key,
|
|
33
|
+
zh: `迁移脚本含有需要审阅的语句(摘要 ${digest})`,
|
|
34
|
+
en: `The migration SQL contains statements that need review (digest ${digest})`,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The reviewed purpose rows of a permission set, and the keys that lack one. A migration warnings
|
|
39
|
+
* digest key gets the kernel-written row of {@link warningsPurpose}: authors cannot write one, and
|
|
40
|
+
* the grant must still bind the digest.
|
|
41
|
+
*/
|
|
42
|
+
export function purposeRowsOf(set) {
|
|
43
|
+
const { rows } = reviewPermissionRows(set);
|
|
44
|
+
const purposes = new Map(rows.map((row) => [row.key, row.purpose]));
|
|
45
|
+
const out = [];
|
|
46
|
+
const missing = [];
|
|
47
|
+
for (const key of permissionKeys(set)) {
|
|
48
|
+
const purpose = purposes.get(key);
|
|
49
|
+
if (key.startsWith(WARNINGS_KEY_PREFIX))
|
|
50
|
+
out.push(warningsPurpose(key));
|
|
51
|
+
else if (purpose)
|
|
52
|
+
out.push({ key, zh: purpose.zh, en: purpose.en });
|
|
53
|
+
else
|
|
54
|
+
missing.push(key);
|
|
55
|
+
}
|
|
56
|
+
return { rows: out, missing };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The legacy decision for a revision's set against its snapshot, by `planApprovals`' approval rule:
|
|
60
|
+
* no snapshot or a new blocking key waits, unless `extensionPermissions.autoApprove` is on. Its
|
|
61
|
+
* dependency rule (an install whose required providers are all pending waits too) is not mirrored:
|
|
62
|
+
* the shadow report compares approval, not load order.
|
|
63
|
+
*/
|
|
64
|
+
export function legacyDecision(set, snapshot, autoApprove = false) {
|
|
65
|
+
if (autoApprove)
|
|
66
|
+
return "approved";
|
|
67
|
+
if (!snapshot)
|
|
68
|
+
return "pending";
|
|
69
|
+
const { added } = diffPermissions(snapshot.permissions, set);
|
|
70
|
+
return blockingKeys(added).length > 0 ? "pending" : "approved";
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The shadow decision for one installed revision: resolved against its own live grant. `approved`
|
|
74
|
+
* is the live grant of the install's approved revision when that is another revision; it only
|
|
75
|
+
* explains whether continuity could carry over (the publisher evidence is not known here, so it
|
|
76
|
+
* never grants by itself).
|
|
77
|
+
*/
|
|
78
|
+
export function resolveRevisionGrant(input) {
|
|
79
|
+
const { revision, set, required, own, approved, legacy } = input;
|
|
80
|
+
const { rows, missing } = purposeRowsOf(set);
|
|
81
|
+
const decide = (status, wouldServe, extra = {}) => ({
|
|
82
|
+
revision,
|
|
83
|
+
status,
|
|
84
|
+
refusedKeys: [],
|
|
85
|
+
ungrantedKeys: [],
|
|
86
|
+
missingPurposes: missing,
|
|
87
|
+
required,
|
|
88
|
+
wouldServe,
|
|
89
|
+
legacy,
|
|
90
|
+
diverges: (legacy === "approved") !== wouldServe,
|
|
91
|
+
...extra,
|
|
92
|
+
});
|
|
93
|
+
if (revision.sha512 === null)
|
|
94
|
+
return decide("unknown_digest", false);
|
|
95
|
+
if (own) {
|
|
96
|
+
const serving = new Set(effectiveKeys(own));
|
|
97
|
+
const granted = new Set(own.keys);
|
|
98
|
+
const keys = permissionKeys(set).filter((key) => !serving.has(key));
|
|
99
|
+
const refusedKeys = keys.filter((key) => granted.has(key));
|
|
100
|
+
const ungrantedKeys = keys.filter((key) => !granted.has(key));
|
|
101
|
+
return decide(keys.length > 0 ? "partially_revoked" : "granted", true, {
|
|
102
|
+
generation: own.generation,
|
|
103
|
+
refusedKeys,
|
|
104
|
+
ungrantedKeys,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const continuity = approved
|
|
108
|
+
? (continuityProblem(approved, {
|
|
109
|
+
purposes: rows,
|
|
110
|
+
realms: {},
|
|
111
|
+
approvalScope: approved.approvalScope,
|
|
112
|
+
publisher: { evidence: "none" },
|
|
113
|
+
}) ?? "continuity is recorded when the revision is promoted")
|
|
114
|
+
: undefined;
|
|
115
|
+
return decide(missing.length > 0 ? "missing_purposes" : "missing_grant", false, {
|
|
116
|
+
...(continuity !== undefined && { continuity }),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const SET_CACHE_ENTRIES = 512;
|
|
120
|
+
/** The installed revisions' shadow decisions, and promotion with continuity. */
|
|
121
|
+
let ExtensionRevisionApprovals = class ExtensionRevisionApprovals {
|
|
122
|
+
kdb;
|
|
123
|
+
txHost;
|
|
124
|
+
grants;
|
|
125
|
+
approvals;
|
|
126
|
+
blobs;
|
|
127
|
+
config;
|
|
128
|
+
/** Permission sets by package sha256: content-addressed, so a cached set never goes stale. */
|
|
129
|
+
sets = new Map();
|
|
130
|
+
constructor(kdb, txHost, grants, approvals, blobs, config) {
|
|
131
|
+
this.kdb = kdb;
|
|
132
|
+
this.txHost = txHost;
|
|
133
|
+
this.grants = grants;
|
|
134
|
+
this.approvals = approvals;
|
|
135
|
+
this.blobs = blobs;
|
|
136
|
+
this.config = config;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The shadow decision of every install record. A package that cannot be read is reported as
|
|
140
|
+
* `unreadable` and never hides the others.
|
|
141
|
+
*/
|
|
142
|
+
async decisions() {
|
|
143
|
+
const installs = await this.kdb.db
|
|
144
|
+
.selectFrom("extension_installs as i")
|
|
145
|
+
.innerJoin("extension_packages as p", "p.sha256", "i.sha256")
|
|
146
|
+
.select([
|
|
147
|
+
"i.name",
|
|
148
|
+
"i.version",
|
|
149
|
+
"i.sha256",
|
|
150
|
+
"i.required",
|
|
151
|
+
"i.approved_version",
|
|
152
|
+
"i.approved_sha512",
|
|
153
|
+
"p.sha512",
|
|
154
|
+
])
|
|
155
|
+
.orderBy("i.name")
|
|
156
|
+
.execute();
|
|
157
|
+
const snapshots = await this.approvals.all();
|
|
158
|
+
const autoApprove = this.config.get().extensionPermissions.autoApprove === true;
|
|
159
|
+
const out = [];
|
|
160
|
+
for (const row of installs) {
|
|
161
|
+
const revision = { plugin: row.name, version: row.version, sha512: row.sha512 };
|
|
162
|
+
let set;
|
|
163
|
+
try {
|
|
164
|
+
set = await this.setOf(row.sha256);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
out.push({
|
|
168
|
+
revision,
|
|
169
|
+
status: "unreadable",
|
|
170
|
+
refusedKeys: [],
|
|
171
|
+
ungrantedKeys: [],
|
|
172
|
+
missingPurposes: [],
|
|
173
|
+
required: row.required,
|
|
174
|
+
wouldServe: false,
|
|
175
|
+
legacy: autoApprove || snapshots.has(row.name) ? "approved" : "pending",
|
|
176
|
+
diverges: autoApprove || snapshots.has(row.name),
|
|
177
|
+
});
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const own = row.sha512 === null
|
|
181
|
+
? undefined
|
|
182
|
+
: await this.grants.live({ ...revision, sha512: row.sha512 });
|
|
183
|
+
const approvedIsOther = row.approved_version !== null &&
|
|
184
|
+
row.approved_sha512 !== null &&
|
|
185
|
+
row.approved_sha512 !== row.sha512;
|
|
186
|
+
const approved = !own && approvedIsOther
|
|
187
|
+
? await this.grants.live({
|
|
188
|
+
plugin: row.name,
|
|
189
|
+
version: row.approved_version,
|
|
190
|
+
sha512: row.approved_sha512,
|
|
191
|
+
})
|
|
192
|
+
: undefined;
|
|
193
|
+
out.push(resolveRevisionGrant({
|
|
194
|
+
revision,
|
|
195
|
+
set,
|
|
196
|
+
required: row.required,
|
|
197
|
+
own,
|
|
198
|
+
approved,
|
|
199
|
+
legacy: legacyDecision(set, snapshots.get(row.name), autoApprove),
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
/** The facts the enforcing start gate would act on; computed and reported, never refused. */
|
|
205
|
+
async startGate() {
|
|
206
|
+
const all = await this.decisions();
|
|
207
|
+
return {
|
|
208
|
+
missingRequired: all.filter((d) => d.required && !d.wouldServe),
|
|
209
|
+
pending: all.filter((d) => !d.required && !d.wouldServe),
|
|
210
|
+
diverging: all.filter((d) => d.diverges),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Makes a revision the approved one of its install. A revision with its own live grant is
|
|
215
|
+
* promoted under it (an upgrade or a rollback). An upgrade without one continues the install's
|
|
216
|
+
* approved revision when that has a live grant and the candidate is a continuation of it (the
|
|
217
|
+
* store's rules): the continuity grant is recorded in the same transaction before the promotion.
|
|
218
|
+
* Anything else leaves the revision as the install's candidate, pending review, with the reason;
|
|
219
|
+
* it never throws for a refusal. A continuation carries no full-access realm (narrower, so safe):
|
|
220
|
+
* realms and the `extension.full_access_continued` audit come with plan tasks 1.2e and 1.3a.
|
|
221
|
+
*
|
|
222
|
+
* The package is read before the transaction: no object storage call holds a connection.
|
|
223
|
+
*/
|
|
224
|
+
async promote(candidate, evidence, kind = "upgrade") {
|
|
225
|
+
const sha256 = await this.sha256Of(candidate);
|
|
226
|
+
let purposes;
|
|
227
|
+
if (sha256 === undefined)
|
|
228
|
+
purposes = "unknown_digest";
|
|
229
|
+
else {
|
|
230
|
+
try {
|
|
231
|
+
purposes = purposeRowsOf(await this.setOf(sha256));
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
// storage unavailable, the package too large or refused: a revision with its own grant
|
|
235
|
+
// still promotes, anything else waits
|
|
236
|
+
purposes = "unreadable";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return this.promoteIn(candidate, evidence, kind, purposes);
|
|
240
|
+
}
|
|
241
|
+
async promoteIn(candidate, evidence, kind, purposes) {
|
|
242
|
+
const own = await this.grants.live(candidate);
|
|
243
|
+
if (own) {
|
|
244
|
+
await this.grants.promote(candidate);
|
|
245
|
+
return { status: "promoted", grant: own, continued: false };
|
|
246
|
+
}
|
|
247
|
+
const pending = async (reason) => {
|
|
248
|
+
await this.grants.setCandidate(candidate.plugin, candidate);
|
|
249
|
+
return { status: "pending", reason };
|
|
250
|
+
};
|
|
251
|
+
const newest = await this.grants.get(candidate);
|
|
252
|
+
if (newest)
|
|
253
|
+
return pending(`its grant is ${newest.state}; it needs a new review`);
|
|
254
|
+
if (kind === "rollback")
|
|
255
|
+
return pending("a rollback is served only under its own grant");
|
|
256
|
+
if (purposes === "unknown_digest")
|
|
257
|
+
return pending("the package digest is not recorded");
|
|
258
|
+
if (purposes === "unreadable")
|
|
259
|
+
return pending("the stored package cannot be read");
|
|
260
|
+
if (purposes.missing.length > 0) {
|
|
261
|
+
return pending(`missing purposes: ${purposes.missing.join(", ")}`);
|
|
262
|
+
}
|
|
263
|
+
const { approved } = await this.grants.installRevisions(candidate.plugin);
|
|
264
|
+
const source = approved ? await this.grants.live(approved) : undefined;
|
|
265
|
+
if (!approved || !source)
|
|
266
|
+
return pending("no live grant to continue");
|
|
267
|
+
const problem = continuityProblem(source, {
|
|
268
|
+
purposes: purposes.rows,
|
|
269
|
+
realms: {},
|
|
270
|
+
approvalScope: source.approvalScope,
|
|
271
|
+
publisher: evidence.publisher,
|
|
272
|
+
});
|
|
273
|
+
if (problem)
|
|
274
|
+
return pending(problem);
|
|
275
|
+
let grant;
|
|
276
|
+
try {
|
|
277
|
+
// The store checks continuity again under the source's share lock.
|
|
278
|
+
grant = await this.grants.record({
|
|
279
|
+
revision: candidate,
|
|
280
|
+
purposes: purposes.rows,
|
|
281
|
+
reviewHash: continuityReviewHash(source, candidate, purposes.rows),
|
|
282
|
+
approvalScope: source.approvalScope,
|
|
283
|
+
source: "continuity",
|
|
284
|
+
publisher: evidence.publisher,
|
|
285
|
+
continuedFrom: source.revision,
|
|
286
|
+
approvedBy: null,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
catch (err) {
|
|
290
|
+
// The source was revoked meanwhile, or no longer continues: the candidate waits.
|
|
291
|
+
if (err instanceof RevisionGrantError &&
|
|
292
|
+
(err.code === "not_live" || err.code === "not_continuous" || err.code === "terminal")) {
|
|
293
|
+
return pending(err.message);
|
|
294
|
+
}
|
|
295
|
+
throw err;
|
|
296
|
+
}
|
|
297
|
+
await this.grants.promote(candidate);
|
|
298
|
+
return { status: "promoted", grant, continued: true };
|
|
299
|
+
}
|
|
300
|
+
/** The stored package of a revision, or undefined while its sha512 is not recorded. */
|
|
301
|
+
async sha256Of(revision) {
|
|
302
|
+
const row = await this.kdb.db
|
|
303
|
+
.selectFrom("extension_packages")
|
|
304
|
+
.select("sha256")
|
|
305
|
+
.where("name", "=", revision.plugin)
|
|
306
|
+
.where("version", "=", revision.version)
|
|
307
|
+
.where("sha512", "=", revision.sha512)
|
|
308
|
+
.executeTakeFirst();
|
|
309
|
+
return row?.sha256;
|
|
310
|
+
}
|
|
311
|
+
async setOf(sha256) {
|
|
312
|
+
const cached = this.sets.get(sha256);
|
|
313
|
+
if (cached)
|
|
314
|
+
return cached;
|
|
315
|
+
const pkg = await readExtensionPackage(await this.blobs.get(sha256), {
|
|
316
|
+
maxBytes: this.config.get().packages.maxBytes,
|
|
317
|
+
hostSdkVersion: SDK_VERSION,
|
|
318
|
+
});
|
|
319
|
+
const set = permissionSetOfPackage(pkg);
|
|
320
|
+
// a bound on memory only: a dropped entry is read again from the package store
|
|
321
|
+
if (this.sets.size >= SET_CACHE_ENTRIES)
|
|
322
|
+
this.sets.clear();
|
|
323
|
+
this.sets.set(sha256, set);
|
|
324
|
+
return set;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
__decorate([
|
|
328
|
+
Transactional()
|
|
329
|
+
], ExtensionRevisionApprovals.prototype, "promoteIn", null);
|
|
330
|
+
ExtensionRevisionApprovals = __decorate([
|
|
331
|
+
Injectable(),
|
|
332
|
+
__param(0, Inject(KernelDb)),
|
|
333
|
+
__param(1, Inject(TransactionHost)),
|
|
334
|
+
__param(2, Inject(ExtensionRevisionGrants)),
|
|
335
|
+
__param(3, Inject(ExtensionApprovals)),
|
|
336
|
+
__param(4, Inject(ExtensionBlobs)),
|
|
337
|
+
__param(5, Inject(KernelConfigService))
|
|
338
|
+
], ExtensionRevisionApprovals);
|
|
339
|
+
export { ExtensionRevisionApprovals };
|
|
340
|
+
/**
|
|
341
|
+
* The review hash of a continuity grant: the kernel reviewed it against its source, so the hash
|
|
342
|
+
* binds the candidate, its purpose rows and the exact source generation.
|
|
343
|
+
*/
|
|
344
|
+
export function continuityReviewHash(source, candidate, rows) {
|
|
345
|
+
const digest = createHash("sha256")
|
|
346
|
+
.update(canonicalJson({
|
|
347
|
+
candidate,
|
|
348
|
+
purposes: rows,
|
|
349
|
+
source: { ...source.revision, generation: source.generation },
|
|
350
|
+
}))
|
|
351
|
+
.digest("hex");
|
|
352
|
+
return `continuity:${digest}`;
|
|
353
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
2
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
3
|
+
/**
|
|
4
|
+
* Revision grants (extension permission model spec 2026-12-04, "Revision grants and enforcement
|
|
5
|
+
* cutover"; plan task 1.2a): the store of admin approvals, each bound to one exact package
|
|
6
|
+
* revision, and of the approved and candidate revision of each install.
|
|
7
|
+
*
|
|
8
|
+
* A revision can be reviewed again after its grant was revoked or retired: each review is a new
|
|
9
|
+
* generation, older generations stay as history, and at most one generation is live.
|
|
10
|
+
*
|
|
11
|
+
* Shadow mode until plan task 1.5a: grants are written and read here, but admission still follows
|
|
12
|
+
* the legacy permission snapshots and nothing refuses on account of a grant. Every write changes a
|
|
13
|
+
* table with the grant epoch trigger (migration 0234), so replicas see the change within the
|
|
14
|
+
* revocation bound once the grant check reads these tables.
|
|
15
|
+
*/
|
|
16
|
+
/** An exact package revision. `sha512` is the lowercase hex digest of the package archive. */
|
|
17
|
+
export interface RevisionIdentity {
|
|
18
|
+
plugin: string;
|
|
19
|
+
version: string;
|
|
20
|
+
sha512: string;
|
|
21
|
+
}
|
|
22
|
+
export type RevisionGrantState = "approved" | "partially_revoked" | "revoked" | "retired";
|
|
23
|
+
/** How a grant was made. */
|
|
24
|
+
export type GrantSource = "review" | "operator_config" | "operator_cli" | "auto" | "continuity";
|
|
25
|
+
/** The approving authority a grant exercises; continuity carries its source grant's. */
|
|
26
|
+
export type GrantAuthority = "super_admin" | "operator" | "auto";
|
|
27
|
+
export type PublisherEvidence = "verified" | "pin_only" | "none";
|
|
28
|
+
export type ExecutionRealm = "server" | "browser" | "device";
|
|
29
|
+
export type ApprovalScope = {
|
|
30
|
+
mode: "all" | "optional";
|
|
31
|
+
} | {
|
|
32
|
+
mode: "named";
|
|
33
|
+
orgs: readonly string[];
|
|
34
|
+
};
|
|
35
|
+
export declare const EXECUTION_REALMS: readonly ExecutionRealm[];
|
|
36
|
+
/** The kernel-generated permission key of full access in one realm, revocable on its own. */
|
|
37
|
+
export declare function fullAccessKey(realm: ExecutionRealm): string;
|
|
38
|
+
/** One reviewed permission row with its bilingual purpose. */
|
|
39
|
+
export interface PurposeRow {
|
|
40
|
+
key: string;
|
|
41
|
+
zh: string;
|
|
42
|
+
en: string;
|
|
43
|
+
}
|
|
44
|
+
export interface PublisherIdentity {
|
|
45
|
+
issuer: string;
|
|
46
|
+
identity: string;
|
|
47
|
+
repository?: string;
|
|
48
|
+
workflow?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface NewRevisionGrant {
|
|
51
|
+
revision: RevisionIdentity;
|
|
52
|
+
/** Exactly one row per canonical key, the key set of the reviewed surface. */
|
|
53
|
+
purposes: readonly PurposeRow[];
|
|
54
|
+
/**
|
|
55
|
+
* Full-access realms granted, each with the forcing keys it covers. Each realm needs its
|
|
56
|
+
* {@link fullAccessKey} among the purposes, and that key needs a realm here.
|
|
57
|
+
*/
|
|
58
|
+
realms?: Partial<Record<ExecutionRealm, readonly string[]>>;
|
|
59
|
+
reviewHash: string;
|
|
60
|
+
approvalScope: ApprovalScope;
|
|
61
|
+
source: GrantSource;
|
|
62
|
+
publisher: {
|
|
63
|
+
evidence: "verified";
|
|
64
|
+
identity: PublisherIdentity;
|
|
65
|
+
} | {
|
|
66
|
+
evidence: "pin_only" | "none";
|
|
67
|
+
};
|
|
68
|
+
/** The revision whose live grant this one continues; required with source `continuity` only. */
|
|
69
|
+
continuedFrom?: RevisionIdentity;
|
|
70
|
+
approvedBy: string | null;
|
|
71
|
+
}
|
|
72
|
+
export interface RevisionGrant {
|
|
73
|
+
revision: RevisionIdentity;
|
|
74
|
+
generation: number;
|
|
75
|
+
keys: string[];
|
|
76
|
+
purposes: PurposeRow[];
|
|
77
|
+
purposesDigest: string;
|
|
78
|
+
realms: Partial<Record<ExecutionRealm, string[]>>;
|
|
79
|
+
reviewHash: string;
|
|
80
|
+
approvalScope: ApprovalScope;
|
|
81
|
+
source: GrantSource;
|
|
82
|
+
authority: GrantAuthority;
|
|
83
|
+
publisher: PublisherIdentity | null;
|
|
84
|
+
publisherEvidence: PublisherEvidence;
|
|
85
|
+
continuedFrom: (RevisionIdentity & {
|
|
86
|
+
generation: number;
|
|
87
|
+
}) | null;
|
|
88
|
+
state: RevisionGrantState;
|
|
89
|
+
disabledKeys: string[];
|
|
90
|
+
retiredKeys: string[];
|
|
91
|
+
retiredForcing: Partial<Record<ExecutionRealm, string[]>>;
|
|
92
|
+
approvedBy: string | null;
|
|
93
|
+
approvedAt: Date;
|
|
94
|
+
stateChangedBy: string | null;
|
|
95
|
+
stateChangedAt: Date | null;
|
|
96
|
+
}
|
|
97
|
+
/** The serving surface of a live grant: its keys minus the revoked and the retired ones. */
|
|
98
|
+
export declare function effectiveKeys(grant: Pick<RevisionGrant, "keys" | "disabledKeys" | "retiredKeys">): string[];
|
|
99
|
+
/**
|
|
100
|
+
* The full-access realms a live grant still covers, each with its remaining forcing keys: a realm
|
|
101
|
+
* whose key was revoked or retired, or whose forcing keys were all retired, is gone.
|
|
102
|
+
*/
|
|
103
|
+
export declare function effectiveRealms(grant: RevisionGrant): Partial<Record<ExecutionRealm, string[]>>;
|
|
104
|
+
export declare function isLive(state: RevisionGrantState): boolean;
|
|
105
|
+
export type RevisionGrantErrorCode = "invalid" | "conflict" | "not_found" | "terminal" | "unknown_keys" | "not_live" | "not_continuous";
|
|
106
|
+
export declare class RevisionGrantError extends Error {
|
|
107
|
+
readonly code: RevisionGrantErrorCode;
|
|
108
|
+
constructor(code: RevisionGrantErrorCode, message: string);
|
|
109
|
+
}
|
|
110
|
+
/** sha256 hex of the canonical encoding of the purpose rows, sorted by key. */
|
|
111
|
+
export declare function purposesDigest(purposes: readonly PurposeRow[]): string;
|
|
112
|
+
/** What a continuation proposes: compared with the live source grant by {@link continuityProblem}. */
|
|
113
|
+
export interface ContinuationCandidate {
|
|
114
|
+
purposes: readonly PurposeRow[];
|
|
115
|
+
realms: Partial<Record<ExecutionRealm, readonly string[]>>;
|
|
116
|
+
approvalScope: ApprovalScope;
|
|
117
|
+
publisher: NewRevisionGrant["publisher"];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The spec's continuity conditions against a live source grant; the reason the candidate is not a
|
|
121
|
+
* continuation, or undefined when it is. The continued surface is a subset of what still serves,
|
|
122
|
+
* with identical purposes, no wider org scope, full access only where the source still covers it,
|
|
123
|
+
* and the same verified publisher. A partially revoked source never resurrects its revoked or
|
|
124
|
+
* retired keys. The store checks it under the source's share lock; the shadow decisions use it to
|
|
125
|
+
* report why a revision would wait for review.
|
|
126
|
+
*/
|
|
127
|
+
export declare function continuityProblem(source: RevisionGrant, candidate: ContinuationCandidate): string | undefined;
|
|
128
|
+
/** The revision grants and the approved / candidate revision of each install (PG). */
|
|
129
|
+
export declare class ExtensionRevisionGrants {
|
|
130
|
+
private readonly kdb;
|
|
131
|
+
readonly txHost: TransactionHost;
|
|
132
|
+
constructor(kdb: KernelDb, txHost: TransactionHost);
|
|
133
|
+
private get db();
|
|
134
|
+
/**
|
|
135
|
+
* Records an approval for one exact revision as a new generation. Recording the same review
|
|
136
|
+
* while that grant is approved returns it; any other review while a grant is live is a
|
|
137
|
+
* conflict. After a revocation or retirement a new review opens the next generation. Two
|
|
138
|
+
* replicas recording at once end in the same outcome: the loser re-reads the winner's row.
|
|
139
|
+
*/
|
|
140
|
+
record(input: NewRevisionGrant): Promise<RevisionGrant>;
|
|
141
|
+
/** The newest generation of a revision's grant in any state, or undefined. */
|
|
142
|
+
get(revision: RevisionIdentity): Promise<RevisionGrant | undefined>;
|
|
143
|
+
/** The live (approved or partially revoked) grant of a revision, or undefined. */
|
|
144
|
+
live(revision: RevisionIdentity): Promise<RevisionGrant | undefined>;
|
|
145
|
+
/** Every generation of a revision's grant, oldest first. */
|
|
146
|
+
history(revision: RevisionIdentity): Promise<RevisionGrant[]>;
|
|
147
|
+
/** Every grant of a plugin, newest approval first. */
|
|
148
|
+
forPlugin(plugin: string): Promise<RevisionGrant[]>;
|
|
149
|
+
/**
|
|
150
|
+
* Revokes keys of a live grant (partial revocation; `execution.full-access:<realm>` revokes one
|
|
151
|
+
* realm). Revoking every key that is not retired revokes the grant. The reviewed rows stay;
|
|
152
|
+
* only `disabled_keys` grows.
|
|
153
|
+
*/
|
|
154
|
+
revokeKeys(revision: RevisionIdentity, keys: readonly string[], actor: string | null): Promise<RevisionGrant>;
|
|
155
|
+
/** Revokes a live grant entirely. */
|
|
156
|
+
revoke(revision: RevisionIdentity, actor: string | null): Promise<RevisionGrant>;
|
|
157
|
+
/**
|
|
158
|
+
* Retires a live grant whose revision is superseded as a whole. Retirement is not revocation,
|
|
159
|
+
* and nothing revives it automatically; a new review opens the next generation.
|
|
160
|
+
*/
|
|
161
|
+
retire(revision: RevisionIdentity, actor: string | null): Promise<RevisionGrant>;
|
|
162
|
+
/**
|
|
163
|
+
* Retires keys of a live grant that are no longer needed; the grant stays live. Retiring every
|
|
164
|
+
* key leaves an approved revision with an empty surface, which is valid: a package may declare
|
|
165
|
+
* no permissions at all. Retire the whole grant with {@link retire} when the revision goes.
|
|
166
|
+
*/
|
|
167
|
+
retireKeys(revision: RevisionIdentity, keys: readonly string[], actor: string | null): Promise<RevisionGrant>;
|
|
168
|
+
/**
|
|
169
|
+
* Retires one forcing key of a full-access realm (the supervisor became available, so
|
|
170
|
+
* `host.no-isolation` no longer applies). When no forcing key of the realm remains, its
|
|
171
|
+
* `execution.full-access:<realm>` key is retired too; the rest of the grant stays live.
|
|
172
|
+
*/
|
|
173
|
+
retireForcing(revision: RevisionIdentity, realm: ExecutionRealm, forcingKey: string, actor: string | null): Promise<RevisionGrant>;
|
|
174
|
+
/** The approved (with its grant generation) and candidate revision of an install. */
|
|
175
|
+
installRevisions(plugin: string): Promise<{
|
|
176
|
+
approved: (RevisionIdentity & {
|
|
177
|
+
generation: number;
|
|
178
|
+
}) | undefined;
|
|
179
|
+
candidate: RevisionIdentity | undefined;
|
|
180
|
+
}>;
|
|
181
|
+
/** Sets or clears the candidate revision of an install; a candidate needs no grant. */
|
|
182
|
+
setCandidate(plugin: string, candidate: RevisionIdentity | null): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Makes a revision the approved one of its install, under its live grant's generation (rechecked
|
|
185
|
+
* under the grant's row lock). The candidate is cleared in the same statement only if it is
|
|
186
|
+
* this revision, so a candidate another request set meanwhile survives.
|
|
187
|
+
*/
|
|
188
|
+
promote(revision: RevisionIdentity): Promise<void>;
|
|
189
|
+
private sameReview;
|
|
190
|
+
private checkKeys;
|
|
191
|
+
private update;
|
|
192
|
+
private requireLive;
|
|
193
|
+
/** The live generation, optionally locked for this transaction (callers run inside one). */
|
|
194
|
+
private liveRow;
|
|
195
|
+
private select;
|
|
196
|
+
private withOrgs;
|
|
197
|
+
}
|