@yaks/wake 0.0.0-stage → 0.2.36

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 (85) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +325 -2
  3. package/_dist/cloudflare.d.ts +68 -0
  4. package/_dist/cloudflare.d.ts.map +1 -0
  5. package/_dist/comp.d.ts +59 -0
  6. package/_dist/comp.d.ts.map +1 -0
  7. package/_dist/deno.d.ts +40 -0
  8. package/_dist/deno.d.ts.map +1 -0
  9. package/_dist/due.d.ts +96 -0
  10. package/_dist/due.d.ts.map +1 -0
  11. package/_dist/every.d.ts +65 -0
  12. package/_dist/every.d.ts.map +1 -0
  13. package/_dist/graph.d.ts +4 -0
  14. package/_dist/graph.d.ts.map +1 -0
  15. package/_dist/held.d.ts +9 -0
  16. package/_dist/held.d.ts.map +1 -0
  17. package/_dist/mod.d.ts +59 -0
  18. package/_dist/mod.d.ts.map +1 -0
  19. package/_dist/plugin.d.ts +20 -0
  20. package/_dist/plugin.d.ts.map +1 -0
  21. package/_dist/run.d.ts +24 -0
  22. package/_dist/run.d.ts.map +1 -0
  23. package/_dist/service.d.ts +9 -0
  24. package/_dist/service.d.ts.map +1 -0
  25. package/_dist/tick.d.ts +36 -0
  26. package/_dist/tick.d.ts.map +1 -0
  27. package/_dist/tools.d.ts +6 -0
  28. package/_dist/tools.d.ts.map +1 -0
  29. package/_dist/vocab.d.ts +7 -0
  30. package/_dist/vocab.d.ts.map +1 -0
  31. package/_dist/watch.d.ts +58 -0
  32. package/_dist/watch.d.ts.map +1 -0
  33. package/_dist/while.d.ts +43 -0
  34. package/_dist/while.d.ts.map +1 -0
  35. package/cloudflare.js +80 -0
  36. package/cloudflare.js.map +1 -0
  37. package/cloudflare.ts +113 -0
  38. package/comp.js +59 -0
  39. package/comp.js.map +1 -0
  40. package/comp.ts +139 -0
  41. package/deno.js +57 -0
  42. package/deno.js.map +1 -0
  43. package/deno.json +1 -0
  44. package/deno.ts +78 -0
  45. package/due.js +147 -0
  46. package/due.js.map +1 -0
  47. package/due.ts +180 -0
  48. package/every.js +203 -0
  49. package/every.js.map +1 -0
  50. package/every.ts +223 -0
  51. package/graph.js +10 -0
  52. package/graph.js.map +1 -0
  53. package/graph.ts +11 -0
  54. package/held.js +29 -0
  55. package/held.js.map +1 -0
  56. package/held.ts +42 -0
  57. package/mod.js +59 -0
  58. package/mod.js.map +1 -0
  59. package/mod.ts +60 -0
  60. package/package.json +49 -4
  61. package/plugin.js +87 -0
  62. package/plugin.js.map +1 -0
  63. package/plugin.ts +94 -0
  64. package/run.js +124 -0
  65. package/run.js.map +1 -0
  66. package/run.ts +117 -0
  67. package/service.js +28 -0
  68. package/service.js.map +1 -0
  69. package/service.ts +42 -0
  70. package/tick.js +168 -0
  71. package/tick.js.map +1 -0
  72. package/tick.ts +181 -0
  73. package/tools.js +83 -0
  74. package/tools.js.map +1 -0
  75. package/tools.ts +76 -0
  76. package/vocab.js +14 -0
  77. package/vocab.js.map +1 -0
  78. package/vocab.json +270 -0
  79. package/vocab.ts +16 -0
  80. package/watch.js +175 -0
  81. package/watch.js.map +1 -0
  82. package/watch.ts +193 -0
  83. package/while.js +159 -0
  84. package/while.js.map +1 -0
  85. package/while.ts +176 -0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Jeff Peterson
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,326 @@
1
- # Temporary Holding Version
1
+ # @yaks/wake
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `@yaks/wake` stores one-shot and recurring schedules in a graph, calculates
4
+ their next occurrence, and marks due schedules as fired. Application rules react
5
+ to the `fired` write and decide what work to perform.
6
+
7
+ A **host** is the process that opened the graph, such as a Deno server,
8
+ Cloudflare Worker, Durable Object, or browser tab. The host supplies the clock
9
+ and decides when to run the scheduler.
10
+
11
+ ```sh
12
+ deno add jsr:@yaks/wake
13
+ ```
14
+
15
+ Entry points:
16
+
17
+ - `@yaks/wake`: components, recurrence functions, due-wake queries, the graph
18
+ plugin, and `tick`.
19
+ - `@yaks/wake/deno`: the long-running `loop` driver.
20
+ - `@yaks/wake/cloudflare`: Cron Trigger and Durable Object alarm helpers.
21
+ - `@yaks/wake/graph`: the `plugins()` plugin list.
22
+ - `@yaks/wake/service`: a Deno background service wrapper.
23
+ - `@yaks/wake/run`: running a wake's tool call at a firing, with
24
+ [@yaks/tools](../tools).
25
+ - `@yaks/wake/tools`: the code behind the `wake_new`, `wake_list` and
26
+ `wake_cancel` tools.
27
+ - `@yaks/wake/vocab`: the vocabulary document without scheduler behavior.
28
+
29
+ ## The rows
30
+
31
+ `wake{at, every, while, target, note, for, brief}` stores the next ISO instant,
32
+ optional recurrence, the conditions it recurs under, related entity, a reason,
33
+ the person it was set for, and words for a fresh session each firing.
34
+ `fired{at}` stores the most recent firing. A one-shot clears `wake.at` after
35
+ firing. A recurring wake advances `wake.at` to its next occurrence.
36
+
37
+ A **bundle** is one entity's components represented as a JSON object:
38
+
39
+ ```ts
40
+ import { graph } from '@yaks/graph'
41
+ import { ram } from '@yaks/ram'
42
+ import { loadVocab } from '@yaks/vocab'
43
+ import { next, wakeDoc, wakes } from '@yaks/wake'
44
+
45
+ const vocab = loadVocab([wakeDoc, { $defs: { sweep: { component: true } } }])
46
+ const g = graph({ storage: ram(vocab), vocab, plugins: [wakes()] })
47
+
48
+ const every = '20 4 * * * America/Detroit'
49
+ await g.apply([{
50
+ entity: { eid: 'cleanup' },
51
+ wake: { every, at: next(every, Date.now()), note: 'collect expired trash' },
52
+ sweep: {},
53
+ }])
54
+ ```
55
+
56
+ `target` identifies what the wake concerns. The package stores no patch or
57
+ callback. Application rules match the components on the wake entity.
58
+
59
+ Use `wakes()` when constructing a graph. It loads `wakeDoc`, initializes a wake
60
+ that has `every` but no `at`, and refuses a `while` condition the graph cannot
61
+ read. An explicit `at: null` remains paused.
62
+
63
+ ```ts ignore
64
+ import { wakes } from '@yaks/wake'
65
+
66
+ const g = graph({ storage, vocab, plugins: [wakes()] })
67
+ ```
68
+
69
+ ## Scheduling one by hand
70
+
71
+ `wake_new` is the tool that writes a wake aimed at something (`yak wake new` at
72
+ a terminal). It takes a `target`, a `note` for whoever is woken, and a time:
73
+ `every` to recur, `at` to fire once, or both, where `at` is the first firing.
74
+ `at` is an instant or a delay from now (`10m`, `2h`, `1d`; `when(at, now)`). A
75
+ cadence or an instant it cannot read is refused. A wake is set for its caller
76
+ (`wake.for`) where the caller is a person the graph knows.
77
+
78
+ ```sh
79
+ yak wake new V-12 --every '0 7 * * * America/Detroit' \
80
+ --note 'Weigh what was filed under this goal since yesterday.'
81
+ ```
82
+
83
+ The tool writes the row and nothing more. Who hears a firing is a rule's answer:
84
+ [@yaks/session](../session) knocks a persona's operator, or the session opened
85
+ on a topic.
86
+
87
+ `wake_list` answers the wakes set for the caller that will fire again, soonest
88
+ first, and `wake_cancel` deletes one of them by its id; another person's wake is
89
+ refused as none. `held(graph, person)` and `cancel(graph, person, id)` are the
90
+ same for a host that serves several people from one graph.
91
+
92
+ ## Firing when a query's rows change
93
+
94
+ A wake carrying `watch{query, local, seen, changes}` fires only when the query
95
+ answers rows that differ from `seen`, the rows its last look saw (by eid, with a
96
+ digest of each, up to `ROWS`, 200). Each time it comes due, `tick` reads the
97
+ query again. A change writes `fired`, the new `seen`, and `changes`, what
98
+ changed in words; a look that finds nothing new moves `at` on and writes no
99
+ `fired`, so a rule matching `*fired` runs only for a change. `look(seen, rows)`
100
+ is the comparison. The query is read over the graph unless the host says
101
+ otherwise: `tick(graph, now, { watching })` reads a wake's query the host's way,
102
+ as yaks.app reads one that is not `local` over everything the wake's person
103
+ reaches.
104
+
105
+ A look the reader refuses for the query's own sake (status 400 to 404, such as
106
+ an app that is gone) would be refused again on every retry. It fires once
107
+ instead, `changes` saying why and `seen` kept, and the wake moves on as any
108
+ firing does. A race, a pace or an outage (409, 429, 5xx) leaves the wake due, so
109
+ it is read again.
110
+
111
+ A watch needs no cadence. Its host hears each commit to what the query reads and
112
+ calls `stir(graph, now, { which })`: each watching wake `which` picks comes due
113
+ `SOON` (two seconds) later unless it is due sooner, so a burst of commits is one
114
+ look. A look that finds nothing new leaves a wake with no `every` resting, with
115
+ no `at`, until the next stir.
116
+
117
+ ## Running a tool call at each firing
118
+
119
+ A wake that carries a `call{to, args}` ([@yaks/tools](../tools)) is a schedule
120
+ for that call, which @yaks/tools' `call_woken` runs once a firing as whoever
121
+ wrote it. A wake set for a person (`wake.for`) owes `wake_run` at each firing,
122
+ where its plugin is given `run: true`: the host that runs work for people does
123
+ what it asks as that person, and handles no `call_woken`. The call is run by
124
+ `run(graph, wake, at, host)` from `@yaks/wake/run`: one
125
+ `occurrence{wake, at, call, error}` a firing, its eid derived from the wake and
126
+ the instant, and the call derived from that, through the runner the host lends
127
+ over the person's tools as they stand when it fires. A replay of the same firing
128
+ finds the occurrence and recalls the call's answer, so nothing runs twice. The
129
+ host marks how it ended (`completed`, `failed`), since a session given the
130
+ wake's `brief` may still be checking its outcome.
131
+
132
+ ## Firing is a write
133
+
134
+ ```ts ignore
135
+ import { tick } from '@yaks/wake'
136
+ import type { Rule } from '@yaks/graph'
137
+
138
+ const cleanup: Rule = {
139
+ phase: 'effect',
140
+ match: '.wake, *fired, .sweep, #Now',
141
+ run: async ({ Now }) => await collectExpiredTrash(Now.at),
142
+ }
143
+
144
+ // Register cleanup in the application graph before ticking it.
145
+ const result = await tick(g, Date.now())
146
+ ```
147
+
148
+ `tick(graph, now)` finds all due wakes. For each wake it submits one **batch**,
149
+ a list of bundles applied in one transaction. The batch writes `fired{at}` and
150
+ updates or clears `wake.at`; `#Now` contains the same instant. A rule using
151
+ `*fired` runs only when that component was written in the current transaction.
152
+
153
+ Each wake has its own transaction. A precondition checks the `wake.at`,
154
+ `wake.every` and `wake.while` values read by the scheduler, preventing
155
+ concurrent drivers from consuming one occurrence twice or overwriting an edited
156
+ schedule. A wake another writer moved since it was read is read again: if it is
157
+ still due it fires as it now stands, and if not, the other writer's instant
158
+ stands. Only a wake that keeps moving under three reads is refused for it. One
159
+ rejected transaction does not stop other wakes. The result contains
160
+ `fired: Bundle[]` and `refused: { wake: Bundle; error: unknown }[]`.
161
+
162
+ Effect failures are reported after commit; they do not roll back the firing.
163
+ After downtime, an overdue recurring wake fires once and advances beyond `now`.
164
+ A process crash after commit can interrupt an effect; this package does not
165
+ durably retry effects.
166
+
167
+ ## Recurrence
168
+
169
+ Durations preserve their cadence:
170
+
171
+ ```text
172
+ 30s 2m 90m 2h 1d 3d 1w
173
+ every 15 minutes every 3 days
174
+ ```
175
+
176
+ Cron expressions use minute, hour, day of month, month, and day of week. Fields
177
+ accept wildcards, ranges, steps, and lists. Sunday is `0` or `7`; if both day
178
+ fields are restricted, either may match. Named schedules are `@hourly`,
179
+ `@daily`, `@weekly`, and `@monthly`.
180
+
181
+ An optional trailing IANA time zone is stored in `every`:
182
+
183
+ ```text
184
+ */15 * * * *
185
+ 5-50/15 9-17 * * 1-5
186
+ 20 4 * * * America/Detroit
187
+ @daily Asia/Kathmandu
188
+ ```
189
+
190
+ UTC is the default. `next(expr, from, tz?)` returns the next ISO instant
191
+ strictly after the epoch millisecond `from`, or `null`. The
192
+ `next(wake, now, { tz })` form keeps a duration anchored to the wake's current
193
+ `at` value.
194
+
195
+ [Croner](https://jsr.io/@hexagon/croner) evaluates calendars with `Intl`. A
196
+ missing spring time moves forward by the daylight-saving gap; a repeated fall
197
+ time fires at its first occurrence. An invalid expression or zone returns
198
+ `null`; a wake with an explicit valid `at` and invalid recurrence fires once and
199
+ stops.
200
+
201
+ ## Repeating while something holds
202
+
203
+ `while` lists conditions in order, each a query over the whole graph and the
204
+ cadence it asks for while that query finds anything. At each firing the first
205
+ that holds sets the next instant; when none does, `every` does, and a wake
206
+ without one sleeps with `at` cleared. `rouse(graph, now)` arms each wake whose
207
+ condition now holds at that cadence from `now`, when that is sooner than the
208
+ instant it holds. A host calls it after its writes commit, so the write that
209
+ makes a condition hold is what wakes the wake. Each pass chooses every wake's
210
+ cadence before writing its first wake. It reads each distinct `match` once and
211
+ shares the answer across wakes. A wake's write can change the next pass's
212
+ choices; each wake still writes separately.
213
+
214
+ ```ts
215
+ import { assertEquals } from '@std/assert'
216
+ import { graph } from '@yaks/graph'
217
+ import { ram } from '@yaks/ram'
218
+ import { loadVocab } from '@yaks/vocab'
219
+ import { rouse, wakeDoc, wakes } from '@yaks/wake'
220
+
221
+ const player = {
222
+ component: true,
223
+ type: 'object',
224
+ properties: { seen: { type: 'string', format: 'date-time' } },
225
+ }
226
+ const vocab = loadVocab([wakeDoc, { $defs: { player } }])
227
+ const g = graph({ storage: ram(vocab), vocab, plugins: [wakes()] })
228
+ const now = Date.parse('2026-09-26T12:00:00Z')
229
+
230
+ await g.apply([{
231
+ entity: { eid: 'world' },
232
+ wake: {
233
+ while: [
234
+ { match: '.player.seen>=1-minute-ago', every: '30s' },
235
+ { match: '.player.seen>=10-minutes-ago', every: '5m' },
236
+ ],
237
+ },
238
+ }])
239
+ await g.apply([{
240
+ entity: { eid: 'bea' },
241
+ player: { seen: new Date(now).toISOString() },
242
+ }])
243
+ const { roused } = await rouse(g, now)
244
+ assertEquals(roused[0].wake, { at: '2026-09-26T12:00:30.000Z' })
245
+ ```
246
+
247
+ ## Drivers per runtime
248
+
249
+ Cloudflare
250
+ [Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/)
251
+ can call `scheduled`, which uses the event's `scheduledTime` and fires every due
252
+ graph row. The trigger expression does not select an individual wake.
253
+
254
+ ```ts
255
+ import type { Driver } from '@yaks/wake'
256
+ import { type Scheduled, scheduled } from '@yaks/wake/cloudflare'
257
+
258
+ declare let graph: Driver
259
+
260
+ export default { scheduled: (event: Scheduled) => scheduled(graph, event) }
261
+ ```
262
+
263
+ A Durable Object can set an earlier alarm:
264
+
265
+ ```ts ignore
266
+ import { arm } from '@yaks/wake/cloudflare'
267
+ import { tick } from '@yaks/wake'
268
+
269
+ await arm(ctx.storage, wake, nextHeartbeat)
270
+ // In alarm(): await tick(graph, Date.now())
271
+ ```
272
+
273
+ `arm(storage, wake, before)` preserves an alarm that is already earlier and
274
+ returns whether this wake needs an alarm. A wake without `at`, or one due at or
275
+ after `before`, leaves the alarm unchanged. A rejected wake waits for the next
276
+ Cron Trigger. The host owns its single alarm, including cancellation after
277
+ schedules change.
278
+
279
+ The runtime gives up on an `alarm()` that keeps failing and still answers its
280
+ instant from `getAlarm()`, so an alarm held more than `LOST` (15 minutes) past
281
+ its instant is taken as lost: `armed(storage)` answers null for it, and `arm`
282
+ sets it again for now, since the run it stood for is still owed.
283
+
284
+ A Deno process can run the scheduler until shutdown:
285
+
286
+ ```ts ignore
287
+ import { loop } from '@yaks/wake/deno'
288
+
289
+ const stop = new AbortController()
290
+ const running = loop(graph, {
291
+ signal: stop.signal,
292
+ cap: 60_000,
293
+ onTick: ({ refused }) => report(refused),
294
+ })
295
+ stop.abort()
296
+ await running
297
+ ```
298
+
299
+ The loop rouses and ticks immediately, then sleeps until the next pending
300
+ instant. Its default one-minute cap limits how long a new wake, or a sleeping
301
+ one a write made hold, waits to be noticed, and how soon a refused wake is
302
+ retried. Aborting releases the timer after any write in progress finishes.
303
+ `clock` (an @yaks/fp `Clock`, the runtime's by default) is what each tick reads
304
+ its instant from and the loop sleeps on. `@yaks/wake/service` exposes the same
305
+ loop as a host service and logs refused wakes.
306
+
307
+ ## Lower-level functions
308
+
309
+ - `due(storage, now)` returns overdue wake bundles, oldest first.
310
+ - `ring(bundle, now)` creates the firing bundles without applying them.
311
+ - `soonest(storage, now)` returns the earliest future instant.
312
+ - `when(at, now)` reads an instant or a delay as epoch milliseconds.
313
+ - `cadence(graph, wake, now)` returns the cadence of the first `while` condition
314
+ that holds, or `null`.
315
+ - `starting(options)` returns the transaction-aware `beforeWrite` hook used by
316
+ `wakes()`. It supplies the first instant when a cadence is first added, but
317
+ preserves an existing instant or a paused recurrence when `at` is omitted.
318
+ - `wakeDoc`, also available from `@yaks/wake/vocab`, declares the components.
319
+
320
+ ## Compatibility
321
+
322
+ The core uses web-standard APIs and runs in Deno, Node, Cloudflare workerd, and
323
+ browsers. Cron evaluation requires `Intl` and no `Temporal` polyfill. The
324
+ Cloudflare adapter accepts alarm storage structurally and imports no Cloudflare
325
+ global. The Deno loop uses `setTimeout` (through its clock) and `AbortSignal`
326
+ and imports no Deno-only API. The core is checked with browser-only types.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Cloudflare decides when to run; the graph holds the schedules. A Cron Trigger
3
+ * calls `scheduled`, while a Durable Object can set its single alarm for a wake
4
+ * that falls before the next Cron Trigger. Neither imports a Cloudflare global,
5
+ * so Deno can exercise the same driver with a stand-in storage.
6
+ * @module
7
+ */ import type { Wake } from "./comp.js";
8
+ import { type Driver, type Ticked } from "./tick.js";
9
+ /** The instant Cloudflare scheduled, independent of delivery latency. */ export type Scheduled = {
10
+ scheduledTime: number;
11
+ };
12
+ /** The alarm methods a Durable Object's storage provides. */ export type Alarm = {
13
+ getAlarm: () => Promise<number | null>;
14
+ setAlarm: (at: number) => Promise<void>;
15
+ };
16
+ /**
17
+ * How long past its instant an alarm may still be held before it is taken as
18
+ * lost. The runtime retries a failing `alarm()` six times, with backoff, then
19
+ * gives up, and `getAlarm()` goes on answering the instant it gave up on; an
20
+ * object whose alarm kept running out of CPU held one for days, and nothing
21
+ * delivered it again.
22
+ */ export declare let LOST: number;
23
+ /**
24
+ * The alarm the runtime will deliver: null when none is set, or when the one
25
+ * set is more than `LOST` past its instant.
26
+ *
27
+ * ```ts
28
+ * import { armed, LOST } from '@yaks/wake/cloudflare'
29
+ * import { assertEquals } from '@std/assert'
30
+ *
31
+ * let storage = (at: number | null) => ({
32
+ * getAlarm: () => Promise.resolve(at),
33
+ * setAlarm: () => Promise.resolve(),
34
+ * })
35
+ * assertEquals(await armed(storage(900), 1000), 900)
36
+ * assertEquals(await armed(storage(1000 - LOST - 1), 1000), null)
37
+ * ```
38
+ */ export declare let armed: (storage: Alarm, now?: number) => Promise<number | null>;
39
+ /**
40
+ * A Worker's scheduled handler, reduced to the graph write every runtime
41
+ * shares. The event's cron string does not select a job: the due wake rows do.
42
+ *
43
+ * ```ts
44
+ * import { scheduled } from '@yaks/wake/cloudflare'
45
+ *
46
+ * // export default { scheduled: (event) => scheduled(graph, event) }
47
+ * ```
48
+ */ export declare let scheduled: (graph: Driver, event: Scheduled) => Promise<Ticked>;
49
+ /**
50
+ * Set a Durable Object's alarm for one wake. An alarm that is already earlier
51
+ * is kept, because the object may hold other wakes. Returns whether this wake
52
+ * needs the alarm at all; a wake with no `at`, or a later one, leaves it alone.
53
+ * An alarm the runtime gave up on (`armed`) is set again for `now`, since the
54
+ * run it stood for is still owed.
55
+ *
56
+ * Call it when a wake is written. In the object's `alarm()`, call `tick(graph)`
57
+ * and then set the alarm for its next pending wake. `before` is for an
58
+ * application that also has a Cron Trigger: pass the time of the next trigger,
59
+ * and a wake falling after it is left for that trigger to fire rather than this
60
+ * object. An application whose alarm is its only clock leaves `before` out.
61
+ *
62
+ * ```ts
63
+ * import { arm } from '@yaks/wake/cloudflare'
64
+ *
65
+ * // await arm(ctx.storage, wake)
66
+ * ```
67
+ */ export declare let arm: (storage: Alarm, wake: Wake, before: number, now?: number) => Promise<boolean>;
68
+ //# sourceMappingURL=cloudflare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudflare.d.ts","sources":["../cloudflare.ts"],"names":[],"mappings":"AAAA;;;;;;CAMC,GAED,cAAc,IAAI,oBAAmB;AACrC,SAAS,KAAK,MAAM,EAAQ,KAAK,MAAM,oBAAmB;AAE1D,uEAAuE,GACvE,YAAY;EAAc,eAAe,MAAM;;AAE/C,2DAA2D,GAC3D,YAAY;EACV,gBAAgB,QAAQ,MAAM,GAAG,IAAI;EACrC,WAAW,IAAI,MAAM,KAAK,QAAQ,IAAI;;AASxC;;;;;;CAMC,GACD,OAAO,YAAI,MAAM,MAAM,CAAc;AAErC;;;;;;;;;;;;;;;CAeC,GACD,OAAO,YAAI,QACT,SAAS,OACT,MAAK,MAAM,KACV,QAAQ,MAAM,GAAG,IAAI,EAGvB;AAKD;;;;;;;;;CASC,GACD,OAAO,YAAI,YACT,OAAO,QACP,OAAO,cACN,QAAQ,QAA2C;AAEtD;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,YAAI,MACT,SAAS,OACT,MAAM,MACN,gBACA,MAAK,MAAM,KACV,QAAQ,OAAO,EAWjB"}
@@ -0,0 +1,59 @@
1
+ import type { Eid, Entity } from "@yaks/graph";
2
+ import type { VocabDoc } from "@yaks/vocab";
3
+ /** The component holding when to come back. */ export declare let WAKE: string;
4
+ /** The component recording when a wake last fired. */ export declare let FIRED: string;
5
+ /**
6
+ * A wake: a scheduled return to something, at a time or on a cadence.
7
+ */ export type Wake = {
8
+ /** when it is next due, an ISO instant — absent once it has finished */ at?: string | null;
9
+ /** how it recurs: a duration, five cron fields or a `@` shorthand, optionally
10
+ * followed by an IANA zone (`0 9 * * 1-5 America/New_York`)
11
+ * (see {@link https://jsr.io/@yaks/wake/doc/~/next | next}) */ every?: string | null;
12
+ /** how it recurs while something holds: in order, a query over the whole
13
+ * graph and the cadence it asks for while that query finds anything; the
14
+ * first that holds wins, else `every` does, else the wake sleeps
15
+ * (see {@link https://jsr.io/@yaks/wake/doc/~/cadence | cadence}) */ while?: {
16
+ match: string;
17
+ every: string;
18
+ }[] | null;
19
+ /** what the wake is about — the entity carrying it, when absent */ target?: Eid | null;
20
+ /** a line for whoever is woken: why the wake was set */ note?: string | null;
21
+ /** the person it was set for, as whom it runs */ for?: Eid | null;
22
+ /** words for a fresh session each firing, given its call's outcome */ brief?: string | null;
23
+ };
24
+ /** The component holding the query a wake watches. */ export declare let WATCH: string;
25
+ /** A query a wake watches: it fires only when its rows changed. */ export type Watch = {
26
+ /** the query, as graph_query takes it */ query: string;
27
+ /** the reading tool it was subscribed to, whose declared query this is */ tool?: string | null;
28
+ /** the arguments that tool was given */ args?: Record<string, unknown> | null;
29
+ /** read over the store the wake is kept in, not all its person reaches */ local?: boolean | null;
30
+ /** the graph its host reads it over, by the host's name for it, where it
31
+ * is neither the store the wake is kept in nor all its person reaches */ over?: string | null;
32
+ /** each row the last look saw, by eid, with a digest of it */ seen?: Record<string, string> | null;
33
+ /** what changed at the look the wake last fired for, in words */ changes?: string | null;
34
+ /** keep successful changes as activity without waking a conversation */ record_only?: boolean | null;
35
+ /** why its reader refused the query at the last look, until a look reads
36
+ * it again */ refused?: string | null;
37
+ };
38
+ /** The component recording one firing that did work. */ export declare let OCCURRENCE: string;
39
+ /** One firing of a wake that did work. */ export type Occurrence = {
40
+ /** the wake that fired */ wake: Eid;
41
+ /** the instant it fired, ISO */ at: string;
42
+ /** the call it made */ call?: Eid | null;
43
+ /** the call's failure, kept while a helper checks it */ error?: string | null;
44
+ };
45
+ /** What a fired wake records: when it last fired. */ export type Fired = {
46
+ /** the instant it fired, ISO */ at: string;
47
+ };
48
+ /** A bundle carrying a wake, as {@link due} returns them. */ export type Waking = {
49
+ entity: Entity;
50
+ } & {
51
+ wake: Wake;
52
+ fired?: Fired;
53
+ };
54
+ /**
55
+ * The wake vocabulary, to load beside your own:
56
+ * `loadVocab([wakeDoc, ...mine])`. It declares nothing about what is being
57
+ * woken — that is an ordinary entity in your own vocabulary — only when.
58
+ */ export declare let wakeDoc: VocabDoc;
59
+ //# sourceMappingURL=comp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"comp.d.ts","sources":["../comp.ts"],"names":[],"mappings":"AA+CA,cAAc,GAAG,EAAE,MAAM,2BAAgC;AACzD,cAAc,QAAQ,2BAAgC;AAGtD,6CAA6C,GAC7C,OAAO,YAAI,aAAa;AAExB,oDAAoD,GACpD,OAAO,YAAI,cAAe;AAE1B;;CAEC,GACD,YAAY;EACV,sEAAsE,GACtE,KAAK,MAAM,GAAG,IAAI;EAClB;;+DAE6D,GAC7D,QAAQ,MAAM,GAAG,IAAI;EACrB;;;qEAGmE,GACnE;IAAU,OAAO,MAAM;IAAE,OAAO,MAAM;QAAO,IAAI;EACjD,iEAAiE,GACjE,SAAS,MAAM,IAAI;EACnB,sDAAsD,GACtD,OAAO,MAAM,GAAG,IAAI;EACpB,+CAA+C,GAC/C,MAAM,MAAM,IAAI;EAChB,oEAAoE,GACpE,QAAQ,MAAM,GAAG,IAAI;;AAGvB,oDAAoD,GACpD,OAAO,YAAI,cAAe;AAE1B,iEAAiE,GACjE,YAAY;EACV,uCAAuC,GACvC,OAAO,MAAM;EACb,wEAAwE,GACxE,OAAO,MAAM,GAAG,IAAI;EACpB,sCAAsC,GACtC,OAAO,OAAO,MAAM,EAAE,OAAO,IAAI,IAAI;EACrC,wEAAwE,GACxE,QAAQ,OAAO,GAAG,IAAI;EACtB;yEACuE,GACvE,OAAO,MAAM,GAAG,IAAI;EACpB,4DAA4D,GAC5D,OAAO,OAAO,MAAM,EAAE,MAAM,IAAI,IAAI;EACpC,+DAA+D,GAC/D,UAAU,MAAM,GAAG,IAAI;EACvB,sEAAsE,GACtE,cAAc,OAAO,GAAG,IAAI;EAC5B;cACY,GACZ,UAAU,MAAM,GAAG,IAAI;;AAGzB,sDAAsD,GACtD,OAAO,YAAI,mBAAyB;AAEpC,wCAAwC,GACxC,YAAY;EACV,wBAAwB,GACxB,MAAM;EACN,8BAA8B,GAC9B,IAAI,MAAM;EACV,qBAAqB,GACrB,OAAO,MAAM,IAAI;EACjB,sDAAsD,GACtD,QAAQ,MAAM,GAAG,IAAI;;AAGvB,mDAAmD,GACnD,YAAY;EACV,8BAA8B,GAC9B,IAAI,MAAM;;AAGZ,2DAA2D,GAC3D,YAAY;EAAW,QAAQ;;EAAa,MAAM;EAAM,QAAQ;;AAEhE;;;;CAIC,GACD,OAAO,YAAI,SAAS,SAAc"}