@orthograph/language-effect 0.0.0-stage → 0.1.0-next.2

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 (141) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +2 -2
  3. package/dist/device.d.ts +5 -0
  4. package/dist/device.js +6 -0
  5. package/dist/index.d.ts +30 -0
  6. package/dist/index.js +25 -0
  7. package/dist/modules/access/internal/path.d.ts +3 -0
  8. package/dist/modules/access/internal/path.js +11 -0
  9. package/dist/modules/access/service.d.ts +5 -0
  10. package/dist/modules/access/service.js +111 -0
  11. package/dist/modules/access/types.d.ts +38 -0
  12. package/dist/modules/access/types.js +3 -0
  13. package/dist/modules/auth/service.d.ts +4 -0
  14. package/dist/modules/auth/service.js +19 -0
  15. package/dist/modules/auth/types.d.ts +38 -0
  16. package/dist/modules/auth/types.js +8 -0
  17. package/dist/modules/calls/internal/run.d.ts +2 -0
  18. package/dist/modules/calls/internal/run.js +28 -0
  19. package/dist/modules/calls/service.d.ts +2 -0
  20. package/dist/modules/calls/service.js +2 -0
  21. package/dist/modules/calls/types.d.ts +32 -0
  22. package/dist/modules/calls/types.js +1 -0
  23. package/dist/modules/handler/internal/attributes.d.ts +17 -0
  24. package/dist/modules/handler/internal/attributes.js +46 -0
  25. package/dist/modules/handler/internal/call-id.d.ts +2 -0
  26. package/dist/modules/handler/internal/call-id.js +5 -0
  27. package/dist/modules/handler/internal/call-metrics.d.ts +17 -0
  28. package/dist/modules/handler/internal/call-metrics.js +35 -0
  29. package/dist/modules/handler/internal/decided.d.ts +14 -0
  30. package/dist/modules/handler/internal/decided.js +106 -0
  31. package/dist/modules/handler/internal/defect.d.ts +4 -0
  32. package/dist/modules/handler/internal/defect.js +19 -0
  33. package/dist/modules/handler/internal/derived.d.ts +21 -0
  34. package/dist/modules/handler/internal/derived.js +96 -0
  35. package/dist/modules/handler/internal/device.d.ts +3 -0
  36. package/dist/modules/handler/internal/device.js +46 -0
  37. package/dist/modules/handler/internal/digest.d.ts +3 -0
  38. package/dist/modules/handler/internal/digest.js +52 -0
  39. package/dist/modules/handler/internal/environment.d.ts +3 -0
  40. package/dist/modules/handler/internal/environment.js +3 -0
  41. package/dist/modules/handler/internal/invariants.d.ts +12 -0
  42. package/dist/modules/handler/internal/invariants.js +28 -0
  43. package/dist/modules/handler/internal/move.d.ts +32 -0
  44. package/dist/modules/handler/internal/move.js +84 -0
  45. package/dist/modules/handler/internal/prehandle.d.ts +23 -0
  46. package/dist/modules/handler/internal/prehandle.js +34 -0
  47. package/dist/modules/handler/internal/read.d.ts +3 -0
  48. package/dist/modules/handler/internal/read.js +79 -0
  49. package/dist/modules/handler/internal/signal.d.ts +3 -0
  50. package/dist/modules/handler/internal/signal.js +10 -0
  51. package/dist/modules/handler/internal/span.d.ts +8 -0
  52. package/dist/modules/handler/internal/span.js +14 -0
  53. package/dist/modules/handler/service.d.ts +7 -0
  54. package/dist/modules/handler/service.js +163 -0
  55. package/dist/modules/handler/types.d.ts +217 -0
  56. package/dist/modules/handler/types.js +24 -0
  57. package/dist/modules/ids/service.d.ts +2 -0
  58. package/dist/modules/ids/service.js +11 -0
  59. package/dist/modules/ids/types.d.ts +8 -0
  60. package/dist/modules/ids/types.js +1 -0
  61. package/dist/modules/journal/service.d.ts +6 -0
  62. package/dist/modules/journal/service.js +4 -0
  63. package/dist/modules/journal/types.d.ts +22 -0
  64. package/dist/modules/journal/types.js +5 -0
  65. package/dist/modules/mcp/internal/input.d.ts +15 -0
  66. package/dist/modules/mcp/internal/input.js +35 -0
  67. package/dist/modules/mcp/internal/output.d.ts +16 -0
  68. package/dist/modules/mcp/internal/output.js +50 -0
  69. package/dist/modules/mcp/internal/tool.d.ts +4 -0
  70. package/dist/modules/mcp/internal/tool.js +56 -0
  71. package/dist/modules/mcp/service.d.ts +2 -0
  72. package/dist/modules/mcp/service.js +38 -0
  73. package/dist/modules/mcp/types.d.ts +101 -0
  74. package/dist/modules/mcp/types.js +1 -0
  75. package/dist/modules/mount/internal/none.d.ts +2 -0
  76. package/dist/modules/mount/internal/none.js +1 -0
  77. package/dist/modules/mount/service.d.ts +2 -0
  78. package/dist/modules/mount/service.js +12 -0
  79. package/dist/modules/mount/types.d.ts +11 -0
  80. package/dist/modules/mount/types.js +4 -0
  81. package/dist/modules/principals/service.d.ts +4 -0
  82. package/dist/modules/principals/service.js +4 -0
  83. package/dist/modules/principals/types.d.ts +15 -0
  84. package/dist/modules/principals/types.js +4 -0
  85. package/dist/modules/records/internal/holder.d.ts +16 -0
  86. package/dist/modules/records/internal/holder.js +55 -0
  87. package/dist/modules/records/internal/list.d.ts +12 -0
  88. package/dist/modules/records/internal/list.js +65 -0
  89. package/dist/modules/records/internal/order.d.ts +18 -0
  90. package/dist/modules/records/internal/order.js +41 -0
  91. package/dist/modules/records/internal/unique.d.ts +31 -0
  92. package/dist/modules/records/internal/unique.js +49 -0
  93. package/dist/modules/records/internal/unit.d.ts +3 -0
  94. package/dist/modules/records/internal/unit.js +135 -0
  95. package/dist/modules/records/service.d.ts +22 -0
  96. package/dist/modules/records/service.js +102 -0
  97. package/dist/modules/records/types.d.ts +170 -0
  98. package/dist/modules/records/types.js +9 -0
  99. package/dist/modules/timers/internal/world.d.ts +4 -0
  100. package/dist/modules/timers/internal/world.js +64 -0
  101. package/dist/modules/timers/service.d.ts +18 -0
  102. package/dist/modules/timers/service.js +125 -0
  103. package/dist/modules/timers/types.d.ts +102 -0
  104. package/dist/modules/timers/types.js +40 -0
  105. package/dist/modules/versions/internal/handlers.d.ts +6 -0
  106. package/dist/modules/versions/internal/handlers.js +36 -0
  107. package/dist/modules/versions/internal/keep.d.ts +21 -0
  108. package/dist/modules/versions/internal/keep.js +36 -0
  109. package/dist/modules/versions/service.d.ts +2 -0
  110. package/dist/modules/versions/service.js +14 -0
  111. package/dist/modules/versions/types.d.ts +27 -0
  112. package/dist/modules/versions/types.js +1 -0
  113. package/dist/modules/visibility/internal/rule.d.ts +7 -0
  114. package/dist/modules/visibility/internal/rule.js +23 -0
  115. package/dist/modules/visibility/internal/schema.d.ts +5 -0
  116. package/dist/modules/visibility/internal/schema.js +12 -0
  117. package/dist/modules/visibility/service.d.ts +15 -0
  118. package/dist/modules/visibility/service.js +27 -0
  119. package/dist/modules/visibility/types.d.ts +60 -0
  120. package/dist/modules/visibility/types.js +1 -0
  121. package/dist/modules/wire/internal/call-telemetry.d.ts +15 -0
  122. package/dist/modules/wire/internal/call-telemetry.js +31 -0
  123. package/dist/modules/wire/internal/refused-payload.d.ts +7 -0
  124. package/dist/modules/wire/internal/refused-payload.js +25 -0
  125. package/dist/modules/wire/internal/shown.d.ts +9 -0
  126. package/dist/modules/wire/internal/shown.js +32 -0
  127. package/dist/modules/wire/service.d.ts +3 -0
  128. package/dist/modules/wire/service.js +14 -0
  129. package/dist/modules/wire/types.d.ts +30 -0
  130. package/dist/modules/wire/types.js +1 -0
  131. package/dist/modules/work/internal/fake-world.d.ts +17 -0
  132. package/dist/modules/work/internal/fake-world.js +43 -0
  133. package/dist/modules/work/internal/fibers.d.ts +2 -0
  134. package/dist/modules/work/internal/fibers.js +5 -0
  135. package/dist/modules/work/service.d.ts +6 -0
  136. package/dist/modules/work/service.js +249 -0
  137. package/dist/modules/work/types.d.ts +103 -0
  138. package/dist/modules/work/types.js +19 -0
  139. package/dist/visibility.d.ts +1 -0
  140. package/dist/visibility.js +3 -0
  141. package/package.json +39 -3
