cortad 0.3.11 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/stores.mjs DELETED
@@ -1,433 +0,0 @@
1
- // Your app's database servers, kept out of the run's way. A run creates people, conversations and
2
- // records by the hundred; an app pointed at Postgres, Redis, MongoDB, Elasticsearch or a Qdrant
3
- // collection kept every one of them. So each store the app's settings name on this machine is copied
4
- // on its own server under a name of ours, or, where the app names its databases in its code, as a
5
- // whole server in a container of ours (lib/container.mjs); the app is started pointed at the copy
6
- // through the same settings, and the copy is deleted when this command ends. The original is only
7
- // ever read. A store no copy is made of holds Run until the person says yes to writing into it. A
8
- // store off this machine is copied where it can be (lib/hosted.mjs) and otherwise holds Run.
9
- import { createHash } from "node:crypto";
10
- import { copyName, removeCopy } from "./container.mjs";
11
- import { serveElastic } from "./elastic.mjs";
12
- import { hostedKeeper } from "./hosted.mjs";
13
- import { ledger } from "./ledger.mjs";
14
- import { cloneMongo, dropMongo, serveMongo, withDb } from "./mongo.mjs";
15
- import { clonePg, dropPg } from "./pg.mjs";
16
- import { cloneQdrant, dropQdrant } from "./qdrant.mjs";
17
- import { cloneRedis, dropRedis } from "./redis.mjs";
18
-
19
- export const ENGINE = { postgres: "Postgres", mysql: "MySQL", mongo: "MongoDB", redis: "Redis", qdrant: "Qdrant", chroma: "Chroma", milvus: "Milvus", weaviate: "Weaviate", elasticsearch: "Elasticsearch", meilisearch: "Meilisearch", typesense: "Typesense", neo4j: "Neo4j", sqlserver: "SQL Server", supabase: "Supabase", pinecone: "Pinecone", firestore: "Firestore" };
20
- // Where each listens when nothing else is said. Also how a connection the hook saw is named.
21
- export const PORT_OF = { postgres: 5432, mysql: 3306, mongo: 27017, redis: 6379, qdrant: 6333, chroma: 8000, milvus: 19530, weaviate: 8080, elasticsearch: 9200, meilisearch: 7700, typesense: 8108, neo4j: 7687, sqlserver: 1433 };
22
- // A connection is named by its port only where the port is the store's own: 8000 and 8080 are
23
- // every other web server's too.
24
- const ENGINE_AT = { ...Object.fromEntries(Object.entries(PORT_OF).filter(([e]) => e !== "chroma" && e !== "weaviate").map(([e, p]) => [p, e])), 6334: "qdrant" };
25
- export const isStorePort = (port) => Boolean(ENGINE_AT[port]);
26
- // Stores reached through their maker's SDK, which a setting rarely names: the hook sees the host.
27
- const HOSTED = [["supabase", /\.supabase\.(?:co|in|com)$/], ["mongo", /\.mongodb\.net$/], ["elasticsearch", /\.elastic-cloud\.com$|\.found\.io$|\.es\.amazonaws\.com$/], ["redis", /\.upstash\.io$|\.redis-cloud\.com$|\.redislabs\.com$/], ["postgres", /\.neon\.tech$/], ["pinecone", /\.pinecone\.io$/], ["qdrant", /\.qdrant\.io$/], ["firestore", /^firestore\.googleapis\.com$/]];
28
- // How each store we copy is copied and dropped: on its own server, under the name given.
29
- const COPY = {
30
- postgres: { copy: (s, clone) => clonePg(s.conn, s.db, clone), drop: (s, clone) => dropPg(s.conn, clone) },
31
- redis: { copy: (s, clone, marker) => cloneRedis(s.conn, s.db, marker), drop: (s, clone, marker) => dropRedis(s.conn, clone, marker) },
32
- mongo: { copy: (s, clone) => cloneMongo(s.conn, s.db, clone), drop: (s, clone) => dropMongo(s.conn, clone, s.db) },
33
- qdrant: { copy: (s, clone) => cloneQdrant(s.conn, s.db, clone), drop: (s, clone) => dropQdrant(s.conn, clone) },
34
- };
35
- // Stores whose app names its databases in its code are copied as a whole server instead.
36
- const SERVER = { mongo: (s, name) => serveMongo(s.conn, name), elasticsearch: (s, name) => serveElastic(s.conn, name) };
37
- const TOOLS = { postgres: "Postgres's own psql and pg_dump are", mongo: "MongoDB's own mongodump, mongorestore and mongosh are" };
38
-
39
- const SCHEME = /^(postgres(?:ql)?|mysql|mariadb|mongodb|rediss?|valkeys?|sqlserver|mssql)((?:\+\w+)*):\/\/([^/?#]*)(\/[^?#]*)?([?#].*)?$/i;
40
- const SCHEME_ENGINE = { postgres: "postgres", postgresql: "postgres", mysql: "mysql", mariadb: "mysql", mongodb: "mongo", redis: "redis", rediss: "redis", valkey: "redis", valkeys: "redis", sqlserver: "sqlserver", mssql: "sqlserver" };
41
- // A search or vector store is named by what its setting is called, since it speaks plain HTTP.
42
- const BY_NAME = [["qdrant", /QDRANT/], ["chroma", /CHROMA/], ["milvus", /MILVUS|ZILLIZ/], ["weaviate", /WEAVIATE/], ["elasticsearch", /ELASTIC|OPENSEARCH|^ES_/], ["meilisearch", /MEILI/], ["typesense", /TYPESENSE/], ["neo4j", /NEO4J/], ["supabase", /SUPABASE/]];
43
- const WORD_ENGINE = [["postgres", /^(?:PG|POSTGRES|PSQL)|POSTGRES/], ["mysql", /MYSQL|MARIA/], ["mongo", /MONGO/], ["redis", /REDIS|VALKEY/]];
44
- const SECRETISH = /(?:^|_)(?:KEY|TOKEN|SECRET|PASSWORD|PASS|PWD)$/;
45
- // Every engine's word in a setting's name, so a setting that carries one is that engine's own.
46
- const ENGINE_WORD = new RegExp([...Object.keys(ENGINE), ...[...BY_NAME, ...WORD_ENGINE].map(([, re]) => re.source)].join("|"), "i");
47
- // A settings file's values without the ones that name a store. An app the person started with
48
- // their own command is handed the rest: which databases it uses is that command's to say.
49
- const STORE_SETTING = new RegExp(`${ENGINE_WORD.source}|(?:^|_)(?:DB|DATABASE)(?:_|$)`, "i");
50
- export const withoutStoreSettings = (values) => Object.fromEntries(Object.entries(values).filter(([name]) => !STORE_SETTING.test(name)));
51
- export const storeSettings = (values) => Object.fromEntries(Object.entries(values).filter(([name]) => STORE_SETTING.test(name)));
52
- // Stores that hold no collections: a bare COLLECTION setting beside one of them is still Qdrant's.
53
- const NO_COLLECTIONS = new Set(["redis", "mysql", "sqlserver", "neo4j", "meilisearch", "pinecone"]);
54
- const LOOPBACK = /^(?:localhost|127(?:\.\d+){3}|::1|\[::1\]|0\.0\.0\.0|host\.docker\.internal)?$/i;
55
- // A socket path (host=/var/run/postgresql) is this machine too.
56
- export const onThisMachine = (host) => LOOPBACK.test(String(host ?? "")) || String(host).startsWith("/");
57
- // A Neon endpoint's pooled host is the same database as its direct one.
58
- const hostKey = (host) => (onThisMachine(host) ? "localhost" : String(host).toLowerCase().replace(/^(ep-[a-z0-9-]+?)-pooler\./, "$1."));
59
- const at = (s) => `${s.engine}|${hostKey(s.host)}|${s.port}`;
60
-
61
- // A part that is no percent-encoding (%DB%) is kept as its maker wrote it.
62
- const dec = (s) => { try { return decodeURIComponent(s); } catch { return s; } };
63
- const authority = (a) => {
64
- const cut = a.lastIndexOf("@");
65
- const cred = cut >= 0 ? a.slice(0, cut) : "";
66
- // A replica set lists its hosts; the first answers for all of them.
67
- const hostPort = (cut >= 0 ? a.slice(cut + 1) : a).split(",")[0];
68
- const m = /^(\[[^\]]*\]|[^:]*)(?::(\d+))?$/.exec(hostPort);
69
- if (!m) return null;
70
- const [user, ...pass] = cred ? cred.split(":") : [];
71
- return { host: m[1].replace(/^\[|\]$/g, ""), port: m[2] ? Number(m[2]) : null, user: user ? dec(user) : "", password: pass.length ? dec(pass.join(":")) : "" };
72
- };
73
-
74
- // A template the code fills (postgresql://%s:%s@localhost/%s, {}:{}) names no user or database of
75
- // its own: tried, it would be refused for a password the settings never carried.
76
- const TEMPLATE = /%s|%\(\w+\)s|\$\{\w+|\{\w*\}/;
77
-
78
- // One setting that is a whole address: postgresql+asyncpg://u:p@localhost:5432/app, redis://localhost/2.
79
- function fromUrl(name, value) {
80
- const m = SCHEME.exec(value);
81
- if (!m || TEMPLATE.test(value)) return null;
82
- const engine = SCHEME_ENGINE[m[1].toLowerCase()];
83
- const a = authority(m[3]);
84
- if (!a) return null;
85
- const query = new URLSearchParams((m[5] ?? "").replace(/^[?#]/, ""));
86
- const path = dec((m[4] ?? "").replace(/^\//, ""));
87
- const db = engine === "redis" ? Number(path || 0) : path;
88
- const host = a.host || query.get("host") || "";
89
- const port = a.port ?? PORT_OF[engine] ?? null;
90
- return {
91
- engine, host, port, db,
92
- conn: { host, port, user: a.user, password: a.password, sslmode: query.get("sslmode") ?? "", tlsOn: /^(?:rediss|valkeys)$/i.test(m[1]), uri: value },
93
- point: engine === "mongo" ? (to) => ({ [name]: withDb(value, to, db) }) : (to) => ({ [name]: `${m[1]}${m[2]}://${m[3]}/${encodeURIComponent(String(to))}${m[5] ?? ""}` }),
94
- // The same address on another server: what a copy of the whole server is reached through.
95
- move: (to, toPort) => ({ [name]: `${m[1]}${m[2]}://${m[3].slice(0, m[3].lastIndexOf("@") + 1)}${to}:${toPort}${m[4] ?? ""}${m[5] ?? ""}` }),
96
- };
97
- }
98
-
99
- // A search or vector store's address: QDRANT_URL=http://localhost:6333, MILVUS_URI=localhost:19530,
100
- // ELASTICSEARCH_URL=http://elastic:pw@localhost:9200.
101
- function fromNamed(name, value) {
102
- const engine = BY_NAME.find(([, re]) => re.test(name))?.[0];
103
- if (!engine || SECRETISH.test(name)) return null;
104
- const m = /^(?:(https?|grpc|bolt|neo4j):\/\/)?(?:([^@/\s]*)@)?([\w.-]+|\[[:\w]+\]):?(\d+)?([/?#].*)?$/i.exec(value);
105
- if (!m || (!m[1] && !m[4])) return null;
106
- const host = m[3].replace(/^\[|\]$/g, "");
107
- const port = m[4] ? Number(m[4]) : m[1] === "https" ? 443 : PORT_OF[engine] ?? 80;
108
- const [user, ...pass] = m[2] ? m[2].split(":") : [];
109
- return {
110
- engine, host, port, db: null,
111
- conn: { base: `${m[1] === "https" ? "https" : "http"}://${host.includes(":") ? `[${host}]` : host}:${port}`, port, ...(user ? { user: dec(user), password: dec(pass.join(":")) } : {}) },
112
- // A copy of the whole server speaks plain HTTP on this machine.
113
- move: (to, toPort) => ({ [name]: `${m[1] ? "http://" : ""}${m[2] ? `${m[2]}@` : ""}${to}:${toPort}${m[5] ?? ""}` }),
114
- };
115
- }
116
-
117
- // A search server's sign-in sits in settings beside its address: ELASTIC_PASSWORD, ES_API_KEY.
118
- const BY_NAME_ELASTIC = BY_NAME.find(([e]) => e === "elasticsearch")[1];
119
- function withLogin(store, values) {
120
- if (store.conn.user) return store;
121
- const own = (re) => values[Object.keys(values).find((n) => BY_NAME_ELASTIC.test(n) && re.test(n) && values[n])];
122
- const apiKey = own(/API_?KEY$/);
123
- const password = own(/(?:PASS|PASSWORD)$/);
124
- if (apiKey) return { ...store, conn: { ...store.conn, apiKey } };
125
- return password ? { ...store, conn: { ...store.conn, user: own(/USER(?:NAME)?$/) ?? "elastic", password } } : store;
126
- }
127
-
128
- // A vector store holds collections, and the app's settings name the ones it uses: QDRANT_COLLECTION,
129
- // COLLECTION_NAME. Each is its own store, pointed at its copy through that setting. A setting that
130
- // carries another engine's word (MONGODB_COLLECTION) is that engine's, and a bare one is this
131
- // store's only when no other store the settings name could own it. The key is the setting beside
132
- // it that ends in KEY.
133
- function withCollections(store, values, others) {
134
- const word = new RegExp(store.engine, "i");
135
- const shared = others.some((e) => e !== store.engine && !NO_COLLECTIONS.has(e));
136
- const key = Object.keys(values).find((n) => word.test(n) && /KEY$/.test(n) && values[n]);
137
- // QDRANT_HOST and QDRANT_PORT name the same server QDRANT_URL does.
138
- const base = store.conn.base ?? `http://${store.host.includes(":") ? `[${store.host}]` : store.host}:${store.port}`;
139
- const conn = { ...store.conn, base, ...(key ? { apiKey: values[key] } : {}) };
140
- const named = Object.keys(values).filter((n) => /COLLECTION/.test(n) && !SECRETISH.test(n) && String(values[n] ?? "").trim() && (word.test(n) || (!ENGINE_WORD.test(n) && !shared)));
141
- if (!named.length) return [{ ...store, conn }];
142
- return named.map((n) => ({ ...store, conn, db: String(values[n]).trim(), names: [...store.names, n], point: (to) => ({ [n]: String(to) }) }));
143
- }
144
-
145
- // MongoDB's tools take one address: the parts' own, written as one.
146
- const mongoUri = (host, port, user, password) => `mongodb://${user ? `${encodeURIComponent(user)}:${encodeURIComponent(password ?? "")}@` : ""}${host || "127.0.0.1"}:${port}/`;
147
-
148
- // Settings that name one store in parts, grouped by what they share before the part:
149
- // POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB; DB_HOST, DB_NAME, DB_CONNECTION=pgsql; PGHOST, PGDATABASE.
150
- const HOST_PART = /^(.+?)_?(?:HOST(?:NAME)?|SERVER)$/;
151
- const PARTS = { port: /^PORT$/, user: /^USER(?:NAME)?$/, password: /^(?:PASS(?:WORD)?|PWD)$/, db: /^(?:DB|DATABASE|DBNAME|DB_?NAME|DATABASE_?NAME|NAME|INDEX)$/, kind: /^(?:ENGINE|DRIVER|DIALECT|CONNECTION|TYPE|CLIENT)$/ };
152
- const KIND_ENGINE = [["postgres", /pg|postgres/], ["mysql", /mysql|maria/], ["mongo", /mongo/], ["redis", /redis|valkey/]];
153
- function fromParts(values) {
154
- const hosts = new Map();
155
- for (const name of Object.keys(values)) { const m = HOST_PART.exec(name); if (m && !hosts.has(m[1])) hosts.set(m[1], name); }
156
- // libpq's own settings need no host: without one it is the local socket.
157
- if (!hosts.has("PG") && ["PGDATABASE", "PGUSER", "PGPORT"].some((n) => values[n])) hosts.set("PG", null);
158
- const out = [];
159
- for (const [p, hostName] of hosts) {
160
- // A host setting left blank is a store the app is set up not to use (QDRANT_HOST= beside QDRANT_PATH).
161
- if (hostName && !String(values[hostName] ?? "").trim()) continue;
162
- // A host setting that holds a whole address (REDIS_HOST=redis://...) is fromUrl's, not a host.
163
- if (hostName && String(values[hostName]).includes("://")) continue;
164
- const parts = {};
165
- for (const name of Object.keys(values)) {
166
- if (!name.startsWith(p) || name === hostName) continue;
167
- const rest = name.slice(p.length).replace(/^_/, "");
168
- const part = Object.keys(PARTS).find((k) => PARTS[k].test(rest));
169
- if (part && !parts[part]) parts[part] = name;
170
- }
171
- const [host = "", inline] = String(hostName ? values[hostName] : "").split(":");
172
- const named = Number(values[parts.port] || inline) || null;
173
- const kind = String(values[parts.kind] ?? "").toLowerCase();
174
- const engine = WORD_ENGINE.find(([, re]) => re.test(p))?.[0] ?? KIND_ENGINE.find(([, re]) => re.test(kind))?.[0] ?? ENGINE_AT[named];
175
- if (!engine) continue;
176
- const port = named ?? PORT_OF[engine];
177
- const db = parts.db ? (engine === "redis" ? Number(values[parts.db] || 0) : values[parts.db]) : engine === "redis" ? 0 : "";
178
- out.push({
179
- engine, host, port, db,
180
- conn: { host, port, user: values[parts.user] ?? "", password: values[parts.password] ?? "", sslmode: p === "PG" ? values.PGSSLMODE ?? "" : "", ...(engine === "mongo" ? { uri: mongoUri(host, port, values[parts.user], values[parts.password]) } : {}) },
181
- names: [hostName ?? parts.user ?? parts.port, parts.db].filter(Boolean),
182
- // Only a setting that holds the database can point the app at another one.
183
- point: parts.db ? (to) => ({ [parts.db]: String(to) }) : null,
184
- move: !hostName ? null : parts.port ? (to, toPort) => ({ [hostName]: to, [parts.port]: String(toPort) }) : inline ? (to, toPort) => ({ [hostName]: `${to}:${toPort}` }) : null,
185
- });
186
- }
187
- return out;
188
- }
189
-
190
- // Every store the app's settings name, one entry per database however many settings name it
191
- // (DATABASE_URL and DIRECT_URL, or DATABASE_URL beside POSTGRES_HOST and POSTGRES_DB). A setting
192
- // that names the server but no database (POSTGRES_USER for a container's first start) is the same
193
- // store as the one database its settings do name there.
194
- export function storesOf(values) {
195
- const found = [];
196
- for (const [name, raw] of Object.entries(values)) {
197
- const value = String(raw ?? "").trim();
198
- if (!value) continue;
199
- const url = fromUrl(name, value);
200
- if (url) { found.push({ ...url, names: [name] }); continue; }
201
- const named = fromNamed(name, value);
202
- if (named) found.push({ ...named, names: [name], point: null });
203
- }
204
- found.push(...fromParts(values));
205
- const engines = found.map((s) => s.engine);
206
- const each = found.flatMap((s) => (s.engine === "qdrant" ? withCollections(s, values, engines) : s.engine === "elasticsearch" ? [withLogin(s, values)] : [s]));
207
- const byKey = new Map();
208
- const merge = (held, s) => {
209
- held.names = [...new Set([...held.names, ...s.names])];
210
- if (s.point) held.points.push(s.point);
211
- if (s.move) held.moves.push(s.move);
212
- held.conn ??= s.conn;
213
- };
214
- const whole = each.filter((s) => s.db !== "" && s.db !== null);
215
- for (const s of [...whole, ...each.filter((x) => !whole.includes(x))]) {
216
- const key = `${at(s)}|${s.db ?? ""}`;
217
- const held = byKey.get(key) ?? (s.db === "" || s.db === null ? [...byKey.values()].find((h) => at(h) === at(s)) : undefined);
218
- if (held) { merge(held, s); continue; }
219
- byKey.set(key, { ...s, key, points: s.point ? [s.point] : [], moves: s.move ? [s.move] : [] });
220
- }
221
- return [...byKey.values()];
222
- }
223
-
224
- // At most three by name, so a sentence stays one sentence.
225
- const listed = (names) => (names.length === 1 ? names[0] : names.length <= 3 ? `${names.slice(0, -1).join(", ")} and ${names.at(-1)}` : `${names.slice(0, 2).join(", ")} and ${names.length - 2} more settings`);
226
- const unit = (s) => (s.engine === "qdrant" ? "collection" : "database");
227
- const whatOf = (s) => (s.engine === "redis" ? `Redis database ${s.db}` : s.db ? `${ENGINE[s.engine]} ${unit(s)} ${s.db}` : `${ENGINE[s.engine]} server`);
228
- const copied = [...new Set([...Object.keys(COPY), ...Object.keys(SERVER)])].map((e) => ENGINE[e]);
229
- const copiedList = `${copied.slice(0, -1).join(", ")} and ${copied.at(-1)}`;
230
-
231
- // Why no copy was made of a store on this machine, in a sentence of theirs, and the one thing to do.
232
- // The store's own words are never quoted: they name users and hosts.
233
- const HERE = {
234
- started: () => ["your app was already running when this command started", "Stop your app"],
235
- engine: (s) => [`we copy ${copiedList} for a run, not ${ENGINE[s.engine]}`, `Point ${s.names[0]} at a scratch ${s.db ? unit(s) : "server"}`],
236
- fixed: (s) => [`no setting names its ${s.engine === "redis" ? "database number" : `${unit(s)} name`} to point the app at a copy`, `Put the ${s.engine === "redis" ? "database number" : `${unit(s)} name`} in a setting`],
237
- auth: (s) => ["the password its settings carry was refused", `Check ${s.names[0]}`],
238
- rights: () => ["the user it signs in as may not create databases", "Give that user the right to create databases"],
239
- down: () => ["nothing answered at the address its settings give", "Start it"],
240
- big: () => ["it is over the size a run copies", "Point it at a smaller scratch copy"],
241
- missing: (s) => [`${TOOLS[s.engine] ?? "its own tools are"} not on this machine's PATH`, "Install them"],
242
- version: () => ["this machine's copy of the store's own tools is older than the server", "Update them"],
243
- oldServer: () => ["the server is older than Redis 6.2, which a copy needs", "Upgrade it"],
244
- full: () => ["it has no empty database left to copy into", "Empty one of its databases"],
245
- cluster: () => ["it runs as a cluster, which has one database only", "Point it at a scratch server"],
246
- docker: () => ["a copy of it is made in Docker, which is not running on this machine", "Start Docker"],
247
- login: (s) => ["your app signs in to it in a way a copy of it cannot be given", `Point ${s.names[0]} at a scratch server, or sign in with a user and password`],
248
- container: () => ["Docker could not start a copy of it", "Check that Docker can pull and run its image"],
249
- other: (s) => ["the copy did not go through", `Point ${s.names[0]} at a scratch ${s.db ? unit(s) : "server"}`],
250
- };
251
- const heldHere = (s, why) => {
252
- const [reason, fix] = (HERE[why] ?? HERE.other)(s);
253
- return `${listed(s.names)} ${s.names.length === 1 ? "points" : "point"} at the ${whatOf(s)} on this machine, and ${reason}, so test conversations would save into it. ${fix}, then run the connect command again.`;
254
- };
255
- const kept = (s, clone, how) => (s.engine === "redis"
256
- ? `Your app runs against a copy of its Redis data in database ${clone} of the same server, through ${listed(s.names)}; database ${s.db} is not touched, and database ${clone} is emptied when this command ends and copied again at the next connect.`
257
- : how === "empty"
258
- ? `Your app makes its ${ENGINE[s.engine]} ${unit(s)} ${s.db} under the name ${clone} on the same server, through ${listed(s.names)}; the server holds no ${s.db} yet, and ${clone} is deleted when this command ends and made again empty at the next connect.`
259
- : `Your app runs against a copy of its ${ENGINE[s.engine]} ${unit(s)} ${s.db}, made on the same server, through ${listed(s.names)}; the original is not touched, and the copy is deleted when this command ends and made again from the original at the next connect.`);
260
- const serverKept = (s) => `Your app runs against a copy of its whole ${ENGINE[s.engine]} server, made in a Docker container of ours, through ${listed(s.names)}; the original is not touched, and the container is deleted when this command ends and made again from the original at the next connect.`;
261
-
262
- // What holds Run for a store off this machine no copy could be made of, and the one thing to do.
263
- // The first reason is the most useful: a Neon store with no key says the key.
264
- const HELD = {
265
- "neon-key": (s) => ["there is no NEON_API_KEY in its settings to make a branch with", `Set NEON_API_KEY, or point ${s.names[0]} at a database on this machine`],
266
- neon: (s) => ["Neon did not make a branch of it", `Check NEON_API_KEY, or point ${s.names[0]} at a database on this machine`],
267
- supabase: (s) => ["a local Supabase needs supabase/config.toml, Docker and the supabase command", `Add them, or point ${s.names[0]} at a local Supabase`],
268
- engine: (s) => [`no copy of a hosted ${ENGINE[s.engine]} is made`, `Point ${s.names[0]} at a ${ENGINE[s.engine]} on this machine`],
269
- parts: (s) => ["its settings name it in parts, which cannot be pointed at a copy", `Put its whole address in one setting, or point ${s.names[0]} at a database on this machine`],
270
- "no-migrations": (s) => ["no migrations were found to build a copy on this machine from", `Point ${s.names[0]} at a database on this machine`],
271
- "no-local": (s) => ["no Postgres on this machine let us make a database to build a copy in", "Start Postgres on this machine"],
272
- migrations: (s, tool) => [`its ${tool} migrations did not run against a copy on this machine`, `Run them against a local database once by hand, or point ${s.names[0]} at one`],
273
- started: () => ["your app was already running when this command started", "Stop your app"],
274
- bypassed: () => ["your app reached it anyway, since it loads its settings file over the settings it was started with", "Load that file without overriding what is already set"],
275
- other: (s) => ["the copy did not go through", `Point ${s.names[0]} at a database on this machine`],
276
- };
277
- // The fact and the way to keep the store out of the run, read by the person on the card and by their
278
- // agent in status. The other way, the owner's yes, is each surface's own to offer: the card as its
279
- // button, status in the sentence after this one (src/local/contact.ts heldLine). Written in here, the
280
- // card told the owner, on the card, that "the owner can say yes to it on the card in the browser".
281
- const heldWhy = (s, why, tool) => {
282
- const [reason, fix] = (HELD[why] ?? HELD.other)(s, tool);
283
- return `${listed(s.names)} ${s.names.length === 1 ? "points" : "point"} at ${s.engine === "redis" || s.db ? "the" : "a"} ${whatOf(s)} that is not on this machine, and ${reason}, so test conversations would save into it. ${fix}, then run the connect command again.`;
284
- };
285
- // A Qdrant server the app writes into is kept by the hook in the app instead of by a copy made here:
286
- // the first write a run makes into a collection copies that collection on the same server and the
287
- // app's later calls go to the copy (lib/trace.cjs, lib/pyhook/sitecustomize.py). So a hosted Qdrant
288
- // holds nothing, and neither does a local one whose collection no setting names.
289
- export const GUARDED_ENGINES = new Set(["qdrant"]);
290
- const guardedKept = (s) => `Your app's ${ENGINE[s.engine]} collections at ${listed(s.names)} are not written into by a run: the first time a run writes into one, that collection is copied on the same server and the app's calls go to the copy, which is deleted when this command ends.`;
291
- const hostedKept = (s, got) => (got.how === "neon"
292
- ? `Your app runs against a Neon branch of its ${whatOf(s)}, made for this session through ${listed(s.names)}; the original is not touched, and the branch is deleted when this command ends and made again from the original at the next connect.`
293
- : got.how === "supabase"
294
- ? `Your app runs against a Supabase on this machine started from supabase/config.toml, through ${listed(s.names)}; the hosted one is not touched, and what test conversations create in the local one stays there${got.started ? ", and it is stopped when this command ends" : ""}.`
295
- : `Your app runs against an empty ${ENGINE[s.engine]} database on this machine built by its own ${got.tool} migrations, through ${listed(s.names)}; the hosted one is not touched, and the copy is deleted when this command ends and built again empty at the next connect.`);
296
- // How the person's yes names a store off this machine: never its address.
297
- export const storeId = (s) => createHash("sha256").update(s.key).digest("hex").slice(0, 16);
298
- // A store off this machine the app was pointed at a copy of and reached anyway (dotenv's override):
299
- // trials write into the original, so it holds Run. `blocked`: the rows already holding it.
300
- export const bypassedOf = (connections, stores, blocked) => stores
301
- .filter((s) => !onThisMachine(s.host) && !GUARDED_ENGINES.has(s.engine) && !blocked.some((b) => b.id === storeId(s)) && connections.some((c) => hostKey(c.host) === hostKey(s.host)))
302
- .map((s) => ({ id: storeId(s), names: s.names, why: heldWhy(s, "bypassed") }));
303
-
304
- // A copy that throws is one that did not go through, said as such; it never stops the app starting.
305
- const drop = (s, clone, marker) => COPY[s.engine].drop(s, clone, marker).catch(() => false);
306
- const copy = (s, clone, marker) => COPY[s.engine].copy(s, clone, marker).catch(() => ({ why: "other" }));
307
- const serve = (s, name) => SERVER[s.engine](s, name).catch(() => ({ why: "other" }));
308
- const undo = (s, row) => (row.kind === "server" ? removeCopy(row.made) : drop(s, row.made, row.marker));
309
- const cloneName = (db) => `${String(db).slice(0, 40)}_cortad_${process.pid}`;
310
- // Refusals a copy of the whole server would meet the same way.
311
- const SERVER_TOO = new Set(["auth", "big", "down"]);
312
-
313
- // One session's copies. keep() copies the stores given that answer and says what it did; a store
314
- // nothing answers at yet is left out, unsaid, since trials cannot write into it: once it is started
315
- // (lib/backing.mjs), keep() is asked again for it. drop() deletes every copy, and copies a session
316
- // that died left behind are deleted by the next one, through the ledger. A store with no copy is
317
- // `blocked`, and `pass` names the hosts of the copies made off this machine.
318
- // `values`, `dirs`, `fetchFn` and `local` are lib/hosted.mjs's.
319
- export function storeKeeper({ ledgerFile, values = {}, dirs = [], fetchFn, local }) {
320
- const book = ledger(ledgerFile);
321
- const hosted = hostedKeeper({ values, dirs, book, fetchFn, local });
322
- const made = [];
323
- const done = new Set();
324
- return {
325
- async keep(stores, { started = true, down = () => false } = {}) {
326
- for (const row of book.orphans("copy")) {
327
- const s = stores.find((x) => x.key === row.key);
328
- if (s && (await drop(s, row.made, row.marker))) book.remove(row);
329
- }
330
- for (const row of book.orphans("server")) if (await removeCopy(row.made)) book.remove(row);
331
- await hosted.orphans();
332
- const env = {};
333
- const copies = [];
334
- const blocked = [];
335
- const pass = [];
336
- for (const s of stores) {
337
- if (done.has(s.key)) continue;
338
- if (GUARDED_ENGINES.has(s.engine) && (!onThisMachine(s.host) || !s.points.length || !s.db)) {
339
- if (await down(s)) continue;
340
- done.add(s.key);
341
- copies.push(guardedKept(s));
342
- continue;
343
- }
344
- if (!onThisMachine(s.host)) {
345
- done.add(s.key);
346
- const got = started ? await hosted.keep(s).catch(() => ({ why: ["other"] })) : { why: ["started"] };
347
- if (!got.env) { blocked.push({ id: storeId(s), names: s.names, why: heldWhy(s, got.why[0], got.tool) }); continue; }
348
- Object.assign(env, got.env);
349
- pass.push(...got.pass);
350
- copies.push(hostedKept(s, got));
351
- continue;
352
- }
353
- if (await down(s)) continue;
354
- done.add(s.key);
355
- if (!started) { blocked.push({ id: storeId(s), names: s.names, why: heldHere(s, "started") }); continue; }
356
- let why = !COPY[s.engine] ? "engine" : !s.points.length || (s.engine !== "redis" && !s.db) ? "fixed" : null;
357
- if (!why) {
358
- const marker = `${process.pid}-${Date.now()}`;
359
- // Recorded before the copy starts, so a session killed mid-copy leaves a row the next one
360
- // undoes. A Redis copy's database is found by its marker then.
361
- const row = { kind: "copy", key: s.key, made: s.engine === "redis" ? null : cloneName(s.db), marker };
362
- book.add(row);
363
- const got = await copy(s, cloneName(s.db), marker);
364
- if (!got.why) {
365
- const clone = got.index ?? cloneName(s.db);
366
- made.push({ s, row });
367
- for (const point of s.points) Object.assign(env, point(clone));
368
- copies.push(kept(s, clone, got.how));
369
- continue;
370
- }
371
- if (await drop(s, row.made, marker)) book.remove(row);
372
- // A database the server does not hold is one trials cannot write into.
373
- if (got.why === "gone") continue;
374
- why = got.why;
375
- }
376
- // The app names its databases in its code, or this machine lacks the store's own tools: the
377
- // whole server is copied into a container of ours and the app's address moved to it.
378
- if (SERVER[s.engine] && s.moves.length && !SERVER_TOO.has(why)) {
379
- const row = { kind: "server", key: s.key, made: copyName(s.engine) };
380
- book.add(row);
381
- const got = await serve(s, row.made);
382
- if (got.port) {
383
- made.push({ s, row });
384
- for (const move of s.moves) Object.assign(env, move("127.0.0.1", got.port));
385
- copies.push(serverKept(s));
386
- continue;
387
- }
388
- if (await removeCopy(row.made)) book.remove(row);
389
- why = got.why;
390
- }
391
- blocked.push({ id: storeId(s), names: s.names, why: heldHere(s, why) });
392
- }
393
- return { env, copies, blocked, pass };
394
- },
395
- drop: async () => {
396
- for (const { s, row } of made.splice(0)) if (await undo(s, row)) book.remove(row);
397
- await hosted.drop();
398
- },
399
- // The collections the hook in the app copied for a run's writes (lib/trace.cjs copyOf), deleted
400
- // with the key the settings hold for that server. Each is in the ledger from the moment it is
401
- // seen, so one a killed session left is deleted by the next.
402
- // ponytail: a server no setting names a key for is asked without one; a copy that refuses stays
403
- // under its _cortad_ name for the person to see. The key the app sent never leaves the app.
404
- async dropCopies(copies, stores) {
405
- const rows = copies.map((c) => ({ kind: "qdrant-copy", key: c.base, made: c.clone }));
406
- for (const row of rows) book.add(row);
407
- const hostOf = (base) => { try { return new URL(base).hostname; } catch { return base; } };
408
- for (const row of [...rows, ...book.orphans("qdrant-copy")]) {
409
- // The hook names the server with its port and QDRANT_URL may not: the key is the host's.
410
- const conn = { base: row.key, apiKey: stores.find((s) => s.engine === "qdrant" && s.conn?.base && hostOf(s.conn.base) === hostOf(row.key))?.conn.apiKey };
411
- if (await dropQdrant(conn, row.made)) book.remove(row);
412
- }
413
- },
414
- };
415
- }
416
-
417
- // The stores the app reached that no setting names, as the hook saw each connection: a store written
418
- // into a config file rather than the environment, one its maker's SDK finds by itself, or the ones an
419
- // app started with the person's own command was given. No copy stands in for any of them, so each
420
- // holds Run for the person's yes, once. `blocked`: the rows already holding it.
421
- export function unnamedHeld(connections, stores, blocked) {
422
- const rows = [];
423
- for (const { host, port } of connections) {
424
- const local = onThisMachine(host);
425
- const engine = local ? ENGINE_AT[port] : HOSTED.find(([, re]) => re.test(String(host).toLowerCase()))?.[0] ?? ENGINE_AT[port];
426
- if (!engine || GUARDED_ENGINES.has(engine) || stores.some((s) => (local ? `${hostKey(s.host)}:${s.port}` === `localhost:${port}` : s.engine === engine && !onThisMachine(s.host)))) continue;
427
- const id = createHash("sha256").update(`${engine}|${local ? "localhost" : String(host).toLowerCase()}|${port}`).digest("hex").slice(0, 16);
428
- if ([...blocked, ...rows].some((b) => b.id === id)) continue;
429
- // Named by its engine where a setting's name would stand: the API takes a name, never an address.
430
- rows.push({ id, names: [ENGINE[engine].replace(/\W+/g, "_")], why: `Your app reaches ${/^[AEIOU]/.test(ENGINE[engine]) ? "an" : "a"} ${ENGINE[engine]} server ${local ? "on" : "off"} this machine that it was not given a copy of, so the run would write what its test conversations create into it as it is. Start your app on a copy of it, then run the connect command again.` });
431
- }
432
- return rows;
433
- }