@capybara-hq/kernel 0.1.0-rc.131 → 0.1.0-rc.133
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/bootstrap.json +5 -5
- package/dist/admin/admin.module.js +3 -0
- package/dist/admin/admin.schemas.d.ts +4 -0
- package/dist/admin/health.service.d.ts +2 -2
- package/dist/admin/health.service.js +6 -2
- package/dist/admin/setup.service.js +6 -1
- package/dist/agents/agents.controller.js +3 -2
- package/dist/agents/agents.service.d.ts +2 -0
- package/dist/agents/agents.service.js +7 -0
- package/dist/catalog/catalog.service.d.ts +2 -0
- package/dist/catalog/catalog.service.js +28 -5
- package/dist/catalog/marketplace-fetch.d.ts +2 -0
- package/dist/catalog/marketplace-fetch.js +15 -0
- package/dist/catalog/marketplaces.service.d.ts +4 -4
- package/dist/catalog/marketplaces.service.js +3 -2
- package/dist/catalog/owned-items.d.ts +2 -0
- package/dist/catalog/owned-items.js +30 -31
- package/dist/catalog/ownership.js +40 -38
- package/dist/catalog/plugin-import.d.ts +6 -5
- package/dist/catalog/plugin-import.js +69 -16
- package/dist/catalog/plugin-upgrade.d.ts +8 -0
- package/dist/catalog/plugin-upgrade.js +95 -7
- package/dist/catalog/publisher-policy.d.ts +9 -0
- package/dist/catalog/publisher-policy.js +12 -0
- package/dist/catalog/unshare.js +66 -58
- package/dist/client/client-access.service.js +5 -3
- package/dist/config/schema.d.ts +1 -0
- package/dist/config/schema.js +9 -1
- package/dist/conversations/conversations.controller.js +4 -14
- package/dist/conversations/conversations.service.d.ts +14 -0
- package/dist/conversations/conversations.service.js +20 -2
- package/dist/ctx/create-ctx.d.ts +14 -4
- package/dist/ctx/create-ctx.js +131 -59
- package/dist/ctx/queue.js +7 -4
- package/dist/db/extension-db.d.ts +30 -0
- package/dist/db/extension-db.js +91 -0
- package/dist/db/extension-rls-predicate.d.ts +1 -0
- package/dist/db/extension-rls-predicate.js +8 -0
- package/dist/db/extension-schema-transfer.d.ts +6 -0
- package/dist/db/extension-schema-transfer.js +141 -0
- package/dist/db/kernel-schema.d.ts +50 -4
- package/dist/db/migrator.d.ts +6 -0
- package/dist/db/migrator.js +35 -7
- package/dist/db/pool.d.ts +6 -2
- package/dist/db/pool.js +37 -2
- package/dist/db/sql-statements.d.ts +3 -0
- package/dist/db/sql-statements.js +15 -5
- package/dist/device-access/device-access.module.js +12 -2
- package/dist/device-access/device-access.service.d.ts +30 -2
- package/dist/device-access/device-access.service.js +38 -4
- package/dist/device-access/host-gate.d.ts +45 -0
- package/dist/device-access/host-gate.js +92 -0
- package/dist/device-access/host-service-stop-sweep.d.ts +33 -0
- package/dist/device-access/host-service-stop-sweep.js +112 -0
- package/dist/device-access/host-service-stop.d.ts +78 -0
- package/dist/device-access/host-service-stop.js +144 -0
- package/dist/device-access/host-services.d.ts +211 -0
- package/dist/device-access/host-services.js +803 -0
- package/dist/device-access/person-handles.d.ts +10 -0
- package/dist/device-access/person-handles.js +25 -0
- package/dist/device-access/service-events.d.ts +199 -0
- package/dist/device-access/service-events.js +704 -0
- package/dist/device-access/token.d.ts +5 -0
- package/dist/device-access/token.js +5 -0
- package/dist/devices/devices.service.js +3 -0
- package/dist/dial-in/access.d.ts +5 -5
- package/dist/dial-in/access.js +19 -12
- package/dist/dial-in/gateway.js +6 -2
- package/dist/dial-in/machines.d.ts +9 -2
- package/dist/dial-in/machines.js +27 -4
- package/dist/erasure/device-cleanup.js +12 -0
- package/dist/erasure/org-catalog.js +2 -2
- package/dist/erasure/org-rules.js +9 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +2 -0
- package/dist/extensions/admin-ops.d.ts +3 -1
- package/dist/extensions/admin-ops.js +19 -9
- package/dist/extensions/deployed-snapshots.d.ts +4 -1
- package/dist/extensions/deployed-snapshots.js +7 -2
- package/dist/extensions/extensions.module.js +3 -0
- package/dist/extensions/host.d.ts +11 -3
- package/dist/extensions/host.js +40 -8
- package/dist/extensions/instance.d.ts +6 -0
- package/dist/extensions/instance.js +7 -1
- package/dist/extensions/ledger.d.ts +1 -1
- package/dist/extensions/lifecycle-hooks.js +3 -3
- package/dist/extensions/migrate-lock.d.ts +4 -1
- package/dist/extensions/migrate-lock.js +19 -2
- package/dist/extensions/org-config.js +6 -1
- package/dist/extensions/permission-set.d.ts +7 -0
- package/dist/extensions/permission-set.js +10 -0
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +10 -1
- package/dist/git/tickets.js +32 -27
- package/dist/http/app.js +5 -3
- package/dist/identity/accounts.d.ts +6 -1
- package/dist/identity/accounts.js +11 -2
- package/dist/identity/api-tokens.js +7 -6
- package/dist/identity/settings.d.ts +9 -1
- package/dist/identity/settings.js +16 -1
- package/dist/inbox/org-notices.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/infra/database/scoped-dialect.d.ts +3 -2
- package/dist/infra/database/scoped-dialect.js +260 -72
- package/dist/infra/database/startup-activation-transaction.d.ts +2 -0
- package/dist/infra/database/startup-activation-transaction.js +3 -2
- package/dist/infra/outbox/registry.d.ts +28 -14
- package/dist/infra/outbox/registry.js +30 -15
- package/dist/infra/queue/queue-names.d.ts +1 -1
- package/dist/infra/queue/queue-names.js +1 -0
- package/dist/infra/queue/run-job.d.ts +4 -1
- package/dist/infra/queue/run-job.js +10 -2
- package/dist/kernel.js +4 -0
- package/dist/kernel.module.js +3 -0
- package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
- package/dist/lifecycle/kernel-lifecycle.js +26 -21
- package/dist/orgs/offboarding.js +18 -9
- package/dist/orgs/settings.service.js +3 -0
- package/dist/packages/npm-publish-keys.json +27 -0
- package/dist/packages/official-publisher.d.ts +19 -0
- package/dist/packages/official-publisher.js +152 -0
- package/dist/packages/provenance.d.ts +7 -0
- package/dist/packages/provenance.js +22 -19
- package/dist/packages/sigstore-trusted-root.json +126 -0
- package/dist/platform/ops/admin.ops.js +2 -1
- package/dist/platform/ops/orgs.ops.js +1 -0
- package/dist/roles/worker-role.d.ts +6 -3
- package/dist/roles/worker-role.js +12 -4
- package/dist/roles/worker.module.js +7 -6
- package/dist/runtime/maintenance.d.ts +5 -4
- package/dist/runtime/maintenance.js +16 -13
- package/dist/sandbox/device-reports.d.ts +4 -4
- package/dist/sandbox/device-reports.js +40 -30
- package/dist/sandbox/device-services.d.ts +2 -1
- package/dist/sandbox/device-services.js +5 -1
- package/dist/sandbox/permission-ceiling-writes.d.ts +8 -0
- package/dist/sandbox/permission-ceiling-writes.js +21 -0
- package/dist/sandbox/permission-config-publication.js +5 -1
- package/dist/themes/theme-state.js +24 -13
- package/dist/tools/call-scope.d.ts +6 -0
- package/dist/tools/call-scope.js +22 -0
- package/dist/tools/registry.js +20 -2
- package/dist/ui/views.js +36 -8
- package/package.json +9 -4
- package/src/migrations/0219_event_org_constraint_check.sql +29 -0
- package/src/migrations/0220_require_event_org.sql +18 -0
- package/src/migrations/0221_drop_connector_declaration_policy.sql +2 -0
- package/src/migrations/0222_device_host_services.sql +27 -0
- package/src/migrations/0223_device_service_events.sql +54 -0
- package/src/migrations/0224_cache_conversation_rls_settings.sql +33 -0
- package/dist/conversations/event-org-backfill.d.ts +0 -111
- package/dist/conversations/event-org-backfill.js +0 -264
- package/dist/conversations/event-org-backfill.module.d.ts +0 -3
- package/dist/conversations/event-org-backfill.module.js +0 -18
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
2
|
-
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
3
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
4
|
-
import { LockService } from "../infra/lock/lock.service.js";
|
|
5
|
-
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
6
|
-
import type { KernelLogger } from "../log.js";
|
|
7
|
-
/** The maintenance job name for one batched backfill step (no `:`). */
|
|
8
|
-
export declare const EVENT_ORG_BACKFILL_JOB = "conversation-events-org-backfill";
|
|
9
|
-
/** The periodic kickoff's name (scheduler key and lock; no `:`). */
|
|
10
|
-
export declare const EVENT_ORG_BACKFILL_KICKOFF_JOB = "conversation-events-org-backfill-kickoff";
|
|
11
|
-
/** How often the worker kickoff checks for remaining NULL `org_id` rows during rolling deploys. */
|
|
12
|
-
export declare const EVENT_ORG_BACKFILL_KICKOFF_PERIOD_MS: number;
|
|
13
|
-
/** Rows committed per short backfill transaction. */
|
|
14
|
-
export declare const EVENT_ORG_BACKFILL_BATCH = 5000;
|
|
15
|
-
/** Keyset cursor in primary-key `(conversation_id, seq)` order. */
|
|
16
|
-
export interface EventOrgBackfillCursor {
|
|
17
|
-
conversationId: string;
|
|
18
|
-
/** `bigint` serialized as decimal string to stay lossless in JSON job payloads. */
|
|
19
|
-
seq: string;
|
|
20
|
-
}
|
|
21
|
-
/** Payload of one {@link EVENT_ORG_BACKFILL_JOB} batch. */
|
|
22
|
-
export interface EventOrgBackfillData {
|
|
23
|
-
/** Exclusive keyset cursor; `null` starts from the beginning of `(conversation_id, seq)`. */
|
|
24
|
-
after: EventOrgBackfillCursor | null;
|
|
25
|
-
/** Optional batch size override (clamped to `1..EVENT_ORG_BACKFILL_BATCH`); tests use small values. */
|
|
26
|
-
batchSize?: number;
|
|
27
|
-
}
|
|
28
|
-
/** Counts produced by the post-backfill parent and ownership integrity check. */
|
|
29
|
-
export interface EventOrgIntegrity {
|
|
30
|
-
/** Events whose `conversation_id` has no matching row in `conversations`. */
|
|
31
|
-
missingParents: number;
|
|
32
|
-
/** Events whose non-null `org_id` disagrees with their parent conversation's `org_id`. */
|
|
33
|
-
mismatchedOrgs: number;
|
|
34
|
-
/** Events whose `org_id` is still `NULL`. */
|
|
35
|
-
remainingNulls: number;
|
|
36
|
-
}
|
|
37
|
-
/** Outcome of one committed backfill batch. */
|
|
38
|
-
export interface EventOrgBackfillBatchResult {
|
|
39
|
-
/** Rows whose `org_id` was filled in this transaction. */
|
|
40
|
-
updated: number;
|
|
41
|
-
/** Last `(conversation_id, seq)` key locked in this batch, or `null` if none matched. */
|
|
42
|
-
last: EventOrgBackfillCursor | null;
|
|
43
|
-
/** Cursor of the next batch owed via the outbox, or `null` when this pass reached the end. */
|
|
44
|
-
next: EventOrgBackfillCursor | null;
|
|
45
|
-
/** True when the pass reached the end and verified zero remaining `NULL` rows. */
|
|
46
|
-
done: boolean;
|
|
47
|
-
/** Populated when the pass finished with no remaining `NULL` rows. */
|
|
48
|
-
integrity?: EventOrgIntegrity;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Deterministic BullMQ / outbox job id for a backfill batch cursor (no `:`).
|
|
52
|
-
* The first batch uses a fixed id so repeated kickoffs deduplicate cleanly; subsequent batches
|
|
53
|
-
* hash the `(conversationId, seq)` key so arbitrary conversation ids never introduce `:`.
|
|
54
|
-
*/
|
|
55
|
-
export declare function eventOrgBackfillJobId(after: EventOrgBackfillCursor | null): string;
|
|
56
|
-
/**
|
|
57
|
-
* Worker-only batched backfill of `conversation_events.org_id` from `conversations` (issue #1175):
|
|
58
|
-
*
|
|
59
|
-
* - Migration 0215 adds the nullable `org_id` column and `NOT VALID` composite FK with short locks
|
|
60
|
-
* only; this job fills existing and rolling-window NULL rows outside migration transactions.
|
|
61
|
-
* - Each invocation commits one short `@Transactional()` batch of up to
|
|
62
|
-
* {@link EVENT_ORG_BACKFILL_BATCH} (5,000) rows ordered by `(conversation_id, seq)` using
|
|
63
|
-
* `FOR UPDATE OF e SKIP LOCKED`, so concurrent appends and two concurrent workers never block or
|
|
64
|
-
* deadlock on each other.
|
|
65
|
-
* - When a full batch is processed, the next resumable cursor job is owed atomically in the same
|
|
66
|
-
* transaction through `OutboxService.add` under a deterministic job id without `:`.
|
|
67
|
-
* - When no `NULL` rows remain, the job verifies that zero events lack a parent conversation and
|
|
68
|
-
* zero events carry a mismatched `org_id`, then stops.
|
|
69
|
-
*/
|
|
70
|
-
export declare class EventOrgBackfillJob {
|
|
71
|
-
private readonly db;
|
|
72
|
-
readonly txHost: TransactionHost;
|
|
73
|
-
private readonly outbox;
|
|
74
|
-
private readonly log;
|
|
75
|
-
constructor(db: KernelDb, txHost: TransactionHost, outbox: Pick<OutboxService, "add">, log: Pick<KernelLogger, "info" | "warn">);
|
|
76
|
-
/** Whether any `conversation_events` row still has `org_id IS NULL`. */
|
|
77
|
-
anyNullOrg(): Promise<boolean>;
|
|
78
|
-
/**
|
|
79
|
-
* Deployment-wide ownership check: counts orphan events (no parent in `conversations`),
|
|
80
|
-
* mismatched non-null `org_id` rows, and remaining `NULL` rows.
|
|
81
|
-
*/
|
|
82
|
-
verifyIntegrity(): Promise<EventOrgIntegrity>;
|
|
83
|
-
/** Owes the first backfill batch via the transactional outbox (deduplicated by fixed job id). */
|
|
84
|
-
oweFirst(opts?: {
|
|
85
|
-
batchSize?: number;
|
|
86
|
-
}): Promise<boolean>;
|
|
87
|
-
/**
|
|
88
|
-
* Executes one committed batch starting strictly after `data.after`, owes the next batch if the
|
|
89
|
-
* batch was full, and verifies integrity once no `NULL` rows remain.
|
|
90
|
-
*/
|
|
91
|
-
runBatch(data?: EventOrgBackfillData): Promise<EventOrgBackfillBatchResult>;
|
|
92
|
-
private commitBatch;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Periodic worker-only kickoff for {@link EventOrgBackfillJob} (`@LockedInterval`, once per tick
|
|
96
|
-
* across replicas and once on worker start; provided only under `WorkerModule`). While any
|
|
97
|
-
* `conversation_events` row has `org_id IS NULL`, it owes the first batch under its deterministic
|
|
98
|
-
* job id; once zero `NULL` rows remain, it is a no-op.
|
|
99
|
-
*/
|
|
100
|
-
export declare class EventOrgBackfillKickoffJob {
|
|
101
|
-
private readonly backfill;
|
|
102
|
-
readonly config: KernelConfigService;
|
|
103
|
-
readonly lockService: LockService;
|
|
104
|
-
readonly log: KernelLogger;
|
|
105
|
-
constructor(backfill: Pick<EventOrgBackfillJob, "anyNullOrg" | "oweFirst">, config: KernelConfigService, lockService: LockService, log: KernelLogger);
|
|
106
|
-
tick(): Promise<void>;
|
|
107
|
-
/** One kickoff check without the tick lock (the interval body; tests call it directly). */
|
|
108
|
-
runOnce(opts?: {
|
|
109
|
-
batchSize?: number;
|
|
110
|
-
}): Promise<boolean>;
|
|
111
|
-
}
|
|
@@ -1,264 +0,0 @@
|
|
|
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 { Inject, Injectable } from "@nestjs/common";
|
|
12
|
-
import { sql } from "kysely";
|
|
13
|
-
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
14
|
-
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
15
|
-
import { asPlatform } from "../infra/database/org-scope.js";
|
|
16
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
17
|
-
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
18
|
-
import { LockService } from "../infra/lock/lock.service.js";
|
|
19
|
-
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
20
|
-
import { LockedInterval } from "../infra/schedule/locked-interval.js";
|
|
21
|
-
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
22
|
-
import { MAINTENANCE_QUEUE_NAME } from "../runtime/maintenance-queue.js";
|
|
23
|
-
/** The maintenance job name for one batched backfill step (no `:`). */
|
|
24
|
-
export const EVENT_ORG_BACKFILL_JOB = "conversation-events-org-backfill";
|
|
25
|
-
/** The periodic kickoff's name (scheduler key and lock; no `:`). */
|
|
26
|
-
export const EVENT_ORG_BACKFILL_KICKOFF_JOB = "conversation-events-org-backfill-kickoff";
|
|
27
|
-
/** How often the worker kickoff checks for remaining NULL `org_id` rows during rolling deploys. */
|
|
28
|
-
export const EVENT_ORG_BACKFILL_KICKOFF_PERIOD_MS = 15 * 60_000;
|
|
29
|
-
/** Rows committed per short backfill transaction. */
|
|
30
|
-
export const EVENT_ORG_BACKFILL_BATCH = 5_000;
|
|
31
|
-
/**
|
|
32
|
-
* Deterministic BullMQ / outbox job id for a backfill batch cursor (no `:`).
|
|
33
|
-
* The first batch uses a fixed id so repeated kickoffs deduplicate cleanly; subsequent batches
|
|
34
|
-
* hash the `(conversationId, seq)` key so arbitrary conversation ids never introduce `:`.
|
|
35
|
-
*/
|
|
36
|
-
export function eventOrgBackfillJobId(after) {
|
|
37
|
-
if (after === null)
|
|
38
|
-
return `${EVENT_ORG_BACKFILL_JOB}-first`;
|
|
39
|
-
const digest = createHash("sha256")
|
|
40
|
-
.update(`${after.conversationId}\0${after.seq}`)
|
|
41
|
-
.digest("hex")
|
|
42
|
-
.slice(0, 16);
|
|
43
|
-
return `${EVENT_ORG_BACKFILL_JOB}-${digest}`;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Worker-only batched backfill of `conversation_events.org_id` from `conversations` (issue #1175):
|
|
47
|
-
*
|
|
48
|
-
* - Migration 0215 adds the nullable `org_id` column and `NOT VALID` composite FK with short locks
|
|
49
|
-
* only; this job fills existing and rolling-window NULL rows outside migration transactions.
|
|
50
|
-
* - Each invocation commits one short `@Transactional()` batch of up to
|
|
51
|
-
* {@link EVENT_ORG_BACKFILL_BATCH} (5,000) rows ordered by `(conversation_id, seq)` using
|
|
52
|
-
* `FOR UPDATE OF e SKIP LOCKED`, so concurrent appends and two concurrent workers never block or
|
|
53
|
-
* deadlock on each other.
|
|
54
|
-
* - When a full batch is processed, the next resumable cursor job is owed atomically in the same
|
|
55
|
-
* transaction through `OutboxService.add` under a deterministic job id without `:`.
|
|
56
|
-
* - When no `NULL` rows remain, the job verifies that zero events lack a parent conversation and
|
|
57
|
-
* zero events carry a mismatched `org_id`, then stops.
|
|
58
|
-
*/
|
|
59
|
-
let EventOrgBackfillJob = class EventOrgBackfillJob {
|
|
60
|
-
db;
|
|
61
|
-
txHost;
|
|
62
|
-
outbox;
|
|
63
|
-
log;
|
|
64
|
-
constructor(db, txHost, outbox, log) {
|
|
65
|
-
this.db = db;
|
|
66
|
-
this.txHost = txHost;
|
|
67
|
-
this.outbox = outbox;
|
|
68
|
-
this.log = log;
|
|
69
|
-
}
|
|
70
|
-
/** Whether any `conversation_events` row still has `org_id IS NULL`. */
|
|
71
|
-
anyNullOrg() {
|
|
72
|
-
// org-filter: deployment-wide existence check for unbackfilled conversation_events rows
|
|
73
|
-
return asPlatform("conversation_events org_id backfill: check for remaining NULL rows", async () => {
|
|
74
|
-
const row = await this.db
|
|
75
|
-
.selectFrom("conversation_events")
|
|
76
|
-
.select("conversation_id")
|
|
77
|
-
.where("org_id", "is", null)
|
|
78
|
-
.limit(1)
|
|
79
|
-
.executeTakeFirst();
|
|
80
|
-
return row !== undefined;
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Deployment-wide ownership check: counts orphan events (no parent in `conversations`),
|
|
85
|
-
* mismatched non-null `org_id` rows, and remaining `NULL` rows.
|
|
86
|
-
*/
|
|
87
|
-
verifyIntegrity() {
|
|
88
|
-
// org-filter: deployment-wide integrity check across all conversation_events and conversations
|
|
89
|
-
return asPlatform("conversation_events org_id backfill: verify parent and org ownership integrity", async () => {
|
|
90
|
-
const res = await sql `
|
|
91
|
-
SELECT
|
|
92
|
-
count(*) FILTER (WHERE c.id IS NULL)::int AS missing_parents,
|
|
93
|
-
count(*) FILTER (
|
|
94
|
-
WHERE c.id IS NOT NULL AND e.org_id IS NOT NULL AND e.org_id <> c.org_id
|
|
95
|
-
)::int AS mismatched_orgs,
|
|
96
|
-
count(*) FILTER (WHERE e.org_id IS NULL)::int AS remaining_nulls
|
|
97
|
-
FROM capybara.conversation_events e
|
|
98
|
-
LEFT JOIN capybara.conversations c ON c.id = e.conversation_id
|
|
99
|
-
`.execute(this.db.db);
|
|
100
|
-
const row = res.rows[0];
|
|
101
|
-
return {
|
|
102
|
-
missingParents: row?.missing_parents ?? 0,
|
|
103
|
-
mismatchedOrgs: row?.mismatched_orgs ?? 0,
|
|
104
|
-
remainingNulls: row?.remaining_nulls ?? 0,
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
/** Owes the first backfill batch via the transactional outbox (deduplicated by fixed job id). */
|
|
109
|
-
async oweFirst(opts = {}) {
|
|
110
|
-
const data = opts.batchSize === undefined ? { after: null } : { after: null, batchSize: opts.batchSize };
|
|
111
|
-
return this.outbox.add({
|
|
112
|
-
queue: MAINTENANCE_QUEUE_NAME,
|
|
113
|
-
name: EVENT_ORG_BACKFILL_JOB,
|
|
114
|
-
jobId: eventOrgBackfillJobId(null),
|
|
115
|
-
data,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Executes one committed batch starting strictly after `data.after`, owes the next batch if the
|
|
120
|
-
* batch was full, and verifies integrity once no `NULL` rows remain.
|
|
121
|
-
*/
|
|
122
|
-
async runBatch(data = { after: null }) {
|
|
123
|
-
const rawSize = data.batchSize ?? EVENT_ORG_BACKFILL_BATCH;
|
|
124
|
-
const batchSize = Math.max(1, Math.min(EVENT_ORG_BACKFILL_BATCH, Math.floor(rawSize)));
|
|
125
|
-
// org-filter: deployment-wide batched backfill of conversation_events.org_id from conversations
|
|
126
|
-
const batch = await asPlatform("conversation_events org_id backfill batch", () => this.commitBatch(data.after ?? null, batchSize, data.batchSize));
|
|
127
|
-
if (batch.next !== null) {
|
|
128
|
-
return { ...batch, done: false };
|
|
129
|
-
}
|
|
130
|
-
if (await this.anyNullOrg()) {
|
|
131
|
-
// Either a concurrent worker still holds uncommitted NULL rows or an old replica inserted a
|
|
132
|
-
// NULL row behind our cursor; the next kickoff or sibling worker finishes it.
|
|
133
|
-
return { ...batch, done: false };
|
|
134
|
-
}
|
|
135
|
-
const integrity = await this.verifyIntegrity();
|
|
136
|
-
if (integrity.missingParents > 0 || integrity.mismatchedOrgs > 0) {
|
|
137
|
-
throw new Error(`conversation_events org_id integrity check failed: missingParents=${integrity.missingParents}, mismatchedOrgs=${integrity.mismatchedOrgs}`);
|
|
138
|
-
}
|
|
139
|
-
if (batch.updated > 0) {
|
|
140
|
-
this.log.info("conversation_events org_id backfill pass finished", { ...integrity });
|
|
141
|
-
}
|
|
142
|
-
return {
|
|
143
|
-
...batch,
|
|
144
|
-
done: integrity.remainingNulls === 0,
|
|
145
|
-
integrity,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
async commitBatch(after, batchSize, explicitBatchSize) {
|
|
149
|
-
// Split the cursor branch so Postgres uses a pure index range scan on conversation_events_pkey
|
|
150
|
-
// when resuming after a cursor instead of falling back to an OR scan from the start.
|
|
151
|
-
const cursorClause = after === null
|
|
152
|
-
? sql ``
|
|
153
|
-
: sql `AND (e.conversation_id, e.seq) > (${after.conversationId}, ${after.seq}::bigint)`;
|
|
154
|
-
const res = await sql `
|
|
155
|
-
WITH batch AS (
|
|
156
|
-
SELECT e.conversation_id, e.seq, c.id AS parent_id, c.org_id
|
|
157
|
-
FROM capybara.conversation_events e
|
|
158
|
-
LEFT JOIN capybara.conversations c ON c.id = e.conversation_id
|
|
159
|
-
WHERE e.org_id IS NULL
|
|
160
|
-
${cursorClause}
|
|
161
|
-
ORDER BY e.conversation_id, e.seq
|
|
162
|
-
LIMIT ${batchSize}
|
|
163
|
-
FOR UPDATE OF e SKIP LOCKED
|
|
164
|
-
),
|
|
165
|
-
updated AS (
|
|
166
|
-
UPDATE capybara.conversation_events e
|
|
167
|
-
SET org_id = batch.org_id
|
|
168
|
-
FROM batch
|
|
169
|
-
WHERE batch.parent_id IS NOT NULL
|
|
170
|
-
AND e.conversation_id = batch.conversation_id
|
|
171
|
-
AND e.seq = batch.seq
|
|
172
|
-
AND e.org_id IS NULL
|
|
173
|
-
RETURNING e.conversation_id, e.seq
|
|
174
|
-
)
|
|
175
|
-
SELECT
|
|
176
|
-
(SELECT count(*)::int FROM batch) AS fetched_count,
|
|
177
|
-
(SELECT count(*)::int FROM batch WHERE parent_id IS NULL) AS missing_parents,
|
|
178
|
-
(SELECT count(*)::int FROM updated) AS updated_count,
|
|
179
|
-
(SELECT conversation_id FROM batch ORDER BY conversation_id DESC, seq DESC LIMIT 1) AS last_conv,
|
|
180
|
-
(SELECT seq::text FROM batch ORDER BY conversation_id DESC, seq DESC LIMIT 1) AS last_seq
|
|
181
|
-
`.execute(this.db.db);
|
|
182
|
-
const row = res.rows[0];
|
|
183
|
-
const fetched = row?.fetched_count ?? 0;
|
|
184
|
-
const missingParents = row?.missing_parents ?? 0;
|
|
185
|
-
if (missingParents > 0) {
|
|
186
|
-
throw new Error(`conversation_events org_id backfill found ${missingParents} event(s) without a parent conversation`);
|
|
187
|
-
}
|
|
188
|
-
const updated = row?.updated_count ?? 0;
|
|
189
|
-
const last = row?.last_conv !== null &&
|
|
190
|
-
row?.last_conv !== undefined &&
|
|
191
|
-
row?.last_seq !== null &&
|
|
192
|
-
row?.last_seq !== undefined
|
|
193
|
-
? { conversationId: row.last_conv, seq: row.last_seq }
|
|
194
|
-
: null;
|
|
195
|
-
const next = fetched >= batchSize && last !== null ? last : null;
|
|
196
|
-
if (next !== null) {
|
|
197
|
-
const nextData = explicitBatchSize === undefined
|
|
198
|
-
? { after: next }
|
|
199
|
-
: { after: next, batchSize: explicitBatchSize };
|
|
200
|
-
await this.outbox.add({
|
|
201
|
-
queue: MAINTENANCE_QUEUE_NAME,
|
|
202
|
-
name: EVENT_ORG_BACKFILL_JOB,
|
|
203
|
-
jobId: eventOrgBackfillJobId(next),
|
|
204
|
-
data: nextData,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
return { updated, last, next };
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
__decorate([
|
|
211
|
-
Transactional()
|
|
212
|
-
], EventOrgBackfillJob.prototype, "oweFirst", null);
|
|
213
|
-
__decorate([
|
|
214
|
-
Transactional()
|
|
215
|
-
], EventOrgBackfillJob.prototype, "commitBatch", null);
|
|
216
|
-
EventOrgBackfillJob = __decorate([
|
|
217
|
-
Injectable(),
|
|
218
|
-
__param(0, Inject(KernelDb)),
|
|
219
|
-
__param(1, Inject(TransactionHost)),
|
|
220
|
-
__param(2, Inject(OutboxService)),
|
|
221
|
-
__param(3, Inject(KERNEL_LOGGER))
|
|
222
|
-
], EventOrgBackfillJob);
|
|
223
|
-
export { EventOrgBackfillJob };
|
|
224
|
-
/**
|
|
225
|
-
* Periodic worker-only kickoff for {@link EventOrgBackfillJob} (`@LockedInterval`, once per tick
|
|
226
|
-
* across replicas and once on worker start; provided only under `WorkerModule`). While any
|
|
227
|
-
* `conversation_events` row has `org_id IS NULL`, it owes the first batch under its deterministic
|
|
228
|
-
* job id; once zero `NULL` rows remain, it is a no-op.
|
|
229
|
-
*/
|
|
230
|
-
let EventOrgBackfillKickoffJob = class EventOrgBackfillKickoffJob {
|
|
231
|
-
backfill;
|
|
232
|
-
config;
|
|
233
|
-
lockService;
|
|
234
|
-
log;
|
|
235
|
-
constructor(backfill, config, lockService, log) {
|
|
236
|
-
this.backfill = backfill;
|
|
237
|
-
this.config = config;
|
|
238
|
-
this.lockService = lockService;
|
|
239
|
-
this.log = log;
|
|
240
|
-
}
|
|
241
|
-
async tick() {
|
|
242
|
-
if (await this.runOnce())
|
|
243
|
-
this.log.info("conversation_events org_id backfill owed");
|
|
244
|
-
}
|
|
245
|
-
/** One kickoff check without the tick lock (the interval body; tests call it directly). */
|
|
246
|
-
async runOnce(opts) {
|
|
247
|
-
if (!(await this.backfill.anyNullOrg()))
|
|
248
|
-
return false;
|
|
249
|
-
return this.backfill.oweFirst(opts);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
__decorate([
|
|
253
|
-
LockedInterval(EVENT_ORG_BACKFILL_KICKOFF_JOB, () => EVENT_ORG_BACKFILL_KICKOFF_PERIOD_MS, {
|
|
254
|
-
runOnStart: true,
|
|
255
|
-
})
|
|
256
|
-
], EventOrgBackfillKickoffJob.prototype, "tick", null);
|
|
257
|
-
EventOrgBackfillKickoffJob = __decorate([
|
|
258
|
-
Injectable(),
|
|
259
|
-
__param(0, Inject(EventOrgBackfillJob)),
|
|
260
|
-
__param(1, Inject(KernelConfigService)),
|
|
261
|
-
__param(2, Inject(LockService)),
|
|
262
|
-
__param(3, Inject(KERNEL_LOGGER))
|
|
263
|
-
], EventOrgBackfillKickoffJob);
|
|
264
|
-
export { EventOrgBackfillKickoffJob };
|
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
import { Module } from "@nestjs/common";
|
|
8
|
-
import { EventOrgBackfillJob } from "./event-org-backfill.js";
|
|
9
|
-
/** Batched `conversation_events.org_id` backfill ({@link EventOrgBackfillJob}), worker-only. */
|
|
10
|
-
let EventOrgBackfillModule = class EventOrgBackfillModule {
|
|
11
|
-
};
|
|
12
|
-
EventOrgBackfillModule = __decorate([
|
|
13
|
-
Module({
|
|
14
|
-
providers: [EventOrgBackfillJob],
|
|
15
|
-
exports: [EventOrgBackfillJob],
|
|
16
|
-
})
|
|
17
|
-
], EventOrgBackfillModule);
|
|
18
|
-
export { EventOrgBackfillModule };
|