@astratra/conscience 1.1.0 → 1.2.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.fr.md CHANGED
@@ -925,6 +925,93 @@ supprimer avec la personne.
925
925
  (schéma : `postgresShadowSchema({ prefix })`, table `<prefix>_runs`). Même
926
926
  contrat, mêmes tests, toute valeur est un paramètre SQL.
927
927
 
928
+ ## Stockage MongoDB
929
+
930
+ Installer le pilote comme dépendance facultative seulement pour MongoDB :
931
+
932
+ ```sh
933
+ npm install mongodb
934
+ ```
935
+
936
+ Les deux magasins reçoivent un `Db` du pilote officiel. Ils n'importent jamais
937
+ le pilote et n'ouvrent aucune connexion. Avec mongoose, attendre la connexion,
938
+ puis fournir `connection.db` et, pour le gouverneur, `connection.getClient()`
939
+ dans l'option `client`. Le pilote est une peerDependency optionnelle
940
+ (`^6.0.0 || ^7.0.0`) ; mongoose n'est pas une dépendance.
941
+
942
+ ```js
943
+ const { MongoClient } = require('mongodb');
944
+ const { createMongoGovernorStore, mongoGovernorIndexes, createGovernor } = require('@astratra/conscience/governor');
945
+ const { createMongoConscienceStore, mongoConscienceIndexes, createConscience } = require('@astratra/conscience');
946
+
947
+ const client = new MongoClient(process.env.DATABASE_URL);
948
+ await client.connect();
949
+ const db = client.db();
950
+ await mongoGovernorIndexes(db, { prefix: 'app_governor' });
951
+ await mongoConscienceIndexes(db, { prefix: 'app_memory' });
952
+ const governor = createGovernor({
953
+ budgets: { perCycle: 1, perAgent: 5, perOrganisation: 20 }
954
+ }, { store: createMongoGovernorStore(db, { prefix: 'app_governor', client }) });
955
+ const conscience = createConscience({
956
+ governor, masker: (texte) => masquerDansApplication(texte),
957
+ store: createMongoConscienceStore(db, { prefix: 'app_memory' })
958
+ });
959
+ ```
960
+
961
+ Les fonctions d'index sont des migrations explicites et idempotentes, à appeler
962
+ avant l'utilisation. Un préfixe contient lettres, chiffres et traits de
963
+ soulignement, commence par une lettre ou un trait de soulignement, et ne dépasse
964
+ pas 41 caractères. Valeurs par défaut : `astratra_governor` et
965
+ `astratra_conscience`. Collections du gouverneur : suffixes `proposals`, `audit`,
966
+ `spend`, `controls`, `objects`, `versions`, `findings`, `sequences`, `locks`.
967
+ Mémoire des décisions : `records` et `sequences`.
968
+
969
+ L'identité est encodée dans l'index unique `_id` : organisation et identifiant,
970
+ avec type et version si nécessaire. Une insertion rend le contenu existant sans
971
+ le modifier. Les approbations comparent atomiquement statut et nombre
972
+ d'approbations ; les objets comparent leur révision. Les identifiants d'audit
973
+ sont uniques, avec un compteur persistant pour l'ordre. Les records ont aussi
974
+ un compteur pour départager les dates égales ; le remplacement conserve l'ordre.
975
+ Filtres, rétention et `conscience.eraseSubject()` suivent le contrat PostgreSQL,
976
+ avec isolation des organisations et invalidation des preuves. L'effacement
977
+ porte sur la mémoire des décisions ; la conservation de l'audit du gouverneur
978
+ reste une politique de l'application, comme avec PostgreSQL.
979
+
980
+ **Les budgets atomiques nécessitent un replica set ou un cluster shardé**
981
+ (MongoDB 4.2+, selon la compatibilité serveur du pilote). Chaque réservation
982
+ modifie un verrou d'organisation dans une transaction avec lecture snapshot et
983
+ écriture majority. Le pilote rejoue les transactions en conflit : deux dépenses
984
+ concurrentes ne peuvent pas dépasser ensemble un plafond. Un serveur standalone
985
+ fait échouer la dépense, sans repli non atomique. L'option `client`, si fournie,
986
+ doit désigner le propriétaire de `db`. L'application ferme ses connexions ; le
987
+ magasin ferme les sessions courtes qu'il crée pour réserver. Une opération
988
+ complète du gouverneur n'est pas transactionnelle : état et audit restent des
989
+ écritures séparées, comme dans l'implémentation PostgreSQL. Une erreur de base
990
+ remonte sans autoriser de dépense. Les limites MongoDB de taille de document
991
+ s'appliquent à chaque record.
992
+
993
+ Les mêmes suites de contrat du gouverneur, des approbations et changements, des
994
+ records et de leur évaluation tournent avec un faux client et MongoDB réel :
995
+
996
+ ```sh
997
+ npm test -w @astratra/conscience
998
+ MONGO_TEST_URL=mongodb://127.0.0.1:27017/conscience_test?replicaSet=rs0 npm test -w @astratra/conscience
999
+ ```
1000
+
1001
+ Sans `MONGO_TEST_URL`, les tests réels sont sautés avec une raison. Avec une URL,
1002
+ la connexion et le replica set ou cluster shardé doivent fonctionner : une erreur
1003
+ fait échouer la suite. Chaque test crée un préfixe aléatoire et supprime seulement
1004
+ ses collections, jamais la base. Un replica set local de `mongodb-memory-server`
1005
+ peut fournir cette URL. Les magasins de vigilance et de mode ombre conservent
1006
+ leurs adaptateurs actuels : ces deux magasins MongoDB couvrent le gouverneur et
1007
+ les records des décisions.
1008
+
1009
+ Vérification par mutations (trois régressions détectées puis restaurées) :
1010
+
1011
+ ```sh
1012
+ node packages/conscience/test/mongo-mutations.js
1013
+ ```
1014
+
928
1015
  ## Tests
