@capybara-hq/kernel 0.1.0-rc.130 → 0.1.0-rc.131
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/account-erasures.d.ts +37 -0
- package/dist/admin/account-erasures.js +119 -0
- package/dist/admin/admin.controller.d.ts +35 -2
- package/dist/admin/admin.controller.js +75 -18
- package/dist/admin/admin.module.js +10 -0
- package/dist/admin/admin.schemas.d.ts +51 -0
- package/dist/admin/admin.schemas.js +20 -0
- package/dist/admin/erasure-dispositions.d.ts +23 -0
- package/dist/admin/erasure-dispositions.js +76 -0
- package/dist/admin/health.service.d.ts +6 -0
- package/dist/admin/health.service.js +27 -1
- package/dist/admin/usage.service.d.ts +5 -0
- package/dist/admin/usage.service.js +21 -2
- package/dist/agents/agents.service.d.ts +1 -1
- package/dist/channels/connections.service.d.ts +1 -1
- package/dist/client/client-reports.service.js +12 -0
- package/dist/config/schema.d.ts +8 -1
- package/dist/config/schema.js +43 -1
- package/dist/conversations/conversations.service.d.ts +9 -1
- package/dist/conversations/conversations.service.js +112 -15
- package/dist/conversations/event-log.d.ts +2 -0
- package/dist/conversations/event-log.js +77 -0
- package/dist/conversations/events.d.ts +11 -0
- package/dist/conversations/events.js +13 -1
- package/dist/conversations/prompt-suggestions.js +4 -0
- package/dist/conversations/titles.js +3 -2
- package/dist/db/kernel-schema.d.ts +71 -8
- package/dist/devices/devices.service.d.ts +13 -1
- package/dist/devices/devices.service.js +122 -9
- package/dist/erasure/account-batches.d.ts +9 -0
- package/dist/erasure/account-batches.js +567 -0
- package/dist/erasure/account-files.d.ts +34 -0
- package/dist/erasure/account-files.js +217 -0
- package/dist/erasure/account-residual.d.ts +15 -0
- package/dist/erasure/account-residual.js +149 -0
- package/dist/erasure/account-worker.d.ts +32 -0
- package/dist/erasure/account-worker.js +392 -0
- package/dist/erasure/device-cleanup.d.ts +23 -0
- package/dist/erasure/device-cleanup.js +221 -0
- package/dist/erasure/erasure.module.d.ts +2 -0
- package/dist/erasure/erasure.module.js +14 -0
- package/dist/erasure/external-evidence.d.ts +4 -0
- package/dist/erasure/external-evidence.js +28 -0
- package/dist/erasure/jobs.d.ts +5 -0
- package/dist/erasure/jobs.js +20 -0
- package/dist/erasure/org-batches.d.ts +11 -0
- package/dist/erasure/org-batches.js +159 -0
- package/dist/erasure/org-catalog.d.ts +2 -0
- package/dist/erasure/org-catalog.js +34 -0
- package/dist/erasure/org-rules.d.ts +14 -0
- package/dist/erasure/org-rules.js +227 -0
- package/dist/erasure/org-worker.d.ts +38 -0
- package/dist/erasure/org-worker.js +501 -0
- package/dist/erasure/requests.d.ts +61 -0
- package/dist/erasure/requests.js +180 -0
- package/dist/erasure/sweep.job.d.ts +19 -0
- package/dist/erasure/sweep.job.js +130 -0
- package/dist/erasure/worker.d.ts +17 -0
- package/dist/erasure/worker.js +110 -0
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +4 -0
- package/dist/evals/judge.d.ts +5 -0
- package/dist/evals/judge.js +16 -1
- package/dist/evals/run-replay.js +12 -1
- package/dist/evals/step-replay.d.ts +5 -3
- package/dist/evals/step-replay.js +9 -5
- package/dist/evals/worker.d.ts +2 -0
- package/dist/evals/worker.js +60 -16
- package/dist/files/blob-objects.d.ts +7 -3
- package/dist/files/blob-objects.js +39 -22
- package/dist/files/blob-sweep.job.d.ts +11 -0
- package/dist/files/blob-sweep.job.js +67 -6
- package/dist/http/api.module.js +5 -1
- package/dist/identity/accounts.js +3 -0
- package/dist/identity/api-tokens.js +5 -0
- package/dist/identity/auth.guard.d.ts +1 -0
- package/dist/identity/auth.guard.js +1 -0
- package/dist/identity/erasure-admission.d.ts +3 -0
- package/dist/identity/erasure-admission.js +14 -0
- package/dist/identity/external.js +5 -0
- package/dist/identity/sessions.js +5 -0
- package/dist/identity/settings.d.ts +86 -1
- package/dist/identity/settings.js +38 -4
- package/dist/infra/config/kernel-config.service.js +1 -0
- package/dist/infra/metrics/kernel-metrics.d.ts +18 -0
- package/dist/infra/metrics/kernel-metrics.js +54 -0
- package/dist/infra/metrics/operational-metrics.d.ts +31 -0
- package/dist/infra/metrics/operational-metrics.js +188 -0
- package/dist/infra/outbox/registry.d.ts +23 -0
- package/dist/infra/outbox/registry.js +12 -0
- package/dist/infra/throttler/credential-throttler.js +8 -1
- package/dist/infra/throttler/named-throttlers.d.ts +6 -0
- package/dist/infra/throttler/named-throttlers.js +6 -0
- package/dist/infra/throttler/principal-throttler.guard.d.ts +10 -0
- package/dist/infra/throttler/principal-throttler.guard.js +50 -0
- package/dist/infra/throttler/redis-throttler-storage.d.ts +1 -0
- package/dist/infra/throttler/redis-throttler-storage.js +27 -5
- package/dist/infra/throttler/throttler.guard.d.ts +2 -3
- package/dist/infra/throttler/throttler.guard.js +2 -3
- package/dist/infra/throttler/throttler.module.js +3 -2
- package/dist/models/budget-admission.d.ts +31 -0
- package/dist/models/budget-admission.js +149 -0
- package/dist/models/budget-counters.d.ts +28 -0
- package/dist/models/budget-counters.js +117 -0
- package/dist/models/budget-policy.d.ts +88 -0
- package/dist/models/budget-policy.js +78 -0
- package/dist/models/embeddings.js +13 -2
- package/dist/models/images.js +12 -2
- package/dist/models/purposes.d.ts +5 -2
- package/dist/models/purposes.js +9 -5
- package/dist/orgs/invitations.service.d.ts +1 -1
- package/dist/orgs/members.service.d.ts +28 -1
- package/dist/orgs/members.service.js +74 -11
- package/dist/orgs/offboarding.js +4 -6
- package/dist/orgs/org-owner.d.ts +2 -0
- package/dist/orgs/org-owner.js +21 -6
- package/dist/orgs/orgs.controller.d.ts +22 -0
- package/dist/orgs/orgs.controller.js +24 -0
- package/dist/orgs/orgs.module.js +4 -0
- package/dist/orgs/orgs.schemas.js +7 -0
- package/dist/orgs/orgs.service.d.ts +5 -1
- package/dist/orgs/orgs.service.js +28 -3
- package/dist/orgs/settings.controller.js +6 -1
- package/dist/orgs/settings.service.d.ts +58 -1
- package/dist/orgs/settings.service.js +74 -9
- package/dist/platform/exclusions.js +20 -1
- package/dist/platform/ops/accounts.ops.js +58 -1
- package/dist/platform/ops/admin.ops.js +13 -5
- package/dist/platform/ops/org-settings.ops.js +11 -4
- package/dist/platform/ops/orgs.ops.js +28 -0
- package/dist/platform/ops/retention.d.ts +16 -0
- package/dist/platform/ops/retention.js +21 -0
- package/dist/retention/jobs.d.ts +21 -0
- package/dist/retention/jobs.js +48 -0
- package/dist/retention/policy.d.ts +70 -0
- package/dist/retention/policy.js +103 -0
- package/dist/retention/purge-batch.d.ts +17 -0
- package/dist/retention/purge-batch.js +355 -0
- package/dist/retention/report-obligations.d.ts +12 -0
- package/dist/retention/report-obligations.js +71 -0
- package/dist/retention/sweep.job.d.ts +17 -0
- package/dist/retention/sweep.job.js +95 -0
- package/dist/retention/worker.d.ts +29 -0
- package/dist/retention/worker.js +306 -0
- package/dist/roles/worker.module.js +12 -2
- package/dist/runtime/maintenance.d.ts +5 -1
- package/dist/runtime/maintenance.js +20 -2
- package/dist/runtime/model-call.d.ts +3 -1
- package/dist/runtime/model-call.js +25 -3
- package/dist/runtime/providers.d.ts +7 -2
- package/dist/runtime/providers.js +76 -10
- package/dist/runtime/quota.d.ts +12 -12
- package/dist/runtime/quota.js +42 -37
- package/dist/runtime/run-finish.js +2 -0
- package/dist/runtime/run-principal.d.ts +9 -0
- package/dist/runtime/run-principal.js +18 -0
- package/dist/runtime/run-worker.js +48 -10
- package/dist/runtime/usage-report-jobs.d.ts +4 -1
- package/dist/runtime/usage-report-jobs.js +36 -13
- package/dist/runtime/usage-report.d.ts +5 -3
- package/dist/runtime/usage-report.js +37 -19
- package/dist/sandbox/review/classifier.d.ts +2 -2
- package/dist/sandbox/review/classifier.js +6 -2
- package/dist/sandbox/sandbox.service.js +18 -7
- package/dist/storage/purge.js +5 -1
- package/dist/storage/storage.service.d.ts +17 -1
- package/dist/storage/storage.service.js +75 -4
- package/dist/tools/builtins/vision/read-image.js +3 -0
- package/dist/tools/builtins/web/extract.js +3 -0
- package/dist/tools/pipeline.js +5 -1
- package/package.json +3 -1
- package/src/migrations/0216_model_budget_counters.sql +24 -0
- package/src/migrations/0217_retention.sql +44 -0
- package/src/migrations/0218_data_erasure.sql +41 -0
|
@@ -0,0 +1,501 @@
|
|
|
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 { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import { ConversationsService } from "../conversations/conversations.service.js";
|
|
14
|
+
import { BlobSweepJob } from "../files/blob-sweep.job.js";
|
|
15
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
18
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
|
+
import { OrgLock } from "../orgs/org-lock.js";
|
|
21
|
+
import { PURGE_BATCH_SIZE } from "../retention/purge-batch.js";
|
|
22
|
+
import { hasOwedUsageReports, RetentionReportObligations, } from "../retention/report-obligations.js";
|
|
23
|
+
import { modelUsageJobId } from "../runtime/usage-report-jobs.js";
|
|
24
|
+
import { revokeSharesIn } from "../shares/revoke.js";
|
|
25
|
+
import { StorageService } from "../storage/storage.service.js";
|
|
26
|
+
import { accountResidual } from "./account-residual.js";
|
|
27
|
+
import { AccountDeviceCleanup } from "./device-cleanup.js";
|
|
28
|
+
import { hasExternalDisposition, hasOpaqueExtensionHistory } from "./external-evidence.js";
|
|
29
|
+
import { eraseOrgBatch, orgCatalogDrift, orgResidual } from "./org-batches.js";
|
|
30
|
+
import { ErasureRequests } from "./requests.js";
|
|
31
|
+
const DAY = 86_400_000;
|
|
32
|
+
const RETRY_DELAY = 5 * 60_000;
|
|
33
|
+
const PREFIX_CLAIM_AGE = 60 * 60_000;
|
|
34
|
+
const Progress = z.object({
|
|
35
|
+
plugins: z.array(z.string()).optional(),
|
|
36
|
+
lifecyclePlugins: z.array(z.string()).optional(),
|
|
37
|
+
storageIdentity: z.string().nullable().optional(),
|
|
38
|
+
});
|
|
39
|
+
let OrgErasure = class OrgErasure {
|
|
40
|
+
db;
|
|
41
|
+
txHost;
|
|
42
|
+
config;
|
|
43
|
+
lock;
|
|
44
|
+
requests;
|
|
45
|
+
conversations;
|
|
46
|
+
blobs;
|
|
47
|
+
devices;
|
|
48
|
+
storage;
|
|
49
|
+
reports;
|
|
50
|
+
constructor(db, txHost, config, lock, requests, conversations, blobs, devices, storage, reports) {
|
|
51
|
+
this.db = db;
|
|
52
|
+
this.txHost = txHost;
|
|
53
|
+
this.config = config;
|
|
54
|
+
this.lock = lock;
|
|
55
|
+
this.requests = requests;
|
|
56
|
+
this.conversations = conversations;
|
|
57
|
+
this.blobs = blobs;
|
|
58
|
+
this.devices = devices;
|
|
59
|
+
this.storage = storage;
|
|
60
|
+
this.reports = reports;
|
|
61
|
+
}
|
|
62
|
+
async run(id, generation, orgId) {
|
|
63
|
+
const usage = await this.prepareUsage(id, generation, orgId);
|
|
64
|
+
const claimed = await this.runStage(id, generation, orgId, usage);
|
|
65
|
+
if (!claimed)
|
|
66
|
+
return;
|
|
67
|
+
try {
|
|
68
|
+
await this.storage.deleteVerifiedOrg(orgId, claimed.plugins, claimed.storageIdentity);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
await this.finishPrefix(id, generation, orgId, claimed.at, false);
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
await this.finishPrefix(id, generation, orgId, claimed.at, true);
|
|
75
|
+
}
|
|
76
|
+
enabled() {
|
|
77
|
+
const policy = this.config.get().retention;
|
|
78
|
+
return policy.erasureEnabled && policy.destructiveEnabled;
|
|
79
|
+
}
|
|
80
|
+
async lockTombstone(orgId) {
|
|
81
|
+
await this.lock.lockForRemoval(orgId);
|
|
82
|
+
return this.db
|
|
83
|
+
.selectFrom("orgs")
|
|
84
|
+
.select(["deleted_at", "storage_purge_claimed_at"])
|
|
85
|
+
.where("id", "=", orgId)
|
|
86
|
+
.where("deleted_at", "is not", null)
|
|
87
|
+
.forNoKeyUpdate()
|
|
88
|
+
.executeTakeFirst();
|
|
89
|
+
}
|
|
90
|
+
defer(row, reason, dueAt = new Date(Date.now() + RETRY_DELAY)) {
|
|
91
|
+
if (row.stage === "done")
|
|
92
|
+
throw new Error("Completed erasure work cannot be deferred");
|
|
93
|
+
return this.requests.advance(row, row.stage, reason, dueAt);
|
|
94
|
+
}
|
|
95
|
+
async runStage(id, generation, orgId, usage) {
|
|
96
|
+
if (!this.enabled())
|
|
97
|
+
return;
|
|
98
|
+
const work = await this.requests.claim(id, generation);
|
|
99
|
+
if (work?.kind !== "org" || work.target_id !== orgId)
|
|
100
|
+
return;
|
|
101
|
+
const org = await this.lockTombstone(orgId);
|
|
102
|
+
if (!org?.deleted_at) {
|
|
103
|
+
await this.defer(work, "org-tombstone");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const drift = await orgCatalogDrift(this.db.db);
|
|
107
|
+
if (drift) {
|
|
108
|
+
await this.defer(work, drift);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const parsed = Progress.safeParse(work.progress ?? {});
|
|
112
|
+
if (!parsed.success) {
|
|
113
|
+
await this.defer(work, "erasure-progress");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const observedStorage = this.storage.erasureIdentity();
|
|
117
|
+
if (!observedStorage ||
|
|
118
|
+
(parsed.data.storageIdentity != null && observedStorage !== parsed.data.storageIdentity)) {
|
|
119
|
+
await this.defer(work, "storage-provider-changed");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (work.stage !== "pending" && parsed.data.storageIdentity !== observedStorage) {
|
|
123
|
+
await this.defer(work, "storage-provider-unverified");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
switch (work.stage) {
|
|
127
|
+
case "pending":
|
|
128
|
+
await this.begin(work, orgId, parsed.data, observedStorage);
|
|
129
|
+
break;
|
|
130
|
+
case "roots":
|
|
131
|
+
await this.roots(work, orgId, org.deleted_at);
|
|
132
|
+
break;
|
|
133
|
+
case "objects":
|
|
134
|
+
await this.objects(work, orgId, org.deleted_at);
|
|
135
|
+
break;
|
|
136
|
+
case "rows":
|
|
137
|
+
if (!(await this.clearUsageBatch(work, orgId, usage))) {
|
|
138
|
+
await this.removeRows(work, orgId, "rows");
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
case "prefix": {
|
|
142
|
+
if (org.storage_purge_claimed_at &&
|
|
143
|
+
Date.now() - org.storage_purge_claimed_at.getTime() < PREFIX_CLAIM_AGE) {
|
|
144
|
+
await this.defer(work, "storage-claimed");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const at = new Date();
|
|
148
|
+
await this.db
|
|
149
|
+
.updateTable("orgs")
|
|
150
|
+
.set({ storage_purge_claimed_at: at })
|
|
151
|
+
.where("id", "=", orgId)
|
|
152
|
+
.execute();
|
|
153
|
+
return { at, plugins: parsed.data.plugins ?? [], storageIdentity: observedStorage };
|
|
154
|
+
}
|
|
155
|
+
case "late":
|
|
156
|
+
await this.removeRows(work, orgId, "late");
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async begin(work, orgId, progress, storageIdentity) {
|
|
161
|
+
const verified = await hasExternalDisposition(this.db.db, work.id, "external-cleanup-unverified");
|
|
162
|
+
// An empty current install list says nothing about previously approved code's data.
|
|
163
|
+
if (!verified &&
|
|
164
|
+
(progress.plugins === undefined ||
|
|
165
|
+
progress.storageIdentity == null ||
|
|
166
|
+
progress.plugins.length > 0 ||
|
|
167
|
+
(await hasOpaqueExtensionHistory(this.db.db)))) {
|
|
168
|
+
await this.defer(work, "external-cleanup-unverified");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const copiedSpecs = await this.db
|
|
172
|
+
.selectFrom("conversations as c")
|
|
173
|
+
.innerJoin("conversation_events as e", "e.conversation_id", "c.id")
|
|
174
|
+
.where("c.org_id", "=", orgId)
|
|
175
|
+
.where("e.type", "in", ["run_start", "model_call"])
|
|
176
|
+
.select("e.seq")
|
|
177
|
+
.limit(1)
|
|
178
|
+
.executeTakeFirst();
|
|
179
|
+
// Inspect catalog ownership even after uninstall and before any source versions disappear.
|
|
180
|
+
const catalog = await this.db
|
|
181
|
+
.selectFrom("catalog_items")
|
|
182
|
+
.select("id")
|
|
183
|
+
.where("owner_org_id", "=", orgId)
|
|
184
|
+
.limit(1)
|
|
185
|
+
.executeTakeFirst();
|
|
186
|
+
const staging = await this.db
|
|
187
|
+
.selectFrom("plugin_import_staging")
|
|
188
|
+
.select("id")
|
|
189
|
+
.where("org_id", "=", orgId)
|
|
190
|
+
.limit(1)
|
|
191
|
+
.executeTakeFirst();
|
|
192
|
+
if ((copiedSpecs || catalog || staging) &&
|
|
193
|
+
!(await hasExternalDisposition(this.db.db, work.id, "catalog-or-tool-ownership-unverified"))) {
|
|
194
|
+
await this.defer(work, "catalog-or-tool-ownership-unverified");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
await this.requests.advance(work, "roots", null, new Date(), { ...progress, storageIdentity });
|
|
198
|
+
}
|
|
199
|
+
async roots(work, orgId, deletedAt) {
|
|
200
|
+
// org-filter: deployment-wide visibility for rows predating conversation event ownership.
|
|
201
|
+
const legacy = await asPlatform("verify org conversation ownership backfill before purging", () => this.db
|
|
202
|
+
.selectFrom("conversation_events as e")
|
|
203
|
+
.innerJoin("conversations as c", "c.id", "e.conversation_id")
|
|
204
|
+
.select("e.seq")
|
|
205
|
+
.where("c.org_id", "=", orgId)
|
|
206
|
+
.where("e.org_id", "is", null)
|
|
207
|
+
.limit(1)
|
|
208
|
+
.executeTakeFirst());
|
|
209
|
+
if (legacy) {
|
|
210
|
+
await this.defer(work, "event-ownership-backfill");
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const root = await this.db
|
|
214
|
+
.selectFrom("conversations")
|
|
215
|
+
.select(["id", "account_id"])
|
|
216
|
+
.where("org_id", "=", orgId)
|
|
217
|
+
.where("parent_conversation_id", "is", null)
|
|
218
|
+
.where("deleted_at", "is", null)
|
|
219
|
+
.orderBy("id")
|
|
220
|
+
.limit(1)
|
|
221
|
+
.executeTakeFirst();
|
|
222
|
+
if (root) {
|
|
223
|
+
await this.conversations.tombstoneForErasure(orgId, root.id, root.account_id, deletedAt);
|
|
224
|
+
await this.requests.advance(work, "roots", null, new Date());
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const remaining = await this.db
|
|
228
|
+
.selectFrom("conversations")
|
|
229
|
+
.select("id")
|
|
230
|
+
.where("org_id", "=", orgId)
|
|
231
|
+
.limit(1)
|
|
232
|
+
.executeTakeFirst();
|
|
233
|
+
if (remaining) {
|
|
234
|
+
await this.defer(work, "conversation-purge");
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
238
|
+
}
|
|
239
|
+
async objects(work, orgId, deletedAt) {
|
|
240
|
+
const dueAt = new Date(deletedAt.getTime() + Math.max(30, this.config.get().storage.purgeDeletedOrgsAfterDays) * DAY);
|
|
241
|
+
if (dueAt.getTime() > Date.now()) {
|
|
242
|
+
await this.defer(work, "recovery-hold", dueAt);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const devices = await this.devices.verifyOrgBatch(orgId);
|
|
246
|
+
if (devices.blocked) {
|
|
247
|
+
await this.defer(work, devices.blocked);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!devices.done) {
|
|
251
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const shares = await this.db
|
|
255
|
+
.selectFrom("file_shares")
|
|
256
|
+
.select("id")
|
|
257
|
+
.where("org_id", "=", orgId)
|
|
258
|
+
.orderBy("id")
|
|
259
|
+
.limit(PURGE_BATCH_SIZE)
|
|
260
|
+
.forUpdate()
|
|
261
|
+
.execute();
|
|
262
|
+
if (shares.length) {
|
|
263
|
+
const ids = shares.map(({ id }) => id);
|
|
264
|
+
await revokeSharesIn(this.db.db, { orgId, shareIds: ids }, "conversation_deleted");
|
|
265
|
+
await this.db
|
|
266
|
+
.deleteFrom("file_shares")
|
|
267
|
+
.where("org_id", "=", orgId)
|
|
268
|
+
.where("id", "in", ids)
|
|
269
|
+
.execute();
|
|
270
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const files = await this.db
|
|
274
|
+
.selectFrom("files")
|
|
275
|
+
.select("id")
|
|
276
|
+
.where("org_id", "=", orgId)
|
|
277
|
+
.where("status", "!=", "deleted")
|
|
278
|
+
.orderBy("id")
|
|
279
|
+
.limit(PURGE_BATCH_SIZE)
|
|
280
|
+
.forUpdate()
|
|
281
|
+
.execute();
|
|
282
|
+
if (files.length) {
|
|
283
|
+
await this.db
|
|
284
|
+
.updateTable("files")
|
|
285
|
+
.set({ status: "deleted", deleted_at: sql `now()`, expires_at: null })
|
|
286
|
+
.where("org_id", "=", orgId)
|
|
287
|
+
.where("id", "in", files.map(({ id }) => id))
|
|
288
|
+
.execute();
|
|
289
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const swept = await this.blobs.sweepFencedOrgBatch(orgId, new Date());
|
|
293
|
+
if (swept.blocked) {
|
|
294
|
+
await this.defer(work, swept.blocked);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (swept.filesDeleted || swept.blobsReleased) {
|
|
298
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const blob = await this.db
|
|
302
|
+
.selectFrom("blobs")
|
|
303
|
+
.select("id")
|
|
304
|
+
.where("org_id", "=", orgId)
|
|
305
|
+
.limit(1)
|
|
306
|
+
.executeTakeFirst();
|
|
307
|
+
if (blob) {
|
|
308
|
+
await this.defer(work, "blob-deletion");
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const expired = await this.db
|
|
312
|
+
.updateTable("attachments")
|
|
313
|
+
.set({ expires_at: sql `now()` })
|
|
314
|
+
.where("org_id", "=", orgId)
|
|
315
|
+
.where("id", "in", (eb) => eb
|
|
316
|
+
.selectFrom("attachments")
|
|
317
|
+
.select("id")
|
|
318
|
+
.where("org_id", "=", orgId)
|
|
319
|
+
.where("conversation_id", "is", null)
|
|
320
|
+
.where("expires_at", "is", null)
|
|
321
|
+
.orderBy("id")
|
|
322
|
+
.limit(PURGE_BATCH_SIZE))
|
|
323
|
+
.executeTakeFirst();
|
|
324
|
+
if (expired.numUpdatedRows) {
|
|
325
|
+
await this.requests.advance(work, "objects", null, new Date());
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
const attachment = await this.db
|
|
329
|
+
.selectFrom("attachments")
|
|
330
|
+
.select("id")
|
|
331
|
+
.where("org_id", "=", orgId)
|
|
332
|
+
.limit(1)
|
|
333
|
+
.executeTakeFirst();
|
|
334
|
+
if (attachment) {
|
|
335
|
+
await this.defer(work, "attachment-deletion");
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
await this.requests.advance(work, "rows", null, new Date());
|
|
339
|
+
}
|
|
340
|
+
async usagePage(orgId, lock = false) {
|
|
341
|
+
return this.db
|
|
342
|
+
.selectFrom("model_usage")
|
|
343
|
+
.select(["id", "reported_at"])
|
|
344
|
+
.where("org_id", "=", orgId)
|
|
345
|
+
.orderBy("id")
|
|
346
|
+
.limit(PURGE_BATCH_SIZE)
|
|
347
|
+
.$if(lock, (q) => q.forUpdate())
|
|
348
|
+
.execute();
|
|
349
|
+
}
|
|
350
|
+
async prepareUsage(id, generation, orgId) {
|
|
351
|
+
if (!this.enabled())
|
|
352
|
+
return;
|
|
353
|
+
const eligible = await this.db
|
|
354
|
+
.selectFrom("data_erasure_requests")
|
|
355
|
+
.select("id")
|
|
356
|
+
.where("org_id", "=", orgId)
|
|
357
|
+
.where("id", "=", id)
|
|
358
|
+
.where("generation", "=", generation)
|
|
359
|
+
.where("stage", "=", "rows")
|
|
360
|
+
.where("due_at", "<=", sql `now()`)
|
|
361
|
+
.executeTakeFirst();
|
|
362
|
+
if (!eligible)
|
|
363
|
+
return;
|
|
364
|
+
const page = await this.usagePage(orgId);
|
|
365
|
+
const checked = page.filter((p) => p.reported_at !== null).map(({ id: usageId }) => usageId);
|
|
366
|
+
const jobs = checked.map(modelUsageJobId);
|
|
367
|
+
return (await this.reports.clear(orgId, jobs)) ? checked : [];
|
|
368
|
+
}
|
|
369
|
+
/** Rechecks marker and outbox evidence after the non-transactional queue check. */
|
|
370
|
+
async clearUsageBatch(work, orgId, checked) {
|
|
371
|
+
const page = await this.usagePage(orgId, true);
|
|
372
|
+
if (!page.length)
|
|
373
|
+
return false;
|
|
374
|
+
const verified = new Set(checked);
|
|
375
|
+
const ids = page
|
|
376
|
+
.filter((p) => verified.has(p.id) && p.reported_at !== null)
|
|
377
|
+
.map(({ id }) => id);
|
|
378
|
+
if (!ids.length || (await hasOwedUsageReports(this.db.db, orgId, ids.map(modelUsageJobId)))) {
|
|
379
|
+
await this.defer(work, "usage-delivery");
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
await this.db
|
|
383
|
+
.deleteFrom("model_usage")
|
|
384
|
+
.where("org_id", "=", orgId)
|
|
385
|
+
.where("id", "in", ids)
|
|
386
|
+
.execute();
|
|
387
|
+
await this.requests.advance(work, "rows", null, new Date());
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
async removeRows(work, orgId, stage) {
|
|
391
|
+
const result = await eraseOrgBatch(this.db.db, orgId, stage);
|
|
392
|
+
if (result.blocked) {
|
|
393
|
+
await this.defer(work, result.blocked);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (result.removed || stage === "rows") {
|
|
397
|
+
await this.requests.advance(work, result.removed ? stage : "prefix", null, new Date());
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
// A final UPDATE lock closes FK admission before checking the last dependencies.
|
|
401
|
+
await this.db
|
|
402
|
+
.selectFrom("orgs")
|
|
403
|
+
.select("id")
|
|
404
|
+
.where("id", "=", orgId)
|
|
405
|
+
.forUpdate()
|
|
406
|
+
.executeTakeFirstOrThrow();
|
|
407
|
+
const guest = await this.eraseAnonymousGuest(orgId);
|
|
408
|
+
if (guest) {
|
|
409
|
+
await this.defer(work, guest);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const residual = await orgResidual(this.db.db, orgId);
|
|
413
|
+
if (residual) {
|
|
414
|
+
await this.defer(work, residual);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const completed = await asPlatform("scrub completed org erasure receipt", () => this.requests.complete(work));
|
|
418
|
+
if (!completed)
|
|
419
|
+
throw new Error("Erasure request changed during finalization");
|
|
420
|
+
await this.db
|
|
421
|
+
.deleteFrom("orgs")
|
|
422
|
+
.where("id", "=", orgId)
|
|
423
|
+
.where("deleted_at", "is not", null)
|
|
424
|
+
.execute();
|
|
425
|
+
}
|
|
426
|
+
async eraseAnonymousGuest(orgId) {
|
|
427
|
+
const mapping = await this.db
|
|
428
|
+
.selectFrom("org_anonymous_accounts")
|
|
429
|
+
.select("account_id")
|
|
430
|
+
.where("org_id", "=", orgId)
|
|
431
|
+
.forUpdate()
|
|
432
|
+
.executeTakeFirst();
|
|
433
|
+
if (!mapping)
|
|
434
|
+
return;
|
|
435
|
+
const { account_id: accountId } = mapping;
|
|
436
|
+
const synthetic = await asPlatform("verify the deleted org's loginless guest account", async () => {
|
|
437
|
+
const account = await this.db
|
|
438
|
+
.selectFrom("accounts")
|
|
439
|
+
.select("id")
|
|
440
|
+
.where("id", "=", accountId)
|
|
441
|
+
.where("email", "is", null)
|
|
442
|
+
.where("is_super_admin", "=", false)
|
|
443
|
+
.forUpdate()
|
|
444
|
+
.executeTakeFirst();
|
|
445
|
+
return (account !== undefined &&
|
|
446
|
+
(await accountResidual(this.db.db, accountId, orgId)) === undefined);
|
|
447
|
+
});
|
|
448
|
+
if (!synthetic)
|
|
449
|
+
return "anonymous-account-dependency";
|
|
450
|
+
await this.db
|
|
451
|
+
.deleteFrom("org_anonymous_accounts")
|
|
452
|
+
.where("org_id", "=", orgId)
|
|
453
|
+
.where("account_id", "=", accountId)
|
|
454
|
+
.execute();
|
|
455
|
+
await this.db.deleteFrom("accounts").where("id", "=", accountId).execute();
|
|
456
|
+
}
|
|
457
|
+
async finishPrefix(id, generation, orgId, at, verified) {
|
|
458
|
+
const work = await this.requests.claim(id, generation);
|
|
459
|
+
if (work?.stage !== "prefix" || work.target_id !== orgId)
|
|
460
|
+
return;
|
|
461
|
+
const org = await this.lockTombstone(orgId);
|
|
462
|
+
if (!org || org.storage_purge_claimed_at?.getTime() !== at.getTime())
|
|
463
|
+
return;
|
|
464
|
+
await this.db
|
|
465
|
+
.updateTable("orgs")
|
|
466
|
+
.set({
|
|
467
|
+
storage_purge_claimed_at: null,
|
|
468
|
+
...(verified ? { storage_purged_at: sql `now()` } : {}),
|
|
469
|
+
})
|
|
470
|
+
.where("id", "=", orgId)
|
|
471
|
+
.execute();
|
|
472
|
+
const progress = Progress.safeParse(work.progress);
|
|
473
|
+
if (verified &&
|
|
474
|
+
this.enabled() &&
|
|
475
|
+
progress.success &&
|
|
476
|
+
progress.data.storageIdentity === this.storage.erasureIdentity())
|
|
477
|
+
await this.requests.advance(work, "late", null, new Date());
|
|
478
|
+
else
|
|
479
|
+
await this.defer(work, "storage-unverified");
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
__decorate([
|
|
483
|
+
Transactional()
|
|
484
|
+
], OrgErasure.prototype, "runStage", null);
|
|
485
|
+
__decorate([
|
|
486
|
+
Transactional()
|
|
487
|
+
], OrgErasure.prototype, "finishPrefix", null);
|
|
488
|
+
OrgErasure = __decorate([
|
|
489
|
+
Injectable(),
|
|
490
|
+
__param(0, Inject(KernelDb)),
|
|
491
|
+
__param(1, Inject(TransactionHost)),
|
|
492
|
+
__param(2, Inject(KernelConfigService)),
|
|
493
|
+
__param(3, Inject(OrgLock)),
|
|
494
|
+
__param(4, Inject(ErasureRequests)),
|
|
495
|
+
__param(5, Inject(ConversationsService)),
|
|
496
|
+
__param(6, Inject(BlobSweepJob)),
|
|
497
|
+
__param(7, Inject(AccountDeviceCleanup)),
|
|
498
|
+
__param(8, Inject(StorageService)),
|
|
499
|
+
__param(9, Inject(RetentionReportObligations))
|
|
500
|
+
], OrgErasure);
|
|
501
|
+
export { OrgErasure };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Selectable } from "kysely";
|
|
2
|
+
import type { DataErasureRequestsTable } from "../db/kernel-schema.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
6
|
+
export type ErasureRow = Selectable<DataErasureRequestsTable>;
|
|
7
|
+
export type ErasureKind = ErasureRow["kind"];
|
|
8
|
+
export type ErasureStage = ErasureRow["stage"];
|
|
9
|
+
export declare class ErasureRequests {
|
|
10
|
+
private readonly db;
|
|
11
|
+
readonly txHost: TransactionHost;
|
|
12
|
+
private readonly outbox;
|
|
13
|
+
constructor(db: KernelDb, txHost: TransactionHost, outbox: OutboxService);
|
|
14
|
+
enqueue(kind: ErasureKind, targetId: string, progress?: Record<string, unknown>): Promise<{
|
|
15
|
+
blockedReason: string | null;
|
|
16
|
+
completedAt: Date | null;
|
|
17
|
+
dueAt: Date;
|
|
18
|
+
id: string;
|
|
19
|
+
kind: "account" | "org";
|
|
20
|
+
scopeBoundaries: "third-party-conversation-content"[];
|
|
21
|
+
stage: "account-external" | "account-final" | "account-orgs" | "blocked" | "done" | "late" | "objects" | "pending" | "prefix" | "roots" | "rows";
|
|
22
|
+
}>;
|
|
23
|
+
active(kind: ErasureKind, targetId: string): Promise<{
|
|
24
|
+
blockedReason: string | null;
|
|
25
|
+
completedAt: Date | null;
|
|
26
|
+
dueAt: Date;
|
|
27
|
+
id: string;
|
|
28
|
+
kind: "account" | "org";
|
|
29
|
+
scopeBoundaries: "third-party-conversation-content"[];
|
|
30
|
+
stage: "account-external" | "account-final" | "account-orgs" | "blocked" | "done" | "late" | "objects" | "pending" | "prefix" | "roots" | "rows";
|
|
31
|
+
} | undefined>;
|
|
32
|
+
status(id: string): Promise<{
|
|
33
|
+
blockedReason: string | null;
|
|
34
|
+
completedAt: Date | null;
|
|
35
|
+
dueAt: Date;
|
|
36
|
+
id: string;
|
|
37
|
+
kind: "account" | "org";
|
|
38
|
+
scopeBoundaries: "third-party-conversation-content"[];
|
|
39
|
+
stage: "account-external" | "account-final" | "account-orgs" | "blocked" | "done" | "late" | "objects" | "pending" | "prefix" | "roots" | "rows";
|
|
40
|
+
} | undefined>;
|
|
41
|
+
resolveOwner(id: string): Promise<{
|
|
42
|
+
org_id: string | null;
|
|
43
|
+
} | undefined>;
|
|
44
|
+
claim(id: string, generation: number): Promise<{
|
|
45
|
+
blocked_reason: string | null;
|
|
46
|
+
completed_at: Date | null;
|
|
47
|
+
due_at: Date;
|
|
48
|
+
generation: number;
|
|
49
|
+
id: string;
|
|
50
|
+
kind: "account" | "org";
|
|
51
|
+
org_id: string | null;
|
|
52
|
+
progress: Record<string, unknown> | null;
|
|
53
|
+
scope_boundaries: "third-party-conversation-content"[];
|
|
54
|
+
stage: "account-external" | "account-final" | "account-orgs" | "blocked" | "done" | "late" | "objects" | "pending" | "prefix" | "roots" | "rows";
|
|
55
|
+
target_id: string | null;
|
|
56
|
+
} | undefined>;
|
|
57
|
+
private transition;
|
|
58
|
+
advance(row: ErasureRow, stage: Exclude<ErasureStage, "done">, blockedReason: string | null, dueAt: Date, progress?: Record<string, unknown> | null, scopeBoundaries?: ErasureRow["scope_boundaries"]): Promise<boolean>;
|
|
59
|
+
complete(row: ErasureRow): Promise<boolean>;
|
|
60
|
+
}
|
|
61
|
+
export type ErasureStatus = NonNullable<Awaited<ReturnType<ErasureRequests["status"]>>>;
|