@astratra/conscience 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.fr.md +901 -0
- package/README.md +864 -2
- package/package.json +50 -4
- package/src/autoconscience/experiments/common.js +76 -0
- package/src/autoconscience/experiments/execution.js +33 -0
- package/src/autoconscience/experiments/holdout.js +88 -0
- package/src/autoconscience/experiments/laboratory.js +99 -0
- package/src/autoconscience/experiments/replay.js +113 -0
- package/src/autoconscience/experiments/runner.js +54 -0
- package/src/autoconscience/experiments/sandbox.js +58 -0
- package/src/autoconscience/index.d.ts +203 -0
- package/src/autoconscience/index.js +116 -0
- package/src/autoconscience/lessons.js +32 -0
- package/src/autoconscience/shadow/candidate.js +69 -0
- package/src/autoconscience/shadow/index.d.ts +169 -0
- package/src/autoconscience/shadow/index.js +11 -0
- package/src/autoconscience/shadow/report.js +125 -0
- package/src/autoconscience/shadow/runner.js +384 -0
- package/src/autoconscience/shadow/store.js +98 -0
- package/src/autoconscience/signals.js +88 -0
- package/src/autoconscience/statistics.js +84 -0
- package/src/autoconscience/vigilance/abstention.js +123 -0
- package/src/autoconscience/vigilance/adversary.js +145 -0
- package/src/autoconscience/vigilance/common.js +56 -0
- package/src/autoconscience/vigilance/drift.js +306 -0
- package/src/autoconscience/vigilance/followups.js +100 -0
- package/src/autoconscience/vigilance/index.d.ts +190 -0
- package/src/autoconscience/vigilance/index.js +23 -0
- package/src/autoconscience/vigilance/lessonTests.js +40 -0
- package/src/autoconscience/vigilance/library.js +162 -0
- package/src/autoconscience/vigilance/roles.js +122 -0
- package/src/autoconscience/vigilance/store.js +166 -0
- package/src/conscience.js +559 -0
- package/src/diff.js +27 -0
- package/src/errors.js +11 -0
- package/src/governor/approvals.js +132 -0
- package/src/governor/changes.js +269 -0
- package/src/governor/governor.js +286 -0
- package/src/governor/index.d.ts +308 -0
- package/src/governor/index.js +20 -0
- package/src/governor/policy.js +136 -0
- package/src/governor/postgres.js +243 -0
- package/src/governor/review.js +191 -0
- package/src/governor/rollout.js +66 -0
- package/src/governor/stores.js +136 -0
- package/src/governor/util.js +53 -0
- package/src/guard.js +21 -0
- package/src/index.d.ts +244 -0
- package/src/index.js +23 -0
- package/src/memoryStore.js +65 -0
- package/src/postgres.js +93 -0
- package/src/records.js +220 -0
package/src/records.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { ConscienceError } = require('./errors');
|
|
3
|
+
const { classifyCorrection } = require('./autoconscience/signals');
|
|
4
|
+
const { minimum, number } = require('./autoconscience/statistics');
|
|
5
|
+
const { isNonEmptyString, copy } = require('./governor/util');
|
|
6
|
+
|
|
7
|
+
const KINDS = Object.freeze(['decision', 'prediction', 'outcome', 'correction', 'lesson', 'self_model']);
|
|
8
|
+
const OUTCOME_STATUSES = Object.freeze(['succeeded', 'failed', 'unknown']);
|
|
9
|
+
const CORRECTION_CLASSES = Object.freeze(['factual', 'style', 'objective', 'disagreement']);
|
|
10
|
+
const LESSON_STATUSES = Object.freeze(['hypothesis', 'verified', 'to_reassess', 'retired']);
|
|
11
|
+
const EVIDENCE_DEGREES = Object.freeze(['none', 'anecdotal', 'observational', 'experimental', 'replicated']);
|
|
12
|
+
const EVIDENCE_KINDS = Object.freeze(['decision', 'prediction', 'outcome', 'correction', 'external']);
|
|
13
|
+
const PROVENANCE_KINDS = Object.freeze(['sensor', 'tool', 'human', 'feedback', 'system', 'model']);
|
|
14
|
+
|
|
15
|
+
const invalid = (message) => { throw new ConscienceError('invalid_input', message); };
|
|
16
|
+
|
|
17
|
+
function text(value, field, { required = true, max = 20000 } = {}) {
|
|
18
|
+
if (value == null || value === '') { if (required) invalid(`${field} is required.`); return null; }
|
|
19
|
+
if (typeof value !== 'string') invalid(`${field} must be a string.`);
|
|
20
|
+
if (value.length > max) invalid(`${field} is longer than ${max} characters.`);
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function date(value, field, { required = true } = {}) {
|
|
25
|
+
if (value == null) { if (required) invalid(`${field} is required (a date).`); return null; }
|
|
26
|
+
const parsed = value instanceof Date ? value : new Date(value);
|
|
27
|
+
if (Number.isNaN(parsed.getTime())) invalid(`${field} must be a valid date.`);
|
|
28
|
+
return parsed.toISOString();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function unit(value, field, { required = true } = {}) {
|
|
32
|
+
if (value == null) { if (required) invalid(`${field} is required (a number between 0 and 1).`); return null; }
|
|
33
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 1) invalid(`${field} must be a number between 0 and 1.`);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function strings(value, field) {
|
|
38
|
+
if (value == null) return [];
|
|
39
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) invalid(`${field} must be an array of strings.`);
|
|
40
|
+
return [...value];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function oneOf(value, list, field) {
|
|
44
|
+
if (!list.includes(value)) invalid(`${field} must be one of ${list.join(', ')}.`);
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function scopeOf(input) {
|
|
49
|
+
if (!input || typeof input !== 'object') invalid('Input must be an object.');
|
|
50
|
+
if (!isNonEmptyString(input.organisationId)) throw new ConscienceError('organisation_required', 'organisationId is required.');
|
|
51
|
+
if (input.userId != null && !isNonEmptyString(input.userId)) invalid('userId must be a non-empty string.');
|
|
52
|
+
return { organisationId: input.organisationId, userId: input.userId ?? null };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function provenanceOf(value, field) {
|
|
56
|
+
if (!value || typeof value !== 'object') invalid(`${field} { kind, id } is required.`);
|
|
57
|
+
return { kind: oneOf(value.kind, PROVENANCE_KINDS, `${field}.kind`), id: text(value.id, `${field}.id`, { max: 500 }) };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function tracesOf(value = []) {
|
|
61
|
+
if (!Array.isArray(value)) invalid('toolTrace must be an array.');
|
|
62
|
+
const ids = new Set();
|
|
63
|
+
return value.map((trace) => {
|
|
64
|
+
const callId = text(trace.callId, 'toolTrace.callId', { max: 500 });
|
|
65
|
+
if (ids.has(callId)) invalid('Duplicate tool trace callId.');
|
|
66
|
+
ids.add(callId);
|
|
67
|
+
if (trace.empty != null && typeof trace.empty !== 'boolean') invalid('toolTrace.empty must be boolean.');
|
|
68
|
+
return { callId, tool: text(trace.tool, 'toolTrace.tool', { max: 500 }), provider: text(trace.provider, 'toolTrace.provider', { required: false, max: 200 }),
|
|
69
|
+
status: oneOf(trace.status, OUTCOME_STATUSES, 'toolTrace.status'), empty: trace.empty ?? false,
|
|
70
|
+
durationMs: number(trace.durationMs, 'toolTrace.durationMs'), labels: strings(trace.labels, 'toolTrace.labels') };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function decision(input) {
|
|
75
|
+
const scope = scopeOf(input);
|
|
76
|
+
const context = input.context;
|
|
77
|
+
if (!context || typeof context !== 'object') invalid('context { at, summary?, data? } is required.');
|
|
78
|
+
const action = input.action;
|
|
79
|
+
if (!action || typeof action !== 'object') invalid('action { name, kind?, input? } is required.');
|
|
80
|
+
const expected = input.expectedOutcome;
|
|
81
|
+
if (!expected || typeof expected !== 'object') invalid('expectedOutcome { description, metric?, target? } is required.');
|
|
82
|
+
const versions = input.versions;
|
|
83
|
+
if (!versions || typeof versions !== 'object') invalid('versions { model?, tools?, instructions? } are required.');
|
|
84
|
+
const alternatives = input.alternatives ?? [];
|
|
85
|
+
if (!Array.isArray(alternatives)) invalid('alternatives must be an array.');
|
|
86
|
+
const tools = input.tools ?? [];
|
|
87
|
+
if (!Array.isArray(tools) || !tools.every((tool) => tool && isNonEmptyString(tool.name))) invalid('tools must be an array of { name, version?, callId? }.');
|
|
88
|
+
const at = date(context.at, 'context.at');
|
|
89
|
+
return {
|
|
90
|
+
...scope,
|
|
91
|
+
agentId: text(input.agentId, 'agentId', { max: 200 }),
|
|
92
|
+
domain: text(input.domain ?? 'general', 'domain', { max: 200 }),
|
|
93
|
+
justification: { claims: strings(input.justification?.claims, 'justification.claims') },
|
|
94
|
+
toolTrace: tracesOf(input.toolTrace),
|
|
95
|
+
objective: text(input.objective, 'objective'),
|
|
96
|
+
context: { at, summary: text(context.summary, 'context.summary', { required: false }), data: copy(context.data) ?? null },
|
|
97
|
+
action: { name: text(action.name, 'action.name', { max: 500 }), kind: action.kind ?? null, input: copy(action.input) ?? null },
|
|
98
|
+
alternatives: alternatives.map((alt) => (typeof alt === 'string' ? { name: alt, reason: null } : { name: text(alt?.name, 'alternatives[].name', { max: 500 }), reason: alt.reason ?? null })),
|
|
99
|
+
expectedOutcome: { description: text(expected.description, 'expectedOutcome.description'), metric: expected.metric ?? null, target: copy(expected.target) ?? null },
|
|
100
|
+
horizon: date(input.horizon, 'horizon'),
|
|
101
|
+
confidence: unit(input.confidence, 'confidence'),
|
|
102
|
+
permissions: strings(input.permissions, 'permissions'),
|
|
103
|
+
versions: { model: versions.model ?? null, tools: copy(versions.tools) ?? null, instructions: versions.instructions ?? null },
|
|
104
|
+
inputSnapshotRef: text(input.inputSnapshotRef, 'inputSnapshotRef', { required: false, max: 2000 }),
|
|
105
|
+
tools: tools.map((tool) => ({ name: tool.name, version: tool.version ?? null, callId: tool.callId ?? null })),
|
|
106
|
+
decidedAt: date(input.decidedAt ?? at, 'decidedAt')
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function prediction(input) {
|
|
111
|
+
return {
|
|
112
|
+
...scopeOf(input),
|
|
113
|
+
decisionId: text(input.decisionId, 'decisionId', { max: 500 }),
|
|
114
|
+
statement: text(input.statement, 'statement'),
|
|
115
|
+
domain: text(input.domain, 'domain', { required: false, max: 200 }),
|
|
116
|
+
sensor: text(input.sensor, 'sensor', { required: false, max: 200 }),
|
|
117
|
+
probability: unit(input.probability, 'probability'),
|
|
118
|
+
resolvesBy: date(input.resolvesBy, 'resolvesBy'),
|
|
119
|
+
madeAt: date(input.madeAt, 'madeAt', { required: false })
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function outcome(input) {
|
|
124
|
+
const confounders = input.confounders ?? [];
|
|
125
|
+
if (!Array.isArray(confounders) || !confounders.every((item) => typeof item === 'string')) invalid('confounders must be an array of strings.');
|
|
126
|
+
return {
|
|
127
|
+
...scopeOf(input),
|
|
128
|
+
decisionId: text(input.decisionId, 'decisionId', { max: 500 }),
|
|
129
|
+
predictionId: text(input.predictionId, 'predictionId', { required: false, max: 500 }),
|
|
130
|
+
status: oneOf(input.status, OUTCOME_STATUSES, 'status'),
|
|
131
|
+
observation: text(input.observation, 'observation', { required: false }),
|
|
132
|
+
sensor: text(input.sensor, 'sensor', { max: 200 }),
|
|
133
|
+
sources: strings(input.sources, 'sources'),
|
|
134
|
+
uncertainty: unit(input.uncertainty, 'uncertainty', { required: false }),
|
|
135
|
+
confounders: [...confounders],
|
|
136
|
+
provenance: provenanceOf(input.provenance, 'provenance'),
|
|
137
|
+
observedAt: date(input.observedAt, 'observedAt')
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function correction(input) {
|
|
142
|
+
return {
|
|
143
|
+
...scopeOf(input),
|
|
144
|
+
decisionId: text(input.decisionId, 'decisionId', { max: 500 }),
|
|
145
|
+
proposed: text(input.proposed, 'proposed', { max: 100000 }),
|
|
146
|
+
final: text(input.final, 'final', { max: 100000 }),
|
|
147
|
+
class: classifyCorrection(input),
|
|
148
|
+
authorRole: text(input.authorRole, 'authorRole', { max: 200 }),
|
|
149
|
+
correctedAt: date(input.correctedAt, 'correctedAt')
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function lesson(input) {
|
|
154
|
+
if (input.factual != null && typeof input.factual !== 'boolean') invalid('factual must be boolean.');
|
|
155
|
+
const evidence = input.evidence ?? [];
|
|
156
|
+
if (!Array.isArray(evidence)) invalid('evidence must be an array.');
|
|
157
|
+
const status = input.status ?? 'hypothesis';
|
|
158
|
+
oneOf(status, LESSON_STATUSES, 'status');
|
|
159
|
+
const degree = input.evidenceDegree ?? (evidence.length ? 'anecdotal' : 'none');
|
|
160
|
+
oneOf(degree, EVIDENCE_DEGREES, 'evidenceDegree');
|
|
161
|
+
const ids = (value, field) => strings(value, field);
|
|
162
|
+
return {
|
|
163
|
+
...scopeOf(input),
|
|
164
|
+
lessonId: input.lessonId == null ? null : text(input.lessonId, 'lessonId', { max: 500 }),
|
|
165
|
+
agentId: input.agentId == null ? null : text(input.agentId, 'agentId', { max: 200 }),
|
|
166
|
+
summary: text(input.summary, 'summary'),
|
|
167
|
+
domain: text(input.domain ?? 'general', 'domain', { max: 200 }),
|
|
168
|
+
subject: text(input.subject, 'subject', { required: false, max: 500 }),
|
|
169
|
+
stance: input.stance == null ? null : oneOf(input.stance, ['supports', 'opposes'], 'stance'),
|
|
170
|
+
factual: input.factual !== false,
|
|
171
|
+
minimumSample: minimum(input.minimumSample),
|
|
172
|
+
evidence: evidence.map((item, index) => {
|
|
173
|
+
if (!item || typeof item !== 'object') invalid(`evidence[${index}] must be { kind, ref, note? }.`);
|
|
174
|
+
return { kind: oneOf(item.kind, EVIDENCE_KINDS, `evidence[${index}].kind`), ref: text(item.ref, `evidence[${index}].ref`, { max: 2000 }), note: text(item.note, `evidence[${index}].note`, { required: false }) };
|
|
175
|
+
}),
|
|
176
|
+
evidenceDegree: degree,
|
|
177
|
+
status,
|
|
178
|
+
validFrom: date(input.validFrom, 'validFrom', { required: false }),
|
|
179
|
+
validUntil: date(input.validUntil, 'validUntil', { required: false }),
|
|
180
|
+
contradictions: ids(input.contradictions, 'contradictions'),
|
|
181
|
+
supersedes: ids(input.supersedes, 'supersedes'),
|
|
182
|
+
consent: input.consent == null ? null : copy(input.consent),
|
|
183
|
+
retention: input.retention == null ? null : copy(input.retention),
|
|
184
|
+
scenarioRef: text(input.scenarioRef, 'scenarioRef', { required: false, max: 2000 })
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function selfModel(input) {
|
|
189
|
+
const scope = scopeOf(input);
|
|
190
|
+
const capabilities = input.capabilities ?? [];
|
|
191
|
+
if (!Array.isArray(capabilities)) invalid('capabilities must be an array.');
|
|
192
|
+
const tools = input.tools ?? [];
|
|
193
|
+
if (!Array.isArray(tools) || !tools.every((tool) => tool && isNonEmptyString(tool.name))) invalid('tools must be an array of { name, version? }.');
|
|
194
|
+
const metrics = input.metrics ?? {};
|
|
195
|
+
if (typeof metrics !== 'object' || Array.isArray(metrics) || !Object.values(metrics).every((value) => typeof value === 'number' && Number.isFinite(value))) invalid('metrics must map names to finite numbers.');
|
|
196
|
+
return {
|
|
197
|
+
...scope,
|
|
198
|
+
agentId: text(input.agentId, 'agentId', { max: 200 }),
|
|
199
|
+
capabilities: capabilities.map((capability, index) => {
|
|
200
|
+
if (!capability || !isNonEmptyString(capability.name)) invalid(`capabilities[${index}].name is required.`);
|
|
201
|
+
const evidence = strings(capability.evidence, `capabilities[${index}].evidence`);
|
|
202
|
+
if (!evidence.length) {
|
|
203
|
+
throw new ConscienceError('capability_without_evidence', `Capability "${capability.name}" has no test evidence: the self-model only records tested facts.`);
|
|
204
|
+
}
|
|
205
|
+
return { name: capability.name, evidence, score: unit(capability.score, `capabilities[${index}].score`, { required: false }), testedAt: date(capability.testedAt, `capabilities[${index}].testedAt`, { required: false }) };
|
|
206
|
+
}),
|
|
207
|
+
tools: tools.map((tool) => ({ name: tool.name, version: tool.version ?? null })),
|
|
208
|
+
limits: strings(input.limits, 'limits'),
|
|
209
|
+
permissions: strings(input.permissions, 'permissions'),
|
|
210
|
+
domains: strings(input.domains, 'domains'),
|
|
211
|
+
metrics: { ...metrics },
|
|
212
|
+
provenance: provenanceOf(input.provenance, 'provenance'),
|
|
213
|
+
at: date(input.at, 'at', { required: false })
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
module.exports = {
|
|
218
|
+
KINDS, OUTCOME_STATUSES, CORRECTION_CLASSES, LESSON_STATUSES, EVIDENCE_DEGREES, EVIDENCE_KINDS, PROVENANCE_KINDS,
|
|
219
|
+
scopeOf, normalize: { decision, prediction, outcome, correction, lesson, self_model: selfModel }
|
|
220
|
+
};
|