@medicine-wheel/storage-provider 0.27.16 → 0.27.17

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 CHANGED
@@ -22,3 +22,13 @@ import { createProvider } from '@medicine-wheel/storage-provider';
22
22
  const store = await createProvider();
23
23
  ```
24
24
 
25
+ ## Before you upgrade a store to the version that brings governed edges
26
+
27
+ An edge may name a governed kinship type (`kinship_type`) and keep the context that bounds it. Such an edge is identified by its two nodes and that name, so one pair of nodes can hold several of them. Two things follow for whoever runs a wheel. The reasons are in `rispecs/agent-kinship.spec.md`.
28
+
29
+ **On Postgres, run `scripts/005-edges-kinship.sql` before you start the upgraded wheel.** The provider reads and writes a `kinship_type` column and the edge key `(from_id, to_id, kinship_type)`. `connect()` creates some tables when they are missing and never creates or alters `nodes`, `edges` or `ceremonies`. Until the script has run on an existing database, every edge operation fails. Stop the wheel, run the script, start the upgraded wheel. A database created from the current `scripts/001-create-medicine-wheel-tables.sql` needs nothing more. Neither the script nor the Postgres code of this change was run against a database by its author: try both on a copy first.
30
+
31
+ **Once a store holds a governed edge, earlier code must not write to it, and going back takes a step first.** Code from before this change knows one edge per ordered pair. Its write on a pair removes the governed edges of that pair, and an earlier MCP server opened on the store's files reads one edge of a pair and drops the others on its next write. So upgrade every process that opens the files before the first governed edge is written. An earlier MCP server that reaches the wheel over HTTP is safe. To go back to earlier code: with the current wheel still running, read every edge that has a `kinship_type` (`GET /api/edges?limit=all`), keep that list outside the store, and release each one (`DELETE /api/edges?from=&to=&kinship_type=`). A store with no governed edge is read and written by earlier code as before.
32
+
33
+ A governed edge keeps the date it was first written through every write.
34
+
@@ -5,7 +5,7 @@
5
5
  * Implementations: JsonlProvider (local/default), NeonProvider (Postgres), RedisProvider (future)
6
6
  */
7
7
  import type { MemoryConversation, MemoryConversationFilters, MemoryConversationPatch, MemoryConversationTurn } from '@medicine-wheel/memory';
8
- import type { DirectionName, NodeType, CeremonyType, RelationalNode as OntologyRelationalNode, RelationalEdge as OntologyRelationalEdge, CeremonyLog as OntologyCeremonyLog } from '@medicine-wheel/ontology-core';
8
+ import type { DirectionName, NodeType, CeremonyType, RelationalNode as OntologyRelationalNode, RelationalEdge as OntologyRelationalEdge, RelationContext, CeremonyLog as OntologyCeremonyLog } from '@medicine-wheel/ontology-core';
9
9
  export interface RelationalNode extends OntologyRelationalNode {
10
10
  description?: string;
11
11
  }
@@ -22,13 +22,18 @@ export interface NodePatch {
22
22
  direction?: DirectionName | null;
23
23
  metadata?: Record<string, unknown>;
24
24
  }
25
- /** Partial update for an edge (identified by from_id + to_id). */
25
+ /**
26
+ * Partial update for an edge. The edge is identified by from_id + to_id, and
27
+ * by its `kinship_type` when it carries one; a patch never changes that name.
28
+ */
26
29
  export interface EdgePatch {
27
30
  relationship_type?: string;
28
31
  strength?: number;
29
32
  ceremony_honored?: boolean;
30
33
  obligations?: string[];
31
34
  description?: string;
35
+ /** Replaces the relation's whole context. */
36
+ context?: RelationContext;
32
37
  }
33
38
  export declare class NodeNotFoundError extends Error {
34
39
  readonly nodeId: string;
@@ -38,8 +43,9 @@ export declare class NodeNotFoundError extends Error {
38
43
  export declare class EdgeNotFoundError extends Error {
39
44
  readonly fromId: string;
40
45
  readonly toId: string;
46
+ readonly kinshipType?: string | undefined;
41
47
  readonly code = "EDGE_NOT_FOUND";
42
- constructor(fromId: string, toId: string);
48
+ constructor(fromId: string, toId: string, kinshipType?: string | undefined);
43
49
  }
44
50
  /**
45
51
  * Deleting a node with living relations is refused: relational accountability
@@ -305,15 +311,17 @@ export interface StorageProvider {
305
311
  updateNode(id: string, patch: NodePatch): Promise<RelationalNode>;
306
312
  /** Throws NodeNotFoundError or NodeHasRelationsError (refusal, never cascade). */
307
313
  deleteNode(id: string): Promise<void>;
314
+ /** Writes the edge, replacing the one with the same identity. */
308
315
  createEdge(edge: RelationalEdge): Promise<void>;
309
- getEdge(fromId: string, toId: string): Promise<RelationalEdge | null>;
316
+ getEdge(fromId: string, toId: string, kinshipType?: string): Promise<RelationalEdge | null>;
310
317
  getAllEdges(limit?: number): Promise<RelationalEdge[]>;
311
318
  getRelatedNodes(nodeId: string, direction?: 'from' | 'to' | 'both'): Promise<string[]>;
319
+ /** Marks every edge of the pair as honored by this ceremony, with or without a kinship type. */
312
320
  updateEdgeCeremony(fromId: string, toId: string, ceremonyId: string): Promise<void>;
313
- /** Throws EdgeNotFoundError when no relation exists between the pair. */
314
- updateEdge(fromId: string, toId: string, patch: EdgePatch): Promise<RelationalEdge>;
315
- /** Throws EdgeNotFoundError when no relation exists between the pair. */
316
- deleteEdge(fromId: string, toId: string): Promise<void>;
321
+ /** Throws EdgeNotFoundError when no such relation exists between the pair. */
322
+ updateEdge(fromId: string, toId: string, patch: EdgePatch, kinshipType?: string): Promise<RelationalEdge>;
323
+ /** Throws EdgeNotFoundError when no such relation exists between the pair. */
324
+ deleteEdge(fromId: string, toId: string, kinshipType?: string): Promise<void>;
317
325
  logCeremony(ceremony: CeremonyLog): Promise<void>;
318
326
  getCeremony(id: string): Promise<CeremonyLog | null>;
319
327
  getCeremoniesTimeline(limit?: number): Promise<CeremonyLog[]>;
package/dist/interface.js CHANGED
@@ -18,11 +18,13 @@ export class NodeNotFoundError extends Error {
18
18
  export class EdgeNotFoundError extends Error {
19
19
  fromId;
20
20
  toId;
21
+ kinshipType;
21
22
  code = 'EDGE_NOT_FOUND';
22
- constructor(fromId, toId) {
23
- super(`Relation not found between ${fromId} and ${toId}`);
23
+ constructor(fromId, toId, kinshipType) {
24
+ super(`Relation not found between ${fromId} and ${toId}${kinshipType ? ` (${kinshipType})` : ''}`);
24
25
  this.fromId = fromId;
25
26
  this.toId = toId;
27
+ this.kinshipType = kinshipType;
26
28
  this.name = 'EdgeNotFoundError';
27
29
  }
28
30
  }
package/dist/jsonl.d.ts CHANGED
@@ -25,12 +25,12 @@ export declare class JsonlProvider implements StorageProvider {
25
25
  updateNode(id: string, patch: NodePatch): Promise<RelationalNode>;
26
26
  deleteNode(id: string): Promise<void>;
27
27
  createEdge(edge: RelationalEdge): Promise<void>;
28
- getEdge(fromId: string, toId: string): Promise<RelationalEdge | null>;
28
+ getEdge(fromId: string, toId: string, kinshipType?: string): Promise<RelationalEdge | null>;
29
29
  getRelatedNodes(nodeId: string, direction?: 'from' | 'to' | 'both'): Promise<string[]>;
30
30
  getAllEdges(limit?: number): Promise<RelationalEdge[]>;
31
31
  updateEdgeCeremony(fromId: string, toId: string, ceremonyId: string): Promise<void>;
32
- updateEdge(fromId: string, toId: string, patch: EdgePatch): Promise<RelationalEdge>;
33
- deleteEdge(fromId: string, toId: string): Promise<void>;
32
+ updateEdge(fromId: string, toId: string, patch: EdgePatch, kinshipType?: string): Promise<RelationalEdge>;
33
+ deleteEdge(fromId: string, toId: string, kinshipType?: string): Promise<void>;
34
34
  logCeremony(ceremony: CeremonyLog): Promise<void>;
35
35
  getCeremony(id: string): Promise<CeremonyLog | null>;
36
36
  getCeremoniesTimeline(limit?: number): Promise<CeremonyLog[]>;
package/dist/jsonl.js CHANGED
@@ -108,8 +108,8 @@ export class JsonlProvider {
108
108
  ceremony_id: edge.last_ceremony,
109
109
  });
110
110
  }
111
- async getEdge(fromId, toId) {
112
- const edge = this.readEdges().find((candidate) => candidate.from_id === fromId && candidate.to_id === toId);
111
+ async getEdge(fromId, toId, kinshipType) {
112
+ const edge = this.readEdges().find((candidate) => isEdge(candidate, fromId, toId, kinshipType));
113
113
  return edge ? this.parseEdge(edge) : null;
114
114
  }
115
115
  async getRelatedNodes(nodeId, direction = 'both') {
@@ -145,12 +145,12 @@ export class JsonlProvider {
145
145
  writeJsonl(this.edgesFile, nextEdges);
146
146
  });
147
147
  }
148
- async updateEdge(fromId, toId, patch) {
148
+ async updateEdge(fromId, toId, patch, kinshipType) {
149
149
  return withWriteLock(this.edgesFile, () => {
150
150
  const edges = this.readEdges();
151
- const index = edges.findIndex((candidate) => candidate.from_id === fromId && candidate.to_id === toId);
151
+ const index = edges.findIndex((candidate) => isEdge(candidate, fromId, toId, kinshipType));
152
152
  if (index === -1)
153
- throw new EdgeNotFoundError(fromId, toId);
153
+ throw new EdgeNotFoundError(fromId, toId, kinshipType);
154
154
  const next = {
155
155
  ...edges[index],
156
156
  ...(patch.relationship_type !== undefined
@@ -162,18 +162,19 @@ export class JsonlProvider {
162
162
  : {}),
163
163
  ...(patch.obligations !== undefined ? { obligations: patch.obligations } : {}),
164
164
  ...(patch.description !== undefined ? { description: patch.description } : {}),
165
+ ...(patch.context !== undefined ? { context: patch.context } : {}),
165
166
  };
166
167
  edges[index] = next;
167
168
  writeJsonl(this.edgesFile, edges);
168
169
  return this.parseEdge(next);
169
170
  });
170
171
  }
171
- async deleteEdge(fromId, toId) {
172
+ async deleteEdge(fromId, toId, kinshipType) {
172
173
  await withWriteLock(this.edgesFile, () => {
173
174
  const edges = this.readEdges();
174
- const remaining = edges.filter((candidate) => !(candidate.from_id === fromId && candidate.to_id === toId));
175
+ const remaining = edges.filter((candidate) => !isEdge(candidate, fromId, toId, kinshipType));
175
176
  if (remaining.length === edges.length) {
176
- throw new EdgeNotFoundError(fromId, toId);
177
+ throw new EdgeNotFoundError(fromId, toId, kinshipType);
177
178
  }
178
179
  writeJsonl(this.edgesFile, remaining);
179
180
  });
@@ -359,8 +360,14 @@ export class JsonlProvider {
359
360
  }
360
361
  async upsertEdge(edge) {
361
362
  await withWriteLock(this.edgesFile, () => {
362
- const nextEdges = this.readEdges().filter((candidate) => edgeKey(candidate) !== edgeKey(edge));
363
- nextEdges.push(edge);
363
+ const edges = this.readEdges();
364
+ const key = edgeKey(edge);
365
+ // A governed edge keeps the date it was first written through every
366
+ // write: that date is how long the relation has held, and a ceremony
367
+ // that honours it or a person who says it again does not restart it.
368
+ const held = edge.kinship_type ? edges.find((candidate) => edgeKey(candidate) === key) : undefined;
369
+ const nextEdges = edges.filter((candidate) => edgeKey(candidate) !== key);
370
+ nextEdges.push(held ? { ...edge, created_at: held.created_at } : edge);
364
371
  writeJsonl(this.edgesFile, nextEdges);
365
372
  });
366
373
  }
@@ -378,7 +385,7 @@ export class JsonlProvider {
378
385
  }
379
386
  parseEdge(edge) {
380
387
  return {
381
- id: edge.id ?? `${edge.from_id}:${edge.to_id}`,
388
+ id: edge.id ?? [edge.from_id, edge.to_id, ...(edge.kinship_type ? [edge.kinship_type] : [])].join(':'),
382
389
  from_id: edge.from_id,
383
390
  to_id: edge.to_id,
384
391
  relationship_type: edge.relationship_type,
@@ -387,6 +394,10 @@ export class JsonlProvider {
387
394
  last_ceremony: edge.last_ceremony ?? edge.ceremony_id,
388
395
  obligations: Array.isArray(edge.obligations) ? edge.obligations : [],
389
396
  ...(typeof edge.description === 'string' && edge.description ? { description: edge.description } : {}),
397
+ ...(typeof edge.kinship_type === 'string' && edge.kinship_type
398
+ ? { kinship_type: edge.kinship_type }
399
+ : {}),
400
+ ...(edge.context && typeof edge.context === 'object' ? { context: edge.context } : {}),
390
401
  created_at: edge.created_at,
391
402
  };
392
403
  }
@@ -530,9 +541,20 @@ function resolveProjectDataDir(currentDir) {
530
541
  }
531
542
  return path.join(cwd, '.mw', 'store');
532
543
  }
544
+ /**
545
+ * What makes two stored edges the same edge: the ordered pair, and the kinship
546
+ * type when the edge carries one. An edge without a kinship type shares its
547
+ * key with no edge that has one, so writing either never replaces the other.
548
+ */
533
549
  function edgeKey(edge) {
534
550
  // JSON-array encoding avoids false collisions when IDs contain the delimiter.
535
- return JSON.stringify([edge.from_id, edge.to_id]);
551
+ return JSON.stringify(edge.kinship_type ? [edge.from_id, edge.to_id, edge.kinship_type] : [edge.from_id, edge.to_id]);
552
+ }
553
+ /** True for the edge of this pair with this kinship type, or with none when none is named. */
554
+ function isEdge(candidate, fromId, toId, kinshipType) {
555
+ return (candidate.from_id === fromId &&
556
+ candidate.to_id === toId &&
557
+ (candidate.kinship_type || '') === (kinshipType || ''));
536
558
  }
537
559
  function sortByNewest(field) {
538
560
  return (left, right) => Date.parse(right[field]) - Date.parse(left[field]);
package/dist/neon.d.ts CHANGED
@@ -20,12 +20,12 @@ export declare class NeonProvider implements StorageProvider {
20
20
  deleteNode(id: string): Promise<void>;
21
21
  private parseNode;
22
22
  createEdge(edge: RelationalEdge): Promise<void>;
23
- getEdge(fromId: string, toId: string): Promise<RelationalEdge | null>;
23
+ getEdge(fromId: string, toId: string, kinshipType?: string): Promise<RelationalEdge | null>;
24
24
  getRelatedNodes(nodeId: string, direction?: 'from' | 'to' | 'both'): Promise<string[]>;
25
25
  getAllEdges(limit?: number): Promise<RelationalEdge[]>;
26
26
  updateEdgeCeremony(fromId: string, toId: string, ceremonyId: string): Promise<void>;
27
- updateEdge(fromId: string, toId: string, patch: EdgePatch): Promise<RelationalEdge>;
28
- deleteEdge(fromId: string, toId: string): Promise<void>;
27
+ updateEdge(fromId: string, toId: string, patch: EdgePatch, kinshipType?: string): Promise<RelationalEdge>;
28
+ deleteEdge(fromId: string, toId: string, kinshipType?: string): Promise<void>;
29
29
  private parseEdge;
30
30
  logCeremony(ceremony: CeremonyLog): Promise<void>;
31
31
  getCeremony(id: string): Promise<CeremonyLog | null>;
package/dist/neon.js CHANGED
@@ -11,6 +11,11 @@ import { mergeCaptureRecords, filterAndOrderCaptures, } from './capture-records.
11
11
  export class NeonProvider {
12
12
  name = 'neon';
13
13
  sql = null;
14
+ // connect() creates the tables below when they are missing. It never creates
15
+ // or alters `nodes`, `edges` or `ceremonies`: those come from the numbered
16
+ // scripts in `scripts/`, run by hand. The edge operations of this provider
17
+ // need `scripts/005-edges-kinship.sql` to have run on an existing database
18
+ // (the `kinship_type` column and its place in the key), and fail until it has.
14
19
  async connect() {
15
20
  const url = getConnectionString();
16
21
  if (!url)
@@ -124,21 +129,26 @@ export class NeonProvider {
124
129
  // ── Edge Operations ──
125
130
  async createEdge(edge) {
126
131
  await this.db `
