memhouse 0.16.0 → 0.18.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/README.md +97 -31
- package/bin/memhouse.js +657 -187
- package/editors/antigravity.js +2 -0
- package/editors/claude.js +3 -1
- package/editors/codebuff.js +2 -0
- package/editors/codex.js +4 -1
- package/editors/commandcode.js +2 -1
- package/editors/copilot-jetbrains.js +2 -1
- package/editors/copilot.js +2 -1
- package/editors/cursor-agent.js +2 -1
- package/editors/cursor.js +2 -0
- package/editors/gemini.js +2 -1
- package/editors/goose.js +2 -1
- package/editors/index.js +8 -1
- package/editors/kiro.js +3 -2
- package/editors/opencode.js +2 -1
- package/editors/scope.js +126 -0
- package/editors/vscode.js +2 -0
- package/editors/zed.js +2 -1
- package/memhouse/DESIGN.md +31 -8
- package/memhouse/channel.js +50 -0
- package/memhouse/delivery/AGENT-INSTALL-PROMPT.md +4 -3
- package/memhouse/delivery/AGENT-INSTALL.md +3 -3
- package/memhouse/delivery/INVITATION.md.tpl +130 -0
- package/memhouse/delivery/PROMPT.md +21 -14
- package/memhouse/delivery/kernel-install.md +4 -2
- package/memhouse/delivery/plugin/.claude-plugin/plugin.json +2 -2
- package/memhouse/delivery/plugin/reference/HOUSE.md +173 -0
- package/memhouse/delivery/plugin/skills/access/SKILL.md +148 -0
- package/memhouse/delivery/plugin/skills/admin/SKILL.md +30 -1
- package/memhouse/delivery/plugin/skills/house/SKILL.md +99 -0
- package/memhouse/delivery/plugin/skills/recall/SKILL.md +119 -0
- package/memhouse/delivery/plugin/skills/sql/SKILL.md +73 -194
- package/memhouse/flags.js +6 -4
- package/memhouse/house/HOUSE.md +25 -11
- package/memhouse/house/house.js +110 -27
- package/memhouse/house/migrate.js +7 -6
- package/memhouse/house/relocate.js +2 -2
- package/memhouse/house/schema.sql.tpl +3 -3
- package/memhouse/provision.js +100 -0
- package/memhouse/share.js +64 -0
- package/memhouse/shipper/ship.js +87 -28
- package/package.json +1 -1
- package/memhouse/delivery/plugin/skills/ask/SKILL.md +0 -131
- package/memhouse/delivery/plugin/skills/hello/SKILL.md +0 -121
- package/memhouse/delivery/plugin/skills/invite/SKILL.md +0 -130
- package/memhouse/delivery/plugin/skills/search/SKILL.md +0 -186
- package/memhouse/delivery/plugin/skills/sessions/SKILL.md +0 -164
- package/memhouse/delivery/plugin/skills/share/SKILL.md +0 -130
- package/memhouse/delivery/plugin/skills/status/SKILL.md +0 -139
- package/memhouse/delivery/plugin/skills/users/SKILL.md +0 -97
package/memhouse/house/house.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
// The house.
|
|
2
2
|
//
|
|
3
|
-
// A house is a ClickHouse DATABASE
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// their own credential, and two columns say who and where every row came from:
|
|
3
|
+
// A house is a ClickHouse DATABASE, `mem` by default. Its rooms are tables — `sessions`,
|
|
4
|
+
// `messages`, `tool_calls` — one set per member, named for them, and two columns say who
|
|
5
|
+
// and where every row came from:
|
|
7
6
|
//
|
|
8
7
|
// user_id String MATERIALIZED currentUser() — stamped by the server, unforgeable by
|
|
9
8
|
// clients (async_insert is pinned to 0
|
|
@@ -12,19 +11,30 @@
|
|
|
12
11
|
// host LowCardinality(String) — the machine's fingerprint, minted once
|
|
13
12
|
// per install (../host.js)
|
|
14
13
|
//
|
|
15
|
-
// That pair IS the provenance model.
|
|
16
|
-
// both write; `WHERE user_id = 'alice'` is one person, `WHERE host = '…'` is one machine.
|
|
17
|
-
// The model is collaborative — housemates trust each other with the house — and the
|
|
18
|
-
// boundary between houses is the database: joining someone's ClickHouse takes a database
|
|
19
|
-
// and a credential (`GRANT ALL ON team_a.* TO alice`), nothing else, because ALL on your
|
|
20
|
-
// own database reaches nothing outside it.
|
|
14
|
+
// That pair IS the provenance model.
|
|
21
15
|
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
16
|
+
// ONE LAYOUT. A house is a database — `mem` unless somebody has a reason — and every
|
|
17
|
+
// member's rooms in it carry the member's name: `mem.alice_messages`, `mem.polat_messages`.
|
|
18
|
+
// A member holds one grant, on the wildcard `mem.<name>_*`, and nothing else in the
|
|
19
|
+
// database. That is the whole access model, and roomNames() below is the only place a
|
|
20
|
+
// table name is produced.
|
|
21
|
+
//
|
|
22
|
+
// alone on a laptop mem.polat_* a house of one
|
|
23
|
+
// a team on one server mem.polat_*, mem.alice_*
|
|
24
|
+
// an agency on a kernel the same, in the kernel's `mem`
|
|
25
|
+
//
|
|
26
|
+
// Nobody ever holds ALL ON <db>.*. That grant is dynamic — it covers rooms created later —
|
|
27
|
+
// and it is what turned two members in one database into a leak: either could read and
|
|
28
|
+
// re-grant the other's transcripts, measured. The wildcard reaches nothing outside the
|
|
29
|
+
// member's own name: measured, a member cannot create, read, list or re-grant a
|
|
30
|
+
// housemate's rooms, and can read their own grant back with SHOW GRANTS in one line.
|
|
31
|
+
//
|
|
32
|
+
// THIS FILE HAS SAID THREE OTHER THINGS. Shared tables with row policies (0.4.0: policies
|
|
33
|
+
// are permissive and OR'd, a catch-all fails open). Suffixed per-member rooms with Merge
|
|
34
|
+
// rooms over them (0.8.0: removed because clients had to resolve names first and
|
|
35
|
+
// isolation "solved a problem the product did not have" — the second half was wrong).
|
|
36
|
+
// Then a house per member with `ALL ON db.*`, plus a prefixed variant beside it, plus the
|
|
37
|
+
// machinery to keep the two from meeting. All three were special cases of this one.
|
|
28
38
|
//
|
|
29
39
|
// `sessions_v` IS A SAVED QUERY, NOT AN OBJECT — substituted into the same `FROM … AS c`
|
|
30
40
|
// position a view name would occupy, running under the caller's own credential. It was
|
|
@@ -38,7 +48,11 @@ const ROOM_TYPES = ['sessions', 'messages', 'tool_calls'];
|
|
|
38
48
|
* them, the shipper's guards do not require them, and a member who cannot create them
|
|
39
49
|
* still ships normally.
|
|
40
50
|
*/
|
|
41
|
-
|
|
51
|
+
// `<member>_meta` / `<member>_events` since the one-layout: every row in them is about ONE
|
|
52
|
+
// member (their schema version, their machines, their last ship, their shares), so the old
|
|
53
|
+
// `house_` prefix named the wrong unit. A pre-one-layout house still has `house_meta` /
|
|
54
|
+
// `house_events`; the shipper's legacy guard names the rename.
|
|
55
|
+
const META_TYPES = ['meta', 'events'];
|
|
42
56
|
|
|
43
57
|
/**
|
|
44
58
|
* What generation of the schema a house is at. Bumped only when existing rooms have to be
|
|
@@ -75,7 +89,7 @@ const SUPPORTED_SCHEMAS = [2];
|
|
|
75
89
|
|
|
76
90
|
/**
|
|
77
91
|
* The floor a house may set under its writers, recorded by `memhouse migrate` in
|
|
78
|
-
*
|
|
92
|
+
* meta['min_writer_schema']. Today it equals SCHEMA_VERSION; a future
|
|
79
93
|
* back-compatible generation can hold it one step lower for a grace window.
|
|
80
94
|
*/
|
|
81
95
|
const MIN_WRITER_SCHEMA = 2;
|
|
@@ -233,15 +247,19 @@ const ROOM_MATERIALIZED = {
|
|
|
233
247
|
tool_calls: ['user_id'],
|
|
234
248
|
};
|
|
235
249
|
|
|
236
|
-
function currentParse(table, epochSource = table) {
|
|
250
|
+
function currentParse(table, epochSource = table, physical = null) {
|
|
251
|
+
// `table` names the ROOM (for the materialized-column lookup); `physical` is what goes
|
|
252
|
+
// into the FROM, which differs whenever a prefix is in play.
|
|
237
253
|
const extra = (ROOM_MATERIALIZED[table] || ['user_id']).join(', ');
|
|
254
|
+
const from = physical || table;
|
|
255
|
+
const epochFrom = physical ? physical.replace(new RegExp(`${table}$`), epochSource) : epochSource;
|
|
238
256
|
return `(
|
|
239
257
|
SELECT *, ${extra}
|
|
240
|
-
FROM ${
|
|
258
|
+
FROM ${from} FINAL
|
|
241
259
|
WHERE origin != 'ship'
|
|
242
260
|
OR (session_id, user_id, epoch) IN (
|
|
243
261
|
SELECT session_id, user_id, max(epoch)
|
|
244
|
-
FROM ${
|
|
262
|
+
FROM ${epochFrom}
|
|
245
263
|
WHERE origin = 'ship'
|
|
246
264
|
GROUP BY session_id, user_id)
|
|
247
265
|
)`;
|
|
@@ -302,24 +320,54 @@ const READ_SETTINGS = { final: 1, join_use_nulls: 1 };
|
|
|
302
320
|
* name it produces. See ship.js's delete, which must BIND the user rather than call
|
|
303
321
|
* currentUser() inside a mutation, where it is not evaluated in the caller's context.
|
|
304
322
|
*/
|
|
323
|
+
/**
|
|
324
|
+
* The table a room type lands in. THE naming rule, and the only copy of it.
|
|
325
|
+
*
|
|
326
|
+
* ONE LAYOUT. Every member's rooms carry the member's own name: `mem.alice_messages`,
|
|
327
|
+
* and `mem.polat_messages` even when polat is the only one in there. A house with one
|
|
328
|
+
* member is a house of one, not a different kind of house — so there is no second case
|
|
329
|
+
* here, no prefix to configure, and nothing for an invite file to carry.
|
|
330
|
+
*
|
|
331
|
+
* Depends on nothing but the member, so a caller holding only a config can name a table
|
|
332
|
+
* without a server round-trip. That matters: when the rule lived only inside roomNames(),
|
|
333
|
+
* callers that could not afford `SELECT currentUser()` spelled tables by hand instead —
|
|
334
|
+
* eight of them, all swallowing the error — and a house silently had no metadata plane.
|
|
335
|
+
*/
|
|
336
|
+
function physicalRoom(type, user) {
|
|
337
|
+
if (!user) throw new Error(`physicalRoom(${type}): a room belongs to a member, and none was given`);
|
|
338
|
+
return `${user}_${type}`;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The wildcard every grant to a member is scoped to: `<member>_*`. */
|
|
342
|
+
function roomPattern(user) {
|
|
343
|
+
if (!user) throw new Error('roomPattern: a member is required');
|
|
344
|
+
return `${user}_*`;
|
|
345
|
+
}
|
|
346
|
+
|
|
305
347
|
function roomNames(user) {
|
|
306
|
-
|
|
348
|
+
if (!user) throw new Error('roomNames: a member is required');
|
|
349
|
+
const out = { member: user, user, pattern: roomPattern(user) };
|
|
350
|
+
// Every room name in the codebase comes from physicalRoom() above — through this
|
|
351
|
+
// function where a caller has the rooms, directly where it has only a member name.
|
|
352
|
+
const phys = (t) => physicalRoom(t, user);
|
|
353
|
+
out.physical = {};
|
|
354
|
+
for (const t of [...ROOM_TYPES, ...META_TYPES]) out.physical[t] = phys(t);
|
|
307
355
|
// The raw table names — for INSERT, for DDL, and for the shipper's own bookkeeping
|
|
308
356
|
// reads, which have to see every epoch to decide which one to write next.
|
|
309
|
-
for (const t of ROOM_TYPES) out[`${t}_raw`] = t;
|
|
357
|
+
for (const t of ROOM_TYPES) out[`${t}_raw`] = phys(t);
|
|
310
358
|
// What everything else gets. `sessions` is unfiltered: it is one metadata row per
|
|
311
359
|
// session by construction, latest-wins, and carries no epoch anyone may read.
|
|
312
|
-
out.sessions = 'sessions';
|
|
313
|
-
out.messages = currentParse('messages');
|
|
360
|
+
out.sessions = phys('sessions');
|
|
361
|
+
out.messages = currentParse('messages', 'messages', phys('messages'));
|
|
314
362
|
// tool_calls takes its epoch from MESSAGES, not from itself. A parse that produces
|
|
315
363
|
// messages but NO tool calls is ordinary — a compaction can remove every assistant turn
|
|
316
364
|
// that called something — and it writes zero rows into this room at the new epoch. Asked
|
|
317
365
|
// for its own max(epoch), the room would answer with the SUPERSEDED epoch and serve the
|
|
318
366
|
// old parse's tool calls beside the new parse's messages. Both rooms are written by the
|
|
319
367
|
// same pass at the same epoch, so messages is the authority for both.
|
|
320
|
-
out.tool_calls = currentParse('tool_calls', 'messages');
|
|
368
|
+
out.tool_calls = currentParse('tool_calls', 'messages', phys('tool_calls'));
|
|
321
369
|
// The house's own record of itself — plain names, nothing to filter.
|
|
322
|
-
for (const t of META_TYPES) { out[t] = t; out[`${t}_raw`] = t; }
|
|
370
|
+
for (const t of META_TYPES) { out[t] = phys(t); out[`${t}_raw`] = phys(t); }
|
|
323
371
|
out.sessions_v = sessionsRollup(out);
|
|
324
372
|
return out;
|
|
325
373
|
}
|
|
@@ -337,7 +385,42 @@ async function resolveRooms(client) {
|
|
|
337
385
|
return roomNames(await currentUser(client));
|
|
338
386
|
}
|
|
339
387
|
|
|
388
|
+
/**
|
|
389
|
+
* The text-index DDL for servers before the tokenizer grammar changed. 25.8 spells a
|
|
390
|
+
* tokenizer as a quoted name with options — `text(tokenizer = 'ngram', ngram_size = 3)`,
|
|
391
|
+
* `text(tokenizer = 'default')` — and rejects the function form with "Expected literal";
|
|
392
|
+
* 26.x spells it as a function — `ngrams(3)`, `splitByNonAlpha` — and rejects the quoted
|
|
393
|
+
* names as "Unknown tokenizer". Neither parses the other. The template carries the
|
|
394
|
+
* current grammar; this rewrites a statement to the older one when a server refuses it.
|
|
395
|
+
* `default` in the old grammar is the non-alphanumeric splitter, which is what
|
|
396
|
+
* `splitByNonAlpha` names in the new one, so the indexes built are the same.
|
|
397
|
+
*/
|
|
398
|
+
function legacyTextIndexDialect(sql) {
|
|
399
|
+
return sql
|
|
400
|
+
.replace(/TYPE text\(tokenizer = ngrams\((\d+)\)\)/g, "TYPE text(tokenizer = 'ngram', ngram_size = $1)")
|
|
401
|
+
.replace(/TYPE text\(tokenizer = splitByNonAlpha\)/g, "TYPE text(tokenizer = 'default')");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Which text-index grammar a server speaks, from `SELECT version()`. Measured on real
|
|
406
|
+
* builds: 25.8 and 25.9 want the quoted-name form, 25.10 onwards the function form, and
|
|
407
|
+
* each rejects the other. Anything unparseable is treated as current; the shipper still
|
|
408
|
+
* falls back on a grammar refusal, so a wrong guess costs one round-trip, not the install.
|
|
409
|
+
*/
|
|
410
|
+
function textIndexDialectFor(version) {
|
|
411
|
+
const m = /^(\d+)\.(\d+)/.exec(String(version || '').trim());
|
|
412
|
+
if (!m) return 'modern';
|
|
413
|
+
const major = Number(m[1]); const minor = Number(m[2]);
|
|
414
|
+
return (major < 25 || (major === 25 && minor < 10)) ? 'legacy' : 'modern';
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** True when a server's refusal is the grammar, not a privilege or a real mistake. */
|
|
418
|
+
function isTextIndexGrammarRefusal(message) {
|
|
419
|
+
return /Expected literal|supports only 'default'|Unknown tokenizer/.test(String(message || ''));
|
|
420
|
+
}
|
|
421
|
+
|
|
340
422
|
module.exports = {
|
|
423
|
+
physicalRoom, roomPattern, legacyTextIndexDialect, isTextIndexGrammarRefusal, textIndexDialectFor,
|
|
341
424
|
ROOM_TYPES, META_TYPES, SCHEMA_VERSION, SUPPORTED_SCHEMAS, MIN_WRITER_SCHEMA,
|
|
342
425
|
MIGRATIONS, ROOM_KEYS, keyProblem,
|
|
343
426
|
READ_SETTINGS, MEMBER_PIN,
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
// the old table until the rename; a late-writes pass copies anything newer than the
|
|
40
40
|
// snapshot, and ReplacingMergeTree makes that idempotent.
|
|
41
41
|
//
|
|
42
|
-
// EVERYTHING IS RECORDED. One
|
|
43
|
-
// after, per object; schema_version in
|
|
42
|
+
// EVERYTHING IS RECORDED. One events pending row before work, applied/failed
|
|
43
|
+
// after, per object; schema_version in meta only after a migration completes.
|
|
44
44
|
|
|
45
45
|
const fs = require('fs');
|
|
46
46
|
const path = require('path');
|
|
@@ -59,7 +59,7 @@ const { ROOM_TYPES, keyProblem, createStatement } = require('./house');
|
|
|
59
59
|
* toVersion the SCHEMA_VERSION a house is at once this has run
|
|
60
60
|
* detect(q, ctx) -> what still needs doing (any truthy non-empty value), or
|
|
61
61
|
* null/[] when the house is already past this migration.
|
|
62
|
-
* THE ROOMS ARE THE TRUTH, never
|
|
62
|
+
* THE ROOMS ARE THE TRUTH, never meta: a hand-migrated
|
|
63
63
|
* house has no record, and a restored backup can carry a
|
|
64
64
|
* record newer than its tables.
|
|
65
65
|
* plan(found) -> lines of human text for the confirm prompt
|
|
@@ -81,7 +81,7 @@ function listMigrations() {
|
|
|
81
81
|
const seen = new Set();
|
|
82
82
|
let lastVersion = 0;
|
|
83
83
|
for (const m of out) {
|
|
84
|
-
// The id is spliced into SQL (
|
|
84
|
+
// The id is spliced into SQL (events predicates) and into kept-table suffixes.
|
|
85
85
|
// It is repo-controlled, but "repo-controlled" is one compromised dependency away
|
|
86
86
|
// from "attacker-controlled" — validate like any other identifier.
|
|
87
87
|
if (!/^[a-z0-9][a-z0-9-]*$/.test(m.id)) throw new Error(`migration id '${m.id}' must match [a-z0-9-]+`);
|
|
@@ -116,7 +116,7 @@ async function detectPending(q, ctx, { component = null } = {}) {
|
|
|
116
116
|
* Refuse when another actor's run is unfinished. An `applied` newer than the last
|
|
117
117
|
* `pending` means done; a dangling `pending` means someone is (or was) mid-copy, and two
|
|
118
118
|
* concurrent rebuilds of one room end with one of them renaming the other's work.
|
|
119
|
-
* Best-effort: a house without
|
|
119
|
+
* Best-effort: a house without events cannot answer, and that is not a reason to
|
|
120
120
|
* block the migration that will create it.
|
|
121
121
|
*/
|
|
122
122
|
async function unfinishedBy(q, ctx, migrationId) {
|
|
@@ -124,7 +124,8 @@ async function unfinishedBy(q, ctx, migrationId) {
|
|
|
124
124
|
const rows = await q.rows(
|
|
125
125
|
`SELECT argMax(status, event_at) AS status, argMax(actor, event_at) AS actor,
|
|
126
126
|
formatDateTime(max(event_at), '%Y-%m-%d %H:%i') AS at
|
|
127
|
-
FROM
|
|
127
|
+
FROM ${(ctx.rooms && ctx.rooms.physical && ctx.rooms.physical.events) || 'events'}
|
|
128
|
+
WHERE kind = 'migration' AND id = '${migrationId}'`);
|
|
128
129
|
const r = rows[0];
|
|
129
130
|
if (r && r.status === 'pending' && r.actor && r.actor !== ctx.member) return r;
|
|
130
131
|
} catch { /* no events table yet */ }
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
// not insertable even with insert_allow_materialized_columns.
|
|
13
13
|
const DERIVED = ['text_ngram', 'text_word'];
|
|
14
14
|
|
|
15
|
-
//
|
|
16
|
-
// Everything else in
|
|
15
|
+
// meta keys that are DURABLE facts of the house — worth carrying to the new host.
|
|
16
|
+
// Everything else in meta is a per-host heartbeat (client_version, client_schema,
|
|
17
17
|
// last_ship:<writer>) that the new shipper rewrites for itself on its first run; carrying
|
|
18
18
|
// a stale one would misreport which machine last shipped until that run lands.
|
|
19
19
|
function isDurableMetaKey(key) {
|
|
@@ -154,10 +154,10 @@ ORDER BY (session_id, user_id, origin, epoch, idx);
|
|
|
154
154
|
-- authoritative and always current, and a mirror of them would be a second truth that
|
|
155
155
|
-- goes stale. What is recorded is what those cannot say: intent, sequence, outcome.
|
|
156
156
|
|
|
157
|
-
CREATE TABLE IF NOT EXISTS
|
|
157
|
+
CREATE TABLE IF NOT EXISTS meta
|
|
158
158
|
(
|
|
159
159
|
-- House-wide keys only ('schema_version', 'house_created_at'). Anything per-member
|
|
160
|
-
-- or per-machine belongs in
|
|
160
|
+
-- or per-machine belongs in events, which is append-only — this room is
|
|
161
161
|
-- latest-wins, so two members writing one key would overwrite each other.
|
|
162
162
|
key String,
|
|
163
163
|
value String,
|
|
@@ -168,7 +168,7 @@ CREATE TABLE IF NOT EXISTS house_meta
|
|
|
168
168
|
ENGINE = ReplacingMergeTree(updated_at)
|
|
169
169
|
ORDER BY (key);
|
|
170
170
|
|
|
171
|
-
CREATE TABLE IF NOT EXISTS
|
|
171
|
+
CREATE TABLE IF NOT EXISTS events
|
|
172
172
|
(
|
|
173
173
|
event_at DateTime64(3, 'UTC') DEFAULT now64(3),
|
|
174
174
|
-- 'migration' — a room rebuild, one row per transition (pending → applied|failed)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Provisioning a member — THE plan, whether memhouse executes it or a DBA does.
|
|
2
|
+
//
|
|
3
|
+
// There used to be three copies of this: the live path (adminBootstrap), the printed one
|
|
4
|
+
// (--print-sql), and install --admin-user. They drifted, and the printed copy handed a
|
|
5
|
+
// non-admin the one configuration the live path refuses — found in a drill, not by a
|
|
6
|
+
// test. A plan that is data cannot drift from itself: the live path executes it,
|
|
7
|
+
// --print-sql prints it, and the unit tests assert on it.
|
|
8
|
+
//
|
|
9
|
+
// The plan is a member and a database, and its centre is one statement:
|
|
10
|
+
//
|
|
11
|
+
// GRANT <room privileges> ON <db>.<member>_* TO <member> WITH GRANT OPTION
|
|
12
|
+
//
|
|
13
|
+
// A wildcard on the member's own name. It covers rooms that do not exist yet, so the
|
|
14
|
+
// member's shipper creates their rooms itself, a schema rebuild can CREATE and EXCHANGE
|
|
15
|
+
// inside the pattern, and a share can name the whole set or one table. It reaches nothing
|
|
16
|
+
// outside the pattern — measured on 26.7: a member cannot create, read, list, drop or
|
|
17
|
+
// re-grant a housemate's rooms — and a colleague can read it back with SHOW GRANTS in one
|
|
18
|
+
// line, which is the property the whole product rests on.
|
|
19
|
+
//
|
|
20
|
+
// Nobody is ever granted ALL ON <db>.*. That grant is dynamic (it covers rooms created
|
|
21
|
+
// later), and it is what turned two members in one database into a leak.
|
|
22
|
+
|
|
23
|
+
const { MEMBER_PIN, assertUsableName, roomPattern } = require('./house/house');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a member may do inside their own pattern. Each entry earns its place:
|
|
27
|
+
* SELECT, INSERT the shipper writes, everything else reads
|
|
28
|
+
* ALTER, OPTIMIZE ensureSchema adds columns in place; doctor compacts
|
|
29
|
+
* CREATE TABLE, DROP TABLE their own rooms on first ship; a rebuild on migrate
|
|
30
|
+
* ROW POLICY * `share --only` scopes a grantee to a project or a session
|
|
31
|
+
*/
|
|
32
|
+
const ROOM_PRIVILEGES = [
|
|
33
|
+
'SELECT', 'INSERT', 'ALTER', 'OPTIMIZE',
|
|
34
|
+
'CREATE TABLE', 'DROP TABLE',
|
|
35
|
+
'CREATE ROW POLICY', 'ALTER ROW POLICY', 'DROP ROW POLICY', 'SHOW ROW POLICIES',
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
function roomGrant(db, member) {
|
|
39
|
+
return `GRANT ${ROOM_PRIVILEGES.join(', ')} ON ${db}.${roomPattern(member)} TO ${member} WITH GRANT OPTION`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function sqlString(v) {
|
|
43
|
+
return `'${String(v).replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The statements that make `member` a member of house `db`, in order.
|
|
48
|
+
*
|
|
49
|
+
* `optional` marks steps that need ACCESS MANAGEMENT the admin may not hold. The live
|
|
50
|
+
* path skips those on refusal and says what the member loses; a refused required step
|
|
51
|
+
* aborts. `password` may be null for a member that already exists (an invite re-run, an
|
|
52
|
+
* install against an account the DBA made) — then the CREATE USER step is omitted and the
|
|
53
|
+
* rest still applies.
|
|
54
|
+
*
|
|
55
|
+
* @returns {Array<{sql:string, why:string, optional:boolean}>}
|
|
56
|
+
*/
|
|
57
|
+
function plan({ db, member, password = null }) {
|
|
58
|
+
assertUsableName(db, 'house');
|
|
59
|
+
assertUsableName(member, 'member');
|
|
60
|
+
const steps = [
|
|
61
|
+
{ sql: `CREATE DATABASE IF NOT EXISTS ${db}`, why: 'the house', optional: false },
|
|
62
|
+
];
|
|
63
|
+
if (password !== null) {
|
|
64
|
+
steps.push({ sql: `CREATE USER IF NOT EXISTS ${member} IDENTIFIED BY ${sqlString(password)}`, why: 'the member', optional: false });
|
|
65
|
+
}
|
|
66
|
+
steps.push(
|
|
67
|
+
{ sql: roomGrant(db, member), why: `their rooms — ${db}.${roomPattern(member)} — and nothing else in the house`, optional: false },
|
|
68
|
+
{ sql: `ALTER USER ${member} ADD SETTING ${MEMBER_PIN}`, why: 'user_id is stamped on every row; an async insert would skip the stamp', optional: false },
|
|
69
|
+
{ sql: `GRANT ALTER USER ON ${member} TO ${member}`, why: '`memhouse passwd` without an admin (their own account only)', optional: true },
|
|
70
|
+
{ sql: `GRANT SHOW USERS ON *.* TO ${member}`, why: 'see who to share with — names only, never data', optional: true },
|
|
71
|
+
{ sql: `GRANT REMOTE ON *.* TO ${member}`, why: '`memhouse relocate` pulls the old house over remoteSecure()', optional: true },
|
|
72
|
+
);
|
|
73
|
+
return steps;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The plan as SQL a person runs — clickhouse-client, the play UI, curl. One statement per
|
|
78
|
+
* line so it can be split on ';' by anything, comments above each saying why.
|
|
79
|
+
*/
|
|
80
|
+
function render(steps, { db, member }) {
|
|
81
|
+
const out = [
|
|
82
|
+
`-- memhouse: make '${member}' a member of house '${db}'.`,
|
|
83
|
+
'-- Run as a user with ACCESS MANAGEMENT (a stock \'default\' with access_management=1 will do).',
|
|
84
|
+
'--',
|
|
85
|
+
`-- One grant, on ${db}.${roomPattern(member)}: their rooms and nothing else in the database.`,
|
|
86
|
+
'-- Housemates keep their own rooms beside these and cannot read them. Do NOT widen it to',
|
|
87
|
+
`-- ${db}.* — that reaches every housemate's rooms, present and future.`,
|
|
88
|
+
'--',
|
|
89
|
+
`-- Their rooms are created by their own first \`memhouse ship\`; nothing to pre-build.`,
|
|
90
|
+
'',
|
|
91
|
+
];
|
|
92
|
+
for (const s of steps) {
|
|
93
|
+
out.push(`-- ${s.why}${s.optional ? ' (optional: needs ACCESS MANAGEMENT; skip if refused)' : ''}`);
|
|
94
|
+
out.push(`${s.sql};`);
|
|
95
|
+
out.push('');
|
|
96
|
+
}
|
|
97
|
+
return out.join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
module.exports = { ROOM_PRIVILEGES, roomGrant, plan, render };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `memhouse share <user> [--only <scope>] [--revoke]` / `memhouse share --list`
|
|
3
|
+
*
|
|
4
|
+
* Full share is one statement: GRANT SELECT on the house. Partial share adds a ROW
|
|
5
|
+
* POLICY per room, so the grantee reads only the rows the scope allows.
|
|
6
|
+
*
|
|
7
|
+
* Why no permissive catch-all policy for everyone else, which is the obvious design:
|
|
8
|
+
* row policies are OR'd, so a `USING 1 TO ALL EXCEPT alice` policy silently overrides
|
|
9
|
+
* the NEXT scoped grantee's filter — measured, bob saw all 22,500 rows instead of his
|
|
10
|
+
* 1,860. It fails OPEN and it fails quietly. ClickHouse already answers this correctly
|
|
11
|
+
* through `users_without_row_policies_can_read_rows`: a reader with no policy on a table
|
|
12
|
+
* still sees every row. Verified against a non-superuser member owning its own house —
|
|
13
|
+
* owner and full-share grantees were both unaffected while a third user was scoped.
|
|
14
|
+
*
|
|
15
|
+
* That setting is server config, not a query setting, and its default has moved between
|
|
16
|
+
* versions. So `probePermissive()` measures the behaviour before the first policy is
|
|
17
|
+
* created rather than assuming it, and refuses when the server would blindfold everyone.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Columns a scope may filter on, and the column each room spells time with. */
|
|
21
|
+
const SCOPE_COLUMNS = {
|
|
22
|
+
session: { column: () => 'session_id', quote: true },
|
|
23
|
+
project: { column: () => 'project', quote: true },
|
|
24
|
+
folder: { column: () => 'folder', quote: true },
|
|
25
|
+
host: { column: () => 'host', quote: true },
|
|
26
|
+
source: { column: () => 'source', quote: true },
|
|
27
|
+
// `sessions` has no `ts`; its clock is `created_at`. One literal predicate cannot span
|
|
28
|
+
// the rooms, so time scopes render per room.
|
|
29
|
+
since: { column: (room) => (room === 'sessions' ? 'created_at' : 'ts'), op: '>=' },
|
|
30
|
+
until: { column: (room) => (room === 'sessions' ? 'created_at' : 'ts'), op: '<' },
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** `project=memhouse,since=2026-01-01` -> [{key, value}], rejecting anything unknown. */
|
|
34
|
+
function parseScope(raw) {
|
|
35
|
+
const out = [];
|
|
36
|
+
for (const part of String(raw).split(',').map((s) => s.trim()).filter(Boolean)) {
|
|
37
|
+
const m = /^([a-z_]+)\s*=\s*(.+)$/.exec(part);
|
|
38
|
+
if (!m) throw new Error(`scope "${part}" is not <key>=<value>`);
|
|
39
|
+
const key = m[1].toLowerCase();
|
|
40
|
+
if (!SCOPE_COLUMNS[key]) {
|
|
41
|
+
throw new Error(`unknown scope key "${key}" — try: ${Object.keys(SCOPE_COLUMNS).join(', ')}`);
|
|
42
|
+
}
|
|
43
|
+
out.push({ key, value: m[2].trim() });
|
|
44
|
+
}
|
|
45
|
+
if (!out.length) throw new Error('--only was given nothing to scope on');
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The WHERE-shaped predicate for one room. Values are escaped, never interpolated raw. */
|
|
50
|
+
function scopePredicate(scope, room, sqlStr) {
|
|
51
|
+
return scope.map(({ key, value }) => {
|
|
52
|
+
const spec = SCOPE_COLUMNS[key];
|
|
53
|
+
const col = spec.column(room);
|
|
54
|
+
if (spec.op) return `${col} ${spec.op} parseDateTimeBestEffort(${sqlStr(value)})`;
|
|
55
|
+
return `${col} = ${sqlStr(value)}`;
|
|
56
|
+
}).join(' AND ');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Policy name for a grantee's room — predictable, so revoke can find every one. */
|
|
60
|
+
function policyName(user, room) {
|
|
61
|
+
return `mh_share_${user}_${room}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { SCOPE_COLUMNS, parseScope, scopePredicate, policyName };
|