@plitzi/sdk-shared 0.38.20 → 0.38.22

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 (39) hide show
  1. package/CHANGELOG.md +133 -0
  2. package/dist/actions/access.d.ts +5 -0
  3. package/dist/actions/access.mjs +11 -2
  4. package/dist/actions/index.d.ts +2 -1
  5. package/dist/actions/index.mjs +10 -9
  6. package/dist/actions/jobs.d.ts +10 -0
  7. package/dist/actions/jobs.mjs +17 -0
  8. package/dist/actions/validateDocument.mjs +56 -56
  9. package/dist/dataSource/globalSources.d.ts +1 -1
  10. package/dist/navigation/routes.mjs +38 -37
  11. package/dist/network/graphql/builder/Mutations/Space/jobs/SpaceControlJobMutation.d.ts +5 -0
  12. package/dist/network/graphql/builder/Mutations/Space/jobs/SpaceControlJobMutation.mjs +4 -0
  13. package/dist/network/graphql/builder/Mutations/index.d.ts +2 -0
  14. package/dist/network/graphql/builder/Mutations/index.mjs +70 -68
  15. package/dist/network/graphql/builder/Queries/Space/SpaceDeploymentsQuery.d.ts +1 -1
  16. package/dist/network/graphql/builder/Queries/Space/SpaceDeploymentsQuery.mjs +1 -1
  17. package/dist/network/graphql/builder/Queries/Space/SpaceJobsQuery.d.ts +10 -0
  18. package/dist/network/graphql/builder/Queries/Space/SpaceJobsQuery.mjs +4 -0
  19. package/dist/network/graphql/builder/Queries/index.d.ts +2 -0
  20. package/dist/network/graphql/builder/Queries/index.mjs +16 -14
  21. package/dist/realtime/index.d.ts +1 -1
  22. package/dist/realtime/index.mjs +4 -4
  23. package/dist/realtime/topics.d.ts +5 -0
  24. package/dist/realtime/topics.mjs +11 -11
  25. package/dist/schema/index.d.ts +1 -0
  26. package/dist/schema/index.mjs +12 -11
  27. package/dist/schema/pageAccess.d.ts +41 -0
  28. package/dist/schema/pageAccess.mjs +54 -0
  29. package/dist/store/storeHooks.d.ts +33 -33
  30. package/dist/types/ActionTypes.d.ts +0 -204
  31. package/dist/types/FunctionTypes.d.ts +41 -0
  32. package/dist/types/JobTypes.d.ts +327 -0
  33. package/dist/types/JobTypes.mjs +0 -0
  34. package/dist/types/ServerTypes.d.ts +17 -3
  35. package/dist/types/SpaceTypes.d.ts +5 -0
  36. package/dist/types/index.d.ts +1 -0
  37. package/package.json +1 -1
  38. package/vite.config.ts +9 -7
  39. package/dist/helpers/twigWrapper/bench.bench.d.ts +0 -1
@@ -542,208 +542,4 @@ export type ActionStreamFrame = {
542
542
  event: 'error';
543
543
  data: ActionCallError;
544
544
  };