929
1016
 
930
1017
  ```sh
package/README.md CHANGED
@@ -887,6 +887,90 @@ organisation is refused. Runs carry the `userId` of the request: register
887
887
  (schema: `postgresShadowSchema({ prefix })`, table `<prefix>_runs`). Same
888
888
  contract, same tests, every value a SQL parameter.
889
889
 
890
+ ## MongoDB storage
891
+
892
+ Install the optional peer dependency only when using MongoDB:
893
+
894
+ ```sh
895
+ npm install mongodb
896
+ ```
897
+
898
+ Both stores accept a native driver `Db`. They never import the driver or open a
899
+ connection themselves. With mongoose, wait for the connection, then pass
900
+ `connection.db`; pass `connection.getClient()` as `client` for the governor.
901
+ The driver is an optional peer (`^6.0.0 || ^7.0.0`), with no mandatory runtime
902
+ installation and no mongoose dependency.
903
+
904
+ ```js
905
+ const { MongoClient } = require('mongodb');
906
+ const { createMongoGovernorStore, mongoGovernorIndexes, createGovernor } = require('@astratra/conscience/governor');
907
+ const { createMongoConscienceStore, mongoConscienceIndexes, createConscience } = require('@astratra/conscience');
908
+
909
+ const client = new MongoClient(process.env.DATABASE_URL);
910
+ await client.connect();
911
+ const db = client.db();
912
+ await mongoGovernorIndexes(db, { prefix: 'app_governor' });
913
+ await mongoConscienceIndexes(db, { prefix: 'app_memory' });
914
+ const governor = createGovernor({
915
+ budgets: { perCycle: 1, perAgent: 5, perOrganisation: 20 }
916
+ }, { store: createMongoGovernorStore(db, { prefix: 'app_governor', client }) });
917
+ const conscience = createConscience({
918
+ governor, masker: (text) => maskInApplication(text),
919
+ store: createMongoConscienceStore(db, { prefix: 'app_memory' })
920
+ });
921
+ ```
922
+
923
+ The index functions are explicit, idempotent migrations; call them before using
924
+ the stores. Prefixes accept letters, digits and underscores, begin with a
925
+ letter or underscore, and contain at most 41 characters. Defaults are
926
+ `astratra_governor` and `astratra_conscience`. Governor collections have suffixes
927
+ `proposals`, `audit`, `spend`, `controls`, `objects`, `versions`, `findings`,
928
+ `sequences`, `locks`; decision memory uses `records` and `sequences`.
929
+
930
+ Identity is encoded in the unique `_id`: organisation plus record id (and kind
931
+ or version where needed). Inserts return existing content unchanged; approval
932
+ transitions compare status and approval count atomically, and object updates
933
+ compare revision. Audit ids are unique and a persistent counter orders entries.
934
+ Records use a persistent counter to order equal timestamps; replacement keeps
935
+ that order. Filters, retention and `conscience.eraseSubject()` use the same
936
+ contract as the PostgreSQL store, including organisation isolation and evidence
937
+ invalidation. Erasure affects decision memory; governor audit retention remains
938
+ application policy, as with PostgreSQL.
939
+
940
+ **Atomic spending requires a replica set or sharded cluster** (MongoDB 4.2+,
941
+ subject to the driver's server compatibility). Each reservation updates an
942
+ organisation lock inside a snapshot transaction, with majority write concern.
943
+ Conflicting transactions retry through the driver, preventing two concurrent
944
+ reservations from exceeding a budget. A standalone server fails spending; there
945
+ is no unsafe fallback. Pass the client owning `db` if supplying `client`.
946
+ Connections and sessions are caller-owned except the short-lived reservation
947
+ sessions, which the store closes. The stores do not make an entire governor
948
+ operation transactional: state and audit writes remain separate, as in the
949
+ PostgreSQL implementation. Database errors propagate; they never authorize a
950
+ spend. MongoDB's normal document size limits apply to individual records.
951
+
952
+ The shared governor, approval/change, conscience and evaluation contract suites
953
+ run against a fake client and, optionally, real MongoDB:
954
+
955
+ ```sh
956
+ npm test -w @astratra/conscience
957
+ MONGO_TEST_URL=mongodb://127.0.0.1:27017/conscience_test?replicaSet=rs0 npm test -w @astratra/conscience
958
+ ```
959
+
960
+ Without `MONGO_TEST_URL`, real MongoDB tests skip with a reason. A supplied URL
961
+ must connect to a replica set or sharded cluster; connection or topology errors
962
+ fail the suite. Tests create random collection prefixes and drop only their own
963
+ collections, never the database. A local `mongodb-memory-server` replica set
964
+ can supply the same URL. The vigilance and shadow stores retain their existing
965
+ storage adapters; these two MongoDB stores cover the governor and decision
966
+ records only.
967
+
968
+ Mutation checks (three regressions, each detected, then restored):
969
+
970
+ ```sh
971
+ node packages/conscience/test/mongo-mutations.js
972
+ ```
973
+
890
974
  ## Tests
891
975
 
892
976
  ```sh
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astratra/conscience",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Documented memory of an AI agent's decisions (outcomes, corrections, lessons with evidence, self-model, lineage, erasure) under a deterministic governor with no language model: constitution, permissions, budgets, isolation, kill switch, human approval.",
5
5
  "engines": {
6
6
  "node": ">=20"
@@ -32,7 +32,8 @@
32
32
  "@astratra/ai": "^1.8.0"
33
33
  },
