@orthograph/language-effect 0.0.0-stage → 0.1.0-next.1
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.
- package/LICENSE +202 -0
- package/README.md +2 -2
- package/dist/device.d.ts +5 -0
- package/dist/device.js +6 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +25 -0
- package/dist/modules/access/internal/path.d.ts +3 -0
- package/dist/modules/access/internal/path.js +11 -0
- package/dist/modules/access/service.d.ts +5 -0
- package/dist/modules/access/service.js +111 -0
- package/dist/modules/access/types.d.ts +38 -0
- package/dist/modules/access/types.js +3 -0
- package/dist/modules/auth/service.d.ts +4 -0
- package/dist/modules/auth/service.js +19 -0
- package/dist/modules/auth/types.d.ts +38 -0
- package/dist/modules/auth/types.js +8 -0
- package/dist/modules/calls/internal/run.d.ts +2 -0
- package/dist/modules/calls/internal/run.js +28 -0
- package/dist/modules/calls/service.d.ts +2 -0
- package/dist/modules/calls/service.js +2 -0
- package/dist/modules/calls/types.d.ts +32 -0
- package/dist/modules/calls/types.js +1 -0
- package/dist/modules/handler/internal/attributes.d.ts +17 -0
- package/dist/modules/handler/internal/attributes.js +46 -0
- package/dist/modules/handler/internal/call-id.d.ts +2 -0
- package/dist/modules/handler/internal/call-id.js +5 -0
- package/dist/modules/handler/internal/call-metrics.d.ts +17 -0
- package/dist/modules/handler/internal/call-metrics.js +35 -0
- package/dist/modules/handler/internal/decided.d.ts +14 -0
- package/dist/modules/handler/internal/decided.js +106 -0
- package/dist/modules/handler/internal/defect.d.ts +4 -0
- package/dist/modules/handler/internal/defect.js +19 -0
- package/dist/modules/handler/internal/derived.d.ts +21 -0
- package/dist/modules/handler/internal/derived.js +96 -0
- package/dist/modules/handler/internal/device.d.ts +3 -0
- package/dist/modules/handler/internal/device.js +46 -0
- package/dist/modules/handler/internal/digest.d.ts +3 -0
- package/dist/modules/handler/internal/digest.js +52 -0
- package/dist/modules/handler/internal/environment.d.ts +3 -0
- package/dist/modules/handler/internal/environment.js +3 -0
- package/dist/modules/handler/internal/invariants.d.ts +12 -0
- package/dist/modules/handler/internal/invariants.js +28 -0
- package/dist/modules/handler/internal/move.d.ts +32 -0
- package/dist/modules/handler/internal/move.js +84 -0
- package/dist/modules/handler/internal/prehandle.d.ts +23 -0
- package/dist/modules/handler/internal/prehandle.js +34 -0
- package/dist/modules/handler/internal/read.d.ts +3 -0
- package/dist/modules/handler/internal/read.js +79 -0
- package/dist/modules/handler/internal/signal.d.ts +3 -0
- package/dist/modules/handler/internal/signal.js +10 -0
- package/dist/modules/handler/internal/span.d.ts +8 -0
- package/dist/modules/handler/internal/span.js +14 -0
- package/dist/modules/handler/service.d.ts +7 -0
- package/dist/modules/handler/service.js +163 -0
- package/dist/modules/handler/types.d.ts +217 -0
- package/dist/modules/handler/types.js +24 -0
- package/dist/modules/ids/service.d.ts +2 -0
- package/dist/modules/ids/service.js +11 -0
- package/dist/modules/ids/types.d.ts +8 -0
- package/dist/modules/ids/types.js +1 -0
- package/dist/modules/journal/service.d.ts +6 -0
- package/dist/modules/journal/service.js +4 -0
- package/dist/modules/journal/types.d.ts +22 -0
- package/dist/modules/journal/types.js +5 -0
- package/dist/modules/mcp/internal/input.d.ts +15 -0
- package/dist/modules/mcp/internal/input.js +35 -0
- package/dist/modules/mcp/internal/output.d.ts +16 -0
- package/dist/modules/mcp/internal/output.js +50 -0
- package/dist/modules/mcp/internal/tool.d.ts +4 -0
- package/dist/modules/mcp/internal/tool.js +56 -0
- package/dist/modules/mcp/service.d.ts +2 -0
- package/dist/modules/mcp/service.js +38 -0
- package/dist/modules/mcp/types.d.ts +101 -0
- package/dist/modules/mcp/types.js +1 -0
- package/dist/modules/mount/internal/none.d.ts +2 -0
- package/dist/modules/mount/internal/none.js +1 -0
- package/dist/modules/mount/service.d.ts +2 -0
- package/dist/modules/mount/service.js +12 -0
- package/dist/modules/mount/types.d.ts +11 -0
- package/dist/modules/mount/types.js +4 -0
- package/dist/modules/principals/service.d.ts +4 -0
- package/dist/modules/principals/service.js +4 -0
- package/dist/modules/principals/types.d.ts +15 -0
- package/dist/modules/principals/types.js +4 -0
- package/dist/modules/records/internal/holder.d.ts +16 -0
- package/dist/modules/records/internal/holder.js +55 -0
- package/dist/modules/records/internal/list.d.ts +12 -0
- package/dist/modules/records/internal/list.js +65 -0
- package/dist/modules/records/internal/order.d.ts +18 -0
- package/dist/modules/records/internal/order.js +41 -0
- package/dist/modules/records/internal/unique.d.ts +31 -0
- package/dist/modules/records/internal/unique.js +49 -0
- package/dist/modules/records/internal/unit.d.ts +3 -0
- package/dist/modules/records/internal/unit.js +135 -0
- package/dist/modules/records/service.d.ts +22 -0
- package/dist/modules/records/service.js +102 -0
- package/dist/modules/records/types.d.ts +170 -0
- package/dist/modules/records/types.js +9 -0
- package/dist/modules/timers/internal/world.d.ts +4 -0
- package/dist/modules/timers/internal/world.js +64 -0
- package/dist/modules/timers/service.d.ts +18 -0
- package/dist/modules/timers/service.js +125 -0
- package/dist/modules/timers/types.d.ts +102 -0
- package/dist/modules/timers/types.js +40 -0
- package/dist/modules/versions/internal/handlers.d.ts +6 -0
- package/dist/modules/versions/internal/handlers.js +36 -0
- package/dist/modules/versions/internal/keep.d.ts +21 -0
- package/dist/modules/versions/internal/keep.js +36 -0
- package/dist/modules/versions/service.d.ts +2 -0
- package/dist/modules/versions/service.js +14 -0
- package/dist/modules/versions/types.d.ts +27 -0
- package/dist/modules/versions/types.js +1 -0
- package/dist/modules/visibility/internal/rule.d.ts +7 -0
- package/dist/modules/visibility/internal/rule.js +23 -0
- package/dist/modules/visibility/internal/schema.d.ts +5 -0
- package/dist/modules/visibility/internal/schema.js +12 -0
- package/dist/modules/visibility/service.d.ts +15 -0
- package/dist/modules/visibility/service.js +27 -0
- package/dist/modules/visibility/types.d.ts +60 -0
- package/dist/modules/visibility/types.js +1 -0
- package/dist/modules/wire/internal/call-telemetry.d.ts +15 -0
- package/dist/modules/wire/internal/call-telemetry.js +31 -0
- package/dist/modules/wire/internal/refused-payload.d.ts +7 -0
- package/dist/modules/wire/internal/refused-payload.js +25 -0
- package/dist/modules/wire/internal/shown.d.ts +9 -0
- package/dist/modules/wire/internal/shown.js +32 -0
- package/dist/modules/wire/service.d.ts +3 -0
- package/dist/modules/wire/service.js +14 -0
- package/dist/modules/wire/types.d.ts +30 -0
- package/dist/modules/wire/types.js +1 -0
- package/dist/modules/work/internal/fake-world.d.ts +17 -0
- package/dist/modules/work/internal/fake-world.js +43 -0
- package/dist/modules/work/internal/fibers.d.ts +2 -0
- package/dist/modules/work/internal/fibers.js +5 -0
- package/dist/modules/work/service.d.ts +6 -0
- package/dist/modules/work/service.js +249 -0
- package/dist/modules/work/types.d.ts +103 -0
- package/dist/modules/work/types.js +19 -0
- package/dist/visibility.d.ts +1 -0
- package/dist/visibility.js +3 -0
- package/package.json +34 -3
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Timers: the only way anything waits. Nothing reads the wall clock and nothing sleeps on its own; a world's clock
|
|
2
|
+
// (real, stepped or scaled) decides when each timer is due. Two services, one table (M8-timers-shape):
|
|
3
|
+
// `Timers` waits inside a fiber; `DurableTimers` sets a timer that outlives the fiber and the process, and the world
|
|
4
|
+
// hands it back through the host's `TimerWakes` when it falls due.
|
|
5
|
+
import { Context, Effect as Fx } from "effect";
|
|
6
|
+
import { Busy } from "#modules/work/types.js";
|
|
7
|
+
export class Timers extends Context.Service()("@orthograph/language-effect/Timers") {
|
|
8
|
+
/**
|
|
9
|
+
* `Effect.all`, for work that sleeps in parallel with other work: the parent only waits, so it is not counted while it does, and the world can fire the sleeper
|
|
10
|
+
* once the working branches are done. A plain `Effect.all` stays counted, with its children, until every one is done (a move meanwhile
|
|
11
|
+
* fails `NeverQuiet`, naming the request).
|
|
12
|
+
*/
|
|
13
|
+
static all = (effects, options) => Fx.flatMap(Fx.service(Busy), (busy) => {
|
|
14
|
+
// Only a call that really forks joins. With the default concurrency the effects run one after another in the calling fiber, which is doing
|
|
15
|
+
// the work itself: marking it joining would leave real I/O uncounted.
|
|
16
|
+
const concurrency = options?.concurrency;
|
|
17
|
+
// Effect runs 0 and negative concurrency, and 1, one after another: only "unbounded" and a number above one fork.
|
|
18
|
+
const forks = effects.length > 1 && (concurrency === "unbounded" || (typeof concurrency === "number" && concurrency > 1));
|
|
19
|
+
return forks ? busy.joining(Fx.all(effects, options)) : Fx.all(effects, options);
|
|
20
|
+
});
|
|
21
|
+
/** `Effect.raceFirst`, as a join. */
|
|
22
|
+
static raceFirst = (a, b) => Fx.flatMap(Fx.service(Busy), (busy) => busy.joining(Fx.raceFirst(a, b)));
|
|
23
|
+
}
|
|
24
|
+
export class DurableTimers extends Context.Service()("@orthograph/language-effect/DurableTimers") {
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The host's side: how a fired durable timer reaches the code that set it, in whatever process now runs it (an evicted
|
|
28
|
+
* Durable Object is woken by its binding). The world calls `wake` at least once per firing, as counted work, and `wake`
|
|
29
|
+
* returns only after the work it woke holds its own token, so the world is never quiet between the two. A handler is
|
|
30
|
+
* idempotent by `(owner, name, at)`: a second firing changes nothing.
|
|
31
|
+
*/
|
|
32
|
+
export class TimerWakes extends Context.Service()("@orthograph/language-effect/TimerWakes") {
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The world's timer table as another process sees it (M8.07): rows kept in the world's Durable Object, which fires them
|
|
36
|
+
* when its clock reaches them. A sleeping row lives as long as the process that parked it; a durable row outlives it.
|
|
37
|
+
* The host provides this (host-cloudflare, over the world's RPC); `TimersWorld` is `Timers` and `DurableTimers` on it.
|
|
38
|
+
*/
|
|
39
|
+
export class WorldTable extends Context.Service()("@orthograph/language-effect/WorldTable") {
|
|
40
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type AnyErrorClass, type Thing } from "orthograph";
|
|
2
|
+
import { type LooseHandler } from "./keep.ts";
|
|
3
|
+
export declare const save: (thing: Thing, stale: AnyErrorClass | undefined) => LooseHandler;
|
|
4
|
+
export declare const restore: (thing: Thing, stale: AnyErrorClass | undefined) => LooseHandler;
|
|
5
|
+
export declare const get: (thing: Thing) => LooseHandler;
|
|
6
|
+
export declare const history: (thing: Thing) => LooseHandler;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The four a thing with versions is served: save, restore, get and history.
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { NotAllowed } from "orthograph";
|
|
4
|
+
import { Store } from "#modules/records/types.js";
|
|
5
|
+
import { contentOf, keep, keyOf, newest } from "./keep.js";
|
|
6
|
+
/** How many versions a history reads at most, a page at a time: a policy is saved by hand, so this is far more than anyone keeps. */
|
|
7
|
+
const HISTORY = 1000;
|
|
8
|
+
export const save = (thing, stale) => (payload, context) => keep(thing, stale, payload["base"], contentOf(thing, payload), context);
|
|
9
|
+
export const restore = (thing, stale) => (payload, context) => Effect.gen(function* () {
|
|
10
|
+
const wanted = yield* newest(thing, payload[keyOf(thing)], { revision: payload["revision"] });
|
|
11
|
+
// A version that was never saved is no one's to read: refused as a read of nothing is.
|
|
12
|
+
if (wanted === undefined)
|
|
13
|
+
return yield* new NotAllowed();
|
|
14
|
+
return yield* keep(thing, stale, payload["base"], { ...contentOf(thing, wanted), [keyOf(thing)]: payload[keyOf(thing)] }, context);
|
|
15
|
+
});
|
|
16
|
+
export const get = (thing) => (payload) => Effect.gen(function* () {
|
|
17
|
+
const row = yield* newest(thing, payload[keyOf(thing)], payload["revision"] === undefined ? {} : { revision: payload["revision"] });
|
|
18
|
+
if (row === undefined)
|
|
19
|
+
return yield* new NotAllowed();
|
|
20
|
+
const { state: _state, ...fields } = row;
|
|
21
|
+
return fields;
|
|
22
|
+
});
|
|
23
|
+
export const history = (thing) => (payload) => Effect.gen(function* () {
|
|
24
|
+
const store = yield* Store;
|
|
25
|
+
const entries = [];
|
|
26
|
+
let after;
|
|
27
|
+
do {
|
|
28
|
+
const page = yield* store.list({ thing, where: { [keyOf(thing)]: payload[keyOf(thing)] }, state: undefined, order: "newest", limit: 100, after });
|
|
29
|
+
for (const row of page.items) {
|
|
30
|
+
const saved = row["saved"];
|
|
31
|
+
entries.push({ revision: Number(row["revision"]), at: saved?.at ?? 0, by: saved?.by ?? "", from: saved?.from ?? 0 });
|
|
32
|
+
}
|
|
33
|
+
after = page.next ?? undefined;
|
|
34
|
+
} while (after !== undefined && entries.length < HISTORY);
|
|
35
|
+
return { entries };
|
|
36
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type AnyErrorClass, type Thing } from "orthograph";
|
|
3
|
+
import { Records, Store, type Instance } from "#modules/records/types.js";
|
|
4
|
+
import type { VersionContext } from "../types.ts";
|
|
5
|
+
/** A handler as written here, failing as its operation declares (a stale save, a read of nothing); `Versions.handler` hands it on typed as the operation's own. */
|
|
6
|
+
export type LooseHandler = (payload: Readonly<Record<string, unknown>>, context: VersionContext) => Effect.Effect<Readonly<Record<string, unknown>>, unknown, Records | Store>;
|
|
7
|
+
export declare const keyOf: (thing: Thing) => string;
|
|
8
|
+
/** The newest version of one key, or none. Every save is a new row, so the newest made is the current one. */
|
|
9
|
+
export declare const newest: (thing: Thing, key: unknown, extra?: Readonly<Record<string, unknown>>) => Effect.Effect<Instance | undefined, never, Store>;
|
|
10
|
+
/** The fields of a version a save carries: the thing's own, but for its id, its revision and the framework's `saved`. */
|
|
11
|
+
export declare const contentOf: (thing: Thing, from: Readonly<Record<string, unknown>>) => {
|
|
12
|
+
[k: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Keep `fields` as the version after the newest, refusing a save made from any version but the newest. The newest row is touched by
|
|
16
|
+
* the same commit, so two saves from one base cannot both land: the one that commits second finds its version moved, and runs again
|
|
17
|
+
* to be refused with the new current one.
|
|
18
|
+
*/
|
|
19
|
+
export declare const keep: (thing: Thing, stale: AnyErrorClass | undefined, base: bigint, fields: Readonly<Record<string, unknown>>, context: VersionContext) => Effect.Effect<{
|
|
20
|
+
revision: bigint;
|
|
21
|
+
}, import("effect/Cause").YieldableError & import("orthograph").BlueprintError, Records | Store>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// What the four share: the newest version of a key, and keeping a new one after it.
|
|
2
|
+
import { Clock, Effect } from "effect";
|
|
3
|
+
import { acting } from "orthograph";
|
|
4
|
+
import { Ids } from "#modules/ids/service.js";
|
|
5
|
+
import { Records, Store } from "#modules/records/types.js";
|
|
6
|
+
export const keyOf = (thing) => thing.versions.key;
|
|
7
|
+
/** The newest version of one key, or none. Every save is a new row, so the newest made is the current one. */
|
|
8
|
+
export const newest = (thing, key, extra = {}) => Effect.gen(function* () {
|
|
9
|
+
const store = yield* Store;
|
|
10
|
+
const found = yield* store.list({ thing, where: { [keyOf(thing)]: key, ...extra }, state: undefined, order: "newest", limit: 1, after: undefined });
|
|
11
|
+
return found.items[0];
|
|
12
|
+
});
|
|
13
|
+
const revisionOf = (row) => (row === undefined ? 0n : row["revision"]);
|
|
14
|
+
/** Who saved: the person, or the person an agent acts for; anyone else (the system) by its kind. */
|
|
15
|
+
const byOf = (context) => acting(context.principal)?.person ?? context.principal.kind;
|
|
16
|
+
/** The fields of a version a save carries: the thing's own, but for its id, its revision and the framework's `saved`. */
|
|
17
|
+
export const contentOf = (thing, from) => Object.fromEntries(Object.keys(thing.fields).filter((k) => !["id", "revision", "saved"].includes(k) && k in from).map((k) => [k, from[k]]));
|
|
18
|
+
/**
|
|
19
|
+
* Keep `fields` as the version after the newest, refusing a save made from any version but the newest. The newest row is touched by
|
|
20
|
+
* the same commit, so two saves from one base cannot both land: the one that commits second finds its version moved, and runs again
|
|
21
|
+
* to be refused with the new current one.
|
|
22
|
+
*/
|
|
23
|
+
export const keep = (thing, stale, base, fields, context) => Effect.gen(function* () {
|
|
24
|
+
const records = yield* Records;
|
|
25
|
+
const latest = yield* newest(thing, fields[keyOf(thing)]);
|
|
26
|
+
const current = revisionOf(latest);
|
|
27
|
+
if (base !== current)
|
|
28
|
+
return yield* (stale === undefined ? Effect.die(new Error("a save or restore of a thing with versions needs the refusal of a stale save")) : Effect.fail(new stale({ current })));
|
|
29
|
+
const at = yield* Clock.currentTimeMillis;
|
|
30
|
+
const id = context.id ?? (yield* Ids.of(thing.fields["id"]));
|
|
31
|
+
if (latest !== undefined)
|
|
32
|
+
yield* records.update(thing, String(latest["id"]), {});
|
|
33
|
+
yield* records.insert(thing, { ...fields, id, revision: current + 1n, saved: { at, by: byOf(context), from: Number(base) } });
|
|
34
|
+
yield* context.move();
|
|
35
|
+
return { revision: current + 1n };
|
|
36
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { VERSION_RULE, versionRule } from "orthograph/server";
|
|
3
|
+
import { get, history, restore, save } from "./internal/handlers.js";
|
|
4
|
+
const VERBS = ["save", "get", "history", "restore"];
|
|
5
|
+
/** An operation that declares no move (a thing with versions and no machine) has nothing to move: its handler makes none. */
|
|
6
|
+
const unmoved = (moves, handler) => (moves ? handler : (payload, context) => handler(payload, { ...context, move: () => Effect.void }));
|
|
7
|
+
export const Versions = {
|
|
8
|
+
rulesOf: (name, thing) => {
|
|
9
|
+
const rule = versionRule(thing);
|
|
10
|
+
return rule === undefined ? [] : [{ name: `${name}.${VERSION_RULE}`, rule }];
|
|
11
|
+
},
|
|
12
|
+
handler: (verb, thing, stale, moves = true) => unmoved(moves, verb === "save" ? save(thing, stale) : verb === "restore" ? restore(thing, stale) : verb === "get" ? get(thing) : history(thing)),
|
|
13
|
+
serves: (thing, verb, payload) => thing.versions !== undefined && VERBS.includes(verb) && payload.includes(thing.versions.key),
|
|
14
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { AnyErrorClass, Principal, Thing } from "orthograph";
|
|
3
|
+
import type { NamedRule, Records, Store } from "#modules/records/types.js";
|
|
4
|
+
/** The four a thing with versions is served: the verbs of its operations. */
|
|
5
|
+
export type VersionVerb = "save" | "get" | "history" | "restore";
|
|
6
|
+
/** What the wrapper hands every handler, as far as these four read it. */
|
|
7
|
+
export interface VersionContext {
|
|
8
|
+
readonly principal: Principal;
|
|
9
|
+
readonly move: (to?: string) => Effect.Effect<void>;
|
|
10
|
+
readonly id: string | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A handler of one of the four: the payload its operation declares, an answer or the stale-save refusal, and the services the store
|
|
14
|
+
* gives. Written for any thing, so what it takes and answers is the declaration's own, which only the generated server knows.
|
|
15
|
+
*/
|
|
16
|
+
export type VersionHandler = (payload: any, context: any) => Effect.Effect<any, never, Records | Store>;
|
|
17
|
+
export interface VersionsApi {
|
|
18
|
+
/** The rule a thing with versions holds beside its own (one row per revision of a key): the runtime and the store enforce it, so two first saves cannot both land. */
|
|
19
|
+
readonly rulesOf: (name: string, thing: Thing) => ReadonlyArray<NamedRule>;
|
|
20
|
+
/**
|
|
21
|
+
* The handler of one verb for a thing with `versions`. `stale` is the refusal a save or restore made from an old version
|
|
22
|
+
* answers with (its one field is `current`): the operation's own error class, which the compiler finds by that shape.
|
|
23
|
+
*/
|
|
24
|
+
readonly handler: (verb: VersionVerb, thing: Thing, stale?: AnyErrorClass, moves?: boolean) => VersionHandler;
|
|
25
|
+
/** Is this operation one of the four of a thing with versions: its verb is one of them, and the thing's key is a field of its payload? */
|
|
26
|
+
readonly serves: (thing: Thing, verb: string, payload: ReadonlyArray<string>) => verb is VersionVerb;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FieldInput } from "orthograph";
|
|
2
|
+
import type { Audience } from "../types.ts";
|
|
3
|
+
/** Whether `audience` is shown a value declared as `field`. */
|
|
4
|
+
export declare const sees: (audience: Audience, field: FieldInput) => boolean;
|
|
5
|
+
/** How a schema for `audience` names a field: always there, there for some callers, or not at all. */
|
|
6
|
+
export type Presence = "required" | "optional" | "absent";
|
|
7
|
+
export declare const presence: (audience: Audience, field: FieldInput) => Presence;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { traceable } from "orthograph/server";
|
|
2
|
+
const holds = (visibility, held) => typeof visibility === "object" && visibility.to.some((role) => held.includes(role));
|
|
3
|
+
/** Whether `audience` is shown a value declared as `field`. */
|
|
4
|
+
export const sees = (audience, field) => {
|
|
5
|
+
const v = field.visibility;
|
|
6
|
+
switch (audience.kind) {
|
|
7
|
+
case "principal": return v === "public" || holds(v, audience.held) || (v === "secret" && audience.issuing && field.issued === true);
|
|
8
|
+
case "agent": return v === "public" || holds(v, audience.held);
|
|
9
|
+
case "telemetry": return traceable(field);
|
|
10
|
+
case "public": return v === "public";
|
|
11
|
+
case "owner": return v !== "secret";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
export const presence = (audience, field) => {
|
|
15
|
+
const v = field.visibility;
|
|
16
|
+
switch (audience.kind) {
|
|
17
|
+
// The wire's schema: a client decodes what a server sent, so every declared field is named and only the ones every
|
|
18
|
+
// caller is shown (public, or the secret this call issued) are required.
|
|
19
|
+
case "principal": return v === "public" || (audience.issuing && field.issued === true) ? "required" : "optional";
|
|
20
|
+
case "agent": return v === "public" ? "required" : typeof v === "object" ? "optional" : "absent";
|
|
21
|
+
default: return sees(audience, field) ? "required" : "absent";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { FieldsInput } from "orthograph";
|
|
3
|
+
import type { Audience } from "../types.ts";
|
|
4
|
+
/** The struct of the fields `audience` can be answered with. */
|
|
5
|
+
export declare const schemaFor: (audience: Audience, fields: FieldsInput, items: FieldsInput | undefined) => Schema.Top;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { presence } from "./rule.js";
|
|
3
|
+
const schemaOf = (field) => (field.kind === "field" ? field.value.schema : field.schema);
|
|
4
|
+
/** The struct of the fields `audience` can be answered with. */
|
|
5
|
+
export const schemaFor = (audience, fields, items) => Schema.Struct(Object.fromEntries(Object.entries(fields).flatMap(([key, field]) => {
|
|
6
|
+
const shown = presence(audience, field);
|
|
7
|
+
if (shown === "absent")
|
|
8
|
+
return [];
|
|
9
|
+
// A list's items are whole instances: each one is cut the same way, by the thing's own fields.
|
|
10
|
+
const schema = key === "items" && items !== undefined ? Schema.Array(schemaFor(audience, items, undefined)) : schemaOf(field);
|
|
11
|
+
return [[key, shown === "optional" ? Schema.optionalKey(schema) : schema]];
|
|
12
|
+
})));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { FieldsInput, Role } from "orthograph";
|
|
2
|
+
import type { Audience as Seen, AudienceApi, ItemFields, VisibilityApi } from "./types.ts";
|
|
3
|
+
/** Who is looking: `Audience.principal(held)`, `Audience.agent(held)`, `Audience.telemetry`, `Audience.public`, `Audience.owner`. Also the type of one. */
|
|
4
|
+
export type Audience = Seen;
|
|
5
|
+
export declare const Audience: AudienceApi;
|
|
6
|
+
export declare const Visibility: VisibilityApi;
|
|
7
|
+
/**
|
|
8
|
+
* What a caller is answered with, cut by the one rule: `answer` as the audience of a caller holding `held` is shown it.
|
|
9
|
+
* An operation's answer is the one call that issued whatever it marks `issued`; a read issues nothing. The server and the
|
|
10
|
+
* device both call this, so no path has a rule of its own.
|
|
11
|
+
*/
|
|
12
|
+
export declare const cutAnswer: (op: {
|
|
13
|
+
readonly kind: string;
|
|
14
|
+
readonly answer: FieldsInput;
|
|
15
|
+
}, answer: object, held: ReadonlyArray<Role<any>>, items?: ItemFields) => Record<string, unknown>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { sees } from "./internal/rule.js";
|
|
2
|
+
import { schemaFor } from "./internal/schema.js";
|
|
3
|
+
export const Audience = {
|
|
4
|
+
principal: (held = [], options) => Object.freeze({ kind: "principal", held, issuing: options?.issuing === true }),
|
|
5
|
+
agent: (held = []) => Object.freeze({ kind: "agent", held }),
|
|
6
|
+
telemetry: Object.freeze({ kind: "telemetry" }),
|
|
7
|
+
public: Object.freeze({ kind: "public" }),
|
|
8
|
+
owner: Object.freeze({ kind: "owner" }),
|
|
9
|
+
};
|
|
10
|
+
/** The keys of `values` that the declaration names and `audience` is shown. A key it does not name is never read on. */
|
|
11
|
+
const cut = (audience, fields, values) => Object.fromEntries(Object.entries(values).filter(([key]) => Object.hasOwn(fields, key) && sees(audience, fields[key])));
|
|
12
|
+
export const Visibility = {
|
|
13
|
+
sees,
|
|
14
|
+
redact: (audience, fields, values, items) => {
|
|
15
|
+
const top = cut(audience, fields, values);
|
|
16
|
+
const listed = top["items"];
|
|
17
|
+
return items !== undefined && Array.isArray(listed) ? { ...top, items: listed.map((item) => cut(audience, items, item)) } : top;
|
|
18
|
+
},
|
|
19
|
+
schema: ((audience, fields, items) => schemaFor(audience, fields, items)),
|
|
20
|
+
needsRoles: (...groups) => groups.some((fields) => Object.values(fields).some((f) => typeof f.visibility === "object")),
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* What a caller is answered with, cut by the one rule: `answer` as the audience of a caller holding `held` is shown it.
|
|
24
|
+
* An operation's answer is the one call that issued whatever it marks `issued`; a read issues nothing. The server and the
|
|
25
|
+
* device both call this, so no path has a rule of its own.
|
|
26
|
+
*/
|
|
27
|
+
export const cutAnswer = (op, answer, held, items) => Visibility.redact(Audience.principal(held, { issuing: op.kind === "operation" }), op.answer, answer, items);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { FieldInput, FieldsInput, Role } from "orthograph";
|
|
2
|
+
import type { structOf } from "orthograph/server";
|
|
3
|
+
/**
|
|
4
|
+
* Who is looking.
|
|
5
|
+
* - `principal`: someone who called, with the roles they hold in this call. `issuing` is true only for the answer of the
|
|
6
|
+
* call that issued a secret, to that call's own caller: it is what lets an `issued` secret out, once.
|
|
7
|
+
* - `agent`: an AI agent acting for a person (MCP), who holds the roles the person holds, and is never given a secret.
|
|
8
|
+
* - `telemetry`: spans, logs and metrics, which a Collector keeps: public, traceable values only.
|
|
9
|
+
* - `public`: anyone who can read the record (the journal, the docs): public values only.
|
|
10
|
+
* - `owner`: whoever runs the deployment, reading its own record: public and private values, never a secret.
|
|
11
|
+
*/
|
|
12
|
+
export type Audience = {
|
|
13
|
+
readonly kind: "principal";
|
|
14
|
+
readonly held: ReadonlyArray<Role<any>>;
|
|
15
|
+
readonly issuing: boolean;
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: "agent";
|
|
18
|
+
readonly held: ReadonlyArray<Role<any>>;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: "telemetry";
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "public";
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: "owner";
|
|
25
|
+
};
|
|
26
|
+
export interface AudienceApi {
|
|
27
|
+
/** A caller with the roles `held` in this call (none for the anonymous). `issuing` marks the answer that issued a secret. */
|
|
28
|
+
readonly principal: (held?: ReadonlyArray<Role<any>>, options?: {
|
|
29
|
+
readonly issuing?: boolean;
|
|
30
|
+
}) => Audience;
|
|
31
|
+
/** An agent acting for a person who holds `held`. */
|
|
32
|
+
readonly agent: (held?: ReadonlyArray<Role<any>>) => Audience;
|
|
33
|
+
readonly telemetry: Audience;
|
|
34
|
+
readonly public: Audience;
|
|
35
|
+
readonly owner: Audience;
|
|
36
|
+
}
|
|
37
|
+
/** The fields of a list read's items: the thing's own, and `state`. */
|
|
38
|
+
export type ItemFields = FieldsInput;
|
|
39
|
+
export interface VisibilityApi {
|
|
40
|
+
/**
|
|
41
|
+
* Whether `audience` is shown a value declared as `field`. Public is shown to all (to telemetry only when traceable); a
|
|
42
|
+
* private field to a principal or an agent who holds one of its roles, and to the owner; a secret to no one, except an
|
|
43
|
+
* `issued` one to the principal whose call issued it.
|
|
44
|
+
*/
|
|
45
|
+
readonly sees: (audience: Audience, field: FieldInput) => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* `values` cut to what `audience` is shown: the keys the declaration `fields` names and that it sees, nothing else. A
|
|
48
|
+
* list read's `items` are cut the same way by `items`. Never reads a key the declaration does not name.
|
|
49
|
+
*/
|
|
50
|
+
readonly redact: (audience: Audience, fields: FieldsInput, values: object, items?: ItemFields) => Record<string, unknown>;
|
|
51
|
+
/**
|
|
52
|
+
* The struct schema of what a caller of this audience is answered with. A field it is always shown is required, one it
|
|
53
|
+
* may be shown (private to roles, per call) is optional, one it is never shown is absent, and an undeclared one is not
|
|
54
|
+
* named. The exception is a `principal`, whose schema is the wire's: it is what a client decodes of whatever a server
|
|
55
|
+
* sent, so it names every declared field and makes the ones not always shown optional (`redact` is what cuts them).
|
|
56
|
+
*/
|
|
57
|
+
readonly schema: <F extends FieldsInput>(audience: Audience, fields: F, items?: ItemFields) => ReturnType<typeof structOf<F>>;
|
|
58
|
+
/** Some field is private to roles, so who holds which role matters to what is shown. */
|
|
59
|
+
readonly needsRoles: (...groups: ReadonlyArray<FieldsInput>) => boolean;
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { HttpApiMiddleware } from "effect/http-api";
|
|
3
|
+
import { Principals } from "#modules/principals/types.js";
|
|
4
|
+
declare const CallTelemetry_base: HttpApiMiddleware.ServiceClass<CallTelemetry, "@orthograph/language-effect/CallTelemetry", {
|
|
5
|
+
requires: never;
|
|
6
|
+
provides: never;
|
|
7
|
+
error: never;
|
|
8
|
+
clientError: never;
|
|
9
|
+
requiredForClient: false;
|
|
10
|
+
security: never;
|
|
11
|
+
}, HttpApiMiddleware.HttpApiMiddleware<never, never, never>>;
|
|
12
|
+
export declare class CallTelemetry extends CallTelemetry_base {
|
|
13
|
+
static readonly layer: Layer.Layer<CallTelemetry, never, Principals>;
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Cause, Clock, Effect, Exit, Layer } from "effect";
|
|
2
|
+
import { HttpMiddleware, HttpServerError, HttpServerRequest } from "effect/http";
|
|
3
|
+
import { HttpApiError, HttpApiMiddleware } from "effect/http-api";
|
|
4
|
+
import { recordCall, RoutedCall } from "#modules/handler/internal/call-metrics.js";
|
|
5
|
+
import { Principals } from "#modules/principals/types.js";
|
|
6
|
+
import { isRefusedPayload, refusedPayload } from "./refused-payload.js";
|
|
7
|
+
const badPayload = (error) => isRefusedPayload(error) ||
|
|
8
|
+
HttpApiError.HttpApiPayloadTooLarge.is(error) ||
|
|
9
|
+
(HttpServerError.isHttpServerError(error) && error.reason._tag === "RequestParseError");
|
|
10
|
+
export class CallTelemetry extends HttpApiMiddleware.Service()("@orthograph/language-effect/CallTelemetry") {
|
|
11
|
+
static layer = Layer.merge(Layer.effect(CallTelemetry, Effect.map(Principals, (principals) => (work, { endpoint, group }) => Effect.gen(function* () {
|
|
12
|
+
const start = yield* Clock.monotonicTimeNanos;
|
|
13
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
14
|
+
const route = { operation: `${group.identifier}.${endpoint.identifier}`, started: false };
|
|
15
|
+
// The handler counts its own calls. At this boundary count calls that never reached the wrapper.
|
|
16
|
+
const rejected = principals.of(request.headers["authorization"]).pipe(Effect.withTracerEnabled(false), Effect.map((principal) => principal.kind), Effect.catchCause(() => Effect.succeed("unknown")), Effect.flatMap((kind) => recordCall(route.operation, kind, start, {
|
|
17
|
+
outcome: "refused", refusal: "InvalidPayload",
|
|
18
|
+
})));
|
|
19
|
+
return yield* work.pipe(Effect.provideService(RoutedCall, route), Effect.onExit((exit) => {
|
|
20
|
+
if (route.started)
|
|
21
|
+
return Effect.void;
|
|
22
|
+
const refused = Exit.isSuccess(exit) ? exit.value.status === 413 || exit.value.status === 415 : badPayload(Cause.squash(exit.cause));
|
|
23
|
+
return refused ? rejected : recordCall(route.operation, "unknown", start, { outcome: "defect" });
|
|
24
|
+
}),
|
|
25
|
+
// Counted above as a refused InvalidPayload; then answered with a body that names each field and why (#988).
|
|
26
|
+
// The cast: `work`'s error is Effect's opaque `unhandled`, which the guard cannot narrow.
|
|
27
|
+
Effect.catchIf(isRefusedPayload, (error) => refusedPayload(error)));
|
|
28
|
+
}))),
|
|
29
|
+
// The operation starts its span after access. An automatic HTTP span would trace a refused request anyway.
|
|
30
|
+
Layer.succeed(HttpMiddleware.TracerDisabledWhen, () => true));
|
|
31
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { HttpServerResponse } from "effect/http";
|
|
3
|
+
import { HttpApiError } from "effect/http-api";
|
|
4
|
+
/** A payload its schema refused, as the decoder reported it. */
|
|
5
|
+
export declare const isRefusedPayload: (error: unknown) => error is HttpApiError.HttpApiSchemaError;
|
|
6
|
+
/** The API's answer to a refused payload: 400, `{ _tag: "InvalidPayload", message }`, naming each refused field and why (#988). */
|
|
7
|
+
export declare const refusedPayload: (error: HttpApiError.HttpApiSchemaError) => Effect.Effect<HttpServerResponse.HttpServerResponse, never, never>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { HttpServerResponse } from "effect/http";
|
|
3
|
+
import { HttpApiError } from "effect/http-api";
|
|
4
|
+
/** Where a payload is refused and why, never what it held: a value can be a password. The SDK's words, so a client reads one shape on both sides. */
|
|
5
|
+
const said = (issue, path = []) => {
|
|
6
|
+
const where = path.length === 0 ? "the payload" : path.map(String).join(".");
|
|
7
|
+
const inner = (next, more = []) => said(next, [...path, ...more]);
|
|
8
|
+
switch (issue._tag) {
|
|
9
|
+
case "Pointer": return inner(issue.issue, issue.path);
|
|
10
|
+
case "Encoding": return inner(issue.issue);
|
|
11
|
+
case "Composite": return (issue.issues ?? []).flatMap((next) => inner(next));
|
|
12
|
+
case "OneOf": return [`${where} matches more than one kind of value`];
|
|
13
|
+
// A union none of whose members takes the value's kind (a list for an object) has no issues of its own.
|
|
14
|
+
case "AnyOf": return issue.issues?.length ? issue.issues.flatMap((next) => inner(next)) : [`${where} is not the right kind of value`];
|
|
15
|
+
case "MissingKey": return [`${where} is missing`];
|
|
16
|
+
case "UnexpectedKey": return [`${where} is not expected`];
|
|
17
|
+
case "InvalidType": return [`${where} is not the right kind of value`];
|
|
18
|
+
case "Filter": return [`${where} does not satisfy its check`];
|
|
19
|
+
default: return [`${where} is refused`];
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/** A payload its schema refused, as the decoder reported it. */
|
|
23
|
+
export const isRefusedPayload = (error) => HttpApiError.HttpApiSchemaError.is(error) && error.kind === "Payload";
|
|
24
|
+
/** The API's answer to a refused payload: 400, `{ _tag: "InvalidPayload", message }`, naming each refused field and why (#988). */
|
|
25
|
+
export const refusedPayload = (error) => Effect.succeed(HttpServerResponse.jsonUnsafe({ _tag: "InvalidPayload", message: [...new Set(said(error.cause.issue))].join("; ") }, { status: 400 }));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { FieldsInput, Principal, Role } from "orthograph";
|
|
3
|
+
import { Store } from "#modules/records/types.js";
|
|
4
|
+
import type { ShownOperation, WireApi } from "../types.ts";
|
|
5
|
+
/** The roles the caller holds in this call: asked only when some field in `groups` is private to roles. */
|
|
6
|
+
export declare const held: (op: ShownOperation, payload: object, principal: Principal, ...groups: ReadonlyArray<FieldsInput>) => Effect.Effect<ReadonlyArray<Role>, never, Store>;
|
|
7
|
+
/** A list read's `items` are the thing's instances, so their fields are the thing's own, and `state`. */
|
|
8
|
+
export declare const itemsOf: (op: ShownOperation) => FieldsInput | undefined;
|
|
9
|
+
export declare const shown: WireApi["shown"];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// What a caller is shown of a call's outcome: the answer and a refusal's fields, cut by the one visibility rule for
|
|
2
|
+
// the audience the caller is in this call (the roles they hold, and whether the call issued a secret to them).
|
|
3
|
+
import { Effect } from "effect";
|
|
4
|
+
import { listItemFields } from "orthograph/server";
|
|
5
|
+
import { Access } from "#modules/access/service.js";
|
|
6
|
+
import { Unit } from "#modules/records/service.js";
|
|
7
|
+
import { Records, Store } from "#modules/records/types.js";
|
|
8
|
+
import { Audience, cutAnswer, Visibility } from "#modules/visibility/service.js";
|
|
9
|
+
/** The roles the caller holds in this call: asked only when some field in `groups` is private to roles. */
|
|
10
|
+
export const held = (op, payload, principal, ...groups) => Visibility.needsRoles(...groups)
|
|
11
|
+
? Effect.gen(function* () {
|
|
12
|
+
const unit = yield* Unit.make(yield* Store);
|
|
13
|
+
return yield* Access.holds({ allowed: op.allowed, payloadFields: op.payload, payload, principal, thing: op.thing }).pipe(Effect.provideService(Records, unit.records));
|
|
14
|
+
})
|
|
15
|
+
: Effect.succeed([]);
|
|
16
|
+
/** A list read's `items` are the thing's instances, so their fields are the thing's own, and `state`. */
|
|
17
|
+
export const itemsOf = (op) => (op.kind === "read" && op.by === "list" ? listItemFields(op.thing) : undefined);
|
|
18
|
+
const tagOf = (e) => typeof e === "object" && e !== null && typeof e._tag === "string" ? e._tag : undefined;
|
|
19
|
+
export const shown = (declared) => (served, payload, principal) => (effect) => {
|
|
20
|
+
const op = served.operation;
|
|
21
|
+
const items = itemsOf(op);
|
|
22
|
+
return effect.pipe(
|
|
23
|
+
// An operation's answer is the call that issued whatever it marks `issued`, to its own caller; a read issues nothing.
|
|
24
|
+
Effect.flatMap((answer) => held(op, payload, principal, op.answer, ...(items ? [items] : [])).pipe(Effect.map((roles) => cutAnswer(op, answer, roles, items)))), Effect.catch((failure) => {
|
|
25
|
+
const tag = tagOf(failure);
|
|
26
|
+
const fields = tag === undefined ? undefined : declared[tag]?.fields;
|
|
27
|
+
// The machine's own refusal, `NotAllowed` and a bad payload carry nothing but public values: they pass as they are.
|
|
28
|
+
if (tag === undefined || fields === undefined)
|
|
29
|
+
return Effect.fail(failure);
|
|
30
|
+
return held(op, payload, principal, fields).pipe(Effect.flatMap((roles) => Effect.fail({ _tag: tag, ...Visibility.redact(Audience.principal(roles), fields, failure) })));
|
|
31
|
+
}));
|
|
32
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { held, shown } from "./internal/shown.js";
|
|
3
|
+
export { CallTelemetry } from "./internal/call-telemetry.js";
|
|
4
|
+
const fieldsOf = (e) => Object.fromEntries(Object.entries(e).filter(([k]) => k !== "_tag"));
|
|
5
|
+
export const Wire = {
|
|
6
|
+
refusals: (classes) => (effect) => Effect.catch(effect, (e) => {
|
|
7
|
+
const tag = typeof e === "object" && e !== null ? e._tag : undefined;
|
|
8
|
+
const Class = typeof tag === "string" ? classes[tag] : undefined;
|
|
9
|
+
// The code is the class's own, set beside the tag.
|
|
10
|
+
return Class ? Effect.fail(new Class({ ...fieldsOf(e), code: Class.code })) : Effect.die(e);
|
|
11
|
+
}),
|
|
12
|
+
shown,
|
|
13
|
+
held,
|
|
14
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { FieldsInput, Operation, Principal, Read, Role, Thing } from "orthograph";
|
|
3
|
+
import type { Store } from "#modules/records/types.js";
|
|
4
|
+
/** The generated wire classes, by error name. */
|
|
5
|
+
export type WireClasses = {
|
|
6
|
+
readonly [name: string]: (new (fields: never) => unknown) & {
|
|
7
|
+
/** The refusal's code, `AL-002`: the thing's prefix and a number the model fixed. */
|
|
8
|
+
readonly code: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
/** What an outcome is cut by: an operation or a read, with its declared answer. */
|
|
12
|
+
export type ShownOperation = Operation | Read<Thing, unknown, unknown>;
|
|
13
|
+
export interface WireApi {
|
|
14
|
+
/** Turn each declared error into its wire class. An error with no wire class was never declared: it dies. */
|
|
15
|
+
readonly refusals: (classes: WireClasses) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, unknown, R>;
|
|
16
|
+
/**
|
|
17
|
+
* What the caller is shown of the call's outcome: the answer cut by the visibility rule (a secret only if the call issued
|
|
18
|
+
* it, a private field only to a holder of its role), and a declared refusal's fields likewise. Undeclared fields never
|
|
19
|
+
* leave. `declared` is each declared refusal's class, by name; any other refusal carries public values only.
|
|
20
|
+
*/
|
|
21
|
+
readonly shown: (declared: {
|
|
22
|
+
readonly [tag: string]: {
|
|
23
|
+
readonly fields: FieldsInput;
|
|
24
|
+
};
|
|
25
|
+
}) => (served: {
|
|
26
|
+
readonly operation: ShownOperation;
|
|
27
|
+
}, payload: object, principal: Principal) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<Partial<A>, unknown, R | Store>;
|
|
28
|
+
/** The roles the caller holds in this call, asked only when some field in `groups` is private to roles. */
|
|
29
|
+
readonly held: (op: ShownOperation, payload: object, principal: Principal, ...groups: ReadonlyArray<FieldsInput>) => Effect.Effect<ReadonlyArray<Role>, never, Store>;
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Deferred, Effect, Layer } from "effect";
|
|
2
|
+
import { Busy, type BusyBox, type BusyShape } from "../types.ts";
|
|
3
|
+
export declare const makeWorld: (request: BusyBox | undefined) => {
|
|
4
|
+
held: () => string[];
|
|
5
|
+
idled: () => number;
|
|
6
|
+
open: () => number;
|
|
7
|
+
reset: () => void;
|
|
8
|
+
close: () => void;
|
|
9
|
+
admit: () => void;
|
|
10
|
+
layer: Layer.Layer<Busy, never, never>;
|
|
11
|
+
};
|
|
12
|
+
export declare const turns: Effect.Effect<void, never, never>;
|
|
13
|
+
export declare const run: <A, E>(world: {
|
|
14
|
+
readonly layer: Layer.Layer<Busy>;
|
|
15
|
+
}, e: Effect.Effect<A, E, Busy>) => Promise<A>;
|
|
16
|
+
/** A branch that sleeps: it lends its token out, and takes it back when `wake` opens. */
|
|
17
|
+
export declare const sleeps: (busy: BusyShape, wake: Deferred.Deferred<void>) => Effect.Effect<void, never, never>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// An in-memory world for the tests of counted work: tokens with `<generation>.<n>` ids, write sections, and a reset that moves the generation on.
|
|
2
|
+
import { Deferred, Effect, Layer } from "effect";
|
|
3
|
+
import { Busy, WorldCount } from "../types.js";
|
|
4
|
+
import { BusyReporting } from "../service.js";
|
|
5
|
+
export const makeWorld = (request) => {
|
|
6
|
+
const tokens = new Map(request === undefined ? [] : [[request.id, request.owner]]);
|
|
7
|
+
const sections = new Map();
|
|
8
|
+
let generation = 0;
|
|
9
|
+
let next = 0;
|
|
10
|
+
/** Closed to writes (a reset is draining): a token the world holds is refused a section as "closed", one it does not hold as stale. */
|
|
11
|
+
let closed = false;
|
|
12
|
+
/** How many times the world's count went empty. */
|
|
13
|
+
let idled = 0;
|
|
14
|
+
const count = Layer.succeed(WorldCount, WorldCount.of({
|
|
15
|
+
enter: (owner) => Effect.sync(() => { const id = `${generation}.${next++}`; tokens.set(id, owner); return id; }),
|
|
16
|
+
leave: (id) => Effect.sync(() => { tokens.delete(id); if (tokens.size === 0)
|
|
17
|
+
idled += 1; }),
|
|
18
|
+
name: (id, owner) => Effect.sync(() => void (tokens.has(id) && tokens.set(id, owner))),
|
|
19
|
+
valid: (id) => Effect.sync(() => tokens.has(id)),
|
|
20
|
+
section: (id) => Effect.sync(() => { if (!tokens.has(id))
|
|
21
|
+
return false; if (closed)
|
|
22
|
+
return "closed"; sections.set(id, (sections.get(id) ?? 0) + 1); return true; }),
|
|
23
|
+
sectionEnd: (id) => Effect.sync(() => void sections.set(id, (sections.get(id) ?? 1) - 1)),
|
|
24
|
+
request: Effect.succeed(request),
|
|
25
|
+
}));
|
|
26
|
+
return {
|
|
27
|
+
held: () => [...tokens.values()].sort(),
|
|
28
|
+
idled: () => idled,
|
|
29
|
+
open: () => [...sections.values()].reduce((a, b) => a + b, 0),
|
|
30
|
+
reset: () => { generation += 1; tokens.clear(); },
|
|
31
|
+
close: () => { closed = true; },
|
|
32
|
+
admit: () => { closed = false; },
|
|
33
|
+
layer: BusyReporting.pipe(Layer.provide(count)),
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export const turns = Effect.forEach(Array.from({ length: 16 }), () => Effect.yieldNow, { discard: true });
|
|
37
|
+
export const run = (world, e) => Effect.runPromise(Effect.provide(e, world.layer));
|
|
38
|
+
/** A branch that sleeps: it lends its token out, and takes it back when `wake` opens. */
|
|
39
|
+
export const sleeps = (busy, wake) => Effect.gen(function* () {
|
|
40
|
+
const lease = yield* busy.lend;
|
|
41
|
+
yield* Deferred.await(wake);
|
|
42
|
+
yield* lease.reclaim;
|
|
43
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The runtime tells us when a fiber starts and ends (`Metric.FiberRuntimeMetrics`), so every fiber a request forks is counted while it runs. Which
|
|
2
|
+
// fiber is running is only known where our own code runs (a sleep lending its token, a combinator joining), by its id (M8.07).
|
|
3
|
+
import { Fiber } from "effect";
|
|
4
|
+
/** The id of the fiber that is running, if any. */
|
|
5
|
+
export const runningId = () => Fiber.getCurrent()?.id;
|