@@ -0,0 +1,6 @@
1
+ import { Layer } from "effect";
2
+ import { Busy, WorldCount } from "./types.ts";
3
+ /** Counted work inside one process. */
4
+ export declare const BusyLive: Layer.Layer<Busy, never, never>;
5
+ /** Counted work that also takes a token in the world's count for each box, so the world knows every process's work in flight (M8.07). */
6
+ export declare const BusyReporting: Layer.Layer<Busy, never, WorldCount>;
@@ -0,0 +1,249 @@
1
+ import { Context, Deferred, Effect, Layer, Metric } from "effect";
2
+ import { Busy, BusyBoxRef, WorldCount, genOf, makeBox } from "./types.js";
3
+ import { runningId } from "./internal/fibers.js";
4
+ /** One process's count of the fibers it runs, and (when `world` is given) the token each box holds in the world's count. */
5
+ const counting = (world) => {
6
+ const active = new Set();
7
+ const shared = new Map();
8
+ let waiters = [];
9
+ /**
10
+ * How many fibers of a box are working now: every fiber it holds (the one that took the token, and every fiber the runtime says started
11
+ * under it, daemon or not, whether or not it forked others), less those that have lent the token out (a sleep) and those that are joining
12
+ * (a combinator of ours waiting for its children). Conservative on purpose: an uncounted fiber that is still working corrupts a run without
13
+ * a sound, and an over-count fails loudly, as `NeverQuiet` naming the owner.
14
+ */
15
+ const activity = (box) => Math.max(0, box.holders - box.lent - box.joining.size);
16
+ /** The fibers running now: a lent one is parked, not running. */
17
+ const running = () => [...active].reduce((n, box) => n + activity(box), 0);
18
+ // Waiters are let go from a fiber of their own, on the next turn: whoever lent a token out finishes recording its lease before any
19
+ // waiter sees the process idle.
20
+ const wake = Effect.suspend(() => {
21
+ if (running() > 0)
22
+ return Effect.void;
23
+ return Effect.asVoid(Effect.provideService(Effect.forkDetach(Effect.andThen(Effect.yieldNow, Effect.suspend(() => {
24
+ if (running() > 0)
25
+ return Effect.void;
26
+ const ready = waiters;
27
+ waiters = [];
28
+ return Effect.forEach(ready, (d) => Deferred.succeed(d, undefined), { discard: true });
29
+ }))), BusyBoxRef, undefined));
30
+ });
31
+ /** Does the world hold this box's token now: a fiber of it runs, or (a request's box) it is between its fibers; never while every fiber is lent. */
32
+ const wants = (box) => !box.stale && (activity(box) > 0 || (box.pinned && box.holders === 0 && box.lent === 0));
33
+ /**
34
+ * One step at a time per box (`box.gate`, which the entry's end takes too), so two fibers cannot take two tokens and the entry cannot
35
+ * end between a take and its record. Telling the world takes time, and what the box wants can change meanwhile: it is read again
36
+ * afterwards, and a token just taken that nothing needs now is given back, so none is left that nobody will release.
37
+ */
38
+ /**
39
+ * Take a token for the box in the world. One of another generation than the box's first is of a run that came after (a reset since): the
40
+ * box belongs to a run that is over, so the token is given back at once, the box is stale, and nothing is taken.
41
+ */
42
+ const mint = (box) => Effect.gen(function* () {
43
+ const id = yield* world.enter(box.owner);
44
+ if (box.gen === undefined)
45
+ box.gen = genOf(id);
46
+ if (box.gen === genOf(id))
47
+ return id;
48
+ box.id = undefined;
49
+ box.stale = true;
50
+ yield* world.leave(id);
51
+ return undefined;
52
+ });
53
+ const sync = (box) => box.gate.withPermits(1)(Effect.gen(function* () {
54
+ if (world === undefined)
55
+ return;
56
+ if (wants(box) && box.id === undefined)
57
+ box.id = yield* mint(box);
58
+ // The box holds an id: the world must still know it. After a reset it does not, and the request would run unseen.
59
+ else if (wants(box) && box.id !== undefined && world.valid !== undefined && !(yield* world.valid(box.id))) {
60
+ // The world does not hold this token any more: a reset ended the run this work belongs to. It is not counted in the new one, and it may not write.
61
+ box.id = undefined;
62
+ box.stale = true;
63
+ }
64
+ if (!wants(box) && box.id !== undefined) {
65
+ // Let whatever the fibers of the box are forking in this same turn (the other branches of an `all`, which are started one after the
66
+ // other) start before deciding that nothing is running.
67
+ yield* Effect.yieldNow;
68
+ yield* Effect.yieldNow;
69
+ if (wants(box))
70
+ return;
71
+ const id = box.id;
72
+ box.id = undefined;
73
+ yield* world.leave(id);
74
+ }
75
+ // A box that is not a request's and has gone idle starts afresh: the owner's shared box lives as long as the isolate, and its next work
76
+ // may be of a later run (a wake in the world after a reset), which is not stale for what the last one was.
77
+ if (!box.pinned && box.holders <= 0 && box.lent === 0 && box.id === undefined) {
78
+ box.gen = undefined;
79
+ box.stale = false;
80
+ }
81
+ }));
82
+ const acquire = (box) => Effect.andThen(Effect.sync(() => { box.holders++; active.add(box); }), sync(box));
83
+ const release = (box) => Effect.andThen(Effect.sync(() => { box.holders--; if (box.holders <= 0)
84
+ active.delete(box); }), Effect.andThen(sync(box), wake));
85
+ /**
86
+ * Every fiber a request forks while it runs under its box (`Effect.all` with concurrency, `race`, `fork`, a daemon, a timeout's children)
87
+ * is a holder of that box for its whole life, whether or not its code ever asks: the runtime tells this hook when a fiber starts and ends
88
+ * (`Metric.FiberRuntimeMetrics`, which each fiber reads from the context it inherits). So a sleeping branch cannot lend the token out from
89
+ * under a branch that is still working, and a child that outlives its parent keeps the token until it ends.
90
+ */
91
+ const hook = (box, previous) => ({
92
+ recordFiberStart: (context) => {
93
+ previous?.recordFiberStart(context);
94
+ if (Context.get(context, BusyBoxRef) !== box)
95
+ return;
96
+ box.holders++;
97
+ active.add(box);
98
+ // A branch that starts after the others have lent the token out takes it back.
99
+ if (box.id === undefined && !box.stale && box.holders > 1)
100
+ Effect.runFork(Effect.ignore(sync(box)));
101
+ },
102
+ recordFiberEnd: (context, exit) => {
103
+ previous?.recordFiberEnd(context, exit);
104
+ if (Context.get(context, BusyBoxRef) !== box)
105
+ return;
106
+ box.holders = Math.max(0, box.holders - 1);
107
+ if (box.holders <= 0)
108
+ active.delete(box);
109
+ // On the next turn, and in a fiber of its own, outside the request: telling the world takes time.
110
+ Effect.runFork(Effect.ignore(Effect.andThen(Effect.yieldNow, Effect.andThen(sync(box), wake))));
111
+ },
112
+ });
113
+ /** `work` under `box`: it, and every fiber it forks, are counted for the box. */
114
+ const under = (box, work) => Effect.suspend(() => {
115
+ // This fiber is a root of the box's tree for as long as the work runs.
116
+ const root = runningId();
117
+ if (root !== undefined)
118
+ box.rootIds.add(root);
119
+ return Effect.ensuring(Effect.flatMap(Effect.service(Metric.FiberRuntimeMetrics), (previous) => Effect.provideService(Effect.provideService(work, BusyBoxRef, box), Metric.FiberRuntimeMetrics, hook(box, previous))),
120
+ // The root is done with its work: whatever it forked and left running is working on its own, and the root is no longer waiting for it.
121
+ Effect.sync(() => { if (root !== undefined)
122
+ box.rootIds.delete(root); }));
123
+ });
124
+ /** The box this fiber runs under, or its request's, when there is one. */
125
+ const requestBox = Effect.gen(function* () {
126
+ const current = yield* Effect.service(BusyBoxRef);
127
+ return current ?? (world?.request === undefined ? undefined : yield* world.request);
128
+ });
129
+ /** Interrupts a request whose token the world no longer holds (a reset since); nothing outside a request. */
130
+ const alive = Effect.gen(function* () {
131
+ const box = yield* requestBox;
132
+ if (box?.stale === true)
133
+ return yield* Effect.interrupt;
134
+ if (box?.id === undefined || world?.valid === undefined)
135
+ return;
136
+ if (!(yield* world.valid(box.id))) {
137
+ box.stale = true;
138
+ box.id = undefined;
139
+ return yield* Effect.interrupt;
140
+ }
141
+ });
142
+ /** The box this fiber runs under; else its request's; else the owner's own, shared by whoever asks for that owner. */
143
+ const boxOf = (fixed) => Effect.gen(function* () {
144
+ const current = yield* Effect.service(BusyBoxRef);
145
+ if (current !== undefined)
146
+ return current;
147
+ const request = world?.request === undefined ? undefined : yield* world.request;
148
+ if (request !== undefined) {
149
+ // The operation names the request's token (the entry named it by its path).
150
+ if (fixed !== undefined && request.owner !== fixed && request.owner.startsWith("entry:")) {
151
+ request.owner = fixed;
152
+ if (request.id !== undefined && world?.name !== undefined)
153
+ yield* world.name(request.id, fixed);
154
+ }
155
+ return request;
156
+ }
157
+ const owner = fixed ?? "unnamed";
158
+ let box = shared.get(owner);
159
+ if (box === undefined) {
160
+ box = makeBox(owner);
161
+ shared.set(owner, box);
162
+ }
163
+ return box;
164
+ });
165
+ const named = (fixed) => {
166
+ const own = () => makeBox(fixed ?? "unnamed");
167
+ return {
168
+ // Taking the token and the cleanup that gives it back are one uninterruptible step: an interrupt that arrives while the world is
169
+ // being told waits for it, then ends the work and gives the token back.
170
+ // Already inside a box, the fiber is counted (the one that took the token, or a child the runtime told us about): nothing more to take.
171
+ counted: (work) => Effect.flatMap(Effect.service(BusyBoxRef), (current) => current !== undefined ? work : Effect.flatMap(boxOf(fixed), (box) =>
172
+ // A refused take (the world is closed while a reset drains) gives the holder back, which `ensuring` below would not reach.
173
+ Effect.uninterruptibleMask((restore) => Effect.andThen(Effect.onError(acquire(box), () => Effect.ignore(release(box))), Effect.ensuring(restore(under(box, work)), release(box)))))),
174
+ // Its own token, taken before this returns and so before the caller's can end. A request the world no longer holds cannot start one.
175
+ // The fiber is forked outside the parent's box (it is not the parent's work) and runs under its own.
176
+ spawn: (work) => Effect.gen(function* () {
177
+ yield* alive;
178
+ const box = own();
179
+ yield* Effect.uninterruptibleMask((restore) => Effect.andThen(acquire(box), Effect.provideService(Effect.forkDetach(Effect.ensuring(restore(under(box, work)), release(box))), BusyBoxRef, undefined)));
180
+ }),
181
+ enter: Effect.uninterruptible(Effect.flatMap(boxOf(fixed), (box) => Effect.onError(acquire(box), () => Effect.ignore(release(box))))),
182
+ leave: Effect.uninterruptible(Effect.flatMap(boxOf(fixed), release)),
183
+ lend: Effect.uninterruptible(Effect.flatMap(boxOf(fixed), (box) => Effect.suspend(() => {
184
+ let taken = false;
185
+ const lease = {
186
+ // Reclaimed once: a second reclaim of the same lease changes nothing.
187
+ reclaim: Effect.suspend(() => {
188
+ if (taken)
189
+ return Effect.void;
190
+ taken = true;
191
+ return Effect.uninterruptible(Effect.andThen(Effect.sync(() => { box.lent--; }), sync(box)));
192
+ }),
193
+ };
194
+ return Effect.as(Effect.andThen(Effect.sync(() => { box.lent++; }), Effect.andThen(sync(box), wake)), lease);
195
+ }))),
196
+ joining: (work) => Effect.flatMap(requestBox, (box) => {
197
+ const id = runningId();
198
+ if (box === undefined || id === undefined)
199
+ return work;
200
+ return Effect.uninterruptibleMask((restore) => Effect.andThen(
201
+ // Marked, and not synced: the children are not forked yet, and the token is let go only when one of them lends it out or ends.
202
+ Effect.sync(() => void box.joining.add(id)), Effect.ensuring(restore(work), Effect.andThen(Effect.sync(() => void box.joining.delete(id)), sync(box)))));
203
+ }),
204
+ fenced: (work) => Effect.gen(function* () {
205
+ const box = yield* requestBox;
206
+ if (box?.stale === true)
207
+ return yield* Effect.interrupt;
208
+ if (world?.section === undefined || box === undefined)
209
+ return yield* work;
210
+ return yield* Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
211
+ // A box whose token is lent out writes under a token of its own for the length of the section, never unfenced: of its own run, or refused.
212
+ const held = box.id;
213
+ const id = held ?? (yield* mint(box));
214
+ if (id === undefined)
215
+ return yield* restore(Effect.interrupt);
216
+ const done = held === undefined ? world.leave(id) : Effect.void;
217
+ // A section the world cannot answer must not leave the token it was taken for.
218
+ const open = yield* Effect.onError(world.section(id), () => Effect.ignore(done));
219
+ if (open !== true) {
220
+ // `false`: the world no longer holds this request's token, so it belongs to a run that is over, and it writes nothing. `"closed"`:
221
+ // the world holds the token and is draining for a reset: refused, with the id kept so the request's cleanup still releases it.
222
+ if (open === false && held !== undefined) {
223
+ box.stale = true;
224
+ box.id = undefined;
225
+ }
226
+ yield* done;
227
+ return yield* restore(Effect.interrupt);
228
+ }
229
+ return yield* Effect.ensuring(restore(work), Effect.andThen(world.sectionEnd === undefined ? Effect.void : world.sectionEnd(id), done));
230
+ }));
231
+ }),
232
+ count: Effect.sync(running),
233
+ held: Effect.sync(() => [...active].flatMap((box) => Array.from({ length: activity(box) }, () => box.owner)).sort()),
234
+ quiet: Effect.suspend(() => {
235
+ if (running() === 0)
236
+ return Effect.void;
237
+ const d = Deferred.makeUnsafe();
238
+ waiters.push(d);
239
+ return Deferred.await(d);
240
+ }),
241
+ as: (o) => named(o),
242
+ };
243
+ };
244
+ return named(undefined);
245
+ };
246
+ /** Counted work inside one process. */
247
+ export const BusyLive = Layer.sync(Busy, () => counting());
248
+ /** Counted work that also takes a token in the world's count for each box, so the world knows every process's work in flight (M8.07). */
249
+ export const BusyReporting = Layer.effect(Busy, Effect.map(Effect.service(WorldCount), (world) => counting(world)));
@@ -0,0 +1,103 @@
1
+ import { Context, Semaphore, type Effect } from "effect";
2
+ /**
3
+ * One token's worth of work: a request (taken at its entry, held until the response or the work ends, retries, authorization and
4
+ * commit included), a spawn, a wake. `holders` are the fibers running under it; `lent` is how many of them are waiting (a sleep lends
5
+ * its token out so the world can move). The world holds the box's token (`id`) while some fiber is running, and, for a request
6
+ * (`pinned`), also in the phases between its fibers.
7
+ */
8
+ export interface BusyBox {
9
+ owner: string;
10
+ /** The world's id for the token this box holds now; nothing while it is lent out. */
11
+ id: string | undefined;
12
+ holders: number;
13
+ lent: number;
14
+ /** A request's box: the token is held from the entry until the entry ends, not only while a fiber runs. */
15
+ pinned: boolean;
16
+ /** The world no longer holds this box's token (a reset since): the work belongs to a run that is over, so it is not counted and may not write. */
17
+ stale: boolean;
18
+ /**
19
+ * The `<epoch>.<generation>` prefix of the first token this box took (a world's ids are `<epoch>.<generation>.<n>`). A token the box mints
20
+ * later under another prefix is of a run that came after: the box belongs to a run that is over, so it is stale and takes nothing.
21
+ */
22
+ gen: string | undefined;
23
+ readonly gate: Semaphore.Semaphore;
24
+ /** The ids of the fibers that took the token under this box (a request's handler, a spawn's work). */
25
+ readonly rootIds: Set<number>;
26
+ /** The fibers that are joining: waiting for children they forked, said so by a combinator of ours (`Busy.joining`). Only these are not counted for waiting. */
27
+ readonly joining: Set<number>;
28
+ }
29
+ /** The `<epoch>.<generation>` of a token id: everything before the last dot. */
30
+ export declare const genOf: (id: string) => string;
31
+ export declare const makeBox: (owner: string, options?: {
32
+ readonly id?: string;
33
+ readonly pinned?: boolean;
34
+ }) => BusyBox;
35
+ /** A token lent out by a sleep or a wait, to be taken back (reclaimed) from wherever the wake happens. */
36
+ export interface Lease {
37
+ readonly reclaim: Effect.Effect<void>;
38
+ }
39
+ /** What counted work offers; `Busy.as(owner)` gives it again with its tokens named. */
40
+ export interface BusyShape {
41
+ /** Run `work` as the world's work: counted from start to end however it ends, under the request's token when it runs in one. */
42
+ readonly counted: <A, E, R>(work: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
43
+ /** Start `work` in the background under a token of its own, taken before this returns. */
44
+ readonly spawn: <A, E, R>(work: Effect.Effect<A, E, R>) => Effect.Effect<void, never, R>;
45
+ /** Take a token without running anything (this fiber's token, or the owner's). */
46
+ readonly enter: Effect.Effect<void>;
47
+ /** Give that token back. */
48
+ readonly leave: Effect.Effect<void>;
49
+ /** A sleep or a wait lends this fiber's token out, so the world may move while it is parked; `reclaim` takes it back, from anywhere. */
50
+ readonly lend: Effect.Effect<Lease>;
51
+ /** How many fibers this process is running (a lent one is not running). */
52
+ readonly count: Effect.Effect<number>;
53
+ /** The owner of each fiber this process is running, once per fiber. */
54
+ readonly held: Effect.Effect<ReadonlyArray<string>>;
55
+ /** Wait until no work holds a token. */
56
+ readonly quiet: Effect.Effect<void>;
57
+ /**
58
+ * A write section: `work` (a commit) runs only while the request's token is live in the world, and a reset waits for it to end. A request
59
+ * whose token is gone (the world was reset under it) is interrupted instead, so nothing of the old run is written into the new one.
60
+ */
61
+ readonly fenced: <A, E, R>(work: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
62
+ /**
63
+ * Run `work`, which forks children and waits for them, as a join: this fiber is not counted while it waits, only its children are. Said only
64
+ * by a combinator that knows it joins (`Timers.all`, `Timers.raceFirst`, a bounded wait). Anything else that forks stays counted, with its
65
+ * children, until every one is done: a fiber that is still working must never go uncounted.
66
+ */
67
+ readonly joining: <A, E, R>(work: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
68
+ /** The same service with its tokens counted for `owner` (a path: an operation's name, `agents/run/<runId>`, ...). */
69
+ readonly as: (owner: string) => BusyShape;
70
+ }
71
+ declare const Busy_base: Context.ServiceClass<Busy, "@orthograph/language-effect/Busy", BusyShape>;
72
+ export declare class Busy extends Busy_base {
73
+ }
74
+ /** The box the fiber is running under: set by `counted` and `spawn`, read by a sleep when it lends the token out. */
75
+ export declare const BusyBoxRef: Context.Reference<BusyBox | undefined>;
76
+ declare const WorldCount_base: Context.ServiceClass<WorldCount, "@orthograph/language-effect/WorldCount", {
77
+ /** Take a token for `owner`: its id, which carries the world's generation. */
78
+ readonly enter: (owner: string) => Effect.Effect<string>;
79
+ /** Release exactly that token. An id of an older generation, or one already released, does nothing. */
80
+ readonly leave: (id: string) => Effect.Effect<void>;
81
+ /** Is this token still held by the world? A box whose id the world no longer knows (a reset) takes a fresh token. */
82
+ readonly valid?: (id: string) => Effect.Effect<boolean>;
83
+ /**
84
+ * Open a write section for this token. `true`: open. `false`: the world no longer holds the token (a reset ended the run), so the request is
85
+ * stale. `"closed"`: the world holds it, and is closed to writes while a reset drains: the write is refused but the token is still the
86
+ * request's, and normal cleanup releases it (a reset that fails opens the world again).
87
+ */
88
+ readonly section?: (id: string) => Effect.Effect<boolean | "closed">;
89
+ /** The write section ended. */
90
+ readonly sectionEnd?: (id: string) => Effect.Effect<void>;
91
+ /** A request's token was taken at its entry under the entry's name; the operation names it when it begins. */
92
+ readonly name?: (id: string, owner: string) => Effect.Effect<void>;
93
+ /** The box of the request this fiber runs in, when the entry took a token for it. */
94
+ readonly request?: Effect.Effect<BusyBox | undefined>;
95
+ }>;
96
+ /**
97
+ * Where a world keeps its count of work in flight, across every process in it. A process that reports here (`BusyReporting`)
98
+ * takes a token for each box and gives it back by its id; the world's Durable Object is quiet when none of the current
99
+ * generation is held (M8.07).
100
+ */
101
+ export declare class WorldCount extends WorldCount_base {
102
+ }
103
+ export {};
@@ -0,0 +1,19 @@
1
+ // Counted work: how a world knows it is quiet. Every piece of work that can read the clock holds a token for as long as it runs
2
+ // (M8-07, call 12). A token has an identity: the world gives each one an id that carries its generation, and only that id releases it.
3
+ import { Context, Semaphore } from "effect";
4
+ /** The `<epoch>.<generation>` of a token id: everything before the last dot. */
5
+ export const genOf = (id) => id.slice(0, Math.max(0, id.lastIndexOf(".")));
6
+ export const makeBox = (owner, options = {}) => ({
7
+ owner, id: options.id, gen: options.id === undefined ? undefined : genOf(options.id), holders: 0, lent: 0, pinned: options.pinned ?? false, stale: false, gate: Semaphore.makeUnsafe(1), rootIds: new Set(), joining: new Set(),
8
+ });
9
+ export class Busy extends Context.Service()("@orthograph/language-effect/Busy") {
10
+ }
11
+ /** The box the fiber is running under: set by `counted` and `spawn`, read by a sleep when it lends the token out. */
12
+ export const BusyBoxRef = Context.Reference("@orthograph/language-effect/BusyBox", { defaultValue: () => undefined });
13
+ /**
14
+ * Where a world keeps its count of work in flight, across every process in it. A process that reports here (`BusyReporting`)
15
+ * takes a token for each box and gives it back by its id; the world's Durable Object is quiet when none of the current
16
+ * generation is held (M8.07).
17
+ */
18
+ export class WorldCount extends Context.Service()("@orthograph/language-effect/WorldCount") {
19
+ }
@@ -0,0 +1 @@
1
+ export { Audience, Visibility } from "#modules/visibility/service.js";
@@ -0,0 +1,3 @@
1
+ // `@orthograph/language-effect/visibility`: the one visibility rule on its own, for generated code that a browser bundles (the
2
+ // API's wire schemas, which the SDK decodes with), without the rest of the runtime.
3
+ export { Audience, Visibility } from "#modules/visibility/service.js";
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@orthograph/language-effect",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-next.2",
4
+ "license": "Apache-2.0",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ },
11
+ "./device": {
12
+ "types": "./dist/device.d.ts",
13
+ "default": "./dist/device.js"
14
+ },
15
+ "./visibility": {
16
+ "types": "./dist/visibility.d.ts",
17
+ "default": "./dist/visibility.js"
18
+ }
19
+ },
20
+ "dependencies": {
21
+ "@cfworker/json-schema": "4.1.4",
22
+ "@modelcontextprotocol/sdk": "1.32.2",
23
+ "effect": "4.1.4",
24
+ "zod": "4.5.4",
25
+ "orthograph": "0.1.0-next.2"
26
+ },
27
+ "imports": {
28
+ "#modules/*.js": "./dist/modules/*.js"
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "publishConfig": {
34
+ "access": "public",
35
+ "tag": "next"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/Open-Harness/orthograph.git",
40
+ "directory": "packages/language-effect"
41
+ }
6
42
  }