545
- /**
546
- * Where a job is, as the only durable answer to "did it happen".
547
- *
548
- * `pending` is waiting to be taken, `running` is held by a worker under a lease, and the other four are terminal —
549
- * except that an operator may put a `failed`, `dead` or `cancelled` one back to `pending`. `dead` is a job that
550
- * used up every attempt it was given: it stays, visibly, because the alternative is work that quietly evaporated.
551
- */
552
- export type ActionJobStatus = 'pending' | 'running' | 'succeeded' | 'failed' | 'dead' | 'cancelled';
553
- /**
554
- * One go at a job, kept forever on the job itself.
555
- *
556
- * `lost` is the attempt nobody reported: a worker took the job, its lease ran out, and another worker found it
557
- * abandoned. It is the record of a replica that died mid-run, which is otherwise the one failure that leaves no
558
- * trace anywhere — and the reason this list exists rather than a single `error` field.
559
- */
560
- export type ActionJobAttempt = {
561
- attempt: number;
562
- status: 'succeeded' | 'failed' | 'cancelled' | 'lost';
563
- /** Epoch ms, from the QUEUE's clock — never a worker's own. */
564
- startedAt: number;
565
- endedAt: number;
566
- /** Which worker held it, so a bad replica is identifiable from the history alone. */
567
- workerId: string;
568
- /** The run this attempt became, when it got far enough to be one. Joins to the run history. */
569
- runId?: string;
570
- error?: string;
571
- };
572
- /** What starting a job needs. Everything else is the queue's to fill in. */
573
- export type ActionJobInput = {
574
- /**
575
- * The job's identity, and the whole of its exactly-once guarantee.
576
- *
577
- * A schedule derives it from the fire it belongs to — `schedule:<space>:<action>:<instant>` — so two replicas
578
- * reaching the same fire write the same id and the second one is a no-op. Enqueue is idempotent by this, which
579
- * is what lets a producer crash between enqueuing and recording that it did.
580
- */
581
- id: string;
582
- spaceId: number;
583
- actionId: string;
584
- environment: Environment;
585
- trigger: ActionTriggerType;
586
- input: Record<string, unknown>;
587
- /** The instant this job was due, as the schedule stored it. Epoch ms. */
588
- dueAt: number;
589
- maxAttempts: number;
590
- /** Fires that went by unclaimed before this one. Shown to an operator; never acted on. */
591
- missed?: number;
592
- /**
593
- * What a run set to run later (`trigger: 'later'`) was named by: setting it again under the same name replaces the
594
- * one still waiting, and cancelling it by name drops it.
595
- */
596
- key?: string;
597
- };
598
- /** One unit of work the queue is holding, as an operator sees it. */
599
- export type ActionJob = ActionJobInput & {
600
- status: ActionJobStatus;
601
- /** Epoch ms before which no worker may take it — a retry's backoff, or the moment it was enqueued. */
602
- runAt: number;
603
- attempts: number;
604
- /** While `running`: the instant the holder's claim lapses and another worker may take it over. */
605
- leaseUntil?: number;
606
- workerId?: string;
607
- runId?: string;
608
- /** Set by an operator who wants it stopped; the worker holding it reads this and aborts. */
609
- cancelRequested?: boolean;
610
- createdAt: number;
611
- updatedAt: number;
612
- /** Why the last attempt ended badly. */
613
- error?: string;
614
- history: ActionJobAttempt[];
615
- };
616
- /**
617
- * How a worker reports what became of a claim.
618
- *
619
- * `delayMs` rather than an instant, and no timestamps: every moment the queue records is read from the QUEUE's
620
- * clock, because a cluster whose nodes sit in different countries agrees about durations and not about now.
621
- */
622
- export type ActionJobSettlement = {
623
- jobId: string;
624
- workerId: string;
625
- status: Extract<ActionJobStatus, 'succeeded' | 'failed' | 'dead' | 'cancelled' | 'pending'>;
626
- /** For a retry: how long from now before it may be claimed again. */
627
- delayMs?: number;
628
- runId?: string;
629
- error?: string;
630
- /** The attempt to append to the history. Omitted only when the claim is being handed back untouched. */
631
- attempt?: Omit<ActionJobAttempt, 'attempt' | 'startedAt' | 'endedAt'>;
632
- };
633
- export type ActionJobQuery = {
634
- spaceIds: number[];
635
- actionId?: string;
636
- status?: ActionJobStatus;
637
- limit: number;
638
- offset: number;
639
- };
640
- /**
641
- * A recurring intent, as a row rather than as a timer.
642
- *
643
- * `nextRunAt` is the point of the whole design: the instant of the next fire is DECIDED ONCE and written down, so
644
- * every replica afterwards reads the same number instead of asking its own clock whether the moment has come. Two
645
- * nodes whose clocks differ by a minute then produce the same fires, in the same order, exactly once each — the
646
- * disagreement moves the WHEN by their skew and can never move the WHETHER.
647
- */
648
- export type ActionSchedule = {
649
- spaceId: number;
650
- actionId: string;
651
- cron: string;
652
- timezone?: string;
653
- environment: Environment;
654
- enabled: boolean;
655
- /** Epoch ms of the next fire this schedule owes. The queue's clock decides when that has arrived. */
656
- nextRunAt: number;
657
- /** The last fire actually enqueued. */
658
- lastFireAt?: number;
659
- /** Fires that went by while nothing was running to take them. Cumulative, and shown to an operator. */
660
- missed: number;
661
- maxAttempts: number;
662
- updatedAt: number;
663
- };
664
- /** A schedule as it is derived from an action document — everything the queue does not own itself. */
665
- export type ActionScheduleInput = Omit<ActionSchedule, 'missed' | 'updatedAt' | 'lastFireAt'>;
666
- /**
667
- * Where a deployment keeps its jobs and schedules.
668
- *
669
- * The same kind of seam as {@link ActionKvAdapter} and for the same reason — the server never learns where a
670
- * deployment stores anything — but with one rule this one cannot be written without, and it is written here so no
671
- * deployment has to rediscover it:
672
- *
673
- * **Every instant comes from the store.** `now` is the cluster's clock, and every comparison the queue makes
674
- * internally — is this schedule due, has this lease lapsed, may this retry be claimed — is made against it, never
675
- * against the caller's. That is why `settle` takes a `delayMs` and not a deadline, and why `claim` takes a
676
- * `leaseMs` and not a `leaseUntil`: durations survive a cluster spread across time zones and machines whose clocks
677
- * drift, and absolute instants minted by a caller do not.
678
- *
679
- * **`enqueue` is idempotent by `id`,** and answers whether it was the one that created the job. That, and nothing
680
- * else, is what makes a fire happen exactly once when several replicas are racing to produce it.
681
- *
682
- * **`claim` is atomic, and it also reaps.** A job whose lease has lapsed is a job whose worker died, and it is
683
- * taken by whoever claims next — the same operation, because a separate reaper is a second thing to deploy, to
684
- * schedule and to forget.
685
- */
686
- export type ActionJobQueue = {
687
- /** The cluster's clock. The only clock anything in the scheduling path is allowed to read. */
688
- now: () => Promise<Date>;
689
- /** True when this call created the job; false when that id was already there. Never throws on a duplicate. */
690
- enqueue: (job: ActionJobInput) => Promise<boolean>;
691
- /**
692
- * Takes up to `limit` jobs that are due, and any whose lease has lapsed — atomically, one worker per job.
693
- *
694
- * A reclaimed job arrives with its previous holder's attempt already written to the history as `lost`.
695
- */
696
- claim: (params: {
697
- workerId: string;
698
- leaseMs: number;
699
- limit: number;
700
- }) => Promise<ActionJob[]>;
701
- /** Extends the lease on jobs this worker still holds, and answers which of them an operator asked to cancel. */
702
- heartbeat: (params: {
703
- jobIds: string[];
704
- workerId: string;
705
- leaseMs: number;
706
- }) => Promise<string[]>;
707
- settle: (settlement: ActionJobSettlement) => Promise<void>;
708
- /** Due schedules, oldest fire first. */
709
- dueSchedules: (limit: number) => Promise<ActionSchedule[]>;
710
- /**
711
- * Moves a schedule forward, but only if it is still where the caller last saw it.
712
- *
713
- * The compare-and-set is what stops two replicas from advancing the same schedule twice. Answers false when
714
- * somebody else got there first, which is not an error — it is the other replica having done the work.
715
- */
716
- advanceSchedule: (params: {
717
- spaceId: number;
718
- actionId: string;
719
- from: number;
720
- to: number;
721
- missed: number;
722
- }) => Promise<boolean>;
723
- /** Replaces what this space has scheduled. An action no longer listed stops having a schedule at all. */
724
- putSchedules: (params: {
725
- spaceId: number;
726
- schedules: ActionScheduleInput[];
727
- }) => Promise<void>;
728
- listJobs: (query: ActionJobQuery) => Promise<{
729
- jobs: ActionJob[];
730
- total: number;
731
- }>;
732
- getJob: (spaceIds: number[], jobId: string) => Promise<ActionJob | undefined>;
733
- listSchedules: (spaceIds: number[]) => Promise<ActionSchedule[]>;
734
- /** An operator putting a finished job back in the queue. Answers false when it is not in a state to go back. */
735
- requeue: (spaceIds: number[], jobId: string) => Promise<boolean>;
736
- /** An operator stopping a job: dropped if it is waiting, aborted at its next step if it is running. */
737
- cancel: (spaceIds: number[], jobId: string) => Promise<boolean>;
738
- /**
739
- * Drops the space's jobs waiting under `key` (`cancelled`), and answers how many. With `olderThan`, only the ones
740
- * enqueued before that job — created earlier, or at the same instant with a smaller id — so of two set at once the
741
- * newer is the one left, on every replica. A running one is left: it is usually the run that set the next.
742
- */
743
- cancelPending: (params: {
744
- spaceId: number;
745
- key: string;
746
- olderThan?: string;
747
- }) => Promise<number>;
748
- };
749
545
  export {};
