@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.
Files changed (155) hide show
  1. package/bootstrap.json +5 -5
  2. package/dist/admin/admin.module.js +3 -0
  3. package/dist/admin/admin.schemas.d.ts +4 -0
  4. package/dist/admin/health.service.d.ts +2 -2
  5. package/dist/admin/health.service.js +6 -2
  6. package/dist/admin/setup.service.js +6 -1
  7. package/dist/agents/agents.controller.js +3 -2
  8. package/dist/agents/agents.service.d.ts +2 -0
  9. package/dist/agents/agents.service.js +7 -0
  10. package/dist/catalog/catalog.service.d.ts +2 -0
  11. package/dist/catalog/catalog.service.js +28 -5
  12. package/dist/catalog/marketplace-fetch.d.ts +2 -0
  13. package/dist/catalog/marketplace-fetch.js +15 -0
  14. package/dist/catalog/marketplaces.service.d.ts +4 -4
  15. package/dist/catalog/marketplaces.service.js +3 -2
  16. package/dist/catalog/owned-items.d.ts +2 -0
  17. package/dist/catalog/owned-items.js +30 -31
  18. package/dist/catalog/ownership.js +40 -38
  19. package/dist/catalog/plugin-import.d.ts +6 -5
  20. package/dist/catalog/plugin-import.js +69 -16
  21. package/dist/catalog/plugin-upgrade.d.ts +8 -0
  22. package/dist/catalog/plugin-upgrade.js +95 -7
  23. package/dist/catalog/publisher-policy.d.ts +9 -0
  24. package/dist/catalog/publisher-policy.js +12 -0
  25. package/dist/catalog/unshare.js +66 -58
  26. package/dist/client/client-access.service.js +5 -3
  27. package/dist/config/schema.d.ts +1 -0
  28. package/dist/config/schema.js +9 -1
  29. package/dist/conversations/conversations.controller.js +4 -14
  30. package/dist/conversations/conversations.service.d.ts +14 -0
  31. package/dist/conversations/conversations.service.js +20 -2
  32. package/dist/ctx/create-ctx.d.ts +14 -4
  33. package/dist/ctx/create-ctx.js +131 -59
  34. package/dist/ctx/queue.js +7 -4
  35. package/dist/db/extension-db.d.ts +30 -0
  36. package/dist/db/extension-db.js +91 -0
  37. package/dist/db/extension-rls-predicate.d.ts +1 -0
  38. package/dist/db/extension-rls-predicate.js +8 -0
  39. package/dist/db/extension-schema-transfer.d.ts +6 -0
  40. package/dist/db/extension-schema-transfer.js +141 -0
  41. package/dist/db/kernel-schema.d.ts +50 -4
  42. package/dist/db/migrator.d.ts +6 -0
  43. package/dist/db/migrator.js +35 -7
  44. package/dist/db/pool.d.ts +6 -2
  45. package/dist/db/pool.js +37 -2
  46. package/dist/db/sql-statements.d.ts +3 -0
  47. package/dist/db/sql-statements.js +15 -5
  48. package/dist/device-access/device-access.module.js +12 -2
  49. package/dist/device-access/device-access.service.d.ts +30 -2
  50. package/dist/device-access/device-access.service.js +38 -4
  51. package/dist/device-access/host-gate.d.ts +45 -0
  52. package/dist/device-access/host-gate.js +92 -0
  53. package/dist/device-access/host-service-stop-sweep.d.ts +33 -0
  54. package/dist/device-access/host-service-stop-sweep.js +112 -0
  55. package/dist/device-access/host-service-stop.d.ts +78 -0
  56. package/dist/device-access/host-service-stop.js +144 -0
  57. package/dist/device-access/host-services.d.ts +211 -0
  58. package/dist/device-access/host-services.js +803 -0
  59. package/dist/device-access/person-handles.d.ts +10 -0
  60. package/dist/device-access/person-handles.js +25 -0
  61. package/dist/device-access/service-events.d.ts +199 -0
  62. package/dist/device-access/service-events.js +704 -0
  63. package/dist/device-access/token.d.ts +5 -0
  64. package/dist/device-access/token.js +5 -0
  65. package/dist/devices/devices.service.js +3 -0
  66. package/dist/dial-in/access.d.ts +5 -5
  67. package/dist/dial-in/access.js +19 -12
  68. package/dist/dial-in/gateway.js +6 -2
  69. package/dist/dial-in/machines.d.ts +9 -2
  70. package/dist/dial-in/machines.js +27 -4
  71. package/dist/erasure/device-cleanup.js +12 -0
  72. package/dist/erasure/org-catalog.js +2 -2
  73. package/dist/erasure/org-rules.js +9 -0
  74. package/dist/errors.d.ts +2 -0
  75. package/dist/errors.js +2 -0
  76. package/dist/extensions/admin-ops.d.ts +3 -1
  77. package/dist/extensions/admin-ops.js +19 -9
  78. package/dist/extensions/deployed-snapshots.d.ts +4 -1
  79. package/dist/extensions/deployed-snapshots.js +7 -2
  80. package/dist/extensions/extensions.module.js +3 -0
  81. package/dist/extensions/host.d.ts +11 -3
  82. package/dist/extensions/host.js +40 -8
  83. package/dist/extensions/instance.d.ts +6 -0
  84. package/dist/extensions/instance.js +7 -1
  85. package/dist/extensions/ledger.d.ts +1 -1
  86. package/dist/extensions/lifecycle-hooks.js +3 -3
  87. package/dist/extensions/migrate-lock.d.ts +4 -1
  88. package/dist/extensions/migrate-lock.js +19 -2
  89. package/dist/extensions/org-config.js +6 -1
  90. package/dist/extensions/permission-set.d.ts +7 -0
  91. package/dist/extensions/permission-set.js +10 -0
  92. package/dist/extensions/permission-view.d.ts +1 -1
  93. package/dist/extensions/permission-view.js +10 -1
  94. package/dist/git/tickets.js +32 -27
  95. package/dist/http/app.js +5 -3
  96. package/dist/identity/accounts.d.ts +6 -1
  97. package/dist/identity/accounts.js +11 -2
  98. package/dist/identity/api-tokens.js +7 -6
  99. package/dist/identity/settings.d.ts +9 -1
  100. package/dist/identity/settings.js +16 -1
  101. package/dist/inbox/org-notices.js +3 -2
  102. package/dist/index.d.ts +1 -0
  103. package/dist/index.js +1 -0
  104. package/dist/infra/database/scoped-dialect.d.ts +3 -2
  105. package/dist/infra/database/scoped-dialect.js +260 -72
  106. package/dist/infra/database/startup-activation-transaction.d.ts +2 -0
  107. package/dist/infra/database/startup-activation-transaction.js +3 -2
  108. package/dist/infra/outbox/registry.d.ts +28 -14
  109. package/dist/infra/outbox/registry.js +30 -15
  110. package/dist/infra/queue/queue-names.d.ts +1 -1
  111. package/dist/infra/queue/queue-names.js +1 -0
  112. package/dist/infra/queue/run-job.d.ts +4 -1
  113. package/dist/infra/queue/run-job.js +10 -2
  114. package/dist/kernel.js +4 -0
  115. package/dist/kernel.module.js +3 -0
  116. package/dist/lifecycle/kernel-lifecycle.d.ts +3 -1
  117. package/dist/lifecycle/kernel-lifecycle.js +26 -21
  118. package/dist/orgs/offboarding.js +18 -9
  119. package/dist/orgs/settings.service.js +3 -0
  120. package/dist/packages/npm-publish-keys.json +27 -0
  121. package/dist/packages/official-publisher.d.ts +19 -0
  122. package/dist/packages/official-publisher.js +152 -0
  123. package/dist/packages/provenance.d.ts +7 -0
  124. package/dist/packages/provenance.js +22 -19
  125. package/dist/packages/sigstore-trusted-root.json +126 -0
  126. package/dist/platform/ops/admin.ops.js +2 -1
  127. package/dist/platform/ops/orgs.ops.js +1 -0
  128. package/dist/roles/worker-role.d.ts +6 -3
  129. package/dist/roles/worker-role.js +12 -4
  130. package/dist/roles/worker.module.js +7 -6
  131. package/dist/runtime/maintenance.d.ts +5 -4
  132. package/dist/runtime/maintenance.js +16 -13
  133. package/dist/sandbox/device-reports.d.ts +4 -4
  134. package/dist/sandbox/device-reports.js +40 -30
  135. package/dist/sandbox/device-services.d.ts +2 -1
  136. package/dist/sandbox/device-services.js +5 -1
  137. package/dist/sandbox/permission-ceiling-writes.d.ts +8 -0
  138. package/dist/sandbox/permission-ceiling-writes.js +21 -0
  139. package/dist/sandbox/permission-config-publication.js +5 -1
  140. package/dist/themes/theme-state.js +24 -13
  141. package/dist/tools/call-scope.d.ts +6 -0
  142. package/dist/tools/call-scope.js +22 -0
  143. package/dist/tools/registry.js +20 -2
  144. package/dist/ui/views.js +36 -8
  145. package/package.json +9 -4
  146. package/src/migrations/0219_event_org_constraint_check.sql +29 -0
  147. package/src/migrations/0220_require_event_org.sql +18 -0
  148. package/src/migrations/0221_drop_connector_declaration_policy.sql +2 -0
  149. package/src/migrations/0222_device_host_services.sql +27 -0
  150. package/src/migrations/0223_device_service_events.sql +54 -0
  151. package/src/migrations/0224_cache_conversation_rls_settings.sql +33 -0
  152. package/dist/conversations/event-org-backfill.d.ts +0 -111
  153. package/dist/conversations/event-org-backfill.js +0 -264
  154. package/dist/conversations/event-org-backfill.module.d.ts +0 -3
  155. package/dist/conversations/event-org-backfill.module.js +0 -18
@@ -0,0 +1,10 @@
1
+ import type { DevicePersonHandle } from "@capybara-hq/plugin-sdk";
2
+ /** A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}. */
3
+ export declare function issuePersonHandle(orgId: string, accountId: string): DevicePersonHandle;
4
+ /** Ends the handle: later host service calls with it are refused. */
5
+ export declare function revokePersonHandle(handle: DevicePersonHandle): void;
6
+ /** The org and account of a live handle the kernel issued; null for anything else. */
7
+ export declare function personOf(handle: unknown): {
8
+ orgId: string;
9
+ accountId: string;
10
+ } | null;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Kernel-issued person handles (`DevicePersonHandle`, local agents spec, call 6): the kernel puts
3
+ * one on the args of each org `ctx.ui.action` / `ctx.ui.query` handler call, for the person the
4
+ * call runs for, and revokes it when the handler settles. A host service call with a person scope
5
+ * is admitted only for a live handle, and the org and account come from it, never from the
6
+ * extension. Per call and per process like a tool call's scope (`tools/call-scope.ts`): the handle
7
+ * never leaves the process that issued it, and nothing is shared across replicas.
8
+ */
9
+ const persons = new WeakMap();
10
+ /** A new handle for `accountId` in `orgId`, live until {@link revokePersonHandle}. */
11
+ export function issuePersonHandle(orgId, accountId) {
12
+ const handle = Object.freeze({ kind: "device-person" });
13
+ persons.set(handle, { orgId, accountId });
14
+ return handle;
15
+ }
16
+ /** Ends the handle: later host service calls with it are refused. */
17
+ export function revokePersonHandle(handle) {
18
+ persons.delete(handle);
19
+ }
20
+ /** The org and account of a live handle the kernel issued; null for anything else. */
21
+ export function personOf(handle) {
22
+ if (typeof handle !== "object" || handle === null)
23
+ return null;
24
+ return persons.get(handle) ?? null;
25
+ }
@@ -0,0 +1,199 @@
1
+ import { type DeviceAccessCtx } from "@capybara-hq/plugin-sdk";
2
+ import { WorkerHost } from "@nestjs/bullmq";
3
+ import type { Job, Worker } from "bullmq";
4
+ import { ExtensionHost } from "../extensions/host.js";
5
+ import { KernelDb } from "../infra/database/kernel-db.js";
6
+ import { TransactionHost } from "../infra/database/transaction-host.js";
7
+ import { LockService } from "../infra/lock/lock.service.js";
8
+ import { OutboxService } from "../infra/outbox/outbox.service.js";
9
+ import type { KernelLogger } from "../log.js";
10
+ /** The queue of device service event deliveries (one consumer, {@link ServiceEventProcessor}). */
11
+ export declare const SERVICE_EVENTS_QUEUE = "cap-device-service-events";
12
+ /** The job name of a delivery (registered in `infra/outbox/registry.ts`). */
13
+ export declare const SERVICE_EVENTS_JOB = "device-service-events";
14
+ /** The sweep of delivered events (scheduler key and lock; no `:`). */
15
+ export declare const SERVICE_EVENTS_SWEEP_JOB = "device-service-events-sweep";
16
+ /** Delivered events are kept this long, then deleted by the sweep. */
17
+ export declare const SERVICE_EVENTS_RETAIN_DAYS = 7;
18
+ /** One handler call's time limit. */
19
+ export declare const SERVICE_EVENT_HANDLER_TIMEOUT_MS = 30000;
20
+ /**
21
+ * How old a service's first undelivered event may get before the sweep owes its delivery again:
22
+ * past the delivery job's retries (about 17 minutes) only for an event whose retries ran out, but
23
+ * a job that is still retrying is owed under another id and simply takes the lock in turn.
24
+ */
25
+ export declare const SERVICE_EVENTS_STALL_MS: number;
26
+ /** The handler an extension registers with `ctx.deviceAccess.onServiceEvent`. */
27
+ export type ServiceEventHandler = Parameters<DeviceAccessCtx["onServiceEvent"]>[1];
28
+ /** The data of a {@link SERVICE_EVENTS_JOB}: one service's events (`orgId` scopes the job). */
29
+ export interface ServiceEventsJobData {
30
+ orgId: string;
31
+ deviceId: string;
32
+ extension: string;
33
+ service: string;
34
+ }
35
+ /**
36
+ * The deterministic job id of the delivery owed by the event `seq` (no `:`; device ids, extension
37
+ * names and service names carry none). One id per event, not per service: a job of the service
38
+ * that is already running would swallow an add of its own id, and the event stored after its last
39
+ * read would wait for the next one. Deliveries of one service take its lock in turn.
40
+ */
41
+ export declare const serviceEventsJobId: (d: ServiceEventsJobData, seq: number) => string;
42
+ /** What `ctx.devices.serviceEvent` reports (the provider is told the event was stored or not). */
43
+ export type ServiceEventOutcome = "stored" | "dropped" | "not-served";
44
+ /** One event as `ctx.devices.serviceEvent` passes it (validated by {@link ServiceEvents.record}). */
45
+ export interface ServiceEventInput {
46
+ deviceId: string;
47
+ /** The provider's service id, `<extension>.<service>`. */
48
+ service: string;
49
+ seq: number;
50
+ body: Uint8Array;
51
+ }
52
+ /** What `ctx.devices.serviceEvent` calls through (`ServiceEvents`), found at the time of use. */
53
+ export type ServiceEventsBackend = Pick<ServiceEvents, "record">;
54
+ /**
55
+ * Device service events (local agents spec, Part 1): a host service on a personal machine emits
56
+ * events; its provider reports each one ({@link record}, `ctx.devices.serviceEvent`) and the
57
+ * kernel delivers it to the extension's `ctx.deviceAccess.onServiceEvent` handler, at least once,
58
+ * in `seq` order per (device, extension, service).
59
+ *
60
+ * - **Store** ({@link record}, any role: the provider's dial-in handler runs on the gateway): the
61
+ * device must be bound to the reporting provider and the service must be a host service the
62
+ * kernel placed there (`device_host_services`) or one it already delivered events of there (a
63
+ * delivery cursor, `device_service_event_cursors`: the events a stopped service sent before its
64
+ * stop still arrive); the row is inserted (a seq stored before is a no-op) with an outbox job
65
+ * owed in the same transaction, so the provider acks the machine only once both are committed.
66
+ * - **Deliver** ({@link deliver}, worker role): under the service's lock, the undelivered rows in
67
+ * `seq` order go to the handler of the extension's instance loaded here, each marked delivered
68
+ * once it resolved. The first failure fails the job (BullMQ retries it) and the events after it
69
+ * wait. An extension not enabled in the org, or loaded without a handler for the service, drops
70
+ * the event with a log line; an extension not loaded on this replica fails the job (another
71
+ * replica or a later attempt has it).
72
+ * - **Sweep** ({@link ServiceEventSweepJob}): delivered rows older than
73
+ * {@link SERVICE_EVENTS_RETAIN_DAYS} days are deleted.
74
+ */
75
+ export declare class ServiceEvents {
76
+ private readonly kdb;
77
+ readonly txHost: TransactionHost;
78
+ private readonly outbox;
79
+ private readonly locks;
80
+ /** The extension's instance loaded on this replica, and its handlers. */
81
+ private readonly host;
82
+ private readonly log;
83
+ constructor(kdb: KernelDb, txHost: TransactionHost, outbox: Pick<OutboxService, "add">, locks: Pick<LockService, "acquire">,
84
+ /** The extension's instance loaded on this replica, and its handlers. */
85
+ host: Pick<ExtensionHost, "current">, log: KernelLogger);
86
+ /**
87
+ * Stores one event `provider` reported and owes its delivery. Throws a `TypeError` for a
88
+ * malformed event (seqs start at 1); `not-served` when the device is not bound to `provider`;
89
+ * `dropped` (logged) when the service is not a host service the kernel placed on the device and
90
+ * none of its events was ever delivered there (an event of a service stopped since, still in
91
+ * flight or resent on a reconnect, is stored: the device numbered it, and dropping it would hold
92
+ * the service's next start behind the hole).
93
+ */
94
+ record(provider: string, e: ServiceEventInput): Promise<ServiceEventOutcome>;
95
+ /**
96
+ * The insert and its owed delivery, in one transaction; false when the service is neither placed
97
+ * on the device nor known there by a delivery cursor (it never ran there as a host service).
98
+ */
99
+ private store;
100
+ /**
101
+ * One delivery job (the consumer {@link ServiceEventProcessor} calls it, in the job's org
102
+ * scope). Order per service: the seqs right after the service's cursor (the last seq delivered,
103
+ * `device_service_event_cursors`, 0 before the first: seqs start at 1) go out in turn. A gap (a
104
+ * seq not stored yet: the device resends what it was not acked for) holds the delivery, at most
105
+ * {@link SERVICE_EVENTS_STALL_MS} after the event behind it was stored; then the missing seqs are
106
+ * skipped with a warning (the sweep owes the held delivery again). A seq at or below the cursor
107
+ * is a duplicate: marked delivered without a call.
108
+ *
109
+ * The service's lock is only tried, never waited for: a busy lock ends the job at once (no
110
+ * worker slot waits). The holder reads until nothing is deliverable, and after releasing the
111
+ * lock checks once more and owes a `-tail` job when an event stored meanwhile became deliverable.
112
+ */
113
+ deliver(d: ServiceEventsJobData, stop: AbortSignal): Promise<void>;
114
+ /**
115
+ * After the lock is released: when the service's first undelivered event can go now (an event
116
+ * stored while the holder was past its last read, whose own job found the lock busy), owes one
117
+ * tail delivery for it.
118
+ */
119
+ private oweTailIfDeliverable;
120
+ private oweTail;
121
+ /** The service's undelivered events in seq order (at most `limit`). */
122
+ private undelivered;
123
+ /** The last seq delivered for the service (0 before the first). */
124
+ private cursorOf;
125
+ /**
126
+ * Owes the delivery again of every service whose first undelivered event is older than
127
+ * {@link SERVICE_EVENTS_STALL_MS} (its job ran out of retries, or a gap holds it), under a fresh
128
+ * id per stall window; logs a stall once (`stalled_at`). Returns the services owed again.
129
+ */
130
+ resumeStalled(signal?: AbortSignal): Promise<number>;
131
+ /** The stalled delivery owed again; true when the stall of `seq` was not noted before. */
132
+ private oweAgain;
133
+ /** One event: to the handler, or dropped with a log line; then marked delivered. */
134
+ private deliverOne;
135
+ /** The device's owner (the person the host service runs for); undefined when it is gone. */
136
+ private ownerOf;
137
+ /**
138
+ * Marks the event delivered and moves the service's cursor to it, in one transaction.
139
+ *
140
+ * Lock order: `devices` (`FOR KEY SHARE`), then `device_service_events`, then
141
+ * `device_service_event_cursors`, matching `DELETE FROM devices` (cascade order from migration
142
+ * 0223) and `AccountDeviceCleanup.batch`. Taking `FOR KEY SHARE` on `devices` first prevents a
143
+ * deadlock when inserting a new cursor row (whose foreign key check locks `devices`), and
144
+ * updating `device_service_events` before `device_service_event_cursors` prevents a deadlock
145
+ * against the cascade order when updating an existing cursor.
146
+ */
147
+ private markDelivered;
148
+ /** A job failed (the worker's `failed` event): a retry warns, the last attempt logs an error. */
149
+ onFailed(j: Job<ServiceEventsJobData> | undefined, err: Error): void;
150
+ }
151
+ /**
152
+ * The consumer of {@link SERVICE_EVENTS_QUEUE}: {@link ServiceEvents.deliver}. Worker role only
153
+ * (`DeviceServiceEventWorkersModule`); built with `autorun: false`, started by the worker role
154
+ * after the extensions loaded (their handlers are registered at setup).
155
+ */
156
+ export declare class ServiceEventProcessor extends WorkerHost<Worker<ServiceEventsJobData>> {
157
+ private readonly events;
158
+ private readonly log;
159
+ private readonly stopping;
160
+ constructor(events: ServiceEvents, log: Pick<KernelLogger, "error">);
161
+ process(job: Job<ServiceEventsJobData>): Promise<void>;
162
+ /** Starts consuming (worker role, after the extensions loaded). */
163
+ start(): void;
164
+ /** Aborts the handler calls in flight (retried) and closes the worker. */
165
+ close(): Promise<void>;
166
+ failed(job: Job<ServiceEventsJobData> | undefined, err: Error): void;
167
+ error(err: Error): void;
168
+ }
169
+ /**
170
+ * The device service event sweep, every 5 minutes (a `@LockedCron`, once per tick across
171
+ * replicas, under `WorkerModule`): owes again the deliveries that stalled
172
+ * ({@link ServiceEvents.resumeStalled}: a job whose retries ran out leaves its events undelivered
173
+ * until something owes a delivery of the service again), then deletes delivered events older
174
+ * than {@link SERVICE_EVENTS_RETAIN_DAYS} days, in batches.
175
+ */
176
+ export declare class ServiceEventSweepJob {
177
+ private readonly kdb;
178
+ private readonly events;
179
+ readonly lockService: LockService;
180
+ readonly log: KernelLogger;
181
+ constructor(kdb: KernelDb, events: Pick<ServiceEvents, "resumeStalled">, lockService: LockService, log: KernelLogger);
182
+ sweep(signal?: AbortSignal): Promise<void>;
183
+ /** One sweep (the test seam): the stalled services owed again and the rows deleted. */
184
+ runOnce(signal?: AbortSignal): Promise<{
185
+ resumed: number;
186
+ deleted: number;
187
+ }>;
188
+ private deleteDelivered;
189
+ }
190
+ /**
191
+ * Device service events on every role: storing them (`ctx.devices.serviceEvent`, found by the
192
+ * extension host at the time of use through {@link SERVICE_EVENTS_BACKEND}) and the queue the
193
+ * outbox adds their deliveries to.
194
+ */
195
+ export declare class DeviceServiceEventsModule {
196
+ }
197
+ /** The deliveries' consumer and the sweep; imported only by the worker role (`WorkerModule`). */
198
+ export declare class DeviceServiceEventWorkersModule {
199
+ }