@volter/world-core 2.0.0
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 +29 -0
- package/app-route.cjs +154 -0
- package/app-route.d.cts +7 -0
- package/attach.cjs +80 -0
- package/dist/app-route.cjs +154 -0
- package/dist/app-route.d.cts +7 -0
- package/dist/attach.cjs +80 -0
- package/dist/generated/pack-facts.json +4306 -0
- package/dist/inject.cjs +1097 -0
- package/dist/network-policy.cjs +92 -0
- package/dist/network-policy.d.cts +10 -0
- package/dist/src/actions.d.ts +276 -0
- package/dist/src/actions.js +436 -0
- package/dist/src/ancestry.d.ts +22 -0
- package/dist/src/ancestry.js +238 -0
- package/dist/src/args.d.ts +3 -0
- package/dist/src/args.js +12 -0
- package/dist/src/blob-store.d.ts +55 -0
- package/dist/src/blob-store.js +186 -0
- package/dist/src/brand-tokens.d.ts +2 -0
- package/dist/src/brand-tokens.js +17 -0
- package/dist/src/changeset.d.ts +431 -0
- package/dist/src/changeset.js +0 -0
- package/dist/src/client-bundle.d.ts +1 -0
- package/dist/src/client-bundle.js +28 -0
- package/dist/src/credential.d.ts +38 -0
- package/dist/src/credential.js +114 -0
- package/dist/src/derived-core.d.ts +452 -0
- package/dist/src/derived-core.js +782 -0
- package/dist/src/derived.d.ts +84 -0
- package/dist/src/derived.js +122 -0
- package/dist/src/emit.d.ts +106 -0
- package/dist/src/emit.js +157 -0
- package/dist/src/executor.d.ts +120 -0
- package/dist/src/executor.js +387 -0
- package/dist/src/file-response.d.ts +3 -0
- package/dist/src/file-response.js +22 -0
- package/dist/src/fork.d.ts +26 -0
- package/dist/src/fork.js +68 -0
- package/dist/src/git/history.d.ts +36 -0
- package/dist/src/git/history.js +298 -0
- package/dist/src/git/index.d.ts +6 -0
- package/dist/src/git/index.js +6 -0
- package/dist/src/git/inflate.d.ts +11 -0
- package/dist/src/git/inflate.js +194 -0
- package/dist/src/git/objects.d.ts +64 -0
- package/dist/src/git/objects.js +161 -0
- package/dist/src/git/pack.d.ts +14 -0
- package/dist/src/git/pack.js +199 -0
- package/dist/src/git/refs.d.ts +19 -0
- package/dist/src/git/refs.js +35 -0
- package/dist/src/git/smart-http.d.ts +45 -0
- package/dist/src/git/smart-http.js +223 -0
- package/dist/src/hash.d.ts +38 -0
- package/dist/src/hash.js +48 -0
- package/dist/src/head.d.ts +140 -0
- package/dist/src/head.js +313 -0
- package/dist/src/history.d.ts +76 -0
- package/dist/src/history.js +322 -0
- package/dist/src/index.d.ts +73 -0
- package/dist/src/index.js +98 -0
- package/dist/src/lifecycle.d.ts +1 -0
- package/dist/src/lifecycle.js +8 -0
- package/dist/src/log.d.ts +254 -0
- package/dist/src/log.js +801 -0
- package/dist/src/mirror-shell.d.ts +2 -0
- package/dist/src/mirror-shell.js +13 -0
- package/dist/src/observe.d.ts +49 -0
- package/dist/src/observe.js +148 -0
- package/dist/src/pack-assets.d.ts +30 -0
- package/dist/src/pack-assets.js +88 -0
- package/dist/src/packRegistry.d.ts +374 -0
- package/dist/src/packRegistry.js +142 -0
- package/dist/src/placeholder-remote.d.ts +22 -0
- package/dist/src/placeholder-remote.js +86 -0
- package/dist/src/proxy.d.ts +25 -0
- package/dist/src/proxy.js +155 -0
- package/dist/src/rateBudget.d.ts +367 -0
- package/dist/src/rateBudget.js +925 -0
- package/dist/src/references.d.ts +18 -0
- package/dist/src/references.js +27 -0
- package/dist/src/remote-execute.d.ts +22 -0
- package/dist/src/remote-execute.js +1 -0
- package/dist/src/resource-blob.d.ts +10 -0
- package/dist/src/resource-blob.js +56 -0
- package/dist/src/scenario.d.ts +197 -0
- package/dist/src/scenario.js +425 -0
- package/dist/src/schemas.d.ts +78 -0
- package/dist/src/schemas.js +50 -0
- package/dist/src/serve-http.d.ts +48 -0
- package/dist/src/serve-http.js +340 -0
- package/dist/src/serve.d.ts +147 -0
- package/dist/src/serve.js +507 -0
- package/dist/src/shared-blob-index.d.ts +4 -0
- package/dist/src/shared-blob-index.js +126 -0
- package/dist/src/state-system.d.ts +70 -0
- package/dist/src/state-system.js +90 -0
- package/dist/src/storage.d.ts +101 -0
- package/dist/src/storage.js +337 -0
- package/dist/src/twin-fetch.d.ts +64 -0
- package/dist/src/twin-fetch.js +91 -0
- package/dist/src/types.d.ts +40 -0
- package/dist/src/types.js +1 -0
- package/dist/src/v1-removed.d.ts +159 -0
- package/dist/src/v1-removed.js +124 -0
- package/dist/src/volter-home.d.ts +5 -0
- package/dist/src/volter-home.js +10 -0
- package/dist/src/world-clock.d.ts +4 -0
- package/dist/src/world-clock.js +32 -0
- package/dist/src/world-env.d.ts +3 -0
- package/dist/src/world-env.js +22 -0
- package/dist/src/world-store-sql.d.ts +27 -0
- package/dist/src/world-store-sql.js +86 -0
- package/dist/src/world-store.d.ts +168 -0
- package/dist/src/world-store.js +475 -0
- package/dist/src/worldConfig.d.ts +9 -0
- package/dist/src/worldConfig.js +17 -0
- package/dist/stream-bridge.cjs +80 -0
- package/dist/vendor-hosts.cjs +200 -0
- package/generated/pack-facts.json +4306 -0
- package/inject.cjs +1097 -0
- package/network-policy.cjs +92 -0
- package/network-policy.d.cts +10 -0
- package/package.json +103 -0
- package/src/actions.ts +564 -0
- package/src/ancestry.ts +213 -0
- package/src/args.ts +14 -0
- package/src/blob-store.ts +185 -0
- package/src/brand-tokens.ts +17 -0
- package/src/changeset.ts +1032 -0
- package/src/client-bundle.ts +29 -0
- package/src/credential.ts +140 -0
- package/src/derived-core.ts +1004 -0
- package/src/derived.ts +176 -0
- package/src/emit.ts +242 -0
- package/src/executor.ts +431 -0
- package/src/file-response.ts +22 -0
- package/src/fork.ts +89 -0
- package/src/git/history.ts +177 -0
- package/src/git/index.ts +6 -0
- package/src/git/inflate.ts +125 -0
- package/src/git/objects.ts +110 -0
- package/src/git/pack.ts +105 -0
- package/src/git/refs.ts +25 -0
- package/src/git/smart-http.ts +149 -0
- package/src/hash.ts +66 -0
- package/src/head.ts +318 -0
- package/src/history.ts +246 -0
- package/src/index.ts +323 -0
- package/src/lifecycle.ts +8 -0
- package/src/log.ts +793 -0
- package/src/mirror-shell.ts +15 -0
- package/src/observe.ts +130 -0
- package/src/pack-assets.ts +81 -0
- package/src/packRegistry.ts +408 -0
- package/src/placeholder-remote.ts +81 -0
- package/src/proxy.ts +183 -0
- package/src/rateBudget.ts +1115 -0
- package/src/references.ts +46 -0
- package/src/remote-execute.ts +26 -0
- package/src/resource-blob.ts +57 -0
- package/src/scenario.ts +479 -0
- package/src/schemas.ts +56 -0
- package/src/serve-http.ts +299 -0
- package/src/serve.ts +618 -0
- package/src/shared-blob-index.ts +108 -0
- package/src/state-system.ts +115 -0
- package/src/storage.ts +407 -0
- package/src/twin-fetch.ts +147 -0
- package/src/types.ts +50 -0
- package/src/v1-removed.ts +172 -0
- package/src/volter-home.ts +11 -0
- package/src/world-clock.ts +33 -0
- package/src/world-env.ts +18 -0
- package/src/world-store-sql.ts +118 -0
- package/src/world-store.ts +572 -0
- package/src/worldConfig.ts +27 -0
- package/stream-bridge.cjs +80 -0
- package/vendor-hosts.cjs +200 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { type RateBudgetDeclaration } from './rateBudget.js';
|
|
2
|
+
import type { TwinAuthStrategy } from './executor.js';
|
|
3
|
+
import { type ReferenceDeclaration } from './references.js';
|
|
4
|
+
import type { TwinEmitter } from './emit.js';
|
|
5
|
+
import { type StateSystemAdapters } from './state-system.js';
|
|
6
|
+
/**
|
|
7
|
+
* How a pack's clients ADDRESS it. The first three are HTTP API styles; `raw-tcp` is the
|
|
8
|
+
* RAW-PROTOCOL class — a pack whose clients speak a line protocol directly over a TCP socket
|
|
9
|
+
* rather than HTTP, so none of the HTTP machinery applies: no `browserRouting`, and no entry in
|
|
10
|
+
* the injector's `VENDOR_HOSTS` host map is possible (the injector patches http/fetch and never
|
|
11
|
+
* sees the traffic). Such a pack is wired into a world through app-read host/port env instead,
|
|
12
|
+
* and this value is what tells a reader that "no injector entry" is STRUCTURAL rather than a
|
|
13
|
+
* missing wiring point. `packages/twin/smtp` is the first.
|
|
14
|
+
*
|
|
15
|
+
* Deliberately the transport CLASS, not the protocol name: naming the wire protocol would put a
|
|
16
|
+
* vendor id in the kernel the moment a pack is named after its protocol, which is exactly what
|
|
17
|
+
* `scripts/architecture.test.ts`'s "kernel does not branch on vendor identity" forbids. The
|
|
18
|
+
* specific protocol belongs on the pack's own `specSource`/`description`.
|
|
19
|
+
*/
|
|
20
|
+
export type PackTransport = 'rest' | 'graphql' | 'web-api' | 'raw-tcp';
|
|
21
|
+
/**
|
|
22
|
+
* The pack's SERVE-FAMILY — the axis the invariant matrix keys strictness off, DECLARED
|
|
23
|
+
* because it is the pack's own claim about what kind of thing it is (a heuristic over file
|
|
24
|
+
* shapes would be the loose-scan disease). Orthogonal facts stay derived: transport is its
|
|
25
|
+
* own field, a mirror is mirrorMutations in gate.ts, webhooks are the events module.
|
|
26
|
+
* 'crud' — stateful resource CRUD behind the vendor's API (the default family;
|
|
27
|
+
* includes ingestion→grouping packs — the ingest door is a trait).
|
|
28
|
+
* 'generative' — model-shaped surface serving deterministic stubs/scenarios; "cannot
|
|
29
|
+
* run the model" is the invariant, never a gap.
|
|
30
|
+
* 'signed-protocol' — the wire is a signature/format protocol (SigV4 + XML/AWS-JSON …):
|
|
31
|
+
* request authentication IS the fidelity surface.
|
|
32
|
+
* 'engine-control' — a control plane driving a REAL execution engine behind an injected
|
|
33
|
+
* seam (fly's runtime, github's git plane, supabase's external stack).
|
|
34
|
+
* 'proxy' — the vendor itself is a forwarding plane (tunnel).
|
|
35
|
+
*/
|
|
36
|
+
/** THE PLATFORM PROTOCOL VERSION (runtime contract R16): `major.minor`. The major names the
|
|
37
|
+
* descriptor shape, the factory signature, the doors and the adapter contract a package is
|
|
38
|
+
* written against; a change that removes or alters a required shape bumps it, an additive
|
|
39
|
+
* change bumps minor. A package declares the major it targets (`protocol`); the kernel serves
|
|
40
|
+
* its own major, the previous one under a deprecation warning for one cycle, and refuses two
|
|
41
|
+
* back. Undeclared reads as the previous major (deprecated) once there is one. */
|
|
42
|
+
export declare const PROTOCOL_VERSION = "2.0";
|
|
43
|
+
export declare const PROTOCOL_MAJOR: number;
|
|
44
|
+
/** Where a package's declared protocol stands against this kernel. */
|
|
45
|
+
export declare function protocolStanding(declared: string | undefined): {
|
|
46
|
+
major: number;
|
|
47
|
+
standing: 'current' | 'deprecated' | 'refused' | 'assumed';
|
|
48
|
+
};
|
|
49
|
+
export declare const PACK_ARCHETYPES: readonly ["crud", "generative", "signed-protocol", "engine-control", "proxy"];
|
|
50
|
+
export type PackArchetype = (typeof PACK_ARCHETYPES)[number];
|
|
51
|
+
/**
|
|
52
|
+
* How often this vendor's REAL API may be pulled — a property of the VENDOR's rate limits, so it
|
|
53
|
+
* belongs to the pack that knows them, not to every world that might sync one (same argument as
|
|
54
|
+
* `browserRouting`: vendor knowledge in the descriptor, kernel stays vendor-agnostic).
|
|
55
|
+
*
|
|
56
|
+
* - `'continuous'` — limits are generous and well-documented, so a scheduled/shadow world may sync
|
|
57
|
+
* this vendor on a cadence (slack, github: thousands/hour, per-method tiers, clear headers).
|
|
58
|
+
* - `'on-demand'` — limits are tight, opaque, cost-based, or punish bursts with long lockouts, so a
|
|
59
|
+
* real pull happens ONLY when a human/agent explicitly asks for one. Never on a schedule, never
|
|
60
|
+
* as a side effect of booting a world. Figma is the cautionary case: undocumented cost-based
|
|
61
|
+
* limits, and a burst of renders cost this project a ~4.5-DAY token lockout (2026-07-25).
|
|
62
|
+
*
|
|
63
|
+
* DEFAULT IS `'on-demand'` when omitted — deliberately the safe direction. A pack that hasn't
|
|
64
|
+
* thought about its limits must not be assumed schedulable; being wrong that way costs a slower
|
|
65
|
+
* sync, while being wrong the other way costs a lockout.
|
|
66
|
+
*/
|
|
67
|
+
export type PullPosture = 'continuous' | 'on-demand';
|
|
68
|
+
/** The posture to assume for a pack that doesn't declare one. Conservative on purpose. */
|
|
69
|
+
export declare const DEFAULT_PULL_POSTURE: PullPosture;
|
|
70
|
+
/** A pack's effective posture — its declaration, or the conservative default. */
|
|
71
|
+
export declare function pullPosture(pack: Pick<TwinPack, 'pullPosture'>): PullPosture;
|
|
72
|
+
/**
|
|
73
|
+
* What may name a vendor at a pull seam: the pack descriptor itself (the pack knows its own
|
|
74
|
+
* posture — no registration needed) or a vendor id resolved through the registry. The id form is
|
|
75
|
+
* the convenient one; it is also the one that FAILS CLOSED, because an id nothing registered
|
|
76
|
+
* resolves to a posture-less descriptor, i.e. the conservative default.
|
|
77
|
+
*/
|
|
78
|
+
export type PullVendor = string | Pick<TwinPack, 'vendor' | 'pullPosture'>;
|
|
79
|
+
/**
|
|
80
|
+
* Why a pull is happening — the distinction the posture rule actually turns on.
|
|
81
|
+
*
|
|
82
|
+
* - `'scheduled'` — a cadence, a poll loop, a shadow tick, or any pull that happens as a side
|
|
83
|
+
* effect of something else (booting a world, a cron). Allowed for `'continuous'` vendors ONLY.
|
|
84
|
+
* - `'explicit'` — a human/agent asked for THIS pull, now. Sanctioned for any vendor; it is the
|
|
85
|
+
* only way an on-demand vendor is ever supposed to be pulled.
|
|
86
|
+
*
|
|
87
|
+
* Omitted ⇒ `'scheduled'`, matching the posture default: an unlabeled pull is treated as the
|
|
88
|
+
* dangerous kind, so labeling is what unlocks the vendor, never silence.
|
|
89
|
+
*/
|
|
90
|
+
export type PullTrigger = 'scheduled' | 'explicit';
|
|
91
|
+
/** The trigger to assume when a caller does not say. Conservative on purpose. */
|
|
92
|
+
export declare const DEFAULT_PULL_TRIGGER: PullTrigger;
|
|
93
|
+
/** Resolve a `PullVendor` to the descriptor whose posture governs it. Unregistered ⇒ no posture. */
|
|
94
|
+
export declare function resolvePullVendor(vendor: PullVendor): Pick<TwinPack, 'vendor' | 'pullPosture'>;
|
|
95
|
+
/**
|
|
96
|
+
* Guard a SCHEDULED/continuous sync. Throws for a vendor that may only be pulled on demand.
|
|
97
|
+
*
|
|
98
|
+
* This is the mechanical form of the rule — worlds differ in what they may pull:
|
|
99
|
+
* • sealed (hermetic OA cycle worlds) pull NOTHING; they seed synthetic state over loopback and
|
|
100
|
+
* an egress guard blocks the internet, so posture is irrelevant there;
|
|
101
|
+
* • shadow/scheduled worlds sync real state on a cadence — `'continuous'` vendors ONLY;
|
|
102
|
+
* • on-demand pulls are explicit, operator-invoked, and may target any vendor.
|
|
103
|
+
* A scheduled world that quietly includes an on-demand vendor is how a lockout happens, so it
|
|
104
|
+
* fails loudly here instead.
|
|
105
|
+
*/
|
|
106
|
+
export declare function assertContinuousPullAllowed(vendor: PullVendor): void;
|
|
107
|
+
/**
|
|
108
|
+
* The ONE sanctioned way to pull a vendor on a cadence. Asserts the posture BEFORE `pull` runs, so
|
|
109
|
+
* a refused vendor makes ZERO vendor requests — the guard has to sit in front of the network call,
|
|
110
|
+
* not behind it (guarding the fold, e.g. `syncPull`, would be theatre: the request already went out).
|
|
111
|
+
*
|
|
112
|
+
* Wrap any scheduler/cron/loop's pull in this and the rule enforces itself:
|
|
113
|
+
* `await pullOnSchedule(pack, () => syncSlackFromReal(client, opts))`
|
|
114
|
+
* An explicit, human-asked pull does NOT go through here — it calls the connector directly, which is
|
|
115
|
+
* exactly the sanctioned path for an on-demand vendor.
|
|
116
|
+
*/
|
|
117
|
+
export declare function pullOnSchedule<T>(vendor: PullVendor, pull: () => Promise<T>): Promise<T>;
|
|
118
|
+
/**
|
|
119
|
+
* One injector host rule — DATA the committed inject.cjs compiles, never a function. Exactly one
|
|
120
|
+
* selector: `host` (exact), `suffix` (endsWith), or `hostPattern` (a RegExp SOURCE over the
|
|
121
|
+
* hostname — regional families like `^s3[.-][a-z0-9-]+\.amazonaws\.com$`). `pathPattern` (a
|
|
122
|
+
* RegExp source over the pathname) splits a host shared between packs. `key` names the routing
|
|
123
|
+
* identity the rule belongs to — the `<KEY>_TWIN_URL` env stem — when it is not the pack's own
|
|
124
|
+
* vendor id (aws's consolidated twin answers under s3 / dynamodb / … ; gemini serves the
|
|
125
|
+
* `googleauth` token exchange); a key belongs to ONE pack. `exclude: true` carves a host out of
|
|
126
|
+
* the key's includes (`.upstash.io` minus `*-vector.upstash.io`): a key matches when any include
|
|
127
|
+
* matches and no exclude does. Validated by scripts/pack-facts.ts: keys are `[a-z0-9-]+`, a
|
|
128
|
+
* foreign key is never another pack's vendor id, belongs to one pack, and must appear in the
|
|
129
|
+
* pack's `adoption.worldIds` (the world wires `<KEY>_TWIN_URL` from there); a pack must claim its
|
|
130
|
+
* own vendor id too; no key is exclude-only; `exclude` is `true` or absent.
|
|
131
|
+
*/
|
|
132
|
+
export type HostRule = ({
|
|
133
|
+
host: string;
|
|
134
|
+
} | {
|
|
135
|
+
suffix: string;
|
|
136
|
+
} | {
|
|
137
|
+
hostPattern: string;
|
|
138
|
+
}) & {
|
|
139
|
+
pathPattern?: string;
|
|
140
|
+
key?: string;
|
|
141
|
+
exclude?: true;
|
|
142
|
+
};
|
|
143
|
+
export type RoundTripWrite = {
|
|
144
|
+
method: string;
|
|
145
|
+
path: string;
|
|
146
|
+
body?: unknown;
|
|
147
|
+
headers?: Record<string, string>;
|
|
148
|
+
};
|
|
149
|
+
export type TwinPack = {
|
|
150
|
+
/** vendor id / service, e.g. 'stripe'. */
|
|
151
|
+
vendor: string;
|
|
152
|
+
transport: PackTransport;
|
|
153
|
+
/** Optional native frontend of the same state owner, selected explicitly on the pack CLI. */
|
|
154
|
+
nativeTransport?: {
|
|
155
|
+
protocol: string;
|
|
156
|
+
flag: string;
|
|
157
|
+
upstreamEnv: string;
|
|
158
|
+
};
|
|
159
|
+
/** The serve-family (see PackArchetype). Every pack declares one. */
|
|
160
|
+
archetype?: PackArchetype;
|
|
161
|
+
/** The platform protocol MAJOR this package targets (R16), e.g. '1'. Undeclared reads as the
|
|
162
|
+
* previous major (deprecated), or as the current major (recorded as assumed) while it is the first;
|
|
163
|
+
* the scaffolder declares it. */
|
|
164
|
+
protocol?: string;
|
|
165
|
+
/** Pack-shipped assets (R20): files or directories under the pack dir the serve path reads through
|
|
166
|
+
* the pack-asset seam (`getActivePackAssets`), never off the host. Declared so the deploy step
|
|
167
|
+
* can place them behind the workerd assets binding. Paths relative to the pack dir. */
|
|
168
|
+
assets?: string[];
|
|
169
|
+
/** R2, resource level: declared subject types the twin genuinely serves but no replay can create —
|
|
170
|
+
* born only of the vendor's own catalog, scheduler, account or a pull. Each names WHY. The cell
|
|
171
|
+
* stays debt (adopted, never faked), but legibly: the runner separates these from open gaps. */
|
|
172
|
+
resourcesUnreachable?: Record<string, string>;
|
|
173
|
+
/** subject types the twin serves, e.g. ['customer','charge','payment_intent']. */
|
|
174
|
+
resources: string[];
|
|
175
|
+
/** the `world-<vendor>` operator bin, if any. */
|
|
176
|
+
bin?: string;
|
|
177
|
+
/** conformance field map { object: { field: type } } — vendored or derived from the spec. */
|
|
178
|
+
conformanceFields?: Record<string, Record<string, string>>;
|
|
179
|
+
/** where the exact surface came from (a spec path) — provenance for re-derivation. */
|
|
180
|
+
specSource?: string;
|
|
181
|
+
/** one-line human description. */
|
|
182
|
+
description?: string;
|
|
183
|
+
/** How this vendor's BROWSER SDK addresses its API — used by the zero-edit dev proxy so
|
|
184
|
+
* the kernel proxy stays vendor-agnostic (it forwards/rewrites by these values, never by a
|
|
185
|
+
* hardcoded vendor table). `apiPathPrefix`: the same-origin path the browser SDK calls
|
|
186
|
+
* (e.g. Stripe.js → '/v1/'). `loaderHost`: the absolute API host to strip from the loaded
|
|
187
|
+
* SDK so its calls become same-origin (e.g. 'https://api.stripe.com'). Omit for vendors
|
|
188
|
+
* with no browser SDK. */
|
|
189
|
+
browserRouting?: {
|
|
190
|
+
apiPathPrefix: string;
|
|
191
|
+
loaderHost?: string;
|
|
192
|
+
};
|
|
193
|
+
/** How often this vendor's REAL API may be pulled. Omitted ⇒ `'on-demand'` (see `PullPosture`).
|
|
194
|
+
* Declare `'continuous'` only for a vendor whose published limits genuinely tolerate a
|
|
195
|
+
* scheduled sync, and say why in `pullPostureReason`. */
|
|
196
|
+
pullPosture?: PullPosture;
|
|
197
|
+
/** Why this posture — the limits that justify it. Required in review for `'continuous'`, since
|
|
198
|
+
* that is the claim that can cost a lockout if it's wrong. */
|
|
199
|
+
pullPostureReason?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The vendor's CLIENT-SIDE RATE BUDGET — the ceiling, window and per-endpoint weights the pack's
|
|
202
|
+
* guarded connector enforces before a live call goes out. Vendor knowledge as DATA, exactly like
|
|
203
|
+
* `browserRouting`: the mechanism is the kernel's (`rateBudget.ts`), the numbers are the pack's.
|
|
204
|
+
*
|
|
205
|
+
* `registerPack` forwards this to `declareRateBudget`, so registering a pack arms its budget.
|
|
206
|
+
* A pack that omits it is NOT unlimited — any budget built for that vendor falls back to
|
|
207
|
+
* `DEFAULT_RATE_BUDGET` (see its docstring). `pullPosture` says "do not SCHEDULE this vendor";
|
|
208
|
+
* this says "and here is the ceiling on an EXPLICIT pull". They are complementary, not
|
|
209
|
+
* substitutes — the posture guards cadence seams, the budget guards the call itself.
|
|
210
|
+
*/
|
|
211
|
+
rateBudget?: RateBudgetDeclaration;
|
|
212
|
+
/**
|
|
213
|
+
* The pack's DELIVER support (`emit` — see emit.ts): how to synthesize this vendor's
|
|
214
|
+
* signed event/webhook deliveries from current twin state. Vendor knowledge on the
|
|
215
|
+
* descriptor, like `browserRouting`; the kernel engine (`emitTwinEvent`) is generic.
|
|
216
|
+
* The operator surface is the pack's own bin (`world-<vendor> emit`); a consumer that
|
|
217
|
+
* registered the pack can also drive it via `volter-twin emit <vendor>`.
|
|
218
|
+
*/
|
|
219
|
+
emitter?: TwinEmitter;
|
|
220
|
+
/**
|
|
221
|
+
* SERVE FACTORY OVERRIDE — normally DERIVED, declared only under ambiguity. The colocated
|
|
222
|
+
* host (world-runtime/src/host.ts) mounts a pack by its `create<Name>TwinServer` factory
|
|
223
|
+
* export (`({port,root,readOnly}) => {port,stop}`); `scripts/pack-facts.ts` reads that
|
|
224
|
+
* export's name off the module, so a pack with exactly ONE such export declares nothing.
|
|
225
|
+
* A pack exporting SEVERAL factories declares here which one `cli.ts serve` would have
|
|
226
|
+
* booted — the judgment the exports alone cannot reveal (linear serves its DERIVED server
|
|
227
|
+
* by default; the hand-written one is behind a flag). Must name a function export of the
|
|
228
|
+
* pack's index module matching /^create\w*TwinServer$/.
|
|
229
|
+
*/
|
|
230
|
+
serveExport?: string;
|
|
231
|
+
/**
|
|
232
|
+
* ADOPTION — how application repos betray that they talk to this vendor, so
|
|
233
|
+
* `volter-world covers`/`init` can attribute the signal to this pack. Vendor knowledge as
|
|
234
|
+
* DATA, same doctrine as `browserRouting`/`rateBudget`: the detector mechanism lives in
|
|
235
|
+
* world-runtime; the names live here. Absorbs the central `SDK_TWINS` / `SDK_SCOPE_VENDORS` /
|
|
236
|
+
* `ENV_STEM_VENDORS` / `VENDOR_WORLD_IDS` maps (the one wiring point NO gate enforced —
|
|
237
|
+
* the class that let `@planetscale/database` escape, twin#255).
|
|
238
|
+
*/
|
|
239
|
+
adoption?: {
|
|
240
|
+
/** every official npm client of the API surface this pack models, e.g. ['stripe']. */
|
|
241
|
+
sdks?: string[];
|
|
242
|
+
/** PyPI distribution names (PEP 503 normalized: lowercase, `-`) the vendor's Python SDKs
|
|
243
|
+
* ship under — the Python half of adoption discovery and coverage (`covers`). */
|
|
244
|
+
pypi?: string[];
|
|
245
|
+
/** npm scope prefixes whose members all belong to this vendor, e.g. ['@upstash/']. */
|
|
246
|
+
scopes?: string[];
|
|
247
|
+
/** credential-env-var stems, e.g. ['STRIPE'] for STRIPE_SECRET_KEY et al. */
|
|
248
|
+
envStems?: string[];
|
|
249
|
+
/** additional world service ids this vendor answers to (the VENDOR_WORLD_IDS case). */
|
|
250
|
+
worldIds?: string[];
|
|
251
|
+
/** Vendor-facing tool packages whose calls belong to supporting workflow rather than the
|
|
252
|
+
* application itself. The use is saved and selected by World discovery policy. */
|
|
253
|
+
tools?: Array<{
|
|
254
|
+
package: string;
|
|
255
|
+
usage: 'build' | 'deployment';
|
|
256
|
+
}>;
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* INTERCEPTION — the vendor hosts whose traffic the injector must route to this twin,
|
|
260
|
+
* as serializable data (the committed `inject.cjs` table is GENERATED from these — it must
|
|
261
|
+
* stay dependency-free preloaded CJS, so it consumes compiled output, never imports packs).
|
|
262
|
+
* Exactly one of `hosts` or `hostsNone` per pack once migration completes: silence is not a
|
|
263
|
+
* ruling. `pathPattern` (a RegExp source string, applied to the URL pathname) splits shared
|
|
264
|
+
* hosts (the youtube/googleauth case). Absorbed `VENDOR_HOSTS`'s per-pack keys + the retired NO_INJECTOR_ENTRY
|
|
265
|
+
* allowlist (hostsNone IS the ruling now).
|
|
266
|
+
*/
|
|
267
|
+
hosts?: HostRule[];
|
|
268
|
+
/** Why this pack deliberately has NO injector entry (explicit-endpoint wiring only). */
|
|
269
|
+
hostsNone?: string;
|
|
270
|
+
/**
|
|
271
|
+
* HOSTS A TWIN CLAIMS WHILE IT RUNS — names a person makes answer to this vendor (a custom domain connected to a
|
|
272
|
+
* bucket), which no descriptor can list. The twin lists the ones it answers now at its own `door` (GET, answering
|
|
273
|
+
* `{ "hosts": [...] }`), and the World routes them to it as DNS would the vendor's; a host a descriptor's `hosts`
|
|
274
|
+
* names is never taken from another pack this way.
|
|
275
|
+
*/
|
|
276
|
+
hostsClaimed?: {
|
|
277
|
+
door: string;
|
|
278
|
+
note: string;
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* WORLD WIRING — the env var the vendor's own SDK documents for overriding its base URL,
|
|
282
|
+
* which `volter-world init` injects pointing at the twin. `endpointEnvNone` declares the
|
|
283
|
+
* deliberate absence WITH its reason (inventing a var the app never reads would make
|
|
284
|
+
* `covers` report coverage while traffic still reaches the real vendor — the exact lie the
|
|
285
|
+
* proof exists to catch). Absorbs init.ts's `APP_READ_ENDPOINT_ENV` map, where these
|
|
286
|
+
* reasons lived as comments. Exactly one of the two once migration completes.
|
|
287
|
+
*/
|
|
288
|
+
endpointEnv?: {
|
|
289
|
+
name: string;
|
|
290
|
+
templates?: Record<string, string>;
|
|
291
|
+
note: string;
|
|
292
|
+
};
|
|
293
|
+
/** Why this pack deliberately injects no endpoint env (see `endpointEnv`). */
|
|
294
|
+
endpointEnvNone?: string;
|
|
295
|
+
/**
|
|
296
|
+
* PRISMA — the driver adapter a Prisma client reaches this database twin through, as DATA.
|
|
297
|
+
* Prisma's client-engine build (the one a tab runs, and the one its edge client uses) refuses
|
|
298
|
+
* to construct without a driver adapter; an application that constructs `new PrismaClient()`
|
|
299
|
+
* against the vendor's URL gets this adapter from ITS OWN dependencies, constructed on the
|
|
300
|
+
* URL in `urlEnv` (the endpoint template's variable), by the injector. The adapter package is
|
|
301
|
+
* never installed by the World: absent from the application, the client is left as it was.
|
|
302
|
+
*/
|
|
303
|
+
prismaAdapter?: {
|
|
304
|
+
adapter: string;
|
|
305
|
+
export: string;
|
|
306
|
+
urlEnv: string;
|
|
307
|
+
};
|
|
308
|
+
/** PROTOCOL 2 — the pack's half of the REAL state system (state-system.ts): perform one entry
|
|
309
|
+
* against the vendor, refresh the parent log from it, ingest one signed webhook. All over the
|
|
310
|
+
* kernel executor; the pack never holds a credential. Registered with the pack. */
|
|
311
|
+
stateSystem?: StateSystemAdapters;
|
|
312
|
+
/** PROTOCOL 2 — how a root is kept current, the pack's way: `every` schedules a pull in a served
|
|
313
|
+
* world (the root's `refresh.every` overrides), `webhook` says the vendor pushes to the ingest
|
|
314
|
+
* door, `onDemand.atMost` is the least time between `volter twin <v> refresh` runs (a throttle for
|
|
315
|
+
* a rate-limited vendor). Absorbs `pullPosture`. */
|
|
316
|
+
refresh?: {
|
|
317
|
+
every?: string;
|
|
318
|
+
webhook?: boolean;
|
|
319
|
+
onDemand?: {
|
|
320
|
+
atMost: string;
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
/** PROTOCOL 2 — a minimal write on the vendor's wire the kernel can send blind (or a short
|
|
324
|
+
* sequence whose LAST write creates something new every time): the branch round-trip
|
|
325
|
+
* (docs/contributing/architecture.md#evidence) sends it, checkpoints, branches, sends it again on the branch, and
|
|
326
|
+
* proves the tree contract with no pack code. */
|
|
327
|
+
roundTrip?: RoundTripWrite | RoundTripWrite[];
|
|
328
|
+
/** PROTOCOL 2 — which fields of which subject types hold another subject's id (docs/contributing/architecture.md
|
|
329
|
+
* #alias-aware-lookup-at-the-request-boundary): the kernel resolves them through an adopted vendor id, in the tree and at the perform. */
|
|
330
|
+
references?: ReferenceDeclaration[];
|
|
331
|
+
/** PROTOCOL 2 — with `references`: a write on the wire that references the `roundTrip` write's subject;
|
|
332
|
+
* `{{field}}` in its path or body is the referenced subject's field. The round-trip gate sends it
|
|
333
|
+
* after simulating the parent's adoption and checks the reference followed. */
|
|
334
|
+
referenceTrip?: RoundTripWrite;
|
|
335
|
+
/** PROTOCOL 2 — shape parity (roadmap 4c): the origin the parity gate hands the refresh adapter when it
|
|
336
|
+
* refreshes the twin from itself (`http://twin` + whatever path the adapter reads its scope from), and
|
|
337
|
+
* `'held'` once the write handler and the refresh adapter store the same shape (the gate then asserts). */
|
|
338
|
+
parityOrigin?: string;
|
|
339
|
+
shapeParity?: 'held';
|
|
340
|
+
/** PROTOCOL 2 — a refresh that cannot be exercised for effect when the parity gate refreshes the twin
|
|
341
|
+
* from itself, and the refusal it answers with instead: `refusal` is text its thrown error carries,
|
|
342
|
+
* `reason` why (an identity pull the twin's own wire refuses without a consent-minted token; a pulled
|
|
343
|
+
* id inside the twin's reserved local namespace). The gate runs the refresh and passes it only on
|
|
344
|
+
* that refusal; any other error fails. */
|
|
345
|
+
refreshRefusal?: {
|
|
346
|
+
refusal: string;
|
|
347
|
+
reason: string;
|
|
348
|
+
};
|
|
349
|
+
/** PROTOCOL 2 — HOW THIS VENDOR AUTHENTICATES, so the kernel executor can apply the sealed
|
|
350
|
+
* credential the way the vendor actually reads it (docs/concepts/the-model.md#the-rules, rule 5). Absent means header
|
|
351
|
+
* replacement, which is what `credential.headers` has always done — declare this ONLY for a
|
|
352
|
+
* vendor that reads its key from the query string or wants a signature computed per request. */
|
|
353
|
+
auth?: TwinAuthStrategy;
|
|
354
|
+
/** PROTOCOL 2 — the ENGINE beside the tree, when the pack's state has a second half that is not a
|
|
355
|
+
* projection (a git plane, S3 bytes, a SQL engine): the module that owns every write outside the
|
|
356
|
+
* world store. The tree references engine objects by hash; the gate keeps such writes there. */
|
|
357
|
+
engine?: {
|
|
358
|
+
module: string;
|
|
359
|
+
note?: string;
|
|
360
|
+
};
|
|
361
|
+
};
|
|
362
|
+
/** Register (or replace) a pack descriptor. Returns it.
|
|
363
|
+
*
|
|
364
|
+
* Side effect, deliberately: a `rateBudget` declaration on the descriptor is ARMED here, so
|
|
365
|
+
* registering a pack is enough to give its vendor the ceiling it declared. `declareRateBudget`
|
|
366
|
+
* is idempotent for an identical declaration and REFUSES a widening one, so re-registering is
|
|
367
|
+
* safe and "register a fatter pack descriptor to buy a bigger budget" is not a move. */
|
|
368
|
+
export declare function registerPack(pack: TwinPack): TwinPack;
|
|
369
|
+
export declare function getPack(vendor: string): TwinPack | undefined;
|
|
370
|
+
/** All registered packs, sorted by vendor (deterministic). */
|
|
371
|
+
export declare function listPacks(): TwinPack[];
|
|
372
|
+
export declare function hasPack(vendor: string): boolean;
|
|
373
|
+
/** Clear the registry (tests). */
|
|
374
|
+
export declare function clearRegistry(): void;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Pack registry (the twins architecture notes → "Optional shared
|
|
2
|
+
// libraries"; #1). A lightweight, dependency-free way for vendor twins to *declare*
|
|
3
|
+
// themselves so tooling can discover them — "add a vendor" gets cheaper. A pack
|
|
4
|
+
// EXPORTS a `TwinPack` descriptor (no import side-effects); a consumer that imports
|
|
5
|
+
// the packs it wants registers them and queries the registry. The kernel never
|
|
6
|
+
// imports packs (no dependency inversion) — discovery is the consumer's choice.
|
|
7
|
+
//
|
|
8
|
+
// This is an optional convenience, NOT a kernel that defines the twin: a pack can
|
|
9
|
+
// ignore the registry entirely. Pure + deterministic.
|
|
10
|
+
import { declareRateBudget } from "./rateBudget.js";
|
|
11
|
+
import { registerReferences } from "./references.js";
|
|
12
|
+
import { registerAuthStrategy, registerStateSystem } from "./state-system.js";
|
|
13
|
+
/**
|
|
14
|
+
* The pack's SERVE-FAMILY — the axis the invariant matrix keys strictness off, DECLARED
|
|
15
|
+
* because it is the pack's own claim about what kind of thing it is (a heuristic over file
|
|
16
|
+
* shapes would be the loose-scan disease). Orthogonal facts stay derived: transport is its
|
|
17
|
+
* own field, a mirror is mirrorMutations in gate.ts, webhooks are the events module.
|
|
18
|
+
* 'crud' — stateful resource CRUD behind the vendor's API (the default family;
|
|
19
|
+
* includes ingestion→grouping packs — the ingest door is a trait).
|
|
20
|
+
* 'generative' — model-shaped surface serving deterministic stubs/scenarios; "cannot
|
|
21
|
+
* run the model" is the invariant, never a gap.
|
|
22
|
+
* 'signed-protocol' — the wire is a signature/format protocol (SigV4 + XML/AWS-JSON …):
|
|
23
|
+
* request authentication IS the fidelity surface.
|
|
24
|
+
* 'engine-control' — a control plane driving a REAL execution engine behind an injected
|
|
25
|
+
* seam (fly's runtime, github's git plane, supabase's external stack).
|
|
26
|
+
* 'proxy' — the vendor itself is a forwarding plane (tunnel).
|
|
27
|
+
*/
|
|
28
|
+
/** THE PLATFORM PROTOCOL VERSION (runtime contract R16): `major.minor`. The major names the
|
|
29
|
+
* descriptor shape, the factory signature, the doors and the adapter contract a package is
|
|
30
|
+
* written against; a change that removes or alters a required shape bumps it, an additive
|
|
31
|
+
* change bumps minor. A package declares the major it targets (`protocol`); the kernel serves
|
|
32
|
+
* its own major, the previous one under a deprecation warning for one cycle, and refuses two
|
|
33
|
+
* back. Undeclared reads as the previous major (deprecated) once there is one. */
|
|
34
|
+
export const PROTOCOL_VERSION = '2.0';
|
|
35
|
+
export const PROTOCOL_MAJOR = Number(PROTOCOL_VERSION.split('.')[0]);
|
|
36
|
+
/** Where a package's declared protocol stands against this kernel. */
|
|
37
|
+
export function protocolStanding(declared) {
|
|
38
|
+
// undeclared: written before a pack could say — the previous major once there is one, and
|
|
39
|
+
// deprecated like any pack on it; the current major only while it is the first
|
|
40
|
+
if (declared === undefined)
|
|
41
|
+
return PROTOCOL_MAJOR > 1 ? { major: PROTOCOL_MAJOR - 1, standing: 'deprecated' } : { major: PROTOCOL_MAJOR, standing: 'assumed' };
|
|
42
|
+
const major = Number(String(declared).split('.')[0]);
|
|
43
|
+
if (!Number.isInteger(major) || major < 1)
|
|
44
|
+
return { major: Number.NaN, standing: 'refused' };
|
|
45
|
+
if (major === PROTOCOL_MAJOR)
|
|
46
|
+
return { major, standing: 'current' };
|
|
47
|
+
if (major === PROTOCOL_MAJOR - 1)
|
|
48
|
+
return { major, standing: 'deprecated' };
|
|
49
|
+
return { major, standing: 'refused' };
|
|
50
|
+
}
|
|
51
|
+
export const PACK_ARCHETYPES = ['crud', 'generative', 'signed-protocol', 'engine-control', 'proxy'];
|
|
52
|
+
/** The posture to assume for a pack that doesn't declare one. Conservative on purpose. */
|
|
53
|
+
export const DEFAULT_PULL_POSTURE = 'on-demand';
|
|
54
|
+
/** A pack's effective posture — its declaration, or the conservative default. */
|
|
55
|
+
export function pullPosture(pack) {
|
|
56
|
+
return pack.pullPosture ?? DEFAULT_PULL_POSTURE;
|
|
57
|
+
}
|
|
58
|
+
/** The trigger to assume when a caller does not say. Conservative on purpose. */
|
|
59
|
+
export const DEFAULT_PULL_TRIGGER = 'scheduled';
|
|
60
|
+
/** Resolve a `PullVendor` to the descriptor whose posture governs it. Unregistered ⇒ no posture. */
|
|
61
|
+
export function resolvePullVendor(vendor) {
|
|
62
|
+
if (typeof vendor !== 'string')
|
|
63
|
+
return vendor;
|
|
64
|
+
return getPack(vendor) ?? { vendor };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Guard a SCHEDULED/continuous sync. Throws for a vendor that may only be pulled on demand.
|
|
68
|
+
*
|
|
69
|
+
* This is the mechanical form of the rule — worlds differ in what they may pull:
|
|
70
|
+
* • sealed (hermetic OA cycle worlds) pull NOTHING; they seed synthetic state over loopback and
|
|
71
|
+
* an egress guard blocks the internet, so posture is irrelevant there;
|
|
72
|
+
* • shadow/scheduled worlds sync real state on a cadence — `'continuous'` vendors ONLY;
|
|
73
|
+
* • on-demand pulls are explicit, operator-invoked, and may target any vendor.
|
|
74
|
+
* A scheduled world that quietly includes an on-demand vendor is how a lockout happens, so it
|
|
75
|
+
* fails loudly here instead.
|
|
76
|
+
*/
|
|
77
|
+
export function assertContinuousPullAllowed(vendor) {
|
|
78
|
+
const pack = resolvePullVendor(vendor);
|
|
79
|
+
if (pullPosture(pack) === 'continuous')
|
|
80
|
+
return;
|
|
81
|
+
const undeclared = pack.pullPosture === undefined;
|
|
82
|
+
throw new Error(`pull posture: "${pack.vendor}" is on-demand — it must NOT be pulled on a schedule or as a side ` +
|
|
83
|
+
`effect of booting a world. Its rate limits are tight/opaque enough that a burst risks a long ` +
|
|
84
|
+
`lockout. Pull it explicitly when someone asks, through the pack's guarded connector; a sealed ` +
|
|
85
|
+
`world should seed synthetic state locally instead. If this vendor's limits are genuinely ` +
|
|
86
|
+
`generous, declare pullPosture:'continuous' on its TwinPack and say why.` +
|
|
87
|
+
(undeclared
|
|
88
|
+
? ` (Nothing declared a posture for "${pack.vendor}" — either no pack is registered under ` +
|
|
89
|
+
`that id (registerPack) or its TwinPack omits pullPosture. Silence is not permission: the ` +
|
|
90
|
+
`conservative default applied. If this pull is a one-off someone asked for, say so at the ` +
|
|
91
|
+
`call site — pass the explicit trigger instead of putting it on a cadence.)`
|
|
92
|
+
: ''));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The ONE sanctioned way to pull a vendor on a cadence. Asserts the posture BEFORE `pull` runs, so
|
|
96
|
+
* a refused vendor makes ZERO vendor requests — the guard has to sit in front of the network call,
|
|
97
|
+
* not behind it (guarding the fold, e.g. `syncPull`, would be theatre: the request already went out).
|
|
98
|
+
*
|
|
99
|
+
* Wrap any scheduler/cron/loop's pull in this and the rule enforces itself:
|
|
100
|
+
* `await pullOnSchedule(pack, () => syncSlackFromReal(client, opts))`
|
|
101
|
+
* An explicit, human-asked pull does NOT go through here — it calls the connector directly, which is
|
|
102
|
+
* exactly the sanctioned path for an on-demand vendor.
|
|
103
|
+
*/
|
|
104
|
+
export async function pullOnSchedule(vendor, pull) {
|
|
105
|
+
assertContinuousPullAllowed(vendor);
|
|
106
|
+
return pull();
|
|
107
|
+
}
|
|
108
|
+
const registry = new Map();
|
|
109
|
+
/** Register (or replace) a pack descriptor. Returns it.
|
|
110
|
+
*
|
|
111
|
+
* Side effect, deliberately: a `rateBudget` declaration on the descriptor is ARMED here, so
|
|
112
|
+
* registering a pack is enough to give its vendor the ceiling it declared. `declareRateBudget`
|
|
113
|
+
* is idempotent for an identical declaration and REFUSES a widening one, so re-registering is
|
|
114
|
+
* safe and "register a fatter pack descriptor to buy a bigger budget" is not a move. */
|
|
115
|
+
export function registerPack(pack) {
|
|
116
|
+
if (!/^[a-z0-9-]+$/.test(pack.vendor))
|
|
117
|
+
throw new Error(`invalid pack vendor id: ${pack.vendor}`);
|
|
118
|
+
if (pack.rateBudget)
|
|
119
|
+
declareRateBudget(pack.vendor, pack.rateBudget);
|
|
120
|
+
if (pack.stateSystem)
|
|
121
|
+
registerStateSystem(pack.vendor, pack.stateSystem);
|
|
122
|
+
if (pack.auth)
|
|
123
|
+
registerAuthStrategy(pack.vendor, pack.auth);
|
|
124
|
+
if (pack.references)
|
|
125
|
+
registerReferences(pack.vendor, pack.references);
|
|
126
|
+
registry.set(pack.vendor, pack);
|
|
127
|
+
return pack;
|
|
128
|
+
}
|
|
129
|
+
export function getPack(vendor) {
|
|
130
|
+
return registry.get(vendor);
|
|
131
|
+
}
|
|
132
|
+
/** All registered packs, sorted by vendor (deterministic). */
|
|
133
|
+
export function listPacks() {
|
|
134
|
+
return [...registry.values()].sort((a, b) => a.vendor.localeCompare(b.vendor));
|
|
135
|
+
}
|
|
136
|
+
export function hasPack(vendor) {
|
|
137
|
+
return registry.has(vendor);
|
|
138
|
+
}
|
|
139
|
+
/** Clear the registry (tests). */
|
|
140
|
+
export function clearRegistry() {
|
|
141
|
+
registry.clear();
|
|
142
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TwinAction } from './actions.js';
|
|
2
|
+
import type { WorldServiceEvent } from './types.js';
|
|
3
|
+
export declare const PLACEHOLDER_REMOTE = "placeholder";
|
|
4
|
+
/** The window is per WORLD SERVICE ROOT, not per state service: a twin may record under a state
|
|
5
|
+
* service that is not its world service id (slack records under `chat`), and every state service
|
|
6
|
+
* of one twin lives under the same root — so the marker sits at the root's state dir. */
|
|
7
|
+
export declare function placeholderPullMarkerPath(_service: string, root?: string): string;
|
|
8
|
+
/** Is the service's placeholder-pull window open? */
|
|
9
|
+
export declare function placeholderPullActive(service: string, root?: string): boolean;
|
|
10
|
+
export declare function beginPlaceholderPull(service: string, root?: string, at?: string): void;
|
|
11
|
+
export declare function endPlaceholderPull(service: string, root?: string): void;
|
|
12
|
+
/** Open the window around `fn` (in-process seeding). */
|
|
13
|
+
export declare function withPlaceholderPull<T>(service: string, root: string | undefined, fn: () => T | Promise<T>): Promise<T>;
|
|
14
|
+
/** The observed events a local write becomes when it is a pull from the placeholder: the write's
|
|
15
|
+
* own subject and fields, plus every resource its projection creates or updates. A projection
|
|
16
|
+
* DELETE has no observed form (the placeholder holds what exists, not what was removed) and is
|
|
17
|
+
* refused loudly — a seed creates the world, it does not un-create it. */
|
|
18
|
+
export declare function placeholderEventsFor(action: TwinAction): WorldServiceEvent[];
|
|
19
|
+
/** Land `action` as the placeholder's observed facts. Caller holds the service locks. */
|
|
20
|
+
export declare function landAsPlaceholder(action: TwinAction, root?: string): {
|
|
21
|
+
events: WorldServiceEvent[];
|
|
22
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// THE PLACEHOLDER REMOTE (docs/concepts/the-model.md#a-branch-is-a-position-not-a-copy, "default data"): a fresh twin is a clone of the
|
|
2
|
+
// coherent default data its pack ships, and that seed is a PULL, never a pending write. While a
|
|
3
|
+
// service's placeholder-pull window is open — a marker file under its state dir, read through the
|
|
4
|
+
// active world store like the world clock — every local `set` write the twin accepts lands as an
|
|
5
|
+
// OBSERVED event from the placeholder (origin `virtual`, provenance `placeholder`) and no action row
|
|
6
|
+
// is appended: `main` equals `origin/main` when the seed ends, `diff` is empty, nothing is pending,
|
|
7
|
+
// and no real remote linked later is ever offered the seed. The placeholder has no origin and no
|
|
8
|
+
// credential, so it can never be a push target; a pull from it again is "back to the defaults".
|
|
9
|
+
import { appendParentEntry, toEntry } from "./log.js";
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { worldPaths, worldStateRoot } from "./storage.js";
|
|
12
|
+
import { WorldServiceEventSchema } from "./schemas.js";
|
|
13
|
+
import { hashFieldValue } from "./hash.js";
|
|
14
|
+
import { getActiveWorldStore } from "./world-store.js";
|
|
15
|
+
export const PLACEHOLDER_REMOTE = 'placeholder';
|
|
16
|
+
/** The window is per WORLD SERVICE ROOT, not per state service: a twin may record under a state
|
|
17
|
+
* service that is not its world service id (slack records under `chat`), and every state service
|
|
18
|
+
* of one twin lives under the same root — so the marker sits at the root's state dir. */
|
|
19
|
+
export function placeholderPullMarkerPath(_service, root) {
|
|
20
|
+
return join(worldStateRoot(root), 'placeholder-pull');
|
|
21
|
+
}
|
|
22
|
+
/** Is the service's placeholder-pull window open? */
|
|
23
|
+
export function placeholderPullActive(service, root) {
|
|
24
|
+
return getActiveWorldStore().exists(placeholderPullMarkerPath(service, root));
|
|
25
|
+
}
|
|
26
|
+
export function beginPlaceholderPull(service, root, at = new Date().toISOString()) {
|
|
27
|
+
const path = placeholderPullMarkerPath(service, root);
|
|
28
|
+
const store = getActiveWorldStore();
|
|
29
|
+
store.mkdir(join(path, '..'));
|
|
30
|
+
store.write(path, `${JSON.stringify({ remote: PLACEHOLDER_REMOTE, openedAt: at })}\n`);
|
|
31
|
+
}
|
|
32
|
+
export function endPlaceholderPull(service, root) {
|
|
33
|
+
const path = placeholderPullMarkerPath(service, root);
|
|
34
|
+
if (getActiveWorldStore().exists(path))
|
|
35
|
+
getActiveWorldStore().remove(path);
|
|
36
|
+
}
|
|
37
|
+
/** Open the window around `fn` (in-process seeding). */
|
|
38
|
+
export async function withPlaceholderPull(service, root, fn) {
|
|
39
|
+
beginPlaceholderPull(service, root);
|
|
40
|
+
try {
|
|
41
|
+
return await fn();
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
endPlaceholderPull(service, root);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The observed events a local write becomes when it is a pull from the placeholder: the write's
|
|
48
|
+
* own subject and fields, plus every resource its projection creates or updates. A projection
|
|
49
|
+
* DELETE has no observed form (the placeholder holds what exists, not what was removed) and is
|
|
50
|
+
* refused loudly — a seed creates the world, it does not un-create it. */
|
|
51
|
+
export function placeholderEventsFor(action) {
|
|
52
|
+
const observations = [];
|
|
53
|
+
if (action.fields)
|
|
54
|
+
observations.push({ subject: action.subject, fields: action.fields });
|
|
55
|
+
for (const c of action.projection?.creates ?? [])
|
|
56
|
+
observations.push({ subject: { type: c.type, id: c.id }, fields: c.fields });
|
|
57
|
+
for (const u of action.projection?.updates ?? [])
|
|
58
|
+
observations.push({ subject: { type: u.type, id: u.id }, fields: u.fields });
|
|
59
|
+
if (action.projection?.deletes?.length)
|
|
60
|
+
throw new Error(`placeholder pull: ${action.service} ${action.operation ?? action.op} deletes ${action.projection.deletes.map((d) => `${d.type}:${d.id}`).join(', ')} — a seed creates the placeholder world, it cannot remove from it`);
|
|
61
|
+
return observations.map((o) => {
|
|
62
|
+
const contentHash = hashFieldValue({ subject: o.subject, fields: o.fields, operation: action.operation ?? action.op });
|
|
63
|
+
const id = `placeholder:${action.service}:${o.subject.type}:${o.subject.id}:${contentHash}`;
|
|
64
|
+
return WorldServiceEventSchema.parse({
|
|
65
|
+
id,
|
|
66
|
+
service: action.service,
|
|
67
|
+
type: `${action.service}.${o.subject.type}.delta`,
|
|
68
|
+
schemaVersion: 1,
|
|
69
|
+
idempotencyKey: id,
|
|
70
|
+
occurredAt: action.occurredAt,
|
|
71
|
+
observedAt: action.occurredAt,
|
|
72
|
+
origin: 'virtual',
|
|
73
|
+
subject: o.subject,
|
|
74
|
+
data: { changed: Object.fromEntries(Object.entries(o.fields).map(([k, v]) => [k, { after: v }])), provenance: PLACEHOLDER_REMOTE, remote: PLACEHOLDER_REMOTE },
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/** Land `action` as the placeholder's observed facts. Caller holds the service locks. */
|
|
79
|
+
export function landAsPlaceholder(action, root) {
|
|
80
|
+
const paths = worldPaths(action.service, root);
|
|
81
|
+
const events = placeholderEventsFor(action);
|
|
82
|
+
// v2: the placeholder's rows land as ENTRIES on the parent log (one row kind), marked as the placeholder's
|
|
83
|
+
for (const event of events)
|
|
84
|
+
appendParentEntry({ ...toEntry(event), provenance: 'placeholder' }, root);
|
|
85
|
+
return { events };
|
|
86
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** How one vendor's browser SDK is routed to its twin. Supplied by the caller. */
|
|
2
|
+
export interface VendorRoute {
|
|
3
|
+
/** The vendor's twin origin, e.g. http://127.0.0.1:12111 */
|
|
4
|
+
origin: string;
|
|
5
|
+
/** Same-origin browser API path that belongs to this vendor, e.g. '/v1/' */
|
|
6
|
+
apiPathPrefix: string;
|
|
7
|
+
/** Absolute API host to strip from the vendor's browser loader so calls become
|
|
8
|
+
* same-origin (e.g. 'https://api.stripe.com'). Omit if the SDK already calls same-origin. */
|
|
9
|
+
loaderHost?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface TwinProxyOptions {
|
|
12
|
+
/** Your app's origin, e.g. http://localhost:3000 */
|
|
13
|
+
target: string;
|
|
14
|
+
/** vendor → routing. */
|
|
15
|
+
map: Record<string, VendorRoute>;
|
|
16
|
+
/** Extra local service origins whose redirects should be rewritten back to this proxy origin. */
|
|
17
|
+
redirectOrigins?: string[];
|
|
18
|
+
/** Listen port (0 = ephemeral). */
|
|
19
|
+
port?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface TwinProxy {
|
|
22
|
+
port: number;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
export declare function createTwinProxy(opts: TwinProxyOptions): TwinProxy;
|