@@ -1,4 +1,6 @@
1
+ import { ActionAccess } from './ActionTypes';
1
2
  import { InteractionCallbackParam } from './InteractionTypes';
3
+ import { JobStatus } from './JobTypes';
2
4
  /**
3
5
  * A space's own functions, as everything outside the runner sees them: the builder, the CLI, the MCP and the
4
6
  * platform that stores them. The contract the code itself is written against is `@plitzi/sdk-server/functions`.
@@ -22,6 +24,37 @@ export type FunctionTaskManifest = {
22
24
  /** What it asks for beyond the default — over what the functions ask for all of their tasks. */
23
25
  limits?: FunctionTimeLimits;
24
26
  };
27
+ /** One job the functions declare (`defineFunctions({ jobs })`): everything but its code. */
28
+ export type FunctionJobManifest = {
29
+ /**
30
+ * Who may watch it besides whoever started it — a channel's access, read against the visitor's permissions in the
31
+ * space. Absent, only whoever started it does.
32
+ */
33
+ watch?: ActionAccess;
34
+ /** How long it may run, every slice together, in seconds — never past what the server allows. */
35
+ maxSeconds?: number;
36
+ };
37
+ /**
38
+ * A job as whoever may watch it sees it: what `ctx.jobs.get` answers, what `$job:<id>` says each time it moves, and
39
+ * what `useJob` holds. Never its input or what it saved: those are its code's.
40
+ */
41
+ export type FunctionJobView = {
42
+ id: string;
43
+ /** The job's name, as the functions declare it. */
44
+ name: string;
45
+ status: JobStatus;
46
+ /** From 0 to 1, as its code last reported. */
47
+ progress?: number;
48
+ /** What it is doing, in its code's words. */
49
+ step?: string;
50
+ /** What it answered, once it succeeded. */
51
+ result?: unknown;
52
+ /** Why it failed — for a refusal, its code's own words; for anything else, that it failed. */
53
+ error?: string;
54
+ /** Epoch ms. */
55
+ createdAt: number;
56
+ updatedAt: number;
57
+ };
25
58
  /** What a space's functions declare, read from the bundle when it is saved: its tasks, routes and reachable hosts. */