127
- INSERT INTO edges (from_id, to_id, relationship_type, strength, ceremony_honored, last_ceremony, obligations, description, created_at)
128
- VALUES (${edge.from_id}, ${edge.to_id}, ${edge.relationship_type}, ${edge.strength},
132
+ INSERT INTO edges (from_id, to_id, kinship_type, relationship_type, strength, ceremony_honored, last_ceremony, obligations, description, context, created_at)
133
+ VALUES (${edge.from_id}, ${edge.to_id}, ${edge.kinship_type || ''}, ${edge.relationship_type}, ${edge.strength},
129
134
  ${edge.ceremony_honored}, ${edge.last_ceremony || null}, ${JSON.stringify(edge.obligations)},
130
- ${edge.description || null}, ${edge.created_at})
131
- ON CONFLICT (from_id, to_id) DO UPDATE SET
135
+ ${edge.description || null}, ${edge.context ? JSON.stringify(edge.context) : null}, ${edge.created_at})
136
+ ON CONFLICT (from_id, to_id, kinship_type) DO UPDATE SET
132
137
  relationship_type = EXCLUDED.relationship_type,
133
138
  strength = EXCLUDED.strength,
134
139
  ceremony_honored = EXCLUDED.ceremony_honored,
135
140
  last_ceremony = EXCLUDED.last_ceremony,
