@mcsherrylabs/pi-meerkat 0.7.0 → 0.8.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.
Files changed (46) hide show
  1. package/README.md +85 -33
  2. package/dist/installer/committee-init.sh +117 -0
  3. package/dist/installer/committee.conf +102 -0
  4. package/dist/installer/node-join.sh +228 -0
  5. package/dist/installer/node.conf +235 -0
  6. package/dist/installer/runtime.json +1 -0
  7. package/dist/installer-runtime/dist/scripts/recover-owner.js +15 -0
  8. package/dist/installer-runtime/dist/scripts/subscriptions-init.js +35 -0
  9. package/dist/installer-runtime/dist/scripts/versions.mjs +86 -0
  10. package/dist/installer-runtime/dist/scripts/wait-for-chain.js +90 -0
  11. package/dist/installer-runtime/dist/src/announcements.js +448 -0
  12. package/dist/installer-runtime/dist/src/app.js +539 -0
  13. package/dist/installer-runtime/dist/src/chainfetch.js +87 -0
  14. package/dist/installer-runtime/dist/src/env.js +425 -0
  15. package/dist/installer-runtime/dist/src/registry/announce.js +77 -0
  16. package/dist/installer-runtime/dist/src/registry/announcement.js +141 -0
  17. package/dist/installer-runtime/dist/src/registry/dependencies.js +28 -0
  18. package/dist/installer-runtime/dist/src/registry/detail.js +87 -0
  19. package/dist/installer-runtime/dist/src/registry/drainer.js +131 -0
  20. package/dist/installer-runtime/dist/src/registry/http.js +194 -0
  21. package/dist/installer-runtime/dist/src/registry/index.js +52 -0
  22. package/dist/installer-runtime/dist/src/registry/lease.js +35 -0
  23. package/dist/installer-runtime/dist/src/registry/ownership.js +31 -0
  24. package/dist/installer-runtime/dist/src/registry/provision.js +180 -0
  25. package/dist/installer-runtime/dist/src/registry/store.js +242 -0
  26. package/dist/installer-runtime/dist/src/registry/sweep.js +9 -0
  27. package/dist/installer-runtime/dist/src/registry/worker-module.js +87 -0
  28. package/dist/installer-runtime/dist/src/runtime.js +164 -0
  29. package/dist/installer-runtime/dist/src/store.js +138 -0
  30. package/dist/installer-runtime/package.json +1 -0
  31. package/dist/installer.d.ts +224 -0
  32. package/dist/installer.d.ts.map +1 -0
  33. package/dist/installer.js +336 -0
  34. package/dist/installer.js.map +1 -0
  35. package/dist/mcp-cli.d.ts.map +1 -1
  36. package/dist/mcp-cli.js +31 -5
  37. package/dist/mcp-cli.js.map +1 -1
  38. package/dist/registry.d.ts.map +1 -1
  39. package/dist/registry.js +8 -4
  40. package/dist/registry.js.map +1 -1
  41. package/installer/Committee.Dockerfile +6 -0
  42. package/installer/Node.Dockerfile +6 -0
  43. package/installer/Registry.Dockerfile +13 -0
  44. package/installer/runtime-control.mjs +172 -0
  45. package/installer/session-files.mjs +16 -0
  46. package/package.json +5 -4