26
59
  /**
27
60
  * One OAuth 2.0 connection a space's code makes on its visitors' (or its own) behalf, declared as data: any provider,
@@ -45,6 +78,12 @@ export type FunctionOAuthConnection = {
45
78
  authorizeParams?: Record<string, string>;
46
79
  /** How the app proves who it is to `tokenUrl`: in the body (the default) or as HTTP Basic. */
47
80
  clientAuth?: 'body' | 'basic';
81
+ /**
82
+ * The code may read the access token (`ctx.oauth.token`), for a provider's SDK that runs in the page and asks for it
83
+ * itself — Spotify's Web Playback, Google's Picker. Off by default: the token then leaves the server, as far as the
84
+ * code hands it. The refresh token never does.
85
+ */
86
+ exposeToken?: boolean;
48
87
  };
49
88
  export type FunctionsManifest = {
50
89
  hosts: string[];
@@ -55,6 +94,8 @@ export type FunctionsManifest = {
55
94
  limits?: FunctionTimeLimits;
56
95
  /** The OAuth connections its code makes (`ctx.oauth`), by the name the code calls them. */
57
96
  oauth?: Record<string, FunctionOAuthConnection>;
97
+ /** The jobs it runs (`ctx.jobs.start`), by name. */
98
+ jobs?: Record<string, FunctionJobManifest>;
58
99
  };
59
100
  /** One reason functions were not saved, where it is — what an editor underlines. */
60
101
  export type FunctionsProblem = {
@@ -0,0 +1,327 @@
1
+ import { ActionAccess, ActionTriggerType } from './ActionTypes';
2
+ import { Environment } from './CommonTypes';
3
+ import { RunUser, SpaceRevision } from './ServerTypes';
4
+ /**
5
+ * The durable queue the server's work waits in: a schedule's fires, the runs set for later, and the jobs of the spaces'
6
+ * functions — one queue, one contract (`JobQueue`), whatever store a deployment keeps it in.
7
+ */
8
+ /**
9
+ * Where a job is, as the only durable answer to "did it happen".
10
+ *
11
+ * `pending` is waiting to be taken, `running` is held by a worker under a lease, `paused` is a job of a space's functions
12
+ * set aside with what it saved, until it is resumed — and the other four are terminal, except that an operator may put
13
+ * a `failed`, `dead` or `cancelled` one back to `pending`. `dead` is a job that used up every attempt it was given: it
14
+ * stays, visibly, because the alternative is work that quietly evaporated.
15
+ */
16
+ export type JobStatus = 'pending' | 'running' | 'paused' | 'succeeded' | 'failed' | 'dead' | 'cancelled';
17
+ /** Why a worker is asked to stop a job it holds: an operator or the code cancelled it, or set it aside. */
18
+ export type JobStop = 'cancel' | 'pause';
19
+ /** A run of one of the space's actions: a schedule's fire, a run set for later. */
20
+ export type ActionJobTarget = {
21
+ kind: 'action';
22
+ /** The action, by its identifier. */
23
+ name: string;
24
+ /** The trigger it is run through — what the action's document lets start it this way. */
25
+ trigger: ActionTriggerType;
26
+ };
27
+ /**
28
+ * One of the space's functions' jobs (`defineFunctions({ jobs })`), run slice by slice — kept as it was when it started:
29
+ * who started it, who may watch it, and the code it runs.
30
+ */
31
+ export type FunctionJobTarget = {
32
+ kind: 'function';
33
+ /** The job, by the name its functions declare it under. */
34
+ name: string;
35
+ /** Who started it: `ctx.user` in every slice, and the one who may always watch it. */
36
+ user?: RunUser;
37
+ /** Who else may watch it (`$job:<id>`): the job's `watch`, as it was declared when it started. */
38
+ watch?: ActionAccess;
39
+ /** The version of the space's code it was started from: every slice runs that code, whatever is published after. */
40
+ at?: SpaceRevision;
41
+ };
42
+ /** What a job runs: one of the space's actions, or one of its functions' jobs. */
43
+ export type JobTarget = ActionJobTarget | FunctionJobTarget;
44
+ /** How far a job is, as its code last said (`job.report`). */
45
+ export type JobProgress = {
46
+ /** From 0 to 1. */
47
+ fraction: number;
48
+ /** What it is doing, in its own words: `epoch 3 of 10`. */
49
+ step?: string;
50
+ /** Epoch ms, by the queue's clock. */
51
+ at: number;
52
+ };
53
+ /**
54
+ * One go at a job, kept forever on the job itself.
55
+ *
56
+ * `lost` is the attempt nobody reported: a worker took the job, its lease ran out, and another worker found it
57
+ * abandoned. It is the record of a replica that died mid-run, which is otherwise the one failure that leaves no
58
+ * trace anywhere — and the reason this list exists rather than a single `error` field.
59
+ */
60
+ export type JobAttempt = {
61
+ attempt: number;
62
+ status: 'succeeded' | 'failed' | 'cancelled' | 'paused' | 'lost';
63
+ /** Epoch ms, from the QUEUE's clock — never a worker's own. */
64
+ startedAt: number;
65
+ endedAt: number;
66
+ /** Which worker held it, so a bad replica is identifiable from the history alone. */
67
+ workerId: string;
68
+ /** The run this attempt became, when it got far enough to be one. Joins to the run history. */
69
+ runId?: string;
70
+ error?: string;
71
+ /** The error is the code's own refusal (`ActionRefusal`), written for whoever asked — what a job's watcher may read. */
72
+ refused?: true;
73
+ };
74
+ /** What starting a job needs. Everything else is the queue's to fill in. */
75
+ export type JobInput = {
76
+ /**
77
+ * The job's identity, and the whole of its exactly-once guarantee.
78
+ *
79
+ * A schedule derives it from the fire it belongs to — `schedule:<space>:<action>:<instant>` — so two replicas
80
+ * reaching the same fire write the same id and the second one is a no-op. Enqueue is idempotent by this, which
81
+ * is what lets a producer crash between enqueuing and recording that it did.
82
+ */
83
+ id: string;
84
+ spaceId: number;
85
+ environment: Environment;
86
+ /** What it runs — see {@link JobTarget}. */
87
+ target: JobTarget;
88
+ input: Record<string, unknown>;
89
+ /** The instant this job was due, as the schedule stored it. Epoch ms. */
90
+ dueAt: number;
91
+ maxAttempts: number;
92
+ /** Fires that went by unclaimed before this one. Shown to an operator; never acted on. */
93
+ missed?: number;
94
+ /**
95
+ * What a run set to run later (`trigger: 'later'`) was named by: setting it again under the same name replaces the
96
+ * one still waiting, and cancelling it by name drops it.
97
+ */
98
+ key?: string;
99
+ };
100
+ /** One unit of work the queue is holding, as an operator sees it. */
101
+ export type Job = JobInput & {
102
+ status: JobStatus;
103
+ /** Epoch ms before which no worker may take it — a retry's backoff, or the moment it was enqueued. */
104
+ runAt: number;
105
+ attempts: number;
106
+ /** While `running`: the instant the holder's claim lapses and another worker may take it over. */
107
+ leaseUntil?: number;
108
+ workerId?: string;
109
+ runId?: string;
110
+ /** Set by an operator who wants it stopped; the worker holding it reads this and aborts. */
111
+ cancelRequested?: boolean;
112
+ /** Set to set a running job aside: the worker holding it stops it, and it waits as `paused` until resumed. */
113
+ pauseRequested?: boolean;
114
+ createdAt: number;
115
+ updatedAt: number;
116
+ /** Why the last attempt ended badly. */
117
+ error?: string;
118
+ history: JobAttempt[];
119
+ /** What a job of the space's functions last saved (`job.save`): where its next slice starts from. */
120
+ state?: unknown;
121
+ progress?: JobProgress;
122
+ /** What a job of the space's functions answered, once it succeeded. */
123
+ result?: unknown;
124
+ /** How long it has run, every slice together, in ms — what its total duration is held to. */
125
+ activeMs?: number;
126
+ };
127
+ /** What a panel may do to a job: set it aside, put it back, stop it, or run a finished one again. */
128
+ export type JobControlAction = 'pause' | 'resume' | 'cancel' | 'retry';
129
+ /**
130
+ * A job as the space's editors read it in the builder — what it runs, where it is and how far — without what it was
131
+ * given, saved or answered (its code's own, up to megabytes) and with who started it said by name alone. What a job
132
+ * does not have is absent where the server writes it and `null` once GraphQL has carried it: both are allowed.
133
+ */
134
+ export type SpaceJob = Pick<Job, 'id' | 'environment' | 'status' | 'attempts' | 'maxAttempts' | 'dueAt' | 'createdAt' | 'updatedAt'> & {
135
+ kind: JobTarget['kind'];
136
+ /** The action's identifier, or the job's name as the functions declare it. */
137
+ name: string;
138
+ /** For a run of an action: the trigger it runs through — `schedule`, `later`. */
139
+ trigger?: ActionTriggerType | null;
140
+ /** For a job of the functions: who started it, by username or email. */
141
+ startedBy?: string | null;
142
+ /** From 0 to 1, as its code last reported. */
143
+ progress?: number | null;
144
+ /** What it is doing, in its code's words. */
145
+ step?: string | null;
146
+ error?: string | null;
147
+ activeMs?: number | null;
148
+ pauseRequested?: boolean | null;
149
+ cancelRequested?: boolean | null;
150
+ key?: string | null;
151
+ missed?: number | null;
152
+ };
153
+ /**
154
+ * How a worker reports what became of a claim.
155
+ *
156
+ * `delayMs` rather than an instant, and no timestamps: every moment the queue records is read from the QUEUE's
157
+ * clock, because a cluster whose nodes sit in different countries agrees about durations and not about now.
158
+ */
159
+ export type JobSettlement = {
160
+ jobId: string;
161
+ workerId: string;
162
+ status: Extract<JobStatus, 'succeeded' | 'failed' | 'dead' | 'cancelled' | 'pending' | 'paused'>;
163
+ /** For a retry: how long from now before it may be claimed again. */
164
+ delayMs?: number;
165
+ runId?: string;
166
+ error?: string;
167
+ /** The attempt to append to the history. Omitted only when the claim is being handed back untouched. */
168
+ attempt?: Omit<JobAttempt, 'attempt' | 'startedAt' | 'endedAt'>;
169
+ /** What a job of the space's functions answered, when it succeeded. */
170
+ result?: unknown;
171
+ /** How long this slice ran, added to the job's `activeMs`. */
172
+ spentMs?: number;
173
+ /**
174
+ * The attempt the claim took is given back: a slice that ended without failing — set aside, cut by its time, or by a
175
+ * replica going away — is not one. Absent, it is given back only for a claim handed back untouched (`pending`, no
176
+ * `attempt`).
177
+ */
178
+ refund?: boolean;
179
+ };
180
+ /** What a job's code saved while it runs — only by the worker holding it, so a stalled one cannot write over the next. */
181
+ export type JobWrite = {
182
+ jobId: string;
183
+ workerId: string;
184
+ state?: unknown;
185
+ progress?: Omit<JobProgress, 'at'>;
186
+ };
187
+ export type JobQuery = {
188
+ spaceIds: number[];
189
+ status?: JobStatus;
190
+ /** Only runs of actions, or only jobs of the spaces' functions. */
191
+ kind?: JobTarget['kind'];
192
+ /** Only what runs this action, or this job (`target.name`). */
193
+ name?: string;
194
+ limit: number;
195
+ offset: number;
196
+ };
197
+ /**
198
+ * A recurring intent, as a row rather than as a timer.
199
+ *
200
+ * `nextRunAt` is the point of the whole design: the instant of the next fire is DECIDED ONCE and written down, so
201
+ * every replica afterwards reads the same number instead of asking its own clock whether the moment has come. Two
202
+ * nodes whose clocks differ by a minute then produce the same fires, in the same order, exactly once each — the
203
+ * disagreement moves the WHEN by their skew and can never move the WHETHER.
204
+ */
205
+ export type ActionSchedule = {
206
+ spaceId: number;
207
+ actionId: string;
208
+ cron: string;
209
+ timezone?: string;
210
+ environment: Environment;
211
+ enabled: boolean;
212
+ /** Epoch ms of the next fire this schedule owes. The queue's clock decides when that has arrived. */
213
+ nextRunAt: number;
214
+ /** The last fire actually enqueued. */
215
+ lastFireAt?: number;
216
+ /** Fires that went by while nothing was running to take them. Cumulative, and shown to an operator. */
217
+ missed: number;
218
+ maxAttempts: number;
219
+ updatedAt: number;
220
+ };
221
+ /** A schedule as it is derived from an action document — everything the queue does not own itself. */
222
+ export type ActionScheduleInput = Omit<ActionSchedule, 'missed' | 'updatedAt' | 'lastFireAt'>;
223
+ /**
224
+ * Where a deployment keeps its jobs and schedules.
225
+ *
226
+ * The same kind of seam as {@link ActionKvAdapter} and for the same reason — the server never learns where a
227
+ * deployment stores anything — but with one rule this one cannot be written without, and it is written here so no
228
+ * deployment has to rediscover it:
229
+ *
230
+ * **Every instant comes from the store.** `now` is the cluster's clock, and every comparison the queue makes
231
+ * internally — is this schedule due, has this lease lapsed, may this retry be claimed — is made against it, never
232
+ * against the caller's. That is why `settle` takes a `delayMs` and not a deadline, and why `claim` takes a
233
+ * `leaseMs` and not a `leaseUntil`: durations survive a cluster spread across time zones and machines whose clocks
234
+ * drift, and absolute instants minted by a caller do not.
235
+ *
236
+ * **`enqueue` is idempotent by `id`,** and answers whether it was the one that created the job. That, and nothing
237
+ * else, is what makes a fire happen exactly once when several replicas are racing to produce it.
238
+ *
239
+ * **`claim` is atomic, and it also reaps.** A job whose lease has lapsed is a job whose worker died, and it is
240
+ * taken by whoever claims next — the same operation, because a separate reaper is a second thing to deploy, to
241
+ * schedule and to forget.
242
+ */
243
+ export type JobQueue = {
244
+ /** The cluster's clock. The only clock anything in the scheduling path is allowed to read. */
245
+ now: () => Promise<Date>;
246
+ /** True when this call created the job; false when that id was already there. Never throws on a duplicate. */
247
+ enqueue: (job: JobInput) => Promise<boolean>;
248
+ /**
249
+ * Takes up to `limit` jobs that are due, and any whose lease has lapsed — atomically, one worker per job.
250
+ *
251
+ * A reclaimed job arrives with its previous holder's attempt already written to the history as `lost`.
252
+ */
253
+ claim: (params: {
254
+ workerId: string;
255
+ leaseMs: number;
256
+ limit: number;
257
+ }) => Promise<Job[]>;
258
+ /**
259
+ * Extends the lease on jobs this worker still holds, and answers which of them it is asked to stop — cancelled, or
260
+ * set aside (`pause`).
261
+ */
262
+ heartbeat: (params: {
263
+ jobIds: string[];
264
+ workerId: string;
265
+ leaseMs: number;
266
+ }) => Promise<{
267
+ jobId: string;
268
+ stop: JobStop;
269
+ }[]>;
270
+ settle: (settlement: JobSettlement) => Promise<void>;
271
+ /** Writes what a running job's code saved or reported. False when this worker no longer holds it. */
272
+ write: (write: JobWrite) => Promise<boolean>;
273
+ /** Due schedules, oldest fire first. */
274
+ dueSchedules: (limit: number) => Promise<ActionSchedule[]>;
275
+ /**
276
+ * Moves a schedule forward, but only if it is still where the caller last saw it.
277
+ *
278
+ * The compare-and-set is what stops two replicas from advancing the same schedule twice. Answers false when
279
+ * somebody else got there first, which is not an error — it is the other replica having done the work.
280
+ */
281
+ advanceSchedule: (params: {
282
+ spaceId: number;
283
+ actionId: string;
284
+ from: number;
285
+ to: number;
286
+ missed: number;
287
+ }) => Promise<boolean>;
288
+ /** Replaces what this space has scheduled. An action no longer listed stops having a schedule at all. */
289
+ putSchedules: (params: {
290
+ spaceId: number;
291
+ schedules: ActionScheduleInput[];
292
+ }) => Promise<void>;
293
+ listJobs: (query: JobQuery) => Promise<{
294
+ jobs: Job[];
295
+ total: number;
296
+ }>;
297
+ getJob: (spaceIds: number[], jobId: string) => Promise<Job | undefined>;
298
+ listSchedules: (spaceIds: number[]) => Promise<ActionSchedule[]>;
299
+ /**
300
+ * Puts a finished job back in the queue — an operator retrying it, or the code starting a keyed job again. Answers false
301
+ * when it is not finished. With `input`, it runs with that; with `fresh`, a job of the space's functions starts over
302
+ * rather than from what it saved.
303
+ */
304
+ requeue: (spaceIds: number[], jobId: string, options?: {
305
+ input?: Record<string, unknown>;
306
+ fresh?: boolean;
307
+ }) => Promise<boolean>;
308
+ /** Stops a job: dropped if it is waiting or set aside, aborted at its next step if it is running. */
309
+ cancel: (spaceIds: number[], jobId: string) => Promise<boolean>;
310
+ /**
311
+ * Sets a job of the space's functions aside: one waiting is `paused` at once, one running is flagged for the worker
312
+ * holding it, which stops it once its code has saved. Answers whether it changed anything.
313
+ */
314
+ pause: (spaceIds: number[], jobId: string) => Promise<boolean>;
315
+ /** Puts a `paused` job back in the queue, to start again from what it saved. */
316
+ resume: (spaceIds: number[], jobId: string) => Promise<boolean>;
317
+ /**
318
+ * Drops the space's jobs waiting under `key` (`cancelled`), and answers how many. With `olderThan`, only the ones
319
+ * enqueued before that job — created earlier, or at the same instant with a smaller id — so of two set at once the
320
+ * newer is the one left, on every replica. A running one is left: it is usually the run that set the next.
321
+ */
322
+ cancelPending: (params: {
323
+ spaceId: number;
324
+ key: string;
325
+ olderThan?: string;
326
+ }) => Promise<number>;
327
+ };
File without changes
@@ -1,9 +1,10 @@
1
- import { ActionEmailConfig, ActionEntry, ActionJobQueue, ActionLimits, ActionRejectReason, ActionRunSummary, ActionRejectRecord, ActionRunRecord, ActionRunReport, ActionRunStatus, ActionTaskDescriptor, ActionTriggerType } from './ActionTypes';
1
+ import { ActionEmailConfig, ActionEntry, ActionLimits, ActionRejectReason, ActionRunSummary, ActionRejectRecord, ActionRunRecord, ActionRunReport, ActionRunStatus, ActionTaskDescriptor, ActionTriggerType } from './ActionTypes';
2
2
  import { Environment, ServerAuth } from './CommonTypes';
3
3
  import { PluginDeclaration } from './ComponentTypes';
4
4
  import { ConnectorEntry } from './ConnectorTypes';
5
5
  import { DataDraft, DataSaveResult } from './DataTypes';
6
6
  import { FunctionsDraft, FunctionsSaveResult } from './FunctionTypes';
7
+ import { JobQueue } from './JobTypes';
7
8
  import { SSRRealtimeConfig } from './RealtimeTypes';
8
9
  import { Schema } from './SchemaTypes';
9
10
  import { AnalyticsConfig, OfflineDataRaw } from './SdkTypes';
@@ -290,6 +291,11 @@ export type SSRUser = {
290
291
  permissions: string[];
291
292
  roles: string[];
292
293
  };
294
+ /**
295
+ * Who a run is for, as the session named them — without the session: what a step, a function and a job see of the
296
+ * person, and what a job keeps of whoever started it, for every slice it runs after their session is gone.
297
+ */
298
+ export type RunUser = Pick<SSRUser, 'id' | 'username' | 'email' | 'verified' | 'permissions' | 'roles'>;
293
299
  export type SSRSpaceDeployment = {
294
300
  /** Who may put this space in an iframe, as CSP `frame-ancestors` sources (e.g. `'self'`, `https://acme.com`,
295
301
  * or `*`). Resolved per space by the consumer, because only it knows the domains that space declares — a
@@ -718,8 +724,8 @@ export type SSRActionConfig = {
718
724
  * replicas in different countries agree about what has run without agreeing about what time it is.
719
725
  */
720
726
  export type SSRActionJobsConfig = {
721
- /** Shaped as `ActionJobQueue`. Omitted → this process's memory. */
722
- queue?: ActionJobQueue;
727
+ /** Shaped as `JobQueue`. Omitted → this process's memory. */
728
+ queue?: JobQueue;
723
729
  /** Jobs this replica runs at once. Default 4; `0` produces without consuming. */
724
730
  workers?: number;
725
731
  /** Whether this replica sweeps due schedules into the queue. Default true. */
@@ -1052,6 +1058,14 @@ export type SSRServerConfig = {
1052
1058
  plugins?: Record<string, unknown>;
1053
1059
  runner?: unknown;
1054
1060
  limits?: Record<string, number>;
1061
+ /** What the spaces' jobs may spend, per space (its plan): see `FunctionJobLimits` in `@plitzi/sdk-server/functions`. */
1062
+ jobs?: {
1063
+ limitsFor?: (spaceId: number) => Promise<Record<string, unknown> | undefined>;
1064
+ };
1065
+ /** `ctx.oauth`: where providers send a visitor back — see `FunctionsConfig` in `@plitzi/sdk-server/functions`. */
1066
+ oauth?: {
1067
+ callbackOrigin?: string;
1068
+ };
1055
1069
  };
1056
1070
  /** Realtime channels the spaces declare — see {@link SSRRealtimeConfig}. On, in memory, when absent. */
1057
1071
  realtime?: SSRRealtimeConfig;
@@ -104,6 +104,11 @@ export type SpaceDeployment = {
104
104
  domain: string;
105
105
  isVerified: boolean;
106
106
  default: boolean;
107
+ /**
108
+ * What serves it: `ssr` a page server — Plitzi's own when there is no `credential`, the space's when there is — or a
109
+ * static bucket (`s3`, `r2`).
110
+ */
111
+ provider: 'ssr' | 's3' | 'r2';
107
112
  credential: SpaceCredential | null;
108
113
  createdAt: number;
109
114
  updatedAt: number;
@@ -13,6 +13,7 @@ export * from './ConnectorTypes';
13
13
  export * from './ActionTypes';
14
14
  export * from './DataTypes';
15
15
  export * from './FunctionTypes';
16
+ export * from './JobTypes';
16
17
  export * from './RealtimeTypes';
17
18
  export * from './ComponentTypes';
18
19
  export * from './StateTypes';