@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,217 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { afterUploadForm, stagingObjectKey } from "../files/blob-objects.js";
|
|
4
|
+
import { blobUnreferenced } from "../files/blob-references.js";
|
|
5
|
+
import { asPlatform, currentScope } from "../infra/database/org-scope.js";
|
|
6
|
+
import { PURGE_BATCH_SIZE } from "../retention/purge-batch.js";
|
|
7
|
+
export const accountFileIntentSchema = z.object({
|
|
8
|
+
phase: z.enum(["staging", "release", "objects"]),
|
|
9
|
+
orgId: z.string().min(1),
|
|
10
|
+
kind: z.enum(["file", "share"]),
|
|
11
|
+
id: z.string().min(1),
|
|
12
|
+
inaccessibleAt: z.iso.datetime(),
|
|
13
|
+
uploadUntil: z.iso.datetime().nullable(),
|
|
14
|
+
stagingKey: z.string().min(1).nullable(),
|
|
15
|
+
blob: z.object({ id: z.string().min(1), key: z.string().min(1) }).nullable(),
|
|
16
|
+
});
|
|
17
|
+
// Capture every object key before a revoked share or soft-deleted file becomes sweepable.
|
|
18
|
+
// An old deleted file may have already lost its blob. Never infer an exclusive key from its hash.
|
|
19
|
+
async function capture(db, orgId, accountId, disabledSince) {
|
|
20
|
+
const share = await sql `SELECT id::text,blob_id,revoked_at AS inaccessible_at,created_at,
|
|
21
|
+
NULL::text AS staging_key,NULL::text AS file_origin,NULL::text AS ref_key FROM capybara.file_shares
|
|
22
|
+
WHERE org_id=${orgId} AND created_by=${accountId} ORDER BY id LIMIT 1 FOR UPDATE`.execute(db);
|
|
23
|
+
const kind = share.rows.length ? "share" : "file";
|
|
24
|
+
const item = share.rows[0] ??
|
|
25
|
+
(await sql `SELECT id,blob_id,deleted_at AS inaccessible_at,created_at,
|
|
26
|
+
staging_key,origin AS file_origin,ref_key FROM capybara.files
|
|
27
|
+
WHERE org_id=${orgId} AND account_id=${accountId} ORDER BY id LIMIT 1 FOR UPDATE`.execute(db)).rows[0];
|
|
28
|
+
if (!item)
|
|
29
|
+
return { done: true };
|
|
30
|
+
const blob = item.blob_id === null
|
|
31
|
+
? null
|
|
32
|
+
: await db
|
|
33
|
+
.selectFrom("blobs")
|
|
34
|
+
.select(["id", "object_key"])
|
|
35
|
+
.where("org_id", "=", orgId)
|
|
36
|
+
.where("id", "=", item.blob_id)
|
|
37
|
+
.forUpdate()
|
|
38
|
+
.executeTakeFirst();
|
|
39
|
+
if (item.blob_id !== null && !blob)
|
|
40
|
+
return { blocked: "account-file-blob-owner" };
|
|
41
|
+
if (item.ref_key && item.ref_key !== blob?.object_key)
|
|
42
|
+
return { blocked: "account-file-historical-object" };
|
|
43
|
+
const now = new Date();
|
|
44
|
+
if (kind === "file") {
|
|
45
|
+
await db
|
|
46
|
+
.updateTable("files")
|
|
47
|
+
.set({ status: "deleted", deleted_at: item.inaccessible_at ?? now })
|
|
48
|
+
.where("org_id", "=", orgId)
|
|
49
|
+
.where("account_id", "=", accountId)
|
|
50
|
+
.where("id", "=", item.id)
|
|
51
|
+
.execute();
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
await sql `UPDATE capybara.file_shares SET revoked_at=coalesce(revoked_at,${now}),
|
|
55
|
+
revoke_reason=coalesce(revoke_reason,'member_offboarded'),updated_at=now()
|
|
56
|
+
WHERE org_id=${orgId} AND id=${item.id} AND created_by=${accountId}`.execute(db);
|
|
57
|
+
}
|
|
58
|
+
const stagingKey = item.file_origin === "upload" ? (item.staging_key ?? stagingObjectKey(item.id)) : null;
|
|
59
|
+
return {
|
|
60
|
+
intent: {
|
|
61
|
+
orgId,
|
|
62
|
+
id: item.id,
|
|
63
|
+
kind,
|
|
64
|
+
phase: stagingKey ? "staging" : "release",
|
|
65
|
+
// Share opens recheck the creator's active account on every request. A library file
|
|
66
|
+
// also becomes inaccessible to the disabled owner, even before we set its tombstone.
|
|
67
|
+
inaccessibleAt: new Date(Math.max(item.created_at.getTime(), Math.min(disabledSince.getTime(), (item.inaccessible_at ?? now).getTime()))).toISOString(),
|
|
68
|
+
uploadUntil: stagingKey ? (afterUploadForm(item.created_at)?.toISOString() ?? null) : null,
|
|
69
|
+
stagingKey,
|
|
70
|
+
blob: blob ? { id: blob.id, key: blob.object_key } : null,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export async function accountFileBatch(db, orgId, accountId, cutoff, prior) {
|
|
75
|
+
const scope = currentScope();
|
|
76
|
+
if (scope?.kind !== "org" || scope.orgId !== orgId || (prior && prior.orgId !== orgId))
|
|
77
|
+
return { blocked: "account-file-scope" };
|
|
78
|
+
const fenced = await db
|
|
79
|
+
.selectFrom("accounts")
|
|
80
|
+
.select(["id", "updated_at"])
|
|
81
|
+
.where("id", "=", accountId)
|
|
82
|
+
.where("status", "=", "disabled")
|
|
83
|
+
.where("erasure_requested", "=", true)
|
|
84
|
+
.forUpdate()
|
|
85
|
+
.executeTakeFirst();
|
|
86
|
+
if (!fenced)
|
|
87
|
+
return { blocked: "account-fence" };
|
|
88
|
+
if (!prior)
|
|
89
|
+
return capture(db, orgId, accountId, fenced.updated_at);
|
|
90
|
+
if (new Date(prior.inaccessibleAt) > cutoff)
|
|
91
|
+
return { blocked: "account-file-recovery-hold" };
|
|
92
|
+
if (prior.uploadUntil && Date.parse(prior.uploadUntil) > Date.now())
|
|
93
|
+
return { blocked: "account-file-upload-window" };
|
|
94
|
+
if (prior.phase !== "release")
|
|
95
|
+
return { intent: prior, external: true };
|
|
96
|
+
const blob = prior.blob;
|
|
97
|
+
const record = blob &&
|
|
98
|
+
(await db
|
|
99
|
+
.selectFrom("blobs")
|
|
100
|
+
.select(["id", "org_id", "object_key", "status"])
|
|
101
|
+
.where("org_id", "=", orgId)
|
|
102
|
+
.where("id", "=", blob.id)
|
|
103
|
+
.forUpdate()
|
|
104
|
+
.executeTakeFirst());
|
|
105
|
+
if (record && (record.object_key !== blob?.key || record.status === "pending"))
|
|
106
|
+
return { blocked: "account-file-blob-state" };
|
|
107
|
+
if (prior.kind === "file") {
|
|
108
|
+
const before = await db
|
|
109
|
+
.selectFrom("files")
|
|
110
|
+
.select(["id", "account_id", "status", "blob_id"])
|
|
111
|
+
.where("org_id", "=", orgId)
|
|
112
|
+
.where("id", "=", prior.id)
|
|
113
|
+
.forUpdate()
|
|
114
|
+
.executeTakeFirst();
|
|
115
|
+
if (before &&
|
|
116
|
+
(before.account_id !== accountId ||
|
|
117
|
+
before.status !== "deleted" ||
|
|
118
|
+
(before.blob_id !== null && before.blob_id !== blob?.id)))
|
|
119
|
+
return { blocked: "account-file-changed" };
|
|
120
|
+
// org-filter: deployment-wide reads protect detached associations and cross-org foreign keys
|
|
121
|
+
const refs = await asPlatform("verify incoming account file references", () => sql `
|
|
122
|
+
SELECT EXISTS(SELECT FROM capybara.conversation_files WHERE file_id=${prior.id}) AS conversation,
|
|
123
|
+
EXISTS(SELECT FROM capybara.file_shares WHERE source_file_id=${prior.id}) AS share`.execute(db));
|
|
124
|
+
if (refs.rows[0]?.conversation)
|
|
125
|
+
return { blocked: "account-file-conversations" };
|
|
126
|
+
if (refs.rows[0]?.share) {
|
|
127
|
+
// A snapshot owns its own blob. Detaching its source pointer preserves the recipient's copy.
|
|
128
|
+
// A different org or a share without an intact snapshot needs a separate disposition.
|
|
129
|
+
const result = await sql `UPDATE capybara.file_shares SET source_file_id=NULL WHERE id IN (
|
|
130
|
+
SELECT s.id FROM capybara.file_shares s JOIN capybara.blobs b ON b.id=s.blob_id AND b.org_id=s.org_id
|
|
131
|
+
WHERE s.org_id=${orgId} AND s.source_file_id=${prior.id} AND s.created_by<>${accountId}
|
|
132
|
+
AND b.status='ready' ORDER BY s.id LIMIT ${PURGE_BATCH_SIZE} FOR UPDATE OF s)`.execute(db);
|
|
133
|
+
if (result.numAffectedRows)
|
|
134
|
+
return { intent: prior };
|
|
135
|
+
return { blocked: "account-file-surviving-share" };
|
|
136
|
+
}
|
|
137
|
+
await db
|
|
138
|
+
.deleteFrom("files")
|
|
139
|
+
.where("org_id", "=", orgId)
|
|
140
|
+
.where("id", "=", prior.id)
|
|
141
|
+
.where("account_id", "=", accountId)
|
|
142
|
+
.where("status", "=", "deleted")
|
|
143
|
+
.execute();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const before = await db
|
|
147
|
+
.selectFrom("file_shares")
|
|
148
|
+
.select(["created_by", "blob_id", "revoked_at"])
|
|
149
|
+
.where("id", "=", prior.id)
|
|
150
|
+
.where("org_id", "=", orgId)
|
|
151
|
+
.forUpdate()
|
|
152
|
+
.executeTakeFirst();
|
|
153
|
+
if (before &&
|
|
154
|
+
(before.created_by !== accountId ||
|
|
155
|
+
!before.revoked_at ||
|
|
156
|
+
(before.blob_id !== null && before.blob_id !== blob?.id)))
|
|
157
|
+
return { blocked: "account-share-changed" };
|
|
158
|
+
await db
|
|
159
|
+
.deleteFrom("file_shares")
|
|
160
|
+
.where("org_id", "=", orgId)
|
|
161
|
+
.where("id", "=", prior.id)
|
|
162
|
+
.where("created_by", "=", accountId)
|
|
163
|
+
.where("revoked_at", "is not", null)
|
|
164
|
+
.execute();
|
|
165
|
+
}
|
|
166
|
+
if (!blob)
|
|
167
|
+
return { intent: { ...prior, phase: "objects", blob: null }, external: true };
|
|
168
|
+
// org-filter: deployment-wide verification of incoming blob FKs before releasing exclusive bytes
|
|
169
|
+
const invalid = await asPlatform("verify blob references belong to the same org", () => sql `
|
|
170
|
+
SELECT FROM capybara.files WHERE org_id<>${orgId} AND blob_id=${blob.id}
|
|
171
|
+
UNION ALL SELECT FROM capybara.file_shares WHERE org_id<>${orgId} AND blob_id=${blob.id} LIMIT 1`.execute(db));
|
|
172
|
+
if (invalid.rows.length)
|
|
173
|
+
return { blocked: "account-file-cross-org-blob" };
|
|
174
|
+
if (record) {
|
|
175
|
+
const exclusive = await db
|
|
176
|
+
.selectFrom("blobs")
|
|
177
|
+
.select("id")
|
|
178
|
+
.where("id", "=", blob.id)
|
|
179
|
+
.where("org_id", "=", orgId)
|
|
180
|
+
.where(blobUnreferenced(orgId))
|
|
181
|
+
.executeTakeFirst();
|
|
182
|
+
if (!exclusive)
|
|
183
|
+
return { intent: { ...prior, phase: "objects", blob: null }, external: true };
|
|
184
|
+
if (record.status === "ready")
|
|
185
|
+
await db
|
|
186
|
+
.updateTable("blobs")
|
|
187
|
+
.set({ status: "deleting" })
|
|
188
|
+
.where("id", "=", blob.id)
|
|
189
|
+
.where("org_id", "=", orgId)
|
|
190
|
+
.execute();
|
|
191
|
+
}
|
|
192
|
+
return { intent: { ...prior, phase: "objects" }, external: true };
|
|
193
|
+
}
|
|
194
|
+
// Run only for a committed intent. Storage calls stay outside the progress transaction.
|
|
195
|
+
export async function verifyAccountFileObjects(store, objects, intent) {
|
|
196
|
+
if (intent.phase === "staging" && intent.stagingKey) {
|
|
197
|
+
if (intent.uploadUntil && Date.parse(intent.uploadUntil) > Date.now())
|
|
198
|
+
throw new Error("File upload window is open");
|
|
199
|
+
if (await store.head(intent.stagingKey)) {
|
|
200
|
+
await objects.deleteStaging({
|
|
201
|
+
orgId: intent.orgId,
|
|
202
|
+
fileId: intent.id,
|
|
203
|
+
objectKey: intent.stagingKey,
|
|
204
|
+
}, store);
|
|
205
|
+
if (await store.head(intent.stagingKey))
|
|
206
|
+
throw new Error("File staging cleanup remains unverified");
|
|
207
|
+
}
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (intent.phase !== "objects")
|
|
211
|
+
throw new Error("Account file intent is not ready for object verification");
|
|
212
|
+
if (intent.blob) {
|
|
213
|
+
await objects.deleteBlob({ orgId: intent.orgId, blobId: intent.blob.id }, store);
|
|
214
|
+
if (await store.head(intent.blob.key))
|
|
215
|
+
throw new Error("Exclusive file blob cleanup remains unverified");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
2
|
+
export declare function accountResidual(db: KernelSchemaDb, accountId: string, anonymousOrgId?: string): Promise<string | undefined>;
|
|
3
|
+
export interface HistoryKey {
|
|
4
|
+
conversationId: string;
|
|
5
|
+
seq: string;
|
|
6
|
+
}
|
|
7
|
+
export interface HistoryCursor {
|
|
8
|
+
end: HistoryKey;
|
|
9
|
+
after?: HistoryKey | undefined;
|
|
10
|
+
}
|
|
11
|
+
/** Appended history owned by other people is immutable and only affects the receipt. */
|
|
12
|
+
export declare function accountHistoryPage(db: KernelSchemaDb, accountId: string, cursor?: HistoryCursor): Promise<{
|
|
13
|
+
hit: boolean;
|
|
14
|
+
next?: HistoryCursor;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
import { asPlatform, currentScope } from "../infra/database/org-scope.js";
|
|
3
|
+
import { orgCatalogDrift } from "./org-catalog.js";
|
|
4
|
+
// Static, read-only verification, including detached keys and all three FK delete actions.
|
|
5
|
+
// A reference with no implemented disposition leaves the account and receipt intact.
|
|
6
|
+
const direct = [
|
|
7
|
+
["account_avatar_uploads", "account_id"],
|
|
8
|
+
["account_initial_passwords", "account_id recipient_account_id"],
|
|
9
|
+
["agent_members", "account_id"],
|
|
10
|
+
["agent_versions", "created_by"],
|
|
11
|
+
["agents", "owner_account_id"],
|
|
12
|
+
["api_tokens", "account_id created_by"],
|
|
13
|
+
["approvals", "decided_by requested_for"],
|
|
14
|
+
["attachments", "account_id"],
|
|
15
|
+
["audit_events", "actor_account_id"],
|
|
16
|
+
["automation_acceptance", "account_id"],
|
|
17
|
+
["automation_offers", "account_id"],
|
|
18
|
+
["automations", "account_id paused_by"],
|
|
19
|
+
["background_tasks", "trigger_account_id cancel_requested_by"],
|
|
20
|
+
["catalog_item_org_switches", "updated_by"],
|
|
21
|
+
["catalog_item_versions", "imported_by"],
|
|
22
|
+
["catalog_items", "created_by"],
|
|
23
|
+
["catalog_org_grants", "granted_by"],
|
|
24
|
+
["catalog_requests", "decided_by org_decided_by requested_by"],
|
|
25
|
+
["channel_connections", "created_by"],
|
|
26
|
+
["channel_deliveries", "pending_thread_account_id"],
|
|
27
|
+
["channel_threads", "account_id"],
|
|
28
|
+
["component_org_switches", "updated_by"],
|
|
29
|
+
["connector_tool_account_policies", "account_id"],
|
|
30
|
+
["connectors", "created_by"],
|
|
31
|
+
["conversation_queue", "account_id"],
|
|
32
|
+
["conversations", "account_id"],
|
|
33
|
+
["deployment_settings", "updated_by"],
|
|
34
|
+
["device_defaults", "account_id"],
|
|
35
|
+
["device_machines", "account_id"],
|
|
36
|
+
["device_projects", "created_by"],
|
|
37
|
+
["devices", "owner_account_id"],
|
|
38
|
+
["directory_exclusions", "account_id created_by"],
|
|
39
|
+
["eval_cases", "added_by"],
|
|
40
|
+
["eval_results", "labeled_by claimed_by"],
|
|
41
|
+
["eval_runs", "created_by"],
|
|
42
|
+
["eval_sets", "created_by"],
|
|
43
|
+
["extension_deployment_configs", "updated_by"],
|
|
44
|
+
["extension_installs", "installed_by updated_by"],
|
|
45
|
+
["extension_org_configs", "updated_by"],
|
|
46
|
+
["extension_org_overrides", "created_by updated_by"],
|
|
47
|
+
["extension_packages", "uploaded_by"],
|
|
48
|
+
["extension_permission_snapshots", "approved_by"],
|
|
49
|
+
["file_shares", "created_by revoked_by"],
|
|
50
|
+
["files", "account_id"],
|
|
51
|
+
["identities", "account_id"],
|
|
52
|
+
["inbox_items", "account_id"],
|
|
53
|
+
["invitations", "accepted_by created_by"],
|
|
54
|
+
["local_passwords", "account_id"],
|
|
55
|
+
["marketplaces", "created_by"],
|
|
56
|
+
["memberships", "account_id"],
|
|
57
|
+
["model_budget_member_usage", "account_id"],
|
|
58
|
+
["model_disabled", "disabled_by"],
|
|
59
|
+
["model_prices", "updated_by"],
|
|
60
|
+
["model_purpose_mappings", "updated_by"],
|
|
61
|
+
["org_anonymous_accounts", "account_id"],
|
|
62
|
+
["org_extension_point_defaults", "updated_by"],
|
|
63
|
+
["org_git_push_denials", "updated_by"],
|
|
64
|
+
["org_network_allowances", "created_by"],
|
|
65
|
+
["org_notices", "dismissed_by"],
|
|
66
|
+
["org_tool_policies", "updated_by"],
|
|
67
|
+
["orgs", "created_by"],
|
|
68
|
+
["permission_call_grants", "account_id decided_by"],
|
|
69
|
+
["platform_agent_template_versions", "created_by"],
|
|
70
|
+
["platform_notices", "dismissed_by"],
|
|
71
|
+
["platform_sealed_inputs", "recipient_account_id"],
|
|
72
|
+
["platform_sealed_reveals", "recipient_account_id"],
|
|
73
|
+
["platform_skill_versions", "created_by"],
|
|
74
|
+
["platform_theme_versions", "created_by"],
|
|
75
|
+
["plugin_import_staging", "created_by"],
|
|
76
|
+
["plugin_installs", "installed_by"],
|
|
77
|
+
["push_preferences", "account_id"],
|
|
78
|
+
["push_registrations", "account_id"],
|
|
79
|
+
["questions", "answered_by"],
|
|
80
|
+
["runs", "trigger_account_id"],
|
|
81
|
+
["sandbox_operation_intents", "account_id"],
|
|
82
|
+
["sessions", "account_id"],
|
|
83
|
+
["skill_change_requests", "decided_by requested_by_account"],
|
|
84
|
+
["skill_versions", "created_by"],
|
|
85
|
+
["skills", "created_by maintainer_account_id"],
|
|
86
|
+
["tool_grants", "account_id"],
|
|
87
|
+
["trigger_cause_handles", "cause_account_id"],
|
|
88
|
+
["trigger_events", "cause_account_id"],
|
|
89
|
+
];
|
|
90
|
+
export async function accountResidual(db, accountId, anonymousOrgId) {
|
|
91
|
+
if (currentScope()?.kind !== "platform")
|
|
92
|
+
return "account-scope";
|
|
93
|
+
const drift = await orgCatalogDrift(db);
|
|
94
|
+
if (drift)
|
|
95
|
+
return drift;
|
|
96
|
+
for (const [table, fields] of direct) {
|
|
97
|
+
const comparisons = fields.split(" ").map((field) => sql `${sql.id(field)} = ${accountId}`);
|
|
98
|
+
const otherAnonymousOrg = table === "org_anonymous_accounts" && anonymousOrgId !== undefined
|
|
99
|
+
? sql `AND org_id <> ${anonymousOrgId}`
|
|
100
|
+
: sql ``;
|
|
101
|
+
const result = await sql `SELECT FROM ${sql.id("capybara", table)}
|
|
102
|
+
WHERE (${sql.join(comparisons, sql ` OR `)}) ${otherAnonymousOrg} LIMIT 1`.execute(db);
|
|
103
|
+
if (result.rows.length)
|
|
104
|
+
return `account-reference-${table}`;
|
|
105
|
+
}
|
|
106
|
+
// org-filter: deployment-wide check of per-user credential keys before removing the account
|
|
107
|
+
const credentials = await asPlatform("verify all account credential keys", () => sql `SELECT FROM capybara.secrets WHERE namespace='kernel'
|
|
108
|
+
AND left(key,16)='credentials:app:' AND right(key,length(${accountId}::text)+6)=':user:' || ${accountId}::text LIMIT 1`.execute(db));
|
|
109
|
+
return credentials.rows.length ? "account-reference-secrets" : undefined;
|
|
110
|
+
}
|
|
111
|
+
const HISTORY_PAGE_SIZE = 1024;
|
|
112
|
+
/** Appended history owned by other people is immutable and only affects the receipt. */
|
|
113
|
+
export async function accountHistoryPage(db, accountId, cursor) {
|
|
114
|
+
// org-filter: deployment-wide upper bound for the append-only history pass.
|
|
115
|
+
const end = cursor?.end ??
|
|
116
|
+
(await asPlatform("set the history pass high-water mark", () => db
|
|
117
|
+
.selectFrom("conversation_events")
|
|
118
|
+
.select(["conversation_id as conversationId", "seq"])
|
|
119
|
+
.orderBy("conversation_id", "desc")
|
|
120
|
+
.orderBy("seq", "desc")
|
|
121
|
+
.limit(1)
|
|
122
|
+
.executeTakeFirst()));
|
|
123
|
+
if (!end)
|
|
124
|
+
return { hit: false };
|
|
125
|
+
const after = cursor?.after;
|
|
126
|
+
const lower = after
|
|
127
|
+
? sql `AND (conversation_id, seq) > (${after.conversationId}, ${after.seq}::bigint)`
|
|
128
|
+
: sql ``;
|
|
129
|
+
// org-filter: deployment-wide reporting of a scope boundary in surviving append-only history.
|
|
130
|
+
const page = await asPlatform("look for mentions in surviving conversation history", () => sql `
|
|
131
|
+
WITH page AS MATERIALIZED (
|
|
132
|
+
SELECT conversation_id, seq, payload FROM capybara.conversation_events
|
|
133
|
+
WHERE (conversation_id, seq) <= (${end.conversationId}, ${end.seq}::bigint) ${lower}
|
|
134
|
+
ORDER BY conversation_id, seq LIMIT ${HISTORY_PAGE_SIZE}
|
|
135
|
+
)
|
|
136
|
+
SELECT conversation_id AS "conversationId", seq,
|
|
137
|
+
jsonb_path_exists(payload, '$.** ? (@ == $account)',
|
|
138
|
+
jsonb_build_object('account', ${accountId}::text)) AS hit
|
|
139
|
+
FROM page ORDER BY conversation_id, seq`.execute(db));
|
|
140
|
+
if (page.rows.some(({ hit }) => hit))
|
|
141
|
+
return { hit: true };
|
|
142
|
+
const last = page.rows.at(-1);
|
|
143
|
+
if (page.rows.length < HISTORY_PAGE_SIZE || !last)
|
|
144
|
+
return { hit: false };
|
|
145
|
+
return {
|
|
146
|
+
hit: false,
|
|
147
|
+
next: { end, after: { conversationId: last.conversationId, seq: last.seq } },
|
|
148
|
+
};
|
|
149
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ConversationsService } from "../conversations/conversations.service.js";
|
|
2
|
+
import { BlobObjects } from "../files/blob-objects.js";
|
|
3
|
+
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
4
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
5
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
6
|
+
import { OrgLock } from "../orgs/org-lock.js";
|
|
7
|
+
import { StorageService } from "../storage/storage.service.js";
|
|
8
|
+
import { AccountDeviceCleanup } from "./device-cleanup.js";
|
|
9
|
+
import { ErasureRequests } from "./requests.js";
|
|
10
|
+
export declare class AccountErasure {
|
|
11
|
+
private readonly db;
|
|
12
|
+
readonly txHost: TransactionHost;
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly lock;
|
|
15
|
+
private readonly requests;
|
|
16
|
+
private readonly conversations;
|
|
17
|
+
private readonly storage;
|
|
18
|
+
private readonly objects;
|
|
19
|
+
private readonly devices;
|
|
20
|
+
constructor(db: KernelDb, txHost: TransactionHost, config: KernelConfigService, lock: OrgLock, requests: ErasureRequests, conversations: ConversationsService, storage: StorageService, objects: BlobObjects, devices: AccountDeviceCleanup);
|
|
21
|
+
run(id: string, generation: number): Promise<void>;
|
|
22
|
+
private enabled;
|
|
23
|
+
private defer;
|
|
24
|
+
private prepareHistory;
|
|
25
|
+
private runStage;
|
|
26
|
+
private externalEvidence;
|
|
27
|
+
private orgBatch;
|
|
28
|
+
private fileResult;
|
|
29
|
+
private avatarResult;
|
|
30
|
+
private clearFinalRows;
|
|
31
|
+
private finishAccount;
|
|
32
|
+
}
|