34
34
  "devDependencies": {
35
- "pg": "^8.13.1"
35
+ "pg": "^8.13.1",
36
+ "mongoose": "^8.24.2"
36
37
  },
37
38
  "exports": {
38
39
  ".": {
@@ -48,5 +49,13 @@
48
49
  "default": "./src/autoconscience/index.js"
49
50
  },
50
51
  "./package.json": "./package.json"
52
+ },
53
+ "peerDependencies": {
54
+ "mongodb": "^6.0.0 || ^7.0.0"
55
+ },
56
+ "peerDependenciesMeta": {
57
+ "mongodb": {
58
+ "optional": true
59
+ }
51
60
  }
52
61
  }
@@ -383,3 +383,11 @@ export function parseRemoteSettings(raw: unknown): RemoteSettings;
383
383
  export function remotePolicy(localPolicy: GovernorPolicyInput, settings: RemoteSettings | null, options?: { costPerCycle?: number }): GovernorPolicyInput;
384
384
  export const REMOTE_SETTINGS_SCHEMA: 'conscience-settings/1';
385
385
  export const DECISION_RESULTS: readonly ['applied', 'already_applied', 'refused', 'failed'];
386
+
387
+ /** Native driver objects; pass connection.db and connection.getClient() with mongoose. */
388
+ export interface MongoDatabase { collection(name: string): any; client?: MongoSessionClient }
389
+ export interface MongoSessionClient { startSession(): any }
390
+ export interface MongoStoreOptions { prefix?: string; client?: MongoSessionClient }
391
+ /** Requires a replica set or sharded cluster for atomic spending. */
392
+ export function createMongoGovernorStore(db: MongoDatabase, options?: MongoStoreOptions): GovernorStore;
393
+ export function mongoGovernorIndexes(db: MongoDatabase, options?: { prefix?: string }): Promise<void>;
@@ -13,7 +13,10 @@ const { SEVERITIES, REPORT_SCHEMA } = require('./review');
13
13
  const { bucket } = require('./rollout');
