@secondlayer/shared 7.9.0 → 7.11.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.
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Lifecycle modules — each runtime plane exposes start/stop/health so a
3
+ * supervisor can restart one without taking the others down.
4
+ */
5
+ declare const MODULE_IDS: readonly ["api", "ingest", "decoder", "subgraph", "notification", "verification", "publisher"];
6
+ type ModuleId = (typeof MODULE_IDS)[number];
7
+ declare const MODULE_STATES: readonly ["stopped", "starting", "running", "degraded", "failed"];
8
+ type ModuleState = (typeof MODULE_STATES)[number];
9
+ type ModuleHealth = {
10
+ id: ModuleId
11
+ state: ModuleState
12
+ detail: string | null
13
+ restarts: number
14
+ };
15
+ type RuntimeModule = {
16
+ id: ModuleId
17
+ start: () => Promise<void>
18
+ stop: () => Promise<void>
19
+ health: () => ModuleHealth
20
+ };
21
+ declare const DEFAULT_PROFILE: readonly ModuleId[];
22
+ declare const PUBLISHER_PROFILE: readonly ModuleId[];
23
+ declare function isModuleId(value: string): value is ModuleId;
24
+ declare function emptyHealth(id: ModuleId): ModuleHealth;
25
+ /** In-memory module used by restart/fault tests. */
26
+ declare function createMemoryModule(id: ModuleId, opts?: {
27
+ failStart?: boolean
28
+ loop?: boolean
29
+ onStart?: () => void
30
+ onStop?: () => void
31
+ }): RuntimeModule & {
32
+ setState: (state: ModuleState, detail?: string) => void
33
+ };
34
+ type SupervisorHealth = {
35
+ status: "healthy" | "degraded" | "failed"
36
+ modules: ModuleHealth[]
37
+ };
38
+ type Supervisor = {
39
+ start: (ids?: readonly ModuleId[]) => Promise<void>
40
+ stop: (id?: ModuleId) => Promise<void>
41
+ restart: (id: ModuleId) => Promise<void>
42
+ health: () => SupervisorHealth
43
+ };
44
+ declare function createSupervisor(modules: readonly RuntimeModule[]): Supervisor;
45
+ /**
46
+ * Handler isolation — untrusted subgraph handlers run behind a timeout,
47
+ * optional network deny, and cannot take down the host process.
48
+ */
49
+ declare const ISOLATION_FAULTS: readonly ["timeout", "throw", "oom", "kill", "network"];
50
+ type IsolationFault = (typeof ISOLATION_FAULTS)[number];
51
+ type IsolationLimits = {
52
+ timeoutMs: number
53
+ memoryMb?: number
54
+ allowNetwork?: boolean
55
+ };
56
+ type IsolationResult = {
57
+ ok: true
58
+ hostAlive: true
59
+ } | {
60
+ ok: false
61
+ fault: IsolationFault
62
+ hostAlive: true
63
+ detail: string
64
+ };
65
+ declare class IsolationNetworkError extends Error {
66
+ readonly name = "IsolationNetworkError";
67
+ }
68
+ declare class IsolationKillError extends Error {
69
+ readonly name = "IsolationKillError";
70
+ }
71
+ declare class IsolationOomError extends Error {
72
+ readonly name = "IsolationOomError";
73
+ }
74
+ declare function isolateHandler(run: (ctx: {
75
+ signal: AbortSignal
76
+ fetch: (url: string) => Promise<unknown>
77
+ }) => Promise<void>, limits: IsolationLimits): Promise<IsolationResult>;
78
+ /**
79
+ * Embedded migration — advisory-locked before listeners/workers start.
80
+ * Concurrent boots serialize. A failed pass releases the lock and refuses
81
+ * to start the rest of the runtime.
82
+ */
83
+ declare const MIGRATION_LOCK_KEY = 1397509447;
84
+ type EmbeddedMigration = {
85
+ applied: string[]
86
+ ok: boolean
87
+ error: string | null
88
+ };
89
+ type MigrationRunner = {
90
+ acquire: (key: number) => Promise<boolean>
91
+ release: (key: number) => Promise<void>
92
+ apply: () => Promise<string[]>
93
+ };
94
+ declare function migrateEmbedded(runner: MigrationRunner): Promise<EmbeddedMigration>;
95
+ /** In-memory lock used by concurrent-startup tests. */
96
+ declare function createMemoryMigrationRunner(opts?: {
97
+ fail?: boolean
98
+ delayMs?: number
99
+ sleep?: (ms: number) => Promise<void>
100
+ }): MigrationRunner & {
101
+ holders: number
102
+ appliedCount: number
103
+ };
104
+ /**
105
+ * Minimal runtime config — six or fewer required non-secret values.
106
+ * Unknown or contradictory values fail before ingest starts.
107
+ */
108
+ declare const NODE_MODES: readonly ["external", "stacks", "full"];
109
+ type NodeMode = (typeof NODE_MODES)[number];
110
+ declare const NETWORKS: readonly ["mainnet", "testnet", "devnet"];
111
+ type RuntimeNetwork = (typeof NETWORKS)[number];
112
+ /** Required non-secret keys. Secrets (tokens, signing keys) come from `sl init`. */
113
+ declare const REQUIRED_KEYS: readonly ["NETWORK", "DATABASE_URL", "NODE_MODE", "DATA_DIR", "API_PORT", "INDEXER_PORT"];
114
+ type RequiredConfigKey = (typeof REQUIRED_KEYS)[number];
115
+ declare const OPTIONAL_KEYS: readonly ["STACKS_NODE_RPC_URL", "LISTEN_HOST", "LOG_LEVEL", "INSTANCE_MODE"];
116
+ declare const SECRET_KEYS: readonly ["INSTANCE_TOKEN", "API_KEY", "SECONDLAYER_SECRETS_KEY", "STREAMS_SIGNING_PRIVATE_KEY", "SECONDLAYER_WEBHOOK_SIGNING_PRIVATE_KEY", "POSTGRES_PASSWORD", "BITCOIN_RPC_PASSWORD"];
117
+ type RuntimeConfig = {
118
+ NETWORK: RuntimeNetwork
119
+ DATABASE_URL: string
120
+ NODE_MODE: NodeMode
121
+ DATA_DIR: string
122
+ API_PORT: number
123
+ INDEXER_PORT: number
124
+ STACKS_NODE_RPC_URL: string | null
125
+ LISTEN_HOST: string
126
+ LOG_LEVEL: string
127
+ };
128
+ type ConfigResult = {
129
+ ok: true
130
+ config: RuntimeConfig
131
+ } | {
132
+ ok: false
133
+ errors: string[]
134
+ };
135
+ declare function parseRuntimeConfig(env: Record<string, string | undefined>): ConfigResult;
136
+ declare const COVERAGE_STATES: readonly ["complete", "syncing", "lagging", "gap", "stale", "failed", "unverified_import", "unanchored", "source_unavailable", "out_of_scope", "disabled"];
137
+ type CoverageState = (typeof COVERAGE_STATES)[number];
138
+ declare const STATUS_PLANES: readonly ["node", "raw", "decoder", "subgraph", "queue", "archive", "disk", "coverage"];
139
+ type StatusPlane = (typeof STATUS_PLANES)[number];
140
+ type PlaneStatus = {
141
+ plane: StatusPlane
142
+ state: string
143
+ action: string
144
+ };
145
+ type RuntimeStatusReport = {
146
+ status: SupervisorHealth["status"]
147
+ modules: ModuleHealth[]
148
+ planes: PlaneStatus[]
149
+ };
150
+ declare function reportRuntimeStatus(input: {
151
+ supervisor: SupervisorHealth
152
+ node?: string
153
+ archive?: string
154
+ disk?: string
155
+ coverage?: CoverageState | string
156
+ }): RuntimeStatusReport;
157
+ declare function actionFor(plane: StatusPlane, state: string): string;
158
+ /**
159
+ * Backup bundle — consistent DB, pinned config, encrypted keys, handler
160
+ * manifests, and scope. Restore onto a wiped host must deep-verify green.
161
+ */
162
+ declare const BACKUP_PARTS: readonly ["db", "config", "keys", "handlers", "scope"];
163
+ type BackupPart = (typeof BACKUP_PARTS)[number];
164
+ type BackupManifest = {
165
+ created_at: string
166
+ network: string
167
+ parts: BackupPart[]
168
+ encrypted: boolean
169
+ scope: {
170
+ from_height: number
171
+ to_height: number | null
172
+ }
173
+ };
174
+ type BackupPlan = {
175
+ ok: boolean
176
+ manifest: BackupManifest
177
+ reason: string
178
+ };
179
+ declare function planBackup(input: {
180
+ network: string
181
+ encryptedKeys: boolean
182
+ fromHeight: number
183
+ toHeight?: number | null
184
+ now?: string
185
+ }): BackupPlan;
186
+ type RestoreResult = {
187
+ ok: true
188
+ applied: boolean
189
+ deep_green: boolean
190
+ } | {
191
+ ok: false
192
+ reason: string
193
+ };
194
+ declare function restoreBackup(plan: BackupPlan, opts?: {
195
+ apply?: boolean
196
+ deepVerify?: boolean
197
+ }): RestoreResult;
198
+ /**
199
+ * Upgrade contract — pin image, preflight schema/disk, backup first,
200
+ * migrate, health/verify, document rollback limits.
201
+ */
202
+ declare const UPGRADE_STEPS: readonly ["pin", "preflight", "backup", "migrate", "health", "verify"];
203
+ type UpgradeStep = (typeof UPGRADE_STEPS)[number];
204
+ type UpgradePlan = {
205
+ from: string
206
+ to: string
207
+ steps: UpgradeStep[]
208
+ ok: boolean
209
+ reason: string
210
+ rollback: string
211
+ };
212
+ declare function planUpgrade(input: {
213
+ from: string
214
+ to: string
215
+ diskOk: boolean
216
+ schemaOk: boolean
217
+ backupDone: boolean
218
+ supported?: readonly string[]
219
+ }): UpgradePlan;
220
+ declare function applyUpgrade(plan: UpgradePlan, opts?: {
221
+ apply?: boolean
222
+ }): {
223
+ ok: boolean
224
+ applied: boolean
225
+ reason: string
226
+ };
227
+ /**
228
+ * Resource guardrails — preflight RAM/disk/Postgres and estimate growth.
229
+ * Constrained fixtures fail early.
230
+ */
231
+ type ResourceSnapshot = {
232
+ ramMb: number
233
+ diskGb: number
234
+ postgresMaxConnections: number
235
+ postgresSharedBuffersMb?: number
236
+ };
237
+ type GuardrailResult = {
238
+ ok: true
239
+ estimates: {
240
+ diskGbPer100kBlocks: number
241
+ }
242
+ } | {
243
+ ok: false
244
+ errors: string[]
245
+ };
246
+ declare const FLOORS: {
247
+ readonly appRamMb: 4096
248
+ readonly fullRamMb: 98304
249
+ readonly appDiskGb: 80
250
+ readonly fullDiskGb: 1500
251
+ readonly postgresMaxConnections: 50
252
+ };
253
+ declare function preflightResources(snap: ResourceSnapshot, mode: "external" | "stacks" | "full"): GuardrailResult;
254
+ declare const MODULE_COMMANDS: Record<ModuleId, readonly string[]>;
255
+ declare function commandFor(id: ModuleId): readonly string[];
256
+ export { restoreBackup, reportRuntimeStatus, preflightResources, planUpgrade, planBackup, parseRuntimeConfig, migrateEmbedded, isolateHandler, isModuleId, emptyHealth, createSupervisor, createMemoryModule, createMemoryMigrationRunner, commandFor, applyUpgrade, actionFor, UpgradeStep, UpgradePlan, UPGRADE_STEPS, SupervisorHealth, Supervisor, StatusPlane, STATUS_PLANES, SECRET_KEYS, RuntimeStatusReport, RuntimeNetwork, RuntimeModule, RuntimeConfig, RestoreResult, ResourceSnapshot, RequiredConfigKey, REQUIRED_KEYS, PlaneStatus, PUBLISHER_PROFILE, OPTIONAL_KEYS, NodeMode, NODE_MODES, NETWORKS, ModuleState, ModuleId, ModuleHealth, MigrationRunner, MODULE_STATES, MODULE_IDS, MODULE_COMMANDS, MIGRATION_LOCK_KEY, IsolationResult, IsolationOomError, IsolationNetworkError, IsolationLimits, IsolationKillError, IsolationFault, ISOLATION_FAULTS, GuardrailResult, FLOORS, EmbeddedMigration, DEFAULT_PROFILE, ConfigResult, BackupPlan, BackupPart, BackupManifest, BACKUP_PARTS };