@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.
Files changed (174) hide show
  1. package/dist/admin/account-erasures.d.ts +37 -0
  2. package/dist/admin/account-erasures.js +119 -0
  3. package/dist/admin/admin.controller.d.ts +35 -2
  4. package/dist/admin/admin.controller.js +75 -18
  5. package/dist/admin/admin.module.js +10 -0
  6. package/dist/admin/admin.schemas.d.ts +51 -0
  7. package/dist/admin/admin.schemas.js +20 -0
  8. package/dist/admin/erasure-dispositions.d.ts +23 -0
  9. package/dist/admin/erasure-dispositions.js +76 -0
  10. package/dist/admin/health.service.d.ts +6 -0
  11. package/dist/admin/health.service.js +27 -1
  12. package/dist/admin/usage.service.d.ts +5 -0
  13. package/dist/admin/usage.service.js +21 -2
  14. package/dist/agents/agents.service.d.ts +1 -1
  15. package/dist/channels/connections.service.d.ts +1 -1
  16. package/dist/client/client-reports.service.js +12 -0
  17. package/dist/config/schema.d.ts +8 -1
  18. package/dist/config/schema.js +43 -1
  19. package/dist/conversations/conversations.service.d.ts +9 -1
  20. package/dist/conversations/conversations.service.js +112 -15
  21. package/dist/conversations/event-log.d.ts +2 -0
  22. package/dist/conversations/event-log.js +77 -0
  23. package/dist/conversations/events.d.ts +11 -0
  24. package/dist/conversations/events.js +13 -1
  25. package/dist/conversations/prompt-suggestions.js +4 -0
  26. package/dist/conversations/titles.js +3 -2
  27. package/dist/db/kernel-schema.d.ts +71 -8
  28. package/dist/devices/devices.service.d.ts +13 -1
  29. package/dist/devices/devices.service.js +122 -9
  30. package/dist/erasure/account-batches.d.ts +9 -0
  31. package/dist/erasure/account-batches.js +567 -0
  32. package/dist/erasure/account-files.d.ts +34 -0
  33. package/dist/erasure/account-files.js +217 -0
  34. package/dist/erasure/account-residual.d.ts +15 -0
  35. package/dist/erasure/account-residual.js +149 -0
  36. package/dist/erasure/account-worker.d.ts +32 -0
  37. package/dist/erasure/account-worker.js +392 -0
  38. package/dist/erasure/device-cleanup.d.ts +23 -0
  39. package/dist/erasure/device-cleanup.js +221 -0
  40. package/dist/erasure/erasure.module.d.ts +2 -0
  41. package/dist/erasure/erasure.module.js +14 -0
  42. package/dist/erasure/external-evidence.d.ts +4 -0
  43. package/dist/erasure/external-evidence.js +28 -0
  44. package/dist/erasure/jobs.d.ts +5 -0
  45. package/dist/erasure/jobs.js +20 -0
  46. package/dist/erasure/org-batches.d.ts +11 -0
  47. package/dist/erasure/org-batches.js +159 -0
  48. package/dist/erasure/org-catalog.d.ts +2 -0
  49. package/dist/erasure/org-catalog.js +34 -0
  50. package/dist/erasure/org-rules.d.ts +14 -0
  51. package/dist/erasure/org-rules.js +227 -0
  52. package/dist/erasure/org-worker.d.ts +38 -0
  53. package/dist/erasure/org-worker.js +501 -0
  54. package/dist/erasure/requests.d.ts +61 -0
  55. package/dist/erasure/requests.js +180 -0
  56. package/dist/erasure/sweep.job.d.ts +19 -0
  57. package/dist/erasure/sweep.job.js +130 -0
  58. package/dist/erasure/worker.d.ts +17 -0
  59. package/dist/erasure/worker.js +110 -0
  60. package/dist/errors.d.ts +4 -0
  61. package/dist/errors.js +4 -0
  62. package/dist/evals/judge.d.ts +5 -0
  63. package/dist/evals/judge.js +16 -1
  64. package/dist/evals/run-replay.js +12 -1
  65. package/dist/evals/step-replay.d.ts +5 -3
  66. package/dist/evals/step-replay.js +9 -5
  67. package/dist/evals/worker.d.ts +2 -0
  68. package/dist/evals/worker.js +60 -16
  69. package/dist/files/blob-objects.d.ts +7 -3
  70. package/dist/files/blob-objects.js +39 -22
  71. package/dist/files/blob-sweep.job.d.ts +11 -0
  72. package/dist/files/blob-sweep.job.js +67 -6
  73. package/dist/http/api.module.js +5 -1
  74. package/dist/identity/accounts.js +3 -0
  75. package/dist/identity/api-tokens.js +5 -0
  76. package/dist/identity/auth.guard.d.ts +1 -0
  77. package/dist/identity/auth.guard.js +1 -0
  78. package/dist/identity/erasure-admission.d.ts +3 -0
  79. package/dist/identity/erasure-admission.js +14 -0
  80. package/dist/identity/external.js +5 -0
  81. package/dist/identity/sessions.js +5 -0
  82. package/dist/identity/settings.d.ts +86 -1
  83. package/dist/identity/settings.js +38 -4
  84. package/dist/infra/config/kernel-config.service.js +1 -0
  85. package/dist/infra/metrics/kernel-metrics.d.ts +18 -0
  86. package/dist/infra/metrics/kernel-metrics.js +54 -0
  87. package/dist/infra/metrics/operational-metrics.d.ts +31 -0
  88. package/dist/infra/metrics/operational-metrics.js +188 -0
  89. package/dist/infra/outbox/registry.d.ts +23 -0
  90. package/dist/infra/outbox/registry.js +12 -0
  91. package/dist/infra/throttler/credential-throttler.js +8 -1
  92. package/dist/infra/throttler/named-throttlers.d.ts +6 -0
  93. package/dist/infra/throttler/named-throttlers.js +6 -0
  94. package/dist/infra/throttler/principal-throttler.guard.d.ts +10 -0
  95. package/dist/infra/throttler/principal-throttler.guard.js +50 -0
  96. package/dist/infra/throttler/redis-throttler-storage.d.ts +1 -0
  97. package/dist/infra/throttler/redis-throttler-storage.js +27 -5
  98. package/dist/infra/throttler/throttler.guard.d.ts +2 -3
  99. package/dist/infra/throttler/throttler.guard.js +2 -3
  100. package/dist/infra/throttler/throttler.module.js +3 -2
  101. package/dist/models/budget-admission.d.ts +31 -0
  102. package/dist/models/budget-admission.js +149 -0
  103. package/dist/models/budget-counters.d.ts +28 -0
  104. package/dist/models/budget-counters.js +117 -0
  105. package/dist/models/budget-policy.d.ts +88 -0
  106. package/dist/models/budget-policy.js +78 -0
  107. package/dist/models/embeddings.js +13 -2
  108. package/dist/models/images.js +12 -2
  109. package/dist/models/purposes.d.ts +5 -2
  110. package/dist/models/purposes.js +9 -5
  111. package/dist/orgs/invitations.service.d.ts +1 -1
  112. package/dist/orgs/members.service.d.ts +28 -1
  113. package/dist/orgs/members.service.js +74 -11
  114. package/dist/orgs/offboarding.js +4 -6
  115. package/dist/orgs/org-owner.d.ts +2 -0
  116. package/dist/orgs/org-owner.js +21 -6
  117. package/dist/orgs/orgs.controller.d.ts +22 -0
  118. package/dist/orgs/orgs.controller.js +24 -0
  119. package/dist/orgs/orgs.module.js +4 -0
  120. package/dist/orgs/orgs.schemas.js +7 -0
  121. package/dist/orgs/orgs.service.d.ts +5 -1
  122. package/dist/orgs/orgs.service.js +28 -3
  123. package/dist/orgs/settings.controller.js +6 -1
  124. package/dist/orgs/settings.service.d.ts +58 -1
  125. package/dist/orgs/settings.service.js +74 -9
  126. package/dist/platform/exclusions.js +20 -1
  127. package/dist/platform/ops/accounts.ops.js +58 -1
  128. package/dist/platform/ops/admin.ops.js +13 -5
  129. package/dist/platform/ops/org-settings.ops.js +11 -4
  130. package/dist/platform/ops/orgs.ops.js +28 -0
  131. package/dist/platform/ops/retention.d.ts +16 -0
  132. package/dist/platform/ops/retention.js +21 -0
  133. package/dist/retention/jobs.d.ts +21 -0
  134. package/dist/retention/jobs.js +48 -0
  135. package/dist/retention/policy.d.ts +70 -0
  136. package/dist/retention/policy.js +103 -0
  137. package/dist/retention/purge-batch.d.ts +17 -0
  138. package/dist/retention/purge-batch.js +355 -0
  139. package/dist/retention/report-obligations.d.ts +12 -0
  140. package/dist/retention/report-obligations.js +71 -0
  141. package/dist/retention/sweep.job.d.ts +17 -0
  142. package/dist/retention/sweep.job.js +95 -0
  143. package/dist/retention/worker.d.ts +29 -0
  144. package/dist/retention/worker.js +306 -0
  145. package/dist/roles/worker.module.js +12 -2
  146. package/dist/runtime/maintenance.d.ts +5 -1
  147. package/dist/runtime/maintenance.js +20 -2
  148. package/dist/runtime/model-call.d.ts +3 -1
  149. package/dist/runtime/model-call.js +25 -3
  150. package/dist/runtime/providers.d.ts +7 -2
  151. package/dist/runtime/providers.js +76 -10
  152. package/dist/runtime/quota.d.ts +12 -12
  153. package/dist/runtime/quota.js +42 -37
  154. package/dist/runtime/run-finish.js +2 -0
  155. package/dist/runtime/run-principal.d.ts +9 -0
  156. package/dist/runtime/run-principal.js +18 -0
  157. package/dist/runtime/run-worker.js +48 -10
  158. package/dist/runtime/usage-report-jobs.d.ts +4 -1
  159. package/dist/runtime/usage-report-jobs.js +36 -13
  160. package/dist/runtime/usage-report.d.ts +5 -3
  161. package/dist/runtime/usage-report.js +37 -19
  162. package/dist/sandbox/review/classifier.d.ts +2 -2
  163. package/dist/sandbox/review/classifier.js +6 -2
  164. package/dist/sandbox/sandbox.service.js +18 -7
  165. package/dist/storage/purge.js +5 -1
  166. package/dist/storage/storage.service.d.ts +17 -1
  167. package/dist/storage/storage.service.js +75 -4
  168. package/dist/tools/builtins/vision/read-image.js +3 -0
  169. package/dist/tools/builtins/web/extract.js +3 -0
  170. package/dist/tools/pipeline.js +5 -1
  171. package/package.json +3 -1
  172. package/src/migrations/0216_model_budget_counters.sql +24 -0
  173. package/src/migrations/0217_retention.sql +44 -0
  174. package/src/migrations/0218_data_erasure.sql +41 -0