136
141
  obligations = EXCLUDED.obligations,
137
- description = EXCLUDED.description
142
+ description = EXCLUDED.description,
143
+ context = EXCLUDED.context
138
144
  `;
139
145
  }
140
- async getEdge(fromId, toId) {
141
- const rows = await this.db `SELECT * FROM edges WHERE from_id = ${fromId} AND to_id = ${toId}`;
146
+ // `kinship_type` is part of the key (scripts/005-edges-kinship.sql). The
147
+ // column holds '' for an edge without one, because a primary key takes no NULL.
148
+ async getEdge(fromId, toId, kinshipType) {
149
+ const rows = await this.db `
150
+ SELECT * FROM edges WHERE from_id = ${fromId} AND to_id = ${toId} AND kinship_type = ${kinshipType || ''}
151
+ `;
142
152
  if (rows.length === 0)
143
153
  return null;
144
154
  return this.parseEdge(rows[0]);
@@ -170,10 +180,10 @@ export class NeonProvider {
170
180
  WHERE from_id = ${fromId} AND to_id = ${toId}
171
181
  `;
172
182
  }
173
- async updateEdge(fromId, toId, patch) {
174
- const existing = await this.getEdge(fromId, toId);
183
+ async updateEdge(fromId, toId, patch, kinshipType) {
184
+ const existing = await this.getEdge(fromId, toId, kinshipType);
175
185
  if (!existing)
176
- throw new EdgeNotFoundError(fromId, toId);
186
+ throw new EdgeNotFoundError(fromId, toId, kinshipType);
177
187
  const next = {
178
188
  ...existing,
179
189
  ...(patch.relationship_type !== undefined
@@ -185,6 +195,7 @@ export class NeonProvider {
185
195
  : {}),
186
196
  ...(patch.obligations !== undefined ? { obligations: patch.obligations } : {}),
187
197
  ...(patch.description !== undefined ? { description: patch.description } : {}),
198
+ ...(patch.context !== undefined ? { context: patch.context } : {}),
188
199
  };
189
200
  await this.db `
190
201
  UPDATE edges SET
@@ -192,21 +203,26 @@ export class NeonProvider {
192
203
  strength = ${next.strength},
193
204
  ceremony_honored = ${next.ceremony_honored},
194
205
  obligations = ${JSON.stringify(next.obligations)},
195
- description = ${next.description || null}
196
- WHERE from_id = ${fromId} AND to_id = ${toId}
206
+ description = ${next.description || null},
207
+ context = ${next.context ? JSON.stringify(next.context) : null}
208
+ WHERE from_id = ${fromId} AND to_id = ${toId} AND kinship_type = ${kinshipType || ''}
197
209
  `;
198
210
  return next;
199
211
  }
200
- async deleteEdge(fromId, toId) {
212
+ async deleteEdge(fromId, toId, kinshipType) {
201
213
  const deleted = await this.db `
202
- DELETE FROM edges WHERE from_id = ${fromId} AND to_id = ${toId} RETURNING from_id
214
+ DELETE FROM edges
215
+ WHERE from_id = ${fromId} AND to_id = ${toId} AND kinship_type = ${kinshipType || ''}
216
+ RETURNING from_id
203
217
  `;
204
218
  if (deleted.length === 0)
205
- throw new EdgeNotFoundError(fromId, toId);
219
+ throw new EdgeNotFoundError(fromId, toId, kinshipType);
206
220
  }
207
221
  parseEdge(row) {
222
+ const kinshipType = typeof row.kinship_type === 'string' && row.kinship_type ? row.kinship_type : undefined;
223
+ const context = parseJsonValue(row.context, null);
208
224
  return {
209
- id: `${row.from_id}:${row.to_id}`,
225
+ id: [row.from_id, row.to_id, ...(kinshipType ? [kinshipType] : [])].join(':'),
210
226
  from_id: row.from_id,
211
227
  to_id: row.to_id,
212
228
  relationship_type: row.relationship_type,
@@ -215,6 +231,8 @@ export class NeonProvider {
215
231
  last_ceremony: row.last_ceremony || undefined,
216
232
  obligations: parseJsonValue(row.obligations, []),
217
233
  ...(typeof row.description === 'string' && row.description ? { description: row.description } : {}),
234
+ ...(kinshipType ? { kinship_type: kinshipType } : {}),
235
+ ...(context && typeof context === 'object' ? { context } : {}),
218
236
  created_at: toIsoString(row.created_at),
219
237
  };
220
238
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medicine-wheel/storage-provider",
3
- "version": "0.27.16",
3
+ "version": "0.27.17",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,8 +16,8 @@
16
16
  "dependencies": {
17
17
  "@neondatabase/serverless": "^0.10.0",
18
18
  "@upstash/redis": "^1.34.3",
19
- "@medicine-wheel/ontology-core": "^0.27.16",
20
- "@medicine-wheel/memory": "^0.27.16"
19
+ "@medicine-wheel/ontology-core": "^0.27.17",
20
+ "@medicine-wheel/memory": "^0.27.17"
21
21
  },
22
22
  "devDependencies": {
23
23
  "typescript": "^5.7.0"