14
14
  const { createRemoteSettings, parseRemoteSettings, remotePolicy, REMOTE_SETTINGS_SCHEMA, DECISION_RESULTS } = require('./remote');
15
15
 
16
+ const { createMongoGovernorStore, mongoGovernorIndexes } = require('./mongo');
17
+
16
18
  module.exports = {
19
+ createMongoGovernorStore, mongoGovernorIndexes,
17
20
  createGovernor, assertGovernorReady, isGovernor, validatePolicy, scopesOf, protectedRulesTouched,
18
21
  createMemoryGovernorStore, createPostgresGovernorStore, postgresGovernorSchema, GovernorError,
19
22
  PROPOSAL_STATUSES: STATUSES, REASONS, PROTECTED_SCOPES, MODIFIABLE_KINDS, AUTOMATIC_CHANGES, DEFAULT_CONSTITUTION,
@@ -0,0 +1,132 @@
1
+ 'use strict';
2
+ const { copy } = require('./util');
3
+ const { boundedLimit } = require('./stores');
4
+ const { collections, read, keyOf, literal, insertOnce, sequence, list } = require('./mongoUtil');
5
+ const suffixes = ['proposals', 'audit', 'spend', 'controls', 'objects', 'versions', 'findings', 'sequences', 'locks'];
6
+ const tables = (db, options) => collections(db, options, 'astratra_governor', suffixes);
7
+ const objectKey = (organisationId, kind, id) => keyOf(organisationId, kind, id);
8
+
9
+ /** Explicit, repeatable migration; never run implicitly by the store. */
10
+ async function mongoGovernorIndexes(db, options) {
11
+ const t = tables(db, options);
12
+ await t.proposals.createIndex({ 'data.organisationId': 1, 'data.status': 1, 'data.createdAt': -1, 'data.id': 1 });
13
+ await t.audit.createIndex({ 'data.id': 1 }, { unique: true });
14
+ await t.audit.createIndex({ 'data.organisationId': 1, seq: -1 });
15
+ await t.spend.createIndex({ organisationId: 1, at: 1 });
16
+ await t.objects.createIndex({ 'data.organisationId': 1, 'data.kind': 1, 'data.id': 1 });
17
+ await t.objects.createIndex({ 'data.candidate': 1 });
18
+ await t.versions.createIndex({ 'data.organisationId': 1, 'data.kind': 1, 'data.id': 1, 'data.number': 1 });
19
+ await t.findings.createIndex({ 'data.organisationId': 1, 'data.receivedAt': -1, 'data.id': 1 });
20
+ // Materialize transaction collections before the first reservation.
21
+ for (const name of ['locks', 'controls', 'sequences']) await t[name].createIndex({ _id: 1 });
22
+ }
23
+
24
+ /** Native Db, also available as a mongoose connection's .db. No driver import. */
25
+ function createMongoGovernorStore(db, options = {}) {
26
+ const t = tables(db, options);
27
+ const client = options.client ?? db.client;
28
+ if (!client || typeof client.startSession !== 'function') throw new TypeError('A MongoClient with startSession() is required.');
29
+ const get = async (table, key) => read(await table.findOne({ _id: key }));
30
+ return {
31
+ putProposal(record) { return insertOnce(t.proposals, keyOf(record.organisationId, record.id), record); },
32
+ getProposal(organisationId, id) { return get(t.proposals, keyOf(organisationId, id)); },
33
+ async transitionProposal(organisationId, id, fromStatus, patch, guard) {
34
+ const filter = { _id: keyOf(organisationId, id), 'data.status': fromStatus };
35
+ if (guard?.approvals !== undefined) filter.$expr = { $eq: [{ $size: { $ifNull: ['$data.approvals', []] } }, guard.approvals] };
36
+ return read(await t.proposals.findOneAndUpdate(filter,
37
+ [{ $set: { data: { $mergeObjects: ['$data', literal(patch)] } } }],
38
+ { returnDocument: 'after', includeResultMetadata: false }));
39
+ },
40
+ listProposals(filter = {}) {
41
+ const query = { 'data.organisationId': filter.organisationId };
42
+ if (filter.status != null) query['data.status'] = { $eq: filter.status };
43
+ if (filter.statuses != null) query.$and = [{ 'data.status': { $in: filter.statuses } }];
44
+ if (filter.since != null) query.$expr = { $gte: [{ $ifNull: ['$data.decidedAt', '$data.createdAt'] }, literal(filter.since)] };
45
+ return list(t.proposals, query, { 'data.createdAt': -1, 'data.id': 1 }, boundedLimit(filter.limit));
46
+ },
47
+ async appendAudit(entry) {
48
+ const seq = await sequence(t.sequences, 'audit');
49
+ await t.audit.insertOne({ _id: entry.id, data: copy(entry), seq });
50
+ return copy(entry);
51
+ },
52
+ listAudit(filter = {}) {
53
+ const query = {};
54
+ for (const field of ['organisationId', 'proposalId', 'objectKind', 'objectId']) {
55
+ if (filter[field] !== undefined && (field === 'organisationId' || filter[field] != null)) query[`data.${field}`] = { $eq: filter[field] };
56
+ }
57
+ if (filter.actions != null) query['data.action'] = { $in: filter.actions };
58
+ if (filter.since != null) query['data.at'] = { $gte: filter.since };
59
+ return list(t.audit, query, { seq: -1 }, boundedLimit(filter.limit));
60
+ },
61
+ async reserveSpend(entry, limits) {
62
+ if (!Number.isFinite(entry.cost) || entry.cost < 0) throw new TypeError('Spend cost must be finite and nonnegative.');
63
+ // A permanent organisation lock document is updated in every transaction.
64
+ // Conflicting reservations retry through withTransaction, with a new snapshot.
65
+ try {
66
+ await t.locks.updateOne({ _id: entry.organisationId }, { $setOnInsert: { revision: 0 } }, { upsert: true });
67
+ } catch (error) {
68
+ if (error.code !== 11000) throw error;
69
+ if (!await t.locks.findOne({ _id: entry.organisationId })) throw error;
70
+ }
71
+ const session = client.startSession();
72
+ try {
73
+ return await session.withTransaction(async () => {
74
+ await t.locks.updateOne({ _id: entry.organisationId }, { $inc: { revision: 1 } }, { session });
75
+ const rows = await t.spend.find({ organisationId: entry.organisationId }, { session }).toArray();
76
+ const sums = () => {
77
+ const totals = { cycle: 0, agent: 0, organisation: 0 };
78
+ for (const row of rows) {
79
+ if (entry.cycleId != null && row.cycleId === entry.cycleId) totals.cycle += row.cost;
80
+ if (row.at >= limits.since) {
81
+ totals.organisation += row.cost;
82
+ if (row.agentId === entry.agentId) totals.agent += row.cost;
83
+ }
84
+ }
85
+ return totals;
86
+ };
87
+ if (rows.some((row) => row._id === keyOf(entry.organisationId, entry.id))) return { reserved: true, created: false, totals: sums() };
88
+ const totals = sums();
89
+ if ((entry.cycleId != null && totals.cycle + entry.cost > limits.perCycle)
90
+ || totals.agent + entry.cost > limits.perAgent || totals.organisation + entry.cost > limits.perOrganisation) return { reserved: false, created: false, totals };
91
+ const row = { ...copy(entry), _id: keyOf(entry.organisationId, entry.id) };
92
+ await t.spend.insertOne(row, { session });
93
+ rows.push(row);
94
+ return { reserved: true, created: true, totals: sums() };
95
+ }, { readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' }, readPreference: 'primary' });
96
+ } finally { await session.endSession(); }
97
+ },
98
+ getControl(key) { return get(t.controls, key); },
99
+ async setControl(key, value) {
100
+ await t.controls.updateOne({ _id: key }, { $set: { data: copy(value) } }, { upsert: true });
101
+ return copy(value);
102
+ },
103
+ getObject(organisationId, kind, id) { return get(t.objects, objectKey(organisationId, kind, id)); },
104
+ createObject(state) { return insertOnce(t.objects, objectKey(state.organisationId, state.kind, state.id), state); },
105
+ async updateObject(organisationId, kind, id, expectedRevision, next) {
106
+ return read(await t.objects.findOneAndUpdate({ _id: objectKey(organisationId, kind, id), 'data.revision': expectedRevision },
107
+ { $set: { data: copy(next) } }, { returnDocument: 'after', includeResultMetadata: false }));
108
+ },
109
+ listObjects(filter = {}) {
110
+ const query = {};
111
+ for (const field of ['organisationId', 'kind']) if (filter[field] != null) query[`data.${field}`] = { $eq: filter[field] };
112
+ if (filter.hasCandidate) query['data.candidate'] = { $ne: null };
113
+ return list(t.objects, query, { 'data.organisationId': 1, 'data.kind': 1, 'data.id': 1 }, boundedLimit(filter.limit));
114
+ },
115
+ putVersion(record) { return insertOnce(t.versions, keyOf(record.organisationId, record.kind, record.id, record.versionId), record); },
116
+ getVersion(organisationId, kind, id, versionId) { return get(t.versions, keyOf(organisationId, kind, id, versionId)); },
117
+ listVersions(filter) {
118
+ return list(t.versions, { 'data.organisationId': filter.organisationId, 'data.kind': filter.kind, 'data.id': filter.id }, { 'data.number': 1 }, boundedLimit(filter.limit, 1000));
119
+ },
120
+ putFinding(record) { return insertOnce(t.findings, keyOf(record.organisationId, record.id), record); },
121
+ listFindings(filter = {}) {
122
+ const query = { 'data.organisationId': filter.organisationId };
123
+ if (filter.since != null || filter.until != null) {
124
+ query['data.receivedAt'] = {};
125
+ if (filter.since != null) query['data.receivedAt'].$gte = filter.since;
126
+ if (filter.until != null) query['data.receivedAt'].$lte = filter.until;
127
+ }
128
+ return list(t.findings, query, { 'data.receivedAt': -1, 'data.id': 1 }, boundedLimit(filter.limit));
129
+ }
130
+ };
131
+ }
132
+ module.exports = { createMongoGovernorStore, mongoGovernorIndexes };
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+ const { prefixOf, copy } = require('./util');
3
+
4
+ function collections(db, options, fallback, suffixes) {
5
+ if (!db || typeof db.collection !== 'function') throw new TypeError('A native MongoDB Db is required.');
6
+ const prefix = prefixOf(options, fallback);
7
+ return Object.fromEntries(suffixes.map((name) => [name, db.collection(`${prefix}_${name}`)]));
8
+ }
9
+ const read = (row) => copy(row?.data ?? null);
10
+ const keyOf = (...parts) => JSON.stringify(parts);
11
+ const literal = (value) => ({ $literal: copy(value) });
12
+
13
+ async function insertOnce(collection, key, data, extra = {}) {
14
+ const document = { _id: key, data: copy(data), ...extra };
15
+ try {
16
+ await collection.insertOne(document);
17
+ return { record: copy(data), created: true };
18
+ } catch (error) {
19
+ if (error.code !== 11000) throw error;
20
+ const old = await collection.findOne({ _id: key });
21
+ if (!old) throw error;
22
+ return { record: read(old), created: false };
23
+ }
24
+ }
25
+ async function sequence(collection, name) {
26
+ const row = await collection.findOneAndUpdate({ _id: name }, { $inc: { value: 1 } },
27
+ { upsert: true, returnDocument: 'after', includeResultMetadata: false });
28
+ return row.value;
29
+ }
30
+ async function list(collection, filter, sort, limit) {
31
+ return (await collection.find(filter).sort(sort).limit(limit).toArray()).map(read);
32
+ }
33
+ module.exports = { collections, read, keyOf, literal, insertOnce, sequence, list };
package/src/index.d.ts CHANGED
@@ -242,3 +242,6 @@ export const EVIDENCE_DEGREES: readonly EvidenceDegree[];
242
242
  export const EVIDENCE_KINDS: readonly EvidenceKind[];
243
243
  export const PROVENANCE_KINDS: readonly ProvenanceKind[];
244
244
  export const ERASURE_MODES: readonly ['delete', 'anonymise'];
245
+
246
+ export function createMongoConscienceStore(db: import('./governor').MongoDatabase, options?: { prefix?: string }): ConscienceStore;
247
+ export function mongoConscienceIndexes(db: import('./governor').MongoDatabase, options?: { prefix?: string }): Promise<void>;
package/src/index.js CHANGED
@@ -13,7 +13,10 @@ const records = require('./records');
13
13
  const { assertGovernorReady, isGovernor } = require('./governor/governor');
14
14
  const { GovernorError } = require('./governor/util');
15
15
 
16
+ const { createMongoConscienceStore, mongoConscienceIndexes } = require('./mongo');
17
+
16
18
  module.exports = {
19
+ createMongoConscienceStore, mongoConscienceIndexes,
17
20
  createConscience, createMemoryConscienceStore, createPostgresConscienceStore, postgresConscienceSchema,
18
21
  frameStoredText, frameRecordsForModel, wordDelta, matchesFilter,
19
22
  ConscienceError, GovernorError, assertGovernorReady, isGovernor,
package/src/mongo.js ADDED
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+ const { copy } = require('./governor/util');
3
+ const { MAX_LIMIT } = require('./memoryStore');
4
+ const { collections, read, keyOf, insertOnce, sequence, list } = require('./governor/mongoUtil');
5
+ const tables = (db, options) => collections(db, options, 'astratra_conscience', ['records', 'sequences']);
6
+ const key = (e) => keyOf(e.organisationId, e.kind, e.id);
7
+
8
+ /** Explicit, repeatable migration. Identity uses MongoDB's unique _id index. */
9
+ async function mongoConscienceIndexes(db, options) {
10
+ const t = tables(db, options);
11
+ for (const fields of [
12
+ { 'data.organisationId': 1, 'data.kind': 1, 'data.recordedAt': 1, seq: 1 },
13
+ { 'data.organisationId': 1, 'data.userId': 1 },
14
+ { 'data.organisationId': 1, 'data.kind': 1, 'data.decisionId': 1 },
15
+ { 'data.organisationId': 1, 'data.kind': 1, 'data.lessonId': 1 },
16
+ { 'data.organisationId': 1, 'data.kind': 1, 'data.eventAt': 1 },
17
+ { 'data.organisationId': 1, 'data.evidenceRefs': 1 }
18
+ ]) await t.records.createIndex(fields);
19
+ await t.sequences.createIndex({ _id: 1 });
20
+ }
21
+ function createMongoConscienceStore(db, options) {
22
+ const t = tables(db, options);
23
+ return {
24
+ async put(envelope) {
25
+ return insertOnce(t.records, key(envelope), envelope, { seq: await sequence(t.sequences, 'records') });
26
+ },
27
+ async get(kind, organisationId, id) { return read(await t.records.findOne({ _id: keyOf(organisationId, kind, id) })); },
28
+ list(kind, filter = {}) {
29
+ const query = { 'data.organisationId': filter.organisationId, 'data.kind': kind };
30
+ for (const field of ['userId', 'agentId', 'decisionId', 'lessonId']) if (filter[field] !== undefined) query[`data.${field}`] = { $eq: filter[field] };
31
+ if (Array.isArray(filter.decisionIds)) query.$and = [{ 'data.decisionId': { $in: filter.decisionIds } }];
32
+ if (filter.evidenceRef != null) query['data.evidenceRefs'] = { $eq: filter.evidenceRef };
33
+ if (filter.eventFrom != null || filter.eventTo != null) {
34
+ query['data.eventAt'] = { $ne: null };
35
+ if (filter.eventFrom != null) query['data.eventAt'].$gte = filter.eventFrom;
36
+ if (filter.eventTo != null) query['data.eventAt'].$lt = filter.eventTo;
37
+ }
38
+ if (filter.recordedTo != null) query['data.recordedAt'] = { $lte: filter.recordedTo };
39
+ const order = filter.order === 'desc' ? -1 : 1;
40
+ return list(t.records, query, { 'data.recordedAt': order, seq: order }, Math.min(Math.max(Number(filter.limit) || 1000, 1), MAX_LIMIT));
41
+ },
42
+ async replace(envelope) {
43
+ return read(await t.records.findOneAndUpdate({ _id: key(envelope) }, { $set: { data: copy(envelope) } },
44
+ { returnDocument: 'after', includeResultMetadata: false }));
45
+ },
46
+ async remove(kind, organisationId, ids) {
47
+ if (!ids.length) return 0;
48
+ return (await t.records.deleteMany({ _id: { $in: ids.map((id) => keyOf(organisationId, kind, id)) } })).deletedCount;
49
+ }
50
+ };
51
+ }
52
+ module.exports = { createMongoConscienceStore, mongoConscienceIndexes };