@@ -0,0 +1,235 @@
1
+ # This node: a NON-GENESIS FOLLOWER on devnet. Chain-wide params come from the genesis
2
+ # spec — never set them here, they must stay byte-identical on every node. Only
3
+ # node-local knobs below.
4
+ include file("/public-genesis/genesis.conf")
5
+
6
+ # 089 chain-spec pin: the canonical hash of the genesis file IS the genesis block id.
7
+ # Any drift refuses to boot rather than forking quietly.
8
+ ce.evolver.genesis-file = "/public-genesis/genesis.conf"
9
+
10
+ # This node's OWN identity, from the first-boot faucet claim (node-join.sh). REQUIRED
11
+ # substitutions (no `?`): a node booting with missing keys must fail fast, not later and
12
+ # obscurely at the first TLS handshake.
13
+ ce.evolver.node {
14
+ private-key = ${EVOLVER_PRIVATE_KEY} # Ed25519 PKCS#8 base64
15
+ public-key = ${EVOLVER_NODE_PUBLIC_KEY} # Ed25519 X.509 base64
16
+ bls-secret = ${EVOLVER_BLS_SEED} # BLS IKM base64
17
+ port = ${EVOLVER_NODE_PORT} # p2p transport (raw TCP, mTLS)
18
+ }
19
+
20
+ evolver.peer-discovery {
21
+ # THIS NODE ADVERTISES NOTHING, AND MUST NOT. `preferred-url` is the address this node
22
+ # gossips as its OWN (`""` = feature off), and a joined app node behind Docker HAS no
23
+ # routable p2p address: its 7000 is container-local and deliberately unpublished. So
24
+ # advertising one means every node on the chain records `<us> -> https://127.0.0.1:7000`
25
+ # and then dials its own loopback.
26
+ #
27
+ # Observed on devnet, and it is worse than noise: this node CONNECTED TO ITSELF, logged
28
+ # `Peer connected: <self>`, sent its own SyncManager `BlockRangeRequest(4841,4845)`, and
29
+ # TipGossip then reported `rotating to <self> (failed=devnet1-node0)` followed by
30
+ # `stalled (upstream=none)`. After that its submits were never forwarded and every write
31
+ # timed out. The table also keeps the dead: `evolvertemplateappnode965813 at
32
+ # 127.0.0.1:7000` from an earlier deployment was still being dialled hours later.
33
+ # ADVERTISED AGAIN, and ⚠️ ONLY BECAUSE IT IS NOW REAL. This was `""` because the node
34
+ # had been advertising `https://127.0.0.1:7000`: every peer that read the discovery
35
+ # table dialled its OWN loopback, so this node connected to itself, sent its own
36
+ # SyncManager block requests, reported `candidates=<self>` / `stalled (upstream=none)`
37
+ # and stopped forwarding submits. An unroutable value reinstates that exactly — which
38
+ # is why `EVOLVER_TRANSPORT_ADDRESS` is REQUIRED here rather than defaulted, and why the
39
+ # command suite refuses a loopback value.
40
+ # ⚠️ EMPTY, DELIBERATELY. This node is a READER behind NAT: it has no routable p2p address.
41
+ # Advertising one is not merely useless, it is harmful — devnet recorded a node that advertised
42
+ # an unroutable address CONNECTING TO ITSELF, sending its own sync requests, reporting
43
+ # `stalled (upstream=none)`, and leaving dead entries other nodes dialled for hours.
44
+ #
45
+ # It costs this node nothing: advertising is how others DIAL YOU, and nothing needs to. Writing
46
+ # still works — this node dials OUT, discovers the network, learns its distance to the committee
47
+ # and forwards transactions along that path. Being undialable only stops bodies being flooded TO
48
+ # it, which is a DA member's concern and not a reader's.
49
+ transport-address = ""
50
+ peer-url = ""
51
+ gossip-interval-seconds = 60
52
+
53
+ # 16 INBOUND, not 0. The 141 leaf recipe (`in=0`) is right for an app node that only
54
+ # talks upstream — and wrong here: the registry's channel requires announcers' nodes to
55
+ # reach THIS node so their flooded bodies can be attested (research D2). Each announcer
56
+ # holds one inbound slot, so 16 is the number of concurrent announcer nodes supported.
57
+ # Generous on both sides so DISCOVERY actually happens. The seed below is pinned so the node
58
+ # always has a way back to the chain, but `allowed-outbound` is deliberately LARGER than the
59
+ # pinned list — the pin is strict only while the two are equal, and a strict pin here would mean
60
+ # this node never dials anything it discovers.
61
+ allowed-inbound = 16
62
+ # `allowed-outbound` and `preferred-peers` are DELIBERATELY ABSENT HERE — node-join.sh renders
63
+ # both into peers.conf so the two cannot drift apart in two files.
64
+ #
65
+ # ⚠️ THIS COMMENT USED TO CLAIM THE PIN IS STRICT. It is NOT, and the difference matters to
66
+ # anyone reasoning about what this node will dial. It said `|preferred| == allowed-outbound`
67
+ # holds "by construction"; node-join.sh renders ONE pinned seed and an outbound budget of 16,
68
+ # so 1 != 16 and the pin is deliberately LOOSE. That is the intended design — a follower has to
69
+ # dial what it discovers or it is a leaf — but the old wording described the opposite property
70
+ # and named the incidents (a dead address from a torn-down deployment still being dialled, this
71
+ # node dialling its own loopback) that a strict pin was supposed to prevent. Those are the COST
72
+ # of the loose pin, not evidence against it: the seed guarantees a way back to the chain, and
73
+ # discovery does the rest. Which is what keeps a shared chain's discovery table out of the
74
+ # dial queue — it keeps the dead (an address from a torn-down deployment was still
75
+ # being dialled hours later) and once made this node dial its own loopback. Raising
76
+ # the budget without naming a peer would silently unpin it; naming a peer without
77
+ # raising the budget could never reach it. One list, both keys, no drift.
78
+ #
79
+ # ⚠️ THIS USED TO BE `out=1`, copied verbatim from the 141 LEAF recipe, and it decided
80
+ # WHICH CHANNELS THIS NODE COULD READ — silently. `ChannelOverlay.peers(ledgerId)` is
81
+ # the channel's DA group ∩ connected, so with the one slot spent on the upstream every
82
+ # channel whose DA member is somebody else resolved to an EMPTY pull set: the node
83
+ # records the miss, sends no request, and says so at DEBUG only (evolver#89). A node
84
+ # reading a channel it does not itself serve needs the upstream AND one reachable DA
85
+ # member per such channel, so a fixed 1 is only ever right for a node that reads
86
+ # nothing but its own.
87
+ }
88
+
89
+ ce.evolver.consensus {
90
+ validate-chain-on-startup = false
91
+ wake-on-work { forward-watermark = 16 }
92
+ }
93
+
94
+ # PROCESSING MODE — off by default, and THE REASON THIS DEPLOYMENT EXISTS once your app
95
+ # reads a channel.
96
+ #
97
+ # `/api/channel/next` is a SINGLE-CONSUMER, FORWARD-ONLY cursor: two readers do not
98
+ # error, they silently split the stream and the message is unrecoverable. So an app that
99
+ # consumes an ordered channel must drain a node NOBODY ELSE drains — which means a node
100
+ # of its own. It cannot share a public one, and a single-node chain (devnet today) IS the
101
+ # committee, so that node is not a processing node and serves no stream at all.
102
+ #
103
+ # And the committee's node must never be put in this mode: processing mode DEFERS apply
104
+ # and paces it from the app's polling, so a committee node in processing mode would
105
+ # couple consensus progress to an HTTP poller. Ordering is its job; give the app its own.
106
+ #
107
+ # Turning it on changes what committed reads MEAN here — they answer "as of MY cursor".
108
+ # Measured in this family (evolver-scheduler): right after a successful faucet claim a
109
+ # quorum node reported `{"balance":5000}` for an identity while a processing node
110
+ # reported `{"error":"not_found"}` for the same one. An app in that mode needs to know
111
+ # which of its questions are "has the chain agreed?" (ask a quorum node) and which are
112
+ # "what have I processed?" (ask this one).
113
+ #
114
+ # ON, because this app owns a channel (`announcements`) and reads its ordered stream —
115
+ # steps 5-7 of src/app.ts. BOTH lines are needed and neither works alone: a node in
116
+ # processing mode with no channels declared has nothing to offer, and a node that
117
+ # declares channels without execution stores bodies it never streams.
118
+ #
119
+ # The channel must be named HERE as well as claimed on chain. A processing node stores
120
+ # bodies only for the channels it lists, so an undeclared channel's message is applied
121
+ # in the slice and never offered — the write looks fine and the stream stays silent,
122
+ # which is the failure the app's step 7 names explicitly.
123
+ # Processing is enabled only by the paired managed composition with its persistent zero-module
124
+ # shared SDK consumer. MCP sessions never drain. Independent remote nodes remain unmanaged.
125
+ ce.evolver.execution.enabled = ${?EVOLVER_EXECUTION_ENABLED}
126
+ # Shared SDK controller is the SOLE writer; node sees the whole config-only directory read-only.
127
+ # Ordinary boot/upgrade cannot merge defaults, resurrect removals or seed a lost declaration.
128
+ include required(file("/subscriptions/channels.conf"))
129
+ # `required` for the same reason: an absent optional include is IGNORED, and a node
130
+ # that silently fell back to the 64/64 defaults would dial the discovery table.
131
+ include required(file("/data/chain/peers.conf"))
132
+
133
+ # ── THE LOCAL ROUTES THIS DEPLOYMENT OPENS TO ITS OWN CONTAINERS ────────────
134
+ #
135
+ # The agent and the registry run BESIDE this node, each in its own container on this compose
136
+ # project's network. These routes carry no token and no client certificate — reaching the address
137
+ # IS the authorization — so the node binds them to loopback unless told otherwise, and loopback
138
+ # inside this container is reachable by nothing else in the stack.
139
+ #
140
+ # `bind-host = 0.0.0.0` here means: THIS COMPOSE NETWORK IS THE BOUNDARY. That claim holds only
141
+ # because none of these ports appear under `ports:` in docker-compose.yml and the stack does not
142
+ # use host networking. Publish one, or run with `--network host`, and the claim is simply false.
143
+ #
144
+ # ⚠️ `ce.evolver.login` is deliberately NOT here and cannot be: that route signs as any identity
145
+ # this host holds a signer for, which on this node is the node's own. Setting `bind-host` under
146
+ # that namespace fails the boot rather than being ignored.
147
+
148
+ # 145 — the agent sends and drains application messages. `/api/p2p/send` relays a message the
149
+ # agent signed ITSELF (its own identity, verified at the far end); `/api/p2p/sign-and-send` makes
150
+ # this node sign as itself. One listener, so opening it grants both.
151
+ ce.evolver.p2p {
152
+ http-port = 8460
153
+ bind-host = "0.0.0.0"
154
+ }
155
+
156
+ # 158 — the agent may ASK an enrolled phone to authorise a transaction. It cannot sign: the device
157
+ # holder approves every one, and that gate does not move when the bind does. Inert until a
158
+ # `mobile-signer` block exists, and the node says so at boot rather than failing.
159
+ ce.evolver.mobile {
160
+ http-port = 8470
161
+ bind-host = "0.0.0.0"
162
+ }
163
+
164
+ # 139 — CONTENT, so the agent can publish a document and the registry can read one back.
165
+ #
166
+ # `POST /content` writes to this node's own store, keyed by the bytes' own digest, and talks to
167
+ # no peer at all (FR-006). `bind-host` below is what lets the agent and the registry reach that
168
+ # route from their own containers. Fetching a document another node holds is the part that uses
169
+ # the overlay, and that is what the TLS listener underneath is for.
170
+ ce.evolver.content {
171
+ enabled = true
172
+ http-port = 8450
173
+ bind-host = "0.0.0.0"
174
+ }
175
+
176
+ data.base = "/data/chain"
177
+ state.snapshots = "snapshots"
178
+ # BOTH LISTENERS, and the pair is the point.
179
+ #
180
+ # 8440 PLAIN is what the agent and the registry talk to. They are ordinary HTTP clients in their
181
+ # own containers, and the alternative is not "use TLS" — this node's HTTPS listener presents its
182
+ # own IDENTITY certificate, self-signed, CN=<nodeId>, ~13 minutes old. It rotates, so it cannot
183
+ # be pinned as a CA, so a client that verifies it must turn verification off process-wide. For
184
+ # the agent that would also stop it verifying GoToSocial, which is not a trade worth making for
185
+ # a hop between two containers on one network.
186
+ #
187
+ # 8441 TLS is what PEERS use, and it exists so `ce.evolver.content` above can: the content peer
188
+ # route identifies its requester by client certificate, and refuses to start without somewhere
189
+ # mTLS to live. NOT PUBLISHED in docker-compose.yml — this node is a reader behind NAT that
190
+ # fetches outbound and serves nobody, so nothing needs to dial it.
191
+ #
192
+ # ⚠️ IF THIS NODE EVER SERVES CONTENT, `evolver.peer-discovery.api-url` must name 8441 and that
193
+ # port must be publicly reachable. It is empty here (see above): a node with no routable address
194
+ # advertises nothing and can only fetch.
195
+ http.port = 8440
196
+ # Only this private Compose network may reach the unsigned processing API.
197
+ http.bind-host = "0.0.0.0"
198
+ # ⚠️ REQUIRED SINCE 131 (#131), and without it this stack breaks. `http.bind-host` now defaults
199
+ # to 127.0.0.1 — the plain listener joined the bind-is-authorization set the four local listeners
200
+ # above already belong to, because it carries the whole API with no token and no client
201
+ # certificate, including (this node runs `ce.evolver.execution.enabled`) the SINGLE-CONSUMER
202
+ # cursor `GET /api/channel/next`, which a stranger can step past the registry's own deliveries.
203
+ #
204
+ # The claim being made here is the same one `ce.evolver.content.bind-host` and the p2p/login ones
205
+ # above make: THIS COMPOSE NETWORK IS THE BOUNDARY. It holds because 8440 is NOT published in
206
+ # docker-compose.yml and nothing uses host networking — the agent and the registry reach this
207
+ # node as `node:8440`, a service name on that network. Publish the port, or run with host
208
+ # networking, and the claim stops being true. The node cannot tell, so it logs a WARN naming it.
209
+ http.bind-host = "0.0.0.0"
210
+ https.port = 8441
211
+ evolver.grpc.port = 9000
212
+
213
+ # REQUIRED, and there is no default: `NodeBootstrap.initChainDb` reads this block
214
+ # directly, so a node without it dies with `No configuration setting found for key
215
+ # 'chainDb'` after the migrations have already run — which is exactly how this omission
216
+ # was found. Same values as the bundled deployment's node.
217
+ chainDb {
218
+ datasource {
219
+ driver = "org.hsqldb.jdbc.JDBCDriver"
220
+ user = "sa"
221
+ pass = ""
222
+ maxPoolSize = 5
223
+ connectionProperties = ""
224
+ cachePrepStmts = true
225
+ prepStmtCacheSize = 250
226
+ prepStmtCacheSqlLimit = 2048
227
+ connectionTimeout = 5000
228
+ useServerPrepStmts = false
229
+ transactionIsolationLevel = "TRANSACTION_READ_COMMITTED"
230
+ }
231
+ viewCachesSize = 50
232
+ useShutdownHook = false
233
+ freeBlobsEarly = false
234
+ }
235
+
@@ -0,0 +1 @@
1
+ {"operatorIdentity":"evolvermeerkatA","sdk":"0.22.1","bootstrap":"0.15.0"}
@@ -0,0 +1,15 @@
1
+ // Explicit offline operator action. Never imported/called by ordinary startup.
2
+ import { pathToFileURL } from "node:url";
3
+ import { recoverProcessorOwnerLock } from "@mcsherrylabs/evolver-client/processor";
4
+ import { currentTarget } from "../src/env.js";
5
+ import { processorStatePath } from "../src/runtime.js";
6
+ export function recoverMeerkatOwner(args, target, say = console.error) {
7
+ if (args.length !== 1 || args[0] !== "--force")
8
+ throw new Error("usage: recover-owner --force (only after stopping ALL collectors and disabling automatic restarts)");
9
+ say("WARNING: --force acknowledges ALL collectors are stopped and automatic restarts disabled; this command cannot verify that.");
10
+ const result = recoverProcessorOwnerLock({ path: processorStatePath(target), force: true });
11
+ say(`${result.removed ? "Removed owner lock" : "No owner lock present"}: ${result.marker}. Database/WAL, progress, subscriptions and keys unchanged. Start ONE collector normally with the SAME target/state environment to acquire a new lock.`);
12
+ return result;
13
+ }
14
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
15
+ recoverMeerkatOwner(process.argv.slice(2), currentTarget());
@@ -0,0 +1,35 @@
1
+ // Explicit fresh-resource operation; NEVER imported/called by ordinary startup or upgrade.
2
+ import { chown, chmod, mkdir, readdir } from "node:fs/promises";
3
+ import { dirname, join } from "node:path";
4
+ import { initializeSubscriptions } from "@mcsherrylabs/evolver-client/processor";
5
+ import { currentTarget, ROOT } from "../src/env.js";
6
+ import { processorConfigPath } from "../src/runtime.js";
7
+ const raw = process.env.EVOLVER_INITIAL_CHANNELS;
8
+ if (raw === undefined)
9
+ throw new Error("set EVOLVER_INITIAL_CHANNELS to an explicit JSON array (including []); initialization is fresh-only");
10
+ const channels = JSON.parse(raw);
11
+ if (!Array.isArray(channels) || channels.some(id => typeof id !== "string"))
12
+ throw new Error("EVOLVER_INITIAL_CHANNELS must be a JSON string array");
13
+ const path = processorConfigPath(currentTarget());
14
+ const directory = dirname(path);
15
+ if (process.getuid?.() === 0) {
16
+ const uid = Number(process.env.EVOLVER_OPERATOR_UID), gid = Number(process.env.EVOLVER_OPERATOR_GID);
17
+ if (![uid, gid].every(id => Number.isSafeInteger(id) && id >= 1))
18
+ throw new Error("root fresh initializer requires non-root operator uid/gid");
19
+ for (const dir of [directory, join(ROOT, "state")]) {
20
+ await mkdir(dir, { recursive: true, mode: 0o750 });
21
+ if ((await readdir(dir)).length !== 0)
22
+ throw new Error("fresh initialization refuses nonempty managed volumes; no valued migration/reseed");
23
+ }
24
+ for (const dir of [directory, join(ROOT, "state")]) {
25
+ await chown(dir, uid, gid);
26
+ await chmod(dir, 0o750);
27
+ }
28
+ process.setgroups([]);
29
+ process.setgid(gid);
30
+ process.setuid(uid);
31
+ }
32
+ else
33
+ await mkdir(directory, { recursive: true, mode: 0o750 });
34
+ await initializeSubscriptions(path, channels);
35
+ console.log(`initialized subscription declaration ${path}; ordinary startup will never reseed it`);
@@ -0,0 +1,86 @@
1
+ // Read .env — the single source of truth for versions and image names.
2
+ //
3
+ // Deliberately hand-parsed rather than pulling in dotenv: this is a template, and a
4
+ // dependency whose whole job is reading six lines of KEY=value is a dependency a reader has
5
+ // to evaluate. docker compose reads the same file natively, so there is one file and two
6
+ // consumers, not two sources.
7
+ import { readFileSync, writeFileSync } from "node:fs";
8
+ import { fileURLToPath } from "node:url";
9
+ import { dirname, join, resolve } from "node:path";
10
+ // fileURLToPath, not url.pathname — see the note in src/env.ts.
11
+ const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
12
+ export const ENV_PATH = join(ROOT, ".env");
13
+ export function readEnv() {
14
+ const out = {};
15
+ for (const line of readFileSync(ENV_PATH, "utf8").split("\n")) {
16
+ const t = line.trim();
17
+ if (!t || t.startsWith("#"))
18
+ continue;
19
+ const i = t.indexOf("=");
20
+ if (i < 0)
21
+ continue;
22
+ // Match docker compose's grammar, because it reads this same file: strip an inline
23
+ // comment and surrounding quotes, and tolerate CRLF. Diverging here is worse than not
24
+ // parsing at all — `npm run image` would tag one thing while `docker compose up` ran
25
+ // another, and nothing would report a conflict.
26
+ let v = t.slice(i + 1).replace(/\r$/, "").trim();
27
+ v = v.replace(/\s+#.*$/, "").trim();
28
+ if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) {
29
+ v = v.slice(1, -1);
30
+ }
31
+ if (!v)
32
+ continue; // empty ⇒ treat as missing, so the required-keys check reports it
33
+ out[t.slice(0, i).trim()] = v;
34
+ }
35
+ for (const k of ["EVOLVER_REGISTRY", "REGISTRY", "EVOLVER_VERSION", "APP_NAME", "APP_VERSION"]) {
36
+ if (!out[k])
37
+ throw new Error(`${ENV_PATH}: ${k} is missing — it is required, see the comments in that file`);
38
+ }
39
+ return out;
40
+ }
41
+ /** Update keys in .env IN PLACE, preserving comments and order — the comments are the
42
+ * documentation, so rewriting the file from an object would delete the explanation. */
43
+ export function updateEnv(updates) {
44
+ const lines = readFileSync(ENV_PATH, "utf8").split("\n");
45
+ const seen = new Set();
46
+ const out = lines.map((line) => {
47
+ const t = line.trim();
48
+ if (!t || t.startsWith("#"))
49
+ return line;
50
+ const i = t.indexOf("=");
51
+ if (i < 0)
52
+ return line;
53
+ const key = t.slice(0, i).trim();
54
+ if (!(key in updates))
55
+ return line;
56
+ seen.add(key);
57
+ return `${key}=${updates[key]}`;
58
+ });
59
+ for (const [k, v] of Object.entries(updates))
60
+ if (!seen.has(k))
61
+ out.push(`${k}=${v}`);
62
+ writeFileSync(ENV_PATH, out.join("\n"));
63
+ }
64
+ export const appImage = (e = readEnv()) => `${e.REGISTRY}/${e.APP_NAME}:${e.APP_VERSION}`;
65
+ export const deployRef = (e = readEnv()) => `${e.REGISTRY}/${e.APP_NAME}-deploy:${e.APP_VERSION}`;
66
+ /** The app-ALONE deployment, pointed at a chain that already exists — from
67
+ * `docker-compose.remote.yml`. */
68
+ export const remoteDeployRef = (e = readEnv()) => `${e.REGISTRY}/${e.APP_NAME}-deploy-remote:${e.APP_VERSION}`;
69
+ /** The app PLUS ITS OWN NODE, that node joining devnet — from `docker-compose.devnet.yml`.
70
+ *
71
+ * Three refs because they are three different things to hand someone, and the difference is
72
+ * not cosmetic: one carries a throwaway chain, one borrows somebody else's node on a real
73
+ * chain, and one brings a node of its own onto that real chain (which is the only shape that
74
+ * can read an ordered channel stream or join a DA overlay). */
75
+ export const devnetDeployRef = (e = readEnv()) => `${e.REGISTRY}/${e.APP_NAME}-deploy-devnet:${e.APP_VERSION}`;
76
+ /** Identity names are `^[A-Za-z0-9]{1,50}$` — no hyphens, dots or underscores — so an app
77
+ * name cannot be used verbatim. Strip it to what a node id may contain and bound it.
78
+ *
79
+ * This exists because the identity names used to be hardcoded `tmplA`/`tmplB`: rename your
80
+ * app to anything you like and it still claimed identities named after the template, which
81
+ * on a SHARED chain like devnet is the one place provenance matters. Empty after stripping
82
+ * (a name of only separators) falls back to `app` rather than producing `A`/`B`. */
83
+ export function identityPrefix(appName) {
84
+ const stripped = String(appName ?? "").replace(/[^A-Za-z0-9]/g, "").slice(0, 40);
85
+ return stripped || "app";
86
+ }
@@ -0,0 +1,90 @@
1
+ // Wait until the target's node is up AND its chain is actually committing.
2
+ //
3
+ // "The container is running" is not the same as "the chain is usable": the node generates
4
+ // keys, writes genesis, migrates two databases and starts consensus before it will accept a
5
+ // transaction. Polling for a COMMITTED round is the only honest readiness signal — a node
6
+ // that answers /status can still refuse every write with `not-ready`.
7
+ //
8
+ // It resolves the node URL through src/env.ts, the SAME place the app does. It used to read
9
+ // EVOLVER_NODE directly and default to localhost, which made it target-BLIND: the published
10
+ // image runs this before the app, so `docker run -e EVOLVER_TARGET=devnet <image>` polled
11
+ // its own localhost for 180 s and exited before the app ever started.
12
+ //
13
+ // An OPTIONAL argument PINS the chain, and the local-chain scripts pass `local`. They have
14
+ // to: once the default chain can be a remote one, a bare currentTarget() here makes
15
+ // `npm run chain:up` boot the local node and then report the REMOTE chain's tip as its
16
+ // readiness — green while nothing local is up at all. The no-argument form is still what the
17
+ // container runs, where the environment names the chain and the answer must follow it.
18
+ import { currentTarget, isTargetName, targetByName, knownTargets, optionalEnv, TargetError } from "../src/env.js";
19
+ const want = process.argv[2];
20
+ let target;
21
+ try {
22
+ if (want && !isTargetName(want)) {
23
+ console.error(`unknown chain '${want}' — available by name: ${knownTargets().map((t) => t.name).join(", ")}`);
24
+ process.exit(1);
25
+ }
26
+ target = want ? targetByName(want) : currentTarget();
27
+ }
28
+ catch (e) {
29
+ if (e instanceof TargetError) {
30
+ console.error(`\n✗ ${e.message}\n`);
31
+ process.exit(1);
32
+ }
33
+ throw e;
34
+ }
35
+ // Through optionalEnv for the reason documented there: compose passes CHAIN_WAIT_MS through
36
+ // as present-and-empty, `Number("")` is 0, and a 0 ms deadline means the loop below never
37
+ // runs even once — the app then reports "chain not ready after 0s" about a chain it never
38
+ // asked. Junk is treated the same way as absent, because "unparseable" must not silently
39
+ // become "do not wait at all".
40
+ const DEADLINE_MS = (() => {
41
+ const raw = optionalEnv("CHAIN_WAIT_MS");
42
+ const n = raw === undefined ? NaN : Number(raw);
43
+ if (raw !== undefined && !(Number.isFinite(n) && n > 0))
44
+ console.error(`(ignoring CHAIN_WAIT_MS='${raw}' — not a positive number of milliseconds)`);
45
+ return Number.isFinite(n) && n > 0 ? n : 180_000;
46
+ })();
47
+ if (process.env.EVOLVER_INSECURE_TLS === "1")
48
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
49
+ const started = Date.now();
50
+ let lastErr = "never reached";
51
+ while (Date.now() - started < DEADLINE_MS) {
52
+ try {
53
+ const res = await fetch(`${target.nodeUrl}/api/explorer/blocks?limit=1`, {
54
+ headers: { Accept: "application/json" },
55
+ });
56
+ if (res.ok) {
57
+ const b = (await res.json());
58
+ const round = b?.blocks?.[0]?.roundNum ?? 0;
59
+ if (round > 0) {
60
+ console.log(`chain ready — ${target.nodeUrl}, committed round ${round}`);
61
+ process.exit(0);
62
+ }
63
+ lastErr = "tip still round 0 (consensus started, nothing committed yet)";
64
+ }
65
+ else {
66
+ lastErr = `HTTP ${res.status}`;
67
+ }
68
+ }
69
+ catch (e) {
70
+ lastErr = e instanceof Error ? e.message.split("\n")[0] : String(e);
71
+ }
72
+ await new Promise((r) => setTimeout(r, 2000));
73
+ }
74
+ // Advice depends on WHERE this is running: inside the published image there is no npm
75
+ // project and no docker compose, so the host-side commands would be a dead end.
76
+ const inContainer = process.env.EVOLVER_IN_CONTAINER === "1";
77
+ const advice = inContainer
78
+ ? [
79
+ ` · this container waits for target '${target.name}' at ${target.nodeUrl}`,
80
+ ` · pointing at a remote chain? docker run -e EVOLVER_TARGET=devnet ...`,
81
+ ` · running the bundled chain? the 'node' service must be healthy first`,
82
+ ]
83
+ : [
84
+ ` · docker compose ps is the node running?`,
85
+ ` · docker compose logs node what did it say?`,
86
+ ` · npm run reset wipe chain + local state and start over`,
87
+ ];
88
+ console.error(`\n✗ chain not ready after ${Math.round(DEADLINE_MS / 1000)}s at ${target.nodeUrl} ` +
89
+ `(target '${target.name}') — last: ${lastErr}\n` + advice.join("\n") + "\n");
90
+ process.exit(1);