@@ -83,6 +83,7 @@ export interface SandboxOperationIntentsTable {
83
83
  updated_at: Ts;
84
84
  }
85
85
  export interface AccountsTable {
86
+ erasure_requested: Generated<boolean>;
86
87
  id: string;
87
88
  display_name: string;
88
89
  email: string | null;
@@ -239,7 +240,7 @@ export interface InvitationsTable {
239
240
  org_id: string;
240
241
  role: "admin" | "member" | "guest";
241
242
  token_hash: string;
242
- created_by: string;
243
+ created_by: string | null;
243
244
  created_at: Ts;
244
245
  expires_at: ColumnType<Date, Date | string, Date | string>;
245
246
  accepted_at: TsNullable;
@@ -268,7 +269,7 @@ export interface ApiTokensTable {
268
269
  role: "admin" | "member" | "guest" | null;
269
270
  name: string;
270
271
  token_hash: string;
271
- created_by: string;
272
+ created_by: string | null;
272
273
  created_at: Ts;
273
274
  last_used_at: TsNullable;
274
275
  expires_at: TsNullable;
@@ -380,7 +381,7 @@ export interface AgentVersionsTable {
380
381
  agent_id: string;
381
382
  version: number;
382
383
  definition: Json;
383
- created_by: string;
384
+ created_by: string | null;
384
385
  created_at: Ts;
385
386
  }
386
387
  /** 0005_agents.sql:visibility='members' 的 agent 名单。 */
@@ -884,7 +885,7 @@ export interface ChannelConnectionsTable {
884
885
  * `{ guideId, version, hash, mode }`; null when unknown.
885
886
  */
886
887
  guide_ack: ColumnType<GuideAckJson | null, string | null | undefined, string | null>;
887
- created_by: string;
888
+ created_by: string | null;
888
889
  created_at: Ts;
889
890
  updated_at: Ts;
890
891
  deleted_at: TsNullable;
@@ -1137,6 +1138,7 @@ type InstanceHandle = ColumnType<{
1137
1138
  * instance plus a kept file system; a dial-in device's machine signs in (device_machines).
1138
1139
  */
1139
1140
  export interface DevicesTable {
1141
+ files_destroyed_at: TsNullable;
1140
1142
  id: string;
1141
1143
  org_id: string;
1142
1144
  owner_kind: "personal";
@@ -1234,7 +1236,7 @@ export interface DeviceProjectsTable {
1234
1236
  name: string;
1235
1237
  /** The project's absolute path on the device. */
1236
1238
  path: string;
1237
- created_by: string;
1239
+ created_by: string | null;
1238
1240
  created_at: Ts;
1239
1241
  }
1240
1242
  /**
@@ -1340,7 +1342,7 @@ export interface EvalSetsTable {
1340
1342
  name: string;
1341
1343
  description: ColumnType<string, string | undefined, string>;
1342
1344
  rubric: string | null;
1343
- created_by: string;
1345
+ created_by: string | null;
1344
1346
  created_at: Ts;
1345
1347
  updated_at: Ts;
1346
1348
  deleted_at: TsNullable;
@@ -1362,7 +1364,7 @@ export interface EvalCasesTable {
1362
1364
  plan_run: Json<unknown>;
1363
1365
  /** 用例原版本的模型(`ModelRef`);没有定义时为 null。 */
1364
1366
  base_model: ColumnType<unknown | null, string | null | undefined, string | null>;
1365
- added_by: string;
1367
+ added_by: string | null;
1366
1368
  created_at: Ts;
1367
1369
  }
1368
1370
  export type EvalRunStatus = "queued" | "running" | "completed" | "cancelled" | "capped" | "failed";
@@ -1382,7 +1384,7 @@ export interface EvalRunsTable {
1382
1384
  consecutive_errors: ColumnType<number, number | undefined, number>;
1383
1385
  error: ColumnType<unknown | null, string | null | undefined, string | null>;
1384
1386
  estimate: Json<unknown>;
1385
- created_by: string;
1387
+ created_by: string | null;
1386
1388
  created_at: Ts;
1387
1389
  started_at: TsNullable;
1388
1390
  finished_at: TsNullable;
@@ -1525,6 +1527,24 @@ export interface ModelDisabledTable {
1525
1527
  disabled_by: string | null;
1526
1528
  disabled_at: Ts;
1527
1529
  }
1530
+ export interface ModelBudgetUsageTable {
1531
+ org_id: string;
1532
+ period_start: string;
1533
+ bucket: string;
1534
+ tokens: string;
1535
+ amount: string;
1536
+ }
1537
+ export interface ModelBudgetOrgUsageTable extends ModelBudgetUsageTable {
1538
+ shard: number;
1539
+ }
1540
+ export interface ModelBudgetMemberUsageTable extends ModelBudgetUsageTable {
1541
+ account_id: string;
1542
+ }
1543
+ export interface AppliedMigrationTable {
1544
+ name: string;
1545
+ checksum: string;
1546
+ applied_at: Ts;
1547
+ }
1528
1548
  /** One model call outside a conversation, e.g. an extension's embed call (0089, spec B §5.4). */
1529
1549
  export interface ModelUsageTable {
1530
1550
  id: Generated<string>;
@@ -1921,7 +1941,47 @@ export interface PermissionConfigPublicationTable {
1921
1941
  projection: Json<PermissionConfigProjection>;
1922
1942
  updated_at: Ts;
1923
1943
  }
1944
+ export interface UsageReportObligationsTable {
1945
+ org_id: string;
1946
+ job_id: string;
1947
+ }
1948
+ export interface DataErasureRequestsTable {
1949
+ id: Generated<string>;
1950
+ kind: "org" | "account";
1951
+ org_id: string | null;
1952
+ target_id: string | null;
1953
+ generation: Generated<number>;
1954
+ stage: Generated<"pending" | "blocked" | "roots" | "objects" | "rows" | "prefix" | "late" | "account-orgs" | "account-external" | "account-final" | "done">;
1955
+ scope_boundaries: Generated<"third-party-conversation-content"[]>;
1956
+ progress: ColumnType<Record<string, unknown> | null, string | null | undefined, string | null>;
1957
+ blocked_reason: NullableCol<string>;
1958
+ due_at: Ts;
1959
+ completed_at: TsNullable;
1960
+ }
1961
+ export interface RetentionWorkTable {
1962
+ id: Generated<string>;
1963
+ org_id: string | null;
1964
+ kind: "purge" | "org-scan" | "platform-audit";
1965
+ target: string;
1966
+ generation: Generated<number>;
1967
+ state: Generated<"ready" | "blocked" | "done">;
1968
+ cursor_id: NullableText;
1969
+ usage_cursor: NullableText;
1970
+ blocked_reason: NullableText;
1971
+ due_at: Ts;
1972
+ }
1973
+ export interface RetentionCoverageTable {
1974
+ org_id: string;
1975
+ usage_incomplete_since: TsNullable;
1976
+ runs_incomplete_since: TsNullable;
1977
+ usage_incomplete_through: TsNullable;
1978
+ runs_incomplete_through: TsNullable;
1979
+ }
1924
1980
  export interface KernelDatabase {
1981
+ data_erasure_requests: DataErasureRequestsTable;
1982
+ retention_work: RetentionWorkTable;
1983
+ usage_report_obligations: UsageReportObligationsTable;
1984
+ retention_coverage: RetentionCoverageTable;
1925
1985
  permission_authority_epochs: PermissionAuthorityEpochsTable;
1926
1986
  accounts: AccountsTable;
1927
1987
  account_avatar_uploads: AccountAvatarUploadsTable;
@@ -2022,6 +2082,9 @@ export interface KernelDatabase {
2022
2082
  model_purpose_mappings: ModelPurposeMappingsTable;
2023
2083
  model_disabled: ModelDisabledTable;
2024
2084
  model_usage: ModelUsageTable;
2085
+ model_budget_org_usage: ModelBudgetOrgUsageTable;
2086
+ model_budget_member_usage: ModelBudgetMemberUsageTable;
2087
+ _migrations: AppliedMigrationTable;
2025
2088
  extension_packages: ExtensionPackagesTable;
2026
2089
  extension_blobs: ExtensionBlobsTable;
2027
2090
  extension_installs: ExtensionInstallsTable;
@@ -226,7 +226,7 @@ export declare class DevicesService {
226
226
  /** Whether a dial-in device's machine is connected now. */
227
227
  relay: Pick<DialInRelay, "online">,
228
228
  /** Each device's provider (whether it serves its devices over the dial-in channel). */
229
- providers: Pick<DeviceProviders, "forDevice">, machines: Pick<DialInMachines, "revokeActive">, ceilings: PermissionCeilingWrites, facts: Pick<PermissionFacts, "deviceSandbox" | "deviceSandboxFacts">, displayNames: Pick<ExtensionDisplayNames, "many">,
229
+ providers: Pick<DeviceProviders, "forDevice">, machines: Pick<DialInMachines, "revokeActive" | "revokeMachine">, ceilings: PermissionCeilingWrites, facts: Pick<PermissionFacts, "deviceSandbox" | "deviceSandboxFacts">, displayNames: Pick<ExtensionDisplayNames, "many">,
230
230
  /** Each device's stored provider report (read only; never asks the provider). */
231
231
  reports: Pick<DeviceReports, "read">,
232
232
  /** The dial-in providers on in an org (a device signed in from a machine). */
@@ -403,6 +403,18 @@ export declare class DevicesService {
403
403
  /** The prelude of {@link remove}: the membership, the device row, the device's write lock. */
404
404
  private lockRemovalSubject;
405
405
  private removeIn;
406
+ /** The erasure worker already holds the request and org fence. Former members have no
407
+ * membership to lock, so verify the disabled account and ownership on the locked row. */
408
+ retireForErasure(orgId: string, accountId: string, id: string): Promise<void>;
409
+ /** Close one remaining enrollment under the deleted org's recovery fence. */
410
+ revokeOrgMachine(orgId: string): Promise<boolean>;
411
+ /** A historical enrollment may name the erased account on a different owner's device.
412
+ * Revoke that credential by machine id without stopping or removing the surviving device. */
413
+ eraseHistoricalMachine(orgId: string, accountId: string): Promise<boolean>;
414
+ /** Recreate only the deterministic destruction intent when there are no handles to use.
415
+ * A device with an unknown historical provider needs independent reconciliation. */
416
+ oweErasureVerification(orgId: string, id: string): Promise<string | undefined>;
417
+ private retireLocked;
406
418
  /**
407
419
  * Starts one of the person's devices over with empty files (after
408
420
  * `device.files_lost`): records the request (audit), then, after the commit, enqueues the
@@ -658,11 +658,120 @@ let DevicesService = class DevicesService {
658
658
  async removeIn(orgId, accountId, id, actorTokenId, auditDetail) {
659
659
  // held since the prelude (the row and the lock are the same; naming them again re-checks)
660
660
  const before = await this.lockOwn(orgId, accountId, id);
661
+ await this.retireLocked(orgId, accountId, id);
662
+ await audit(this.db.db, {
663
+ actorAccountId: accountId,
664
+ ...(actorTokenId !== undefined && { actorTokenId }),
665
+ orgId,
666
+ action: "device.deleted",
667
+ target: id,
668
+ detail: { name: before.name, isDefault: before.is_default, ...auditDetail },
669
+ });
670
+ }
671
+ /** The erasure worker already holds the request and org fence. Former members have no
672
+ * membership to lock, so verify the disabled account and ownership on the locked row. */
673
+ async retireForErasure(orgId, accountId, id) {
674
+ await this.db
675
+ .selectFrom("devices as d")
676
+ .innerJoin("accounts as a", "a.id", "d.owner_account_id")
677
+ .select("d.id")
678
+ .where("d.id", "=", id)
679
+ .where("d.org_id", "=", orgId)
680
+ .where("d.owner_kind", "=", "personal")
681
+ .where("d.owner_account_id", "=", accountId)
682
+ .where("d.deleted_at", "is", null)
683
+ .where("a.status", "=", "disabled")
684
+ .where("a.erasure_requested", "=", true)
685
+ .forUpdate("d")
686
+ .executeTakeFirstOrThrow();
687
+ await this.retireLocked(orgId, accountId, id);
688
+ }
689
+ /** Close one remaining enrollment under the deleted org's recovery fence. */
690
+ async revokeOrgMachine(orgId) {
691
+ const machine = await this.db
692
+ .selectFrom("device_machines as m")
693
+ .innerJoin("devices as d", "d.id", "m.device_id")
694
+ .innerJoin("orgs as o", "o.id", "d.org_id")
695
+ .select("m.device_id")
696
+ .where("m.org_id", "=", orgId)
697
+ .where("d.org_id", "=", orgId)
698
+ .where("o.deleted_at", "is not", null)
699
+ .where("m.revoked_at", "is", null)
700
+ .orderBy("m.id")
701
+ .limit(1)
702
+ .forUpdate("d")
703
+ .executeTakeFirst();
704
+ if (!machine)
705
+ return false;
706
+ await this.machines.revokeActive(machine.device_id, "deleted");
707
+ return true;
708
+ }
709
+ /** A historical enrollment may name the erased account on a different owner's device.
710
+ * Revoke that credential by machine id without stopping or removing the surviving device. */
711
+ async eraseHistoricalMachine(orgId, accountId) {
712
+ const machine = await this.db
713
+ .selectFrom("device_machines as m")
714
+ .innerJoin("devices as d", "d.id", "m.device_id")
715
+ .innerJoin("accounts as a", "a.id", "m.account_id")
716
+ .select(["m.id", "m.revoked_at"])
717
+ .where("m.org_id", "=", orgId)
718
+ .where("d.org_id", "=", orgId)
719
+ .where("a.id", "=", accountId)
720
+ .where("a.status", "=", "disabled")
721
+ .where("a.erasure_requested", "=", true)
722
+ .where("d.owner_account_id", "!=", accountId)
723
+ .orderBy("m.id")
724
+ .limit(1)
725
+ .forUpdate("d")
726
+ .executeTakeFirst();
727
+ if (!machine)
728
+ return false;
729
+ if (!machine.revoked_at) {
730
+ await this.machines.revokeMachine(machine.id);
731
+ }
732
+ else {
733
+ await this.db
734
+ .deleteFrom("device_machines")
735
+ .where("org_id", "=", orgId)
736
+ .where("account_id", "=", accountId)
737
+ .where("id", "=", machine.id)
738
+ .where("revoked_at", "is not", null)
739
+ .execute();
740
+ }
741
+ return true;
742
+ }
743
+ /** Recreate only the deterministic destruction intent when there are no handles to use.
744
+ * A device with an unknown historical provider needs independent reconciliation. */
745
+ async oweErasureVerification(orgId, id) {
746
+ const device = await this.db
747
+ .selectFrom("devices as d")
748
+ .innerJoin("orgs as o", "o.id", "d.org_id")
749
+ .select(["d.provider", "d.demoted_from_provider"])
750
+ .where("d.id", "=", id)
751
+ .where("d.org_id", "=", orgId)
752
+ .where("d.files_destroyed_at", "is", null)
753
+ .where((eb) => eb.or([eb("d.deleted_at", "is not", null), eb("o.deleted_at", "is not", null)]))
754
+ .forUpdate("d")
755
+ .executeTakeFirstOrThrow();
756
+ const provider = this.providers.forDevice(device.provider);
757
+ if (!provider || device.demoted_from_provider)
758
+ return "device-provider-unavailable";
759
+ if (servesDialIn(provider))
760
+ return "device-local-destruction-unverified";
761
+ await this.db
762
+ .insertInto("device_instances")
763
+ .values({ device_id: id, org_id: orgId, provider: provider.plugin, status: "creating" })
764
+ .onConflict((conflict) => conflict.column("device_id").doNothing())
765
+ .execute();
766
+ await oweDeviceDestroy(this.outbox, id, orgId);
767
+ return undefined;
768
+ }
769
+ async retireLocked(orgId, accountId, id) {
661
770
  await this.ceilings.lock({ orgId, deviceId: id }, "device");
662
771
  await this.ceilings.publishLocked({ orgId, deviceId: id }, "device", true, true);
663
772
  await this.db
664
773
  .updateTable("devices")
665
- .set({ deleted_at: sql `now()`, updated_at: sql `now()` })
774
+ .set({ deleted_at: sql `now()`, updated_at: sql `now()`, files_destroyed_at: null })
666
775
  .where("id", "=", id)
667
776
  .where("org_id", "=", orgId)
668
777
  .execute();
@@ -676,14 +785,6 @@ let DevicesService = class DevicesService {
676
785
  // its loopback port grants end with it (browser loopback port grants design §4)
677
786
  await revokeLoopbackGrantsOfDevice(this.db.db, this.outbox, { orgId, deviceId: id });
678
787
  await revokeSiteGrantsOfDevice(this.db.db, { orgId, deviceId: id });
679
- await audit(this.db.db, {
680
- actorAccountId: accountId,
681
- ...(actorTokenId !== undefined && { actorTokenId }),
682
- orgId,
683
- action: "device.deleted",
684
- target: id,
685
- detail: { name: before.name, isDefault: before.is_default, ...auditDetail },
686
- });
687
788
  await oweDeviceDestroy(this.outbox, id, orgId);
688
789
  }
689
790
  /**
@@ -1062,6 +1163,18 @@ __decorate([
1062
1163
  __decorate([
1063
1164
  Transactional()
1064
1165
  ], DevicesService.prototype, "removeIn", null);
1166
+ __decorate([
1167
+ Transactional({ propagation: "mandatory" })
1168
+ ], DevicesService.prototype, "retireForErasure", null);
1169
+ __decorate([
1170
+ Transactional({ propagation: "mandatory" })
1171
+ ], DevicesService.prototype, "revokeOrgMachine", null);
1172
+ __decorate([
1173
+ Transactional({ propagation: "mandatory" })
1174
+ ], DevicesService.prototype, "eraseHistoricalMachine", null);
1175
+ __decorate([
1176
+ Transactional({ propagation: "mandatory" })
1177
+ ], DevicesService.prototype, "oweErasureVerification", null);
1065
1178
  __decorate([
1066
1179
  Transactional()
1067
1180
  ], DevicesService.prototype, "recordReset", null);
@@ -0,0 +1,9 @@
1
+ import type { KernelSchemaDb } from "../db/kernel-schema.js";
2
+ type Result = {
3
+ removed: number;
4
+ blocked?: string;
5
+ };
6
+ export declare function accountRootBlocker(db: KernelSchemaDb, orgId: string, accountId: string): Promise<"account-scope" | "private-source-eval" | "shared-conversation-tree" | undefined>;
7
+ export declare function accountRootBatch(db: KernelSchemaDb, orgId: string, accountId: string): Promise<Result>;
8
+ export declare function accountErasureBatch(db: KernelSchemaDb, accountId: string, orgId: string | null): Promise<Result>;
9
+ export { accountResidual } from "./account-residual.js";