@docstack/client 0.1.6 → 0.1.8

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.
@@ -49,4 +49,5 @@ export declare const createGuardedDb: <T extends {}>(db: PouchDB.Database<T>) =>
49
49
  export declare const createReplicationDb: <T extends {}>(db: PouchDB.Database<T>, pristine: {
50
50
  bulkDocs: Function;
51
51
  bulkGet: Function;
52
+ get: Function;
52
53
  }) => PouchDB.Database<T>;
@@ -3,6 +3,7 @@ import Class from "./class.js";
3
3
  import Domain from './domain.js';
4
4
  import { Trigger } from "./trigger/index.js";
5
5
  import { JobEngine } from "./job-engine/index.js";
6
+ import { JobScheduler } from "./job-engine/scheduler.js";
6
7
  import Attribute from './attribute.js';
7
8
  import { AttributeType, ClientCredentials, StackConfig } from "@docstack/shared";
8
9
  import { DocStackSyncHandle } from './sync/index.js';
@@ -56,6 +57,13 @@ declare class DocStack extends EventTarget {
56
57
  private pendingStacks;
57
58
  /** The handle from the last {@link sync} call. */
58
59
  private syncHandle?;
60
+ /**
61
+ * What the last un-scoped {@link sync} call asked for, kept so a stack added
62
+ * later can be bound to the same replication. `null` when sync was never
63
+ * called, was called with an explicit `stacks` list (a caller who named three
64
+ * databases asked for three), or was cancelled. See ADR-0033.
65
+ */
66
+ private syncOptions;
59
67
  private logger;
60
68
  /**
61
69
  * Splits a {@link StackConfig} into the connection string and the options a stack
@@ -125,6 +133,25 @@ declare class DocStack extends EventTarget {
125
133
  * ```
126
134
  */
127
135
  sync: (options: DocStackSyncOptions) => Promise<DocStackSyncHandle>;
136
+ /**
137
+ * Binds one stack to the live sync, if there is one and it was un-scoped.
138
+ *
139
+ * Failure here must not fail {@link addStack} - the stack itself opened fine -
140
+ * but it must not be silent either, silence being this defect's whole shape:
141
+ * it is logged and dispatched as an `error` event on the sync handle.
142
+ */
143
+ private bindStackToSync;
144
+ /**
145
+ * Which open stacks the current sync covers, and which it does not.
146
+ *
147
+ * An idle stack and an unbound one are opposite problems, and `getStatus()`
148
+ * cannot tell them apart - the unbound one has no key at all. With no sync
149
+ * running, every open stack is unbound.
150
+ */
151
+ getSyncCoverage: () => {
152
+ bound: string[];
153
+ unbound: string[];
154
+ };
128
155
  /**
129
156
  * Returns the handle from the last {@link sync} call, or `null`.
130
157
  */
@@ -272,9 +299,13 @@ declare class DocStack extends EventTarget {
272
299
  *
273
300
  * @module @docstack/client
274
301
  */
275
- export { ClientStack, Trigger, Class, Attribute, Domain, JobEngine };
276
- export { StackSyncHandle, DocStackSyncHandle, SyncSchemaMismatchError, SYNC_META_DOC_ID, readRemoteSchemaVersion, publishSchemaVersion, createReplicationFilter, isInternalDoc, resolveInternalClasses, createClassFilter, hasClassRules, DATA_MODEL_CLASSES, withFilterIdentity, describeFilter, INTERNAL_DOC_IDS, INTERNAL_DOC_ID_PREFIXES, INTERNAL_DOC_CLASSES, OPTIONAL_INTERNAL_DOC_CLASSES, } from "./sync/index.js";
277
- export type { SyncDirection, SyncState, SyncStatus, StackSyncOptions, DocStackSyncOptions, RemoteResolver, SyncMetaDoc, InternalDocFilterOptions, ClassFilterOptions, } from "./sync/index.js";
302
+ export { ClientStack, Trigger, Class, Attribute, Domain, JobEngine, JobScheduler };
303
+ export { JOB_SCHEDULE_DOC_ID } from "./job-engine/scheduler.js";
304
+ export type { SchedulerOptions, SchedulerHost, JobScheduleState, TickReport, SkipReason, } from "./job-engine/scheduler.js";
305
+ export { parseSchedule, nextOccurrence, MIN_PERIOD_MS } from "./job-engine/schedule.js";
306
+ export type { ParsedSchedule } from "./job-engine/schedule.js";
307
+ export { StackSyncHandle, DocStackSyncHandle, SyncSchemaMismatchError, SYNC_META_DOC_ID, readRemoteSchemaVersion, publishSchemaVersion, createReplicationFilter, isInternalDoc, resolveInternalClasses, createClassFilter, hasClassRules, DATA_MODEL_CLASSES, withFilterIdentity, describeFilter, INTERNAL_DOC_IDS, INTERNAL_DOC_ID_PREFIXES, INTERNAL_DOC_CLASSES, OPTIONAL_INTERNAL_DOC_CLASSES, deriveTenantScope, classTenants, } from "./sync/index.js";
308
+ export type { SyncDirection, SyncState, SyncStatus, StackSyncOptions, DocStackSyncOptions, RemoteResolver, SyncMetaDoc, InternalDocFilterOptions, ClassFilterOptions, TenantScope, } from "./sync/index.js";
278
309
  export type { ClassBuildOptions } from "./class.js";
279
310
  export { CONTENT_EXPORT_FORMAT, META_CLASSES, isContentClassName, isContentDocument, isContentRelation, stripTransientFields, } from "./content-transfer.js";
280
311
  export type { ContentExport, ContentExportOptions, ContentImportOptions, ContentImportReport, ContentImportIssue, } from "./content-transfer.js";
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The schedule grammar a client can actually honour.
3
+ *
4
+ * `JobModel.schedule` is a string, and the obvious thing to put in it is cron. Cron is
5
+ * not offered here, and the reason is not implementation cost: cron's entire vocabulary
6
+ * is about *naming occurrences* — "02:15 on the 3rd of every month" — and a client
7
+ * cannot promise to be running at an occurrence. It is a closed tab, a suspended app, a
8
+ * sleeping laptop. Accepting the syntax would promise a precision the runtime has no way
9
+ * to keep, and the failure would be silent: the job simply never runs on the 3rd.
10
+ *
11
+ * So the grammar says only what a client can honour, which is a floor rather than a
12
+ * moment: *not more often than this*.
13
+ *
14
+ * | Form | Meaning |
15
+ * | --- | --- |
16
+ * | `@every 30m`, `@every 6h`, `@every 7d` | Fixed interval since the last run. |
17
+ * | `@hourly` | Top of each local hour. |
18
+ * | `@daily` | Local midnight. |
19
+ * | `@daily@09:00` | A local wall-clock time. |
20
+ * | `@weekly` | Monday, local midnight. |
21
+ * | `@weekly@09:00` | Monday, at a local wall-clock time. |
22
+ *
23
+ * Anchored forms are computed against *local* time through `Date`, so they follow the
24
+ * device across daylight-saving changes: `@daily@09:00` stays 09:00 to the person
25
+ * reading the screen, which is the only definition of "nine" that a campaign cares
26
+ * about.
27
+ *
28
+ * @module
29
+ */
30
+ /** Shortest interval `@every` will accept. Below this a client is polling, not scheduling. */
31
+ export declare const MIN_PERIOD_MS = 30000;
32
+ /** A schedule string after parsing. `source` is kept so callers can report what they read. */
33
+ export type ParsedSchedule = {
34
+ kind: "interval";
35
+ source: string;
36
+ periodMs: number;
37
+ } | {
38
+ kind: "hourly";
39
+ source: string;
40
+ } | {
41
+ kind: "daily";
42
+ source: string;
43
+ minutes: number;
44
+ } | {
45
+ kind: "weekly";
46
+ source: string;
47
+ weekday: number;
48
+ minutes: number;
49
+ };
50
+ /**
51
+ * Reads a schedule string, or returns `null` when it is not one.
52
+ *
53
+ * `null` is a value the scheduler acts on rather than an error to throw: a job document
54
+ * carrying a schedule nobody can parse should be skipped and reported, not allowed to
55
+ * take down the tick that would have run the other jobs.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * parseSchedule("@every 6h"); // { kind: "interval", periodMs: 21600000, ... }
60
+ * parseSchedule("@daily@09:00"); // { kind: "daily", minutes: 540, ... }
61
+ * parseSchedule("0 9 * * *"); // null — cron is not accepted, see the module docblock
62
+ * ```
63
+ */
64
+ export declare const parseSchedule: (schedule: string | null | undefined) => ParsedSchedule | null;
65
+ /**
66
+ * The first moment this schedule comes due strictly after `from`.
67
+ *
68
+ * **The occurrences between the last run and `from` are not returned, and there is no
69
+ * way to ask for them.** That absence is the design: a device that was closed for a
70
+ * fortnight has missed fourteen occurrences of a daily job, and replaying them would run
71
+ * the campaign fourteen times over data that only justifies running it once. A sweep
72
+ * that reads current state does the right thing in a single pass; fourteen sweeps do the
73
+ * same thing plus a stampede.
74
+ *
75
+ * @param schedule - A parsed schedule.
76
+ * @param from - The instant to measure from, normally "now".
77
+ * @returns The next due timestamp, strictly greater than `from`.
78
+ */
79
+ export declare const nextOccurrence: (schedule: ParsedSchedule, from: number) => number;
80
+ /** The longest a schedule may legitimately wait — the ceiling used to detect a bad clock. */
81
+ export declare const periodCeilingMs: (schedule: ParsedSchedule) => number;
82
+ /**
83
+ * Whether a stored `nextRunAt` is too far in the future to have been computed honestly.
84
+ *
85
+ * The device clock belongs to the user: it can be wrong, and it can be set back. A run
86
+ * recorded while the clock read 2031 leaves a `nextRunAt` that would suppress the job for
87
+ * years once the clock is corrected. Anything further out than two periods did not come
88
+ * from this schedule, so the scheduler recomputes it from now rather than honouring it.
89
+ */
90
+ export declare const isImplausible: (nextRunAt: number, schedule: ParsedSchedule, now: number) => boolean;
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Running jobs unattended, on a client.
3
+ *
4
+ * {@link JobEngine} executes a job when something asks it to. This decides *when* to
5
+ * ask, on a device that a server-side scheduler's assumptions do not describe:
6
+ *
7
+ * 1. **The app is closed most of the time.** "Daily at 09:00" is missed on most days.
8
+ * 2. **Timers are throttled or frozen.** A background tab gets about one tick a minute;
9
+ * a suspended app gets none, and `setInterval` does not catch up on wake.
10
+ * 3. **There are several instances.** Two devices, or two tabs, run this against
11
+ * replicas of the same `~Job` documents.
12
+ * 4. **A run can vanish mid-flight.** A closed tab kills a `RUNNING` job with no
13
+ * `catch`, no `finally`, and no process left to notice.
14
+ * 5. **The clock is the user's.** It can be wrong, and it can move backwards.
15
+ *
16
+ * Four rules answer those, and each is load-bearing:
17
+ *
18
+ * - **Missed occurrences collapse into one run** ({@link nextOccurrence}). Never a
19
+ * backlog.
20
+ * - **Schedule state is device-local**, in a `_local/` document. `JobModel.nextRunTimestamp`
21
+ * looks like the place for it and is not: an application's `~Job` documents replicate —
22
+ * `DATA_MODEL_CLASSES` keeps them even under an `include` allow-list — so every device
23
+ * would write that field on every run and collide on a document whose `content` field is
24
+ * executable code. A losing revision there does not lose a timestamp, it forks what the
25
+ * job does.
26
+ * - **Duplicate work is answered by the jobs, not by a lock.** Leader election needs a
27
+ * consensus point that two offline replicas do not have. Jobs that run here must write
28
+ * documents whose `_id` is derived from what they are about (`ReviewRequest-<orderId>`),
29
+ * so a second device's sweep collides into one document instead of sending a second
30
+ * email. The scheduler cannot enforce that; it is the price of running campaign logic
31
+ * on clients.
32
+ * - **Only named jobs run unattended** ({@link SchedulerOptions.jobs}). `~Job.content` is
33
+ * JavaScript, it replicates, and {@link Job} hydrates it with `new Function` — which
34
+ * runs with full ambient authority, whatever the docs call it. Until now a human was
35
+ * always behind an execution. An allow-list keeps that true: a job document arriving
36
+ * over sync cannot become code that runs itself.
37
+ *
38
+ * @module
39
+ */
40
+ import type { JobRunModel } from "@docstack/shared";
41
+ /** The `_local/` document holding this device's schedule state. Never replicates. */
42
+ export declare const JOB_SCHEDULE_DOC_ID = "_local/docstack-job-schedule";
43
+ /** What the scheduler needs from a stack. Narrow on purpose, so it can be tested without one. */
44
+ export interface SchedulerHost {
45
+ db: {
46
+ get: (id: string) => Promise<any>;
47
+ put: (doc: any) => Promise<any>;
48
+ bulkDocs: (docs: any[]) => Promise<any>;
49
+ find: (request: any) => Promise<{
50
+ docs: any[];
51
+ }>;
52
+ };
53
+ jobEngine: {
54
+ executeJob: (jobId: string, runtimeArgs?: Record<string, any>, triggerType?: string) => Promise<JobRunModel>;
55
+ };
56
+ }
57
+ /** Per-job bookkeeping, as stored in {@link JOB_SCHEDULE_DOC_ID}. */
58
+ export interface JobScheduleState {
59
+ /** When this job next comes due on this device. */
60
+ nextRunAt: number;
61
+ /** When this device last dispatched it. */
62
+ lastRunAt?: number;
63
+ /** Outcome of that dispatch. */
64
+ lastStatus?: string;
65
+ /** Drives the backoff. Reset by any success. */
66
+ consecutiveFailures: number;
67
+ /** The schedule string this state was computed from; a change to it resets the state. */
68
+ schedule?: string;
69
+ }
70
+ /** Why a due-looking job was not dispatched. Reported rather than thrown. */
71
+ export type SkipReason = "missing" | "disabled" | "no-schedule" | "unparseable-schedule" | "hash-mismatch" | "in-flight" | "not-due";
72
+ export interface TickReport {
73
+ /** The instant the tick was evaluated at. */
74
+ at: number;
75
+ /** Jobs dispatched by this tick. They may still be running when the report is returned. */
76
+ dispatched: string[];
77
+ skipped: {
78
+ jobId: string;
79
+ reason: SkipReason;
80
+ }[];
81
+ /** Abandoned `RUNNING` runs moved to `CANCELED`. */
82
+ sweptRuns: number;
83
+ }
84
+ export interface SchedulerOptions {
85
+ /**
86
+ * Job ids allowed to run unattended.
87
+ *
88
+ * Required, and there is deliberately no "all": see the module docblock. Manual
89
+ * {@link JobEngine.executeJob} is unaffected — it already has a human behind it.
90
+ */
91
+ jobs: string[];
92
+ /**
93
+ * Expected `hash` per job, for jobs whose code must not change under the application.
94
+ *
95
+ * `JobModel.hash` is stored beside the content it certifies, so a peer that writes
96
+ * one writes the other: on its own it detects corruption, not authorship. Pinning the
97
+ * value *in application code* is what makes it mean something.
98
+ */
99
+ pinnedHashes?: Record<string, string>;
100
+ /** Floor between automatic ticks. Default 60s, minimum 5s. */
101
+ intervalMs?: number;
102
+ /** A `RUNNING` run older than this is swept to `CANCELED`. Default 15 minutes. */
103
+ staleRunMs?: number;
104
+ /** First retry delay after a failure; doubles per consecutive failure. Default 5 minutes. */
105
+ backoffBaseMs?: number;
106
+ /** Ceiling for that doubling. Default 6 hours. */
107
+ maxBackoffMs?: number;
108
+ /** Injectable clock, for tests. */
109
+ now?: () => number;
110
+ /** Called with each completed run, successful or not. */
111
+ onRun?: (run: JobRunModel) => void;
112
+ }
113
+ /**
114
+ * Decides when the jobs an application has approved should run, and dispatches them.
115
+ *
116
+ * Mounted at `stack.jobScheduler`, but never started by the stack: what may run
117
+ * unattended is the application's decision.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * stack.jobScheduler.start({
122
+ * jobs: ["Job-review-campaign", "Job-cross-sell"],
123
+ * pinnedHashes: { "Job-review-campaign": "9f2c…" },
124
+ * });
125
+ *
126
+ * // Wake sources are the application's, because `core/` imports no DOM:
127
+ * document.addEventListener("visibilitychange", () => {
128
+ * if (document.visibilityState === "visible") void stack.jobScheduler.tick();
129
+ * });
130
+ * ```
131
+ */
132
+ export declare class JobScheduler {
133
+ private readonly host;
134
+ private options;
135
+ private timer;
136
+ /** Jobs this device has dispatched and not yet seen finish. */
137
+ private readonly inFlight;
138
+ /** Dispatches still running, so {@link drain} can wait for them. */
139
+ private readonly pending;
140
+ /** Deduplicates concurrent ticks — a wake signal and the interval can land together. */
141
+ private ticking;
142
+ /** Serialises read-modify-write of the `_local/` document. */
143
+ private stateWrites;
144
+ /** Last state read, for {@link status} — reporting must not require a database round-trip. */
145
+ private snapshot;
146
+ constructor(host: SchedulerHost);
147
+ /**
148
+ * Begins scheduling, and evaluates once immediately.
149
+ *
150
+ * The immediate evaluation is the point: a client's most reliable clock signal is
151
+ * "the app just opened", not a timer that was frozen while it was closed.
152
+ */
153
+ start(options: SchedulerOptions): void;
154
+ /**
155
+ * Stops scheduling. Jobs already dispatched keep running — a hydrated `new Function`
156
+ * has no cancellation, and pretending otherwise would leave a `RUNNING` run behind.
157
+ */
158
+ stop(): void;
159
+ /** Whether {@link start} is in effect. */
160
+ get isRunning(): boolean;
161
+ /**
162
+ * Evaluates every allowed job and dispatches those that are due.
163
+ *
164
+ * Idempotent and safe to call from any wake signal. Concurrent calls share one
165
+ * evaluation. It returns once dispatch has *started*: a long job does not hold the
166
+ * tick open, because a tick that waits is a tick that stops the others.
167
+ */
168
+ tick(): Promise<TickReport>;
169
+ /** Resolves when every job this scheduler started has finished. For teardown and tests. */
170
+ drain(): Promise<void>;
171
+ /** What the scheduler believes, without touching the database. */
172
+ status(): {
173
+ running: boolean;
174
+ inFlight: string[];
175
+ jobs: Record<string, JobScheduleState>;
176
+ };
177
+ private runTick;
178
+ /** Runs one job and records what happened. Never throws: a tick outlives its jobs. */
179
+ private dispatch;
180
+ private recordOutcome;
181
+ /**
182
+ * Moves this device's abandoned runs to `CANCELED`.
183
+ *
184
+ * A `~JobRun` only changes status inside `Job.execute`'s `try`/`catch`, so a tab
185
+ * closed mid-run leaves one `RUNNING` for ever — and `hasRunningInstance` then skips
186
+ * that singleton job on this device permanently. Unattended execution turns that from
187
+ * a latent oddity into a job that silently stops working, so the sweep runs before
188
+ * every dispatch. `~JobRun` never replicates, so these are unambiguously *this*
189
+ * device's abandoned runs.
190
+ */
191
+ private sweepAbandonedRuns;
192
+ private readState;
193
+ private writeState;
194
+ /**
195
+ * Read-modify-write against the `_local/` document, serialised.
196
+ *
197
+ * Everything that touches the state document goes through here - tick evaluation
198
+ * and outcome recording alike - so within this instance no write can land between
199
+ * another's read and write. Two *instances* (two tabs) still contend; there the 409
200
+ * in {@link writeState} stands, logged, and the jobs' own idempotency is the answer,
201
+ * as the module docblock requires of them.
202
+ */
203
+ private withState;
204
+ private track;
205
+ }
206
+ export default JobScheduler;
@@ -6,6 +6,7 @@ import { StackSyncHandle } from "./sync/index.js";
6
6
  import type { StackSyncOptions, SyncStatus } from "./sync/index.js";
7
7
  import type { SelectAST, UnionAST } from "./query-engine/index.js";
8
8
  import { JobEngine } from "./job-engine/index.js";
9
+ import { JobScheduler } from "./job-engine/scheduler.js";
9
10
  import { PolicyEngine } from "./policy-engine/index.js";
10
11
  import { CryptoEngine } from "./crypto-engine/index.js";
11
12
  import type { ContentExport, ContentExportOptions, ContentImportOptions, ContentImportReport } from "./content-transfer.js";
@@ -144,6 +145,19 @@ declare class ClientStack extends Stack {
144
145
  * ```
145
146
  */
146
147
  jobEngine: JobEngine;
148
+ /**
149
+ * Decides when approved jobs run, and dispatches them.
150
+ *
151
+ * Constructed with the stack but deliberately **not started by it**: `~Job.content`
152
+ * is JavaScript that replicates, so which jobs may run with nobody watching is the
153
+ * application's decision, named at {@link JobScheduler.start}.
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * stack.jobScheduler.start({ jobs: ['Job-review-campaign'] });
158
+ * ```
159
+ */
160
+ jobScheduler: JobScheduler;
147
161
  /**
148
162
  * Engine for enforcing read/write access control policies.
149
163
  * Policies are evaluated based on user session and document content.
@@ -296,6 +296,15 @@ export interface DocStackSyncOptions extends Omit<StackSyncOptions, "remote"> {
296
296
  * Which stacks to sync. Defaults to all of them.
297
297
  */
298
298
  stacks?: string[];
299
+ /**
300
+ * The tenant entitlement this replication serves, compiled into per-stack
301
+ * configuration by {@link deriveTenantScope}: stacks outside the scope are not
302
+ * synced at all - withheld structurally, not filtered - and stacks holding a mix of
303
+ * declarations get a class filter. Combines with `stacks` (which pre-narrows the
304
+ * candidates) but not with `classes`, whose slot the compiled rules occupy; narrow
305
+ * further with `filter`. See ADR-0030.
306
+ */
307
+ tenants?: string[];
299
308
  }
300
309
  /**
301
310
  * Every stack's replication under one object.
@@ -315,6 +324,13 @@ export declare class DocStackSyncHandle extends EventTarget {
315
324
  readonly handles: Map<string, StackSyncHandle>;
316
325
  /** @internal - use {@link DocStack.sync}. */
317
326
  add(name: string, handle: StackSyncHandle): void;
327
+ /** The stacks this handle covers. What is missing from this list is not
328
+ * replicating - compare against `DocStack.getStacks()`, or use
329
+ * `DocStack.getSyncCoverage()` which does exactly that. */
330
+ get names(): string[];
331
+ /** @internal - use {@link DocStack.removeStack}. Cancels and drops one stack's
332
+ * replication; the rest are untouched. */
333
+ remove(name: string): boolean;
318
334
  /** Every stack's status, keyed by stack name. */
319
335
  getStatus(): Record<string, SyncStatus>;
320
336
  /**
@@ -331,4 +347,6 @@ export { createReplicationFilter, isInternalDoc, resolveInternalClasses, INTERNA
331
347
  export type { InternalDocFilterOptions } from "./internal-docs.js";
332
348
  export { createClassFilter, hasClassRules, DATA_MODEL_CLASSES } from "./class-filter.js";
333
349
  export type { ClassFilterOptions } from "./class-filter.js";
350
+ export { deriveTenantScope, classTenants } from "./tenants.js";
351
+ export type { TenantScope } from "./tenants.js";
334
352
  export { withFilterIdentity, describeFilter, composeFilterIdentity } from "./filter-identity.js";
@@ -0,0 +1,67 @@
1
+ import type { ClassModel } from "@docstack/shared";
2
+ import type { ClassFilterOptions } from "./class-filter.js";
3
+ import type ClientStack from "../stack.js";
4
+ /**
5
+ * Tenant scoping for replication channels.
6
+ *
7
+ * A tenant is a stack (ADR-0030). `Class.tenants` declares which tenant spaces a class
8
+ * belongs to - a static list, so partitioning and channel scopes are derivable before
9
+ * any data exists. This module compiles a channel's *entitlement* (the tenants it may
10
+ * see) into configuration the sync layer already understands: which stacks the channel
11
+ * is served at all, and a class filter for stacks holding a mix of declarations.
12
+ *
13
+ * The split matters (ADR-0030 §5): the *partition* is the datamodel's, but the
14
+ * *entitlement* is the serving side's to grant - it cannot come from the datamodel,
15
+ * because every application ships its own model and none is authoritative about the
16
+ * others' entitlements. Enforcement then rides the existing filter chain, which is a
17
+ * conjunction: a derived filter can only narrow what the declarations admit.
18
+ *
19
+ * The strongest grant here is the one that is not a filter at all: a stack outside the
20
+ * scope is simply never served, so its namespace is unreachable rather than filtered.
21
+ *
22
+ * @module
23
+ */
24
+ /** A channel's entitlement, compiled into sync configuration. */
25
+ export interface TenantScope {
26
+ /**
27
+ * Stacks this entitlement is served at all. A stack not listed is withheld
28
+ * structurally - no replication is started against it, which is a stronger grant
29
+ * than any filter.
30
+ */
31
+ stacks: string[];
32
+ /**
33
+ * Class rules per served stack, present only where the stack needs them: an
34
+ * `exclude` where classes declared for other tenants share an entitled stack, an
35
+ * `include` where a stack is served only because entitled classes live in it.
36
+ */
37
+ classes: Record<string, ClassFilterOptions>;
38
+ }
39
+ /**
40
+ * Normalizes a class model's tenant declaration.
41
+ *
42
+ * @param model - Any object carrying (or omitting) a `tenants` declaration.
43
+ * @returns The declared tenant names; empty for a tenant-neutral class.
44
+ */
45
+ export declare const classTenants: (model: Partial<Pick<ClassModel, "tenants">> | null | undefined) => string[];
46
+ /**
47
+ * Compiles an entitlement into the stacks it reaches and the class rules it needs.
48
+ *
49
+ * A stack is served when its name is an entitled tenant - a tenant *is* a stack - or
50
+ * when it holds at least one class declared for an entitled tenant. Within a served
51
+ * stack:
52
+ *
53
+ * - classes declared for an entitled tenant travel;
54
+ * - tenant-neutral classes follow their stack: they travel when the stack itself is the
55
+ * entitled tenant, which is what keeps a datamodel with no declarations at today's
56
+ * behavior exactly;
57
+ * - classes declared only for other tenants are excluded.
58
+ *
59
+ * Resolved once per call, the way the sync layer resolves ephemeral classes when a
60
+ * replication starts (ADR-0028): a declaration that changes later takes effect on the
61
+ * next `sync()`, never silently mid-stream.
62
+ *
63
+ * @param stacks - The candidate stacks, typically every open stack.
64
+ * @param entitlement - The tenant names this channel may see.
65
+ * @returns Which stacks to serve, and per-stack class rules where needed.
66
+ */
67
+ export declare const deriveTenantScope: (stacks: ClientStack[], entitlement: string[]) => Promise<TenantScope>;
package/lib/index.d.ts CHANGED
@@ -1,5 +1,16 @@
1
1
  import { DocStack } from "./core/index.js";
2
2
  export { ClientStack, Class, Domain, Attribute, Trigger, DocStack } from "./core/index.js";
3
+ /**
4
+ * Running jobs unattended.
5
+ *
6
+ * `JobEngine` executes a job when asked; `JobScheduler` decides when to ask, under the
7
+ * constraints a client imposes — an app that is closed most of the time, timers that
8
+ * freeze, several devices holding replicas of the same `~Job`, and job content that
9
+ * replicates and is executable. It is mounted at `stack.jobScheduler` and started by the
10
+ * application, which names the jobs allowed to run with nobody watching.
11
+ */
12
+ export { JobEngine, JobScheduler, JOB_SCHEDULE_DOC_ID, parseSchedule, nextOccurrence } from "./core/index.js";
13
+ export type { SchedulerOptions, SchedulerHost, JobScheduleState, TickReport, SkipReason, ParsedSchedule, } from "./core/index.js";
3
14
  /**
4
15
  * The sync layer: lifecycle, filtering, convergence state and the schema gate.
5
16
  *
@@ -7,7 +18,7 @@ export { ClientStack, Class, Domain, Attribute, Trigger, DocStack } from "./core
7
18
  * application hands over, so DocStack never learns about Google Drive, Firestore or
8
19
  * anything else, and no consumer pays for a transport it does not use.
9
20
  */
10
- export { StackSyncHandle, DocStackSyncHandle, SyncSchemaMismatchError, SYNC_META_DOC_ID, readRemoteSchemaVersion, publishSchemaVersion, createReplicationFilter, isInternalDoc, resolveInternalClasses, createClassFilter, hasClassRules, DATA_MODEL_CLASSES, withFilterIdentity, describeFilter, INTERNAL_DOC_IDS, INTERNAL_DOC_ID_PREFIXES, INTERNAL_DOC_CLASSES, OPTIONAL_INTERNAL_DOC_CLASSES, StackWriteGuardError, StackLockedError, deriveKeyId, isEncryptedPayload, } from "./core/index.js";
21
+ export { StackSyncHandle, DocStackSyncHandle, SyncSchemaMismatchError, SYNC_META_DOC_ID, readRemoteSchemaVersion, publishSchemaVersion, createReplicationFilter, isInternalDoc, resolveInternalClasses, createClassFilter, hasClassRules, DATA_MODEL_CLASSES, withFilterIdentity, describeFilter, INTERNAL_DOC_IDS, INTERNAL_DOC_ID_PREFIXES, INTERNAL_DOC_CLASSES, OPTIONAL_INTERNAL_DOC_CLASSES, StackWriteGuardError, StackLockedError, deriveKeyId, isEncryptedPayload, deriveTenantScope, classTenants, } from "./core/index.js";
11
22
  export { SYSTEM_SEEDED_DOC_IDS, collectQueryClasses } from "./core/index.js";
12
23
  export type { EncryptedPayload, ClassBuildOptions } from "./core/index.js";
13
24
  /**
@@ -15,7 +26,7 @@ export type { EncryptedPayload, ClassBuildOptions } from "./core/index.js";
15
26
  */
16
27
  export { CONTENT_EXPORT_FORMAT, META_CLASSES, isContentClassName, isContentDocument, isContentRelation, } from "./core/index.js";
17
28
  export type { ContentExport, ContentExportOptions, ContentImportOptions, ContentImportReport, ContentImportIssue, } from "./core/index.js";
18
- export type { SyncDirection, SyncState, SyncStatus, StackSyncOptions, DocStackSyncOptions, RemoteResolver, SyncMetaDoc, InternalDocFilterOptions, ClassFilterOptions, } from "./core/index.js";
29
+ export type { SyncDirection, SyncState, SyncStatus, StackSyncOptions, DocStackSyncOptions, RemoteResolver, SyncMetaDoc, InternalDocFilterOptions, ClassFilterOptions, TenantScope, } from "./core/index.js";
19
30
  /**
20
31
  * Document-modelling types, re-exported from `@docstack/shared`.
21
32
  *