@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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
* Aggregation of ShadowRuns into an honest report (proposition 21). Pure and
|
|
4
|
+
* deterministic: the same runs give the same report. It reuses the statistics
|
|
5
|
+
* of P2 (Wilson intervals, minimum sample, "insufficient data") and the
|
|
6
|
+
* anti-Goodhart check of P2 (target against counter-measures). The conclusion is
|
|
7
|
+
* one of four codes and never says "proven": a shadow comparison on a sample of
|
|
8
|
+
* real requests can only make an improvement promising, never established.
|
|
9
|
+
*/
|
|
10
|
+
const { honestStatistics, wilsonInterval } = require('../statistics');
|
|
11
|
+
const { checkCounterMetrics } = require('../signals');
|
|
12
|
+
const { differenceInterval } = require('../vigilance/drift');
|
|
13
|
+
|
|
14
|
+
const CONCLUSIONS = Object.freeze(['insufficient_data', 'regression', 'no_difference', 'promising']);
|
|
15
|
+
const round = (value) => (value == null ? null : Math.round(value * 1e6) / 1e6);
|
|
16
|
+
const mean = (list) => (list.length ? list.reduce((sum, v) => sum + v, 0) / list.length : null);
|
|
17
|
+
const roundInterval = (i) => (i ? { low: round(i.low), high: round(i.high), level: i.level } : null);
|
|
18
|
+
|
|
19
|
+
function statsOf(statuses, options) {
|
|
20
|
+
const s = honestStatistics(statuses, options);
|
|
21
|
+
return { succeeded: s.succeeded, failed: s.failed, unknown: s.unknown, sampleSize: s.sampleSize, minimumSample: s.minimumSample,
|
|
22
|
+
rate: round(s.rate), interval: roundInterval(s.interval), status: s.status, conclusion: s.conclusion };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function metricReport(metric, runs, options) {
|
|
26
|
+
const cand = [], stable = [];
|
|
27
|
+
const paired = { bothSucceeded: 0, onlyStableSucceeded: 0, onlyCandidateSucceeded: 0, bothFailed: 0, unknown: 0 };
|
|
28
|
+
for (const run of runs) {
|
|
29
|
+
const score = run.scores?.[metric.name] ?? { candidate: 'unknown', stable: 'unknown' };
|
|
30
|
+
cand.push(score.candidate); stable.push(score.stable);
|
|
31
|
+
if (score.candidate === 'unknown' || score.stable === 'unknown') paired.unknown += 1;
|
|
32
|
+
else if (score.candidate === 'succeeded') paired[score.stable === 'succeeded' ? 'bothSucceeded' : 'onlyCandidateSucceeded'] += 1;
|
|
33
|
+
else paired[score.stable === 'succeeded' ? 'onlyStableSucceeded' : 'bothFailed'] += 1;
|
|
34
|
+
}
|
|
35
|
+
const candidate = statsOf(cand, options), stableStats = statsOf(stable, options);
|
|
36
|
+
const observed = candidate.status === 'observed' && stableStats.status === 'observed';
|
|
37
|
+
// Difference of two proportions (candidate minus stable), Newcombe hybrid score interval built on the Wilson intervals of P2.
|
|
38
|
+
const difference = observed ? differenceInterval(stableStats.succeeded, stableStats.sampleSize, candidate.succeeded, candidate.sampleSize, options.level) : null;
|
|
39
|
+
return { name: metric.name, critical: metric.critical === true, candidate, stable: stableStats, paired, status: observed ? 'observed' : 'insufficient_data',
|
|
40
|
+
difference: difference && { difference: round(difference.difference), low: round(difference.low), high: round(difference.high), level: difference.level } };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function buildReport({ runs, metrics, target, options, limits = {} }) {
|
|
44
|
+
const stats = { minimumSample: options.minimumSample, level: options.level };
|
|
45
|
+
const reasons = [];
|
|
46
|
+
const perMetric = Object.fromEntries(metrics.map((metric) => [metric.name, metricReport(metric, runs, stats)]));
|
|
47
|
+
|
|
48
|
+
const criticalRegressions = [];
|
|
49
|
+
for (const run of runs) {
|
|
50
|
+
for (const metric of metrics) {
|
|
51
|
+
const score = run.scores?.[metric.name];
|
|
52
|
+
if (metric.critical === true && score?.stable === 'succeeded' && score.candidate === 'failed') {
|
|
53
|
+
criticalRegressions.push({ runId: run.id, metric: metric.name, createdAt: run.createdAt, request: run.request,
|
|
54
|
+
candidateOutput: run.candidate?.output ?? null, stableOutput: run.stable?.output ?? null, incidents: [...(run.incidents ?? [])] });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const withIncident = runs.filter((run) => (run.incidents ?? []).length > 0);
|
|
60
|
+
const incidentInterval = wilsonInterval(withIncident.length, runs.length, options.level);
|
|
61
|
+
const byCode = {};
|
|
62
|
+
for (const run of runs) for (const code of new Set(run.incidents ?? [])) byCode[code] = (byCode[code] ?? 0) + 1;
|
|
63
|
+
const incidents = { runs: withIncident.length, rate: runs.length ? round(withIncident.length / runs.length) : null, interval: roundInterval(incidentInterval),
|
|
64
|
+
byCode, status: runs.length >= options.minimumSample ? 'observed' : 'insufficient_data', maxRate: options.maxIncidentRate };
|
|
65
|
+
|
|
66
|
+
const agree = statsOf(runs.map((run) => (run.agreement ? 'succeeded' : 'failed')), stats);
|
|
67
|
+
const writes = { total: 0, runsWithWrites: 0, byTool: {}, neverExecuted: true };
|
|
68
|
+
for (const run of runs) {
|
|
69
|
+
const list = run.writeIntentions ?? [];
|
|
70
|
+
writes.total += list.length; if (list.length) writes.runsWithWrites += 1;
|
|
71
|
+
for (const w of list) writes.byTool[w.tool] = (writes.byTool[w.tool] ?? 0) + 1;
|
|
72
|
+
}
|
|
73
|
+
const costs = runs.map((run) => run.cost).filter((v) => typeof v === 'number');
|
|
74
|
+
const stableCosts = runs.map((run) => run.stableCost).filter((v) => typeof v === 'number');
|
|
75
|
+
const durations = runs.map((run) => run.durationMs).filter((v) => typeof v === 'number');
|
|
76
|
+
const cost = { total: round(costs.reduce((a, b) => a + b, 0)), mean: round(mean(costs)), stableMean: stableCosts.length === runs.length ? round(mean(stableCosts)) : null };
|
|
77
|
+
const duration = { mean: round(mean(durations)), max: durations.length ? Math.max(...durations) : null };
|
|
78
|
+
|
|
79
|
+
// Anti-Goodhart (P2): the target improves only if no counter-measure got worse than its limit.
|
|
80
|
+
const targetReport = perMetric[target];
|
|
81
|
+
const counters = [];
|
|
82
|
+
for (const metric of metrics) {
|
|
83
|
+
if (metric.name === target) continue;
|
|
84
|
+
const report = perMetric[metric.name];
|
|
85
|
+
if (report.status !== 'observed') continue;
|
|
86
|
+
counters.push({ name: metric.name, direction: 'higher_is_better', before: report.stable.rate, after: report.candidate.rate, maxRegression: metric.maxRegression });
|
|
87
|
+
}
|
|
88
|
+
let costCounterUnmeasured = false;
|
|
89
|
+
if (limits.cost != null) {
|
|
90
|
+
if (cost.stableMean == null || cost.mean == null) costCounterUnmeasured = true;
|
|
91
|
+
else counters.push({ name: 'cost', direction: 'lower_is_better', before: cost.stableMean, after: cost.mean, maxRegression: limits.cost });
|
|
92
|
+
}
|
|
93
|
+
const observed = targetReport.status === 'observed';
|
|
94
|
+
const antiGoodhart = observed ? { target, ...checkCounterMetrics({ name: target, direction: 'higher_is_better', before: targetReport.stable.rate, after: targetReport.candidate.rate }, counters),
|
|
95
|
+
counterMeasures: counters.length } : null;
|
|
96
|
+
|
|
97
|
+
const insufficient = runs.length < options.minimumSample || Object.values(perMetric).some((m) => m.status !== 'observed') || costCounterUnmeasured;
|
|
98
|
+
let conclusion;
|
|
99
|
+
if (criticalRegressions.length > options.maxCriticalRegressions) { conclusion = 'regression'; reasons.push('critical_regression'); }
|
|
100
|
+
else if (insufficient) {
|
|
101
|
+
conclusion = 'insufficient_data'; reasons.push('insufficient_data');
|
|
102
|
+
if (runs.length < options.minimumSample) reasons.push('below_minimum_sample');
|
|
103
|
+
for (const m of Object.values(perMetric)) if (m.status !== 'observed') reasons.push(`metric_insufficient:${m.name}`);
|
|
104
|
+
if (costCounterUnmeasured) reasons.push('cost_unmeasured');
|
|
105
|
+
} else {
|
|
106
|
+
const declined = Object.values(perMetric).filter((m) => m.difference && m.difference.high < 0).map((m) => m.name);
|
|
107
|
+
const unsafe = incidentInterval && incidentInterval.low > options.maxIncidentRate;
|
|
108
|
+
if (declined.length) reasons.push(...declined.map((name) => `significant_decline:${name}`));
|
|
109
|
+
if (unsafe) reasons.push('candidate_incidents');
|
|
110
|
+
if (antiGoodhart.violations.length) reasons.push(...antiGoodhart.violations.map((v) => `counter_measure_regressed:${v.name}`));
|
|
111
|
+
if (reasons.length) conclusion = 'regression';
|
|
112
|
+
else if (targetReport.difference.low > 0 && antiGoodhart.status === 'accepted') { conclusion = 'promising'; reasons.push('target_improved_counter_measures_held'); }
|
|
113
|
+
else { conclusion = 'no_difference'; reasons.push('no_significant_difference'); }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const times = runs.map((run) => run.createdAt).sort();
|
|
117
|
+
return {
|
|
118
|
+
conclusion, reasons,
|
|
119
|
+
sample: { runs: runs.length, minimumSample: options.minimumSample, level: options.level, status: runs.length >= options.minimumSample ? 'observed' : 'insufficient_data',
|
|
120
|
+
firstAt: times[0] ?? null, lastAt: times.at(-1) ?? null, sampleRates: [...new Set(runs.map((run) => run.sampleRate))].sort() },
|
|
121
|
+
target, metrics: perMetric, antiGoodhart, criticalRegressions, incidents, agreement: agree, writeIntentions: writes, cost, durationMs: duration
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = { buildReport, CONCLUSIONS };
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
* Shadow mode (propositions 21 and 32). A candidate strategy, already approved
|
|
4
|
+
* by humans, processes a share of the real requests in silence, in parallel to
|
|
5
|
+
* the stable strategy. Its answers are never returned: they are compared with
|
|
6
|
+
* the stable answers by deterministic metrics and kept, masked, as ShadowRuns.
|
|
7
|
+
*
|
|
8
|
+
* - every tool of the candidate goes through the P3 sandbox: reads are
|
|
9
|
+
* simulated or replayed from recordings, writes are never executed and only
|
|
10
|
+
* journalled as intentions; the candidate has no network and no host object;
|
|
11
|
+
* - sampling is deterministic, the budget is capped, the governor kill switch
|
|
12
|
+
* stops everything at once;
|
|
13
|
+
* - `observe` never blocks the stable answer and never throws;
|
|
14
|
+
* - the report concludes with a code (never "proven") and can only feed a
|
|
15
|
+
* PROPOSAL to the governor, never a promotion.
|
|
16
|
+
*
|
|
17
|
+
* No language model is imported here. A candidate may call an injected model
|
|
18
|
+
* only as a sandbox tool (simulated or recorded), never over the network.
|
|
19
|
+
*/
|
|
20
|
+
const crypto = require('node:crypto');
|
|
21
|
+
const { performance } = require('node:perf_hooks');
|
|
22
|
+
const { ConscienceError } = require('../../errors');
|
|
23
|
+
const { fingerprintOf, copy } = require('../../governor/util');
|
|
24
|
+
const { ready: governorReady, data, agentSource, timeoutOf, invalid, deepFreeze } = require('../experiments/common');
|
|
25
|
+
const { sandboxState } = require('../experiments/sandbox');
|
|
26
|
+
const { execute } = require('../experiments/execution');
|
|
27
|
+
const { statisticsOptions } = require('../experiments/common');
|
|
28
|
+
const { number } = require('../statistics');
|
|
29
|
+
const { maskerOf, actorOf, isNonEmptyString } = require('../vigilance/common');
|
|
30
|
+
const { resolveCandidate } = require('./candidate');
|
|
31
|
+
const { buildReport } = require('./report');
|
|
32
|
+
const { createMemoryShadowStore, SHADOW_STORE_METHODS, SHADOW_RUN_LIMIT, shadowEraser } = require('./store');
|
|
33
|
+
|
|
34
|
+
const MAX_STORED_CHARS = 20000;
|
|
35
|
+
const METRIC_NAME = /^[a-z][a-z0-9_]{0,60}$/;
|
|
36
|
+
const bindingOf = (options) => ({ governor: options.governor, organisationId: options.organisationId });
|
|
37
|
+
const isHalt = (error) => error?.code === 'governor_halted';
|
|
38
|
+
|
|
39
|
+
function metricsOf(list, target, limits) {
|
|
40
|
+
if (!Array.isArray(list) || !list.length || list.length > 20) invalid('metrics must be a non-empty list (at most 20).');
|
|
41
|
+
const names = new Set();
|
|
42
|
+
const out = list.map((metric) => {
|
|
43
|
+
if (!metric || typeof metric.name !== 'string' || !METRIC_NAME.test(metric.name) || typeof metric.score !== 'function') invalid('A metric needs a name and a score({ input, output, side }) function.');
|
|
44
|
+
if (names.has(metric.name)) invalid('Duplicate metric name.');
|
|
45
|
+
names.add(metric.name);
|
|
46
|
+
if (metric.critical != null && typeof metric.critical !== 'boolean') invalid('critical must be a boolean.');
|
|
47
|
+
const maxRegression = metric.maxRegression == null ? 0.02 : number(metric.maxRegression, 'maxRegression', 0, 1);
|
|
48
|
+
return Object.freeze({ name: metric.name, score: metric.score, critical: metric.critical === true, maxRegression });
|
|
49
|
+
});
|
|
50
|
+
if (typeof target !== 'string' || !names.has(target)) invalid('target must name one of the metrics.');
|
|
51
|
+
if (out.length < 2 && limits.cost == null) invalid('Anti-Goodhart: declare at least one counter-measure besides the target (another metric, or limits.cost).');
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function limitsOf(input) {
|
|
56
|
+
if (input == null) return {};
|
|
57
|
+
if (typeof input !== 'object') invalid('limits must be an object.');
|
|
58
|
+
return input.cost == null ? {} : { cost: number(input.cost, 'limits.cost', 0) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function budgetOf(input, timeoutMs) {
|
|
62
|
+
if (!input || typeof input !== 'object') invalid('A budget is required: { maxRuns?, maxCost?, maxDurationMs? } with at least one cap.');
|
|
63
|
+
const out = { costPerRun: input.costPerRun == null ? 1 : number(input.costPerRun, 'budget.costPerRun', 0), costOf: input.costOf ?? null, governorAgentId: input.governorAgentId ?? null };
|
|
64
|
+
if (out.costOf != null && typeof out.costOf !== 'function') invalid('budget.costOf must be a function.');
|
|
65
|
+
if (out.governorAgentId != null && !isNonEmptyString(out.governorAgentId)) invalid('budget.governorAgentId must be a non-empty string.');
|
|
66
|
+
for (const key of ['maxRuns', 'maxCost', 'maxDurationMs']) out[key] = input[key] == null ? null : number(input[key], `budget.${key}`, 0);
|
|
67
|
+
if (out.maxRuns != null && !Number.isInteger(out.maxRuns)) invalid('budget.maxRuns must be an integer.');
|
|
68
|
+
if (out.maxRuns == null && out.maxCost == null && out.maxDurationMs == null) invalid('A budget needs at least one cap (maxRuns, maxCost or maxDurationMs).');
|
|
69
|
+
out.maxRuns = Math.min(out.maxRuns ?? SHADOW_RUN_LIMIT, SHADOW_RUN_LIMIT);
|
|
70
|
+
void timeoutMs;
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Mask every string of a plain JSON value. Own `__proto__` keys stay ordinary data. */
|
|
75
|
+
async function maskData(mask, value, depth = 0) {
|
|
76
|
+
if (typeof value === 'string') {
|
|
77
|
+
const masked = await mask(value);
|
|
78
|
+
if (typeof masked !== 'string') throw new ConscienceError('masker_invalid', 'The masker must return a string.');
|
|
79
|
+
return masked;
|
|
80
|
+
}
|
|
81
|
+
if (value == null || typeof value !== 'object' || depth > 32) return value;
|
|
82
|
+
if (Array.isArray(value)) return Promise.all(value.map((item) => maskData(mask, item, depth + 1)));
|
|
83
|
+
const entries = [];
|
|
84
|
+
for (const key of Object.keys(value)) entries.push([key, await maskData(mask, value[key], depth + 1)]);
|
|
85
|
+
return Object.fromEntries(entries);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function storable(mask, value) {
|
|
89
|
+
const masked = await maskData(mask, value);
|
|
90
|
+
const text = JSON.stringify(masked);
|
|
91
|
+
if (text !== undefined && text.length > MAX_STORED_CHARS) return { masked: { truncated: true, size: text.length, fingerprint: fingerprintOf(masked) }, fingerprint: fingerprintOf(masked) };
|
|
92
|
+
return { masked, fingerprint: fingerprintOf(masked) };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function mergeRecordings(config, recordings) {
|
|
96
|
+
if (!recordings) return config;
|
|
97
|
+
const out = copy(config);
|
|
98
|
+
for (const [tool, list] of Object.entries(recordings)) {
|
|
99
|
+
if (!Object.hasOwn(out, tool) || out[tool].kind !== 'read') invalid(`Recordings are for read tools of the sandbox only (${tool}).`);
|
|
100
|
+
if (!Array.isArray(list) || !list.every((r) => r && Object.hasOwn(r, 'input') && Object.hasOwn(r, 'output'))) invalid('recordings must contain input/output pairs.');
|
|
101
|
+
out[tool].recordings = [...list, ...(out[tool].recordings ?? [])];
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function scoreStatus(value) {
|
|
107
|
+
if (value === true) return 'succeeded';
|
|
108
|
+
if (value === false) return 'failed';
|
|
109
|
+
return 'unknown';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @returns {Promise<ShadowRunner>}
|
|
114
|
+
*/
|
|
115
|
+
async function createShadowRunner(options = {}) {
|
|
116
|
+
await governorReady(options);
|
|
117
|
+
const binding = bindingOf(options);
|
|
118
|
+
const { governor, organisationId } = binding;
|
|
119
|
+
const mask = maskerOf(options.masker);
|
|
120
|
+
const store = options.store ?? createMemoryShadowStore();
|
|
121
|
+
for (const method of SHADOW_STORE_METHODS) if (typeof store[method] !== 'function') throw new ConscienceError('store_invalid', `Shadow store is missing ${method}().`);
|
|
122
|
+
const now = options.now ?? Date.now;
|
|
123
|
+
const iso = () => new Date(now()).toISOString();
|
|
124
|
+
|
|
125
|
+
const candidateInput = options.candidate;
|
|
126
|
+
if (!candidateInput || typeof candidateInput !== 'object') invalid('candidate is required: { agent, proposalId } or { agent, kind, id }.');
|
|
127
|
+
const source = agentSource(candidateInput.agent);
|
|
128
|
+
const reference = { proposalId: candidateInput.proposalId, kind: candidateInput.kind, id: candidateInput.id, versionId: candidateInput.versionId };
|
|
129
|
+
const verified = await resolveCandidate(governor, organisationId, reference);
|
|
130
|
+
|
|
131
|
+
if (!options.sandbox) invalid('A sandbox created with createSandbox() is required: the candidate has no other way to use a tool.');
|
|
132
|
+
const sandboxConfig = (await sandboxState(options.sandbox, binding)).config;
|
|
133
|
+
|
|
134
|
+
const sampleRate = number(options.sampleRate, 'sampleRate', Number.MIN_VALUE, 1);
|
|
135
|
+
const timeoutMs = timeoutOf(options.timeoutMs);
|
|
136
|
+
const budget = budgetOf(options.budget, timeoutMs);
|
|
137
|
+
const limits = limitsOf(options.limits);
|
|
138
|
+
const metrics = metricsOf(options.metrics, options.target, limits);
|
|
139
|
+
const target = options.target;
|
|
140
|
+
const defaults = { ...statisticsOptions(options),
|
|
141
|
+
maxCriticalRegressions: options.maxCriticalRegressions == null ? 0 : number(options.maxCriticalRegressions, 'maxCriticalRegressions', 0),
|
|
142
|
+
maxIncidentRate: options.maxIncidentRate == null ? 0.05 : number(options.maxIncidentRate, 'maxIncidentRate', 0, 1) };
|
|
143
|
+
const maxConcurrent = options.maxConcurrent == null ? 4 : number(options.maxConcurrent, 'maxConcurrent', 1, 64);
|
|
144
|
+
const deadlineMs = options.deadlineMs == null ? timeoutMs + 3000 : number(options.deadlineMs, 'deadlineMs', 10);
|
|
145
|
+
|
|
146
|
+
const agentFingerprint = fingerprintOf(source);
|
|
147
|
+
const shadowId = options.id ?? `shadow_${fingerprintOf({ organisationId, candidate: verified.fingerprint, agent: agentFingerprint, sandbox: fingerprintOf(sandboxConfig) }).slice(0, 24)}`;
|
|
148
|
+
if (!isNonEmptyString(shadowId)) invalid('id must be a non-empty string.');
|
|
149
|
+
const descriptor = deepFreeze({ source: verified.source, kind: verified.kind, id: verified.id, versionId: verified.versionId, proposalId: verified.proposalId,
|
|
150
|
+
fingerprint: verified.fingerprint, agentFingerprint });
|
|
151
|
+
|
|
152
|
+
const stats = { seen: 0, sampled: 0, executed: 0, recorded: 0, skipped: {}, errors: {} };
|
|
153
|
+
const bump = (bag, key) => { bag[key] = (bag[key] ?? 0) + 1; };
|
|
154
|
+
let stopped = null;
|
|
155
|
+
const stop = (reason) => { if (!stopped) stopped = { reason, at: iso() }; };
|
|
156
|
+
const inflight = new Set();
|
|
157
|
+
const pending = new Set();
|
|
158
|
+
|
|
159
|
+
/* ---- budget: runs and cost are reserved before a run, time is settled after ---- */
|
|
160
|
+
let used = null;
|
|
161
|
+
const loading = (async () => {
|
|
162
|
+
const runs = await store.listShadowRuns({ organisationId, shadowId, limit: SHADOW_RUN_LIMIT });
|
|
163
|
+
used = { runs: runs.length, cost: runs.reduce((s, r) => s + (r.cost ?? 0), 0), durationMs: runs.reduce((s, r) => s + (r.durationMs ?? 0), 0) };
|
|
164
|
+
})();
|
|
165
|
+
loading.catch(() => {}); // The failure is raised again, inside the run that needs the totals.
|
|
166
|
+
async function reserve() {
|
|
167
|
+
await loading;
|
|
168
|
+
const full = used.runs + 1 > budget.maxRuns || (budget.maxCost != null && used.cost + budget.costPerRun > budget.maxCost)
|
|
169
|
+
|| (budget.maxDurationMs != null && used.durationMs >= budget.maxDurationMs);
|
|
170
|
+
if (full) return null;
|
|
171
|
+
used.runs += 1; used.cost += budget.costPerRun;
|
|
172
|
+
return { cost: budget.costPerRun };
|
|
173
|
+
}
|
|
174
|
+
const release = (ticket) => { if (ticket) { used.runs -= 1; used.cost -= ticket.cost; } };
|
|
175
|
+
const settle = (ticket, cost, durationMs) => { used.cost += cost - ticket.cost; used.durationMs += durationMs; };
|
|
176
|
+
|
|
177
|
+
const sampled = (requestId) => crypto.createHash('sha256').update(`${shadowId}\u0000${requestId}`).digest().readUIntBE(0, 6) / 2 ** 48 < sampleRate;
|
|
178
|
+
|
|
179
|
+
async function stillAllowed() {
|
|
180
|
+
try { await governorReady(binding); } catch (error) {
|
|
181
|
+
if (isHalt(error)) { stop('governor_halted'); return 'governor_halted'; }
|
|
182
|
+
return 'governor_unavailable';
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const current = await resolveCandidate(governor, organisationId, reference);
|
|
186
|
+
if (current.fingerprint !== verified.fingerprint) { stop('candidate_changed'); return 'candidate_changed'; }
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (isHalt(error)) { stop('governor_halted'); return 'governor_halted'; }
|
|
189
|
+
stop('candidate_unavailable'); return 'candidate_unavailable';
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function scoreRun({ input, candidateOutput, completed, stableOutput }, incidents) {
|
|
195
|
+
const scores = {};
|
|
196
|
+
const one = async (metric, output, side) => {
|
|
197
|
+
try {
|
|
198
|
+
const verdict = await metric.score({ input: copy(input), output: copy(output), side });
|
|
199
|
+
if (verdict !== true && verdict !== false && verdict != null) { incidents.add('scoring_invalid'); return 'unknown'; }
|
|
200
|
+
return scoreStatus(verdict);
|
|
201
|
+
} catch (_error) { incidents.add('scoring_failed'); return 'unknown'; }
|
|
202
|
+
};
|
|
203
|
+
for (const metric of metrics) {
|
|
204
|
+
// A candidate that produced nothing cannot have succeeded: it is a failure of the candidate, not an unknown.
|
|
205
|
+
scores[metric.name] = { candidate: completed ? await one(metric, candidateOutput, 'candidate') : 'failed', stable: await one(metric, stableOutput, 'stable') };
|
|
206
|
+
}
|
|
207
|
+
return scores;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function pipeline(prepared, control) {
|
|
211
|
+
const { request, stableResult, recordings, runKey } = prepared;
|
|
212
|
+
const refusal = await stillAllowed();
|
|
213
|
+
if (refusal) { bump(stats.skipped, refusal); return { status: 'skipped', reason: refusal }; }
|
|
214
|
+
if (await store.getShadowRun(organisationId, runKey)) { bump(stats.skipped, 'duplicate'); return { status: 'skipped', reason: 'duplicate' }; }
|
|
215
|
+
|
|
216
|
+
const ticket = await reserve();
|
|
217
|
+
if (!ticket) { stop('budget_exhausted'); bump(stats.skipped, 'budget_exhausted'); return { status: 'skipped', reason: 'budget_exhausted' }; }
|
|
218
|
+
try {
|
|
219
|
+
if (budget.governorAgentId) {
|
|
220
|
+
const spend = await governor.authorizeSpend({ organisationId, id: `shadow:${runKey}`, agentId: budget.governorAgentId, cycleId: shadowId, cost: budget.costPerRun });
|
|
221
|
+
if (!spend.allowed) {
|
|
222
|
+
const halted = spend.reasons.includes('governor_halted');
|
|
223
|
+
stop(halted ? 'governor_halted' : 'governor_budget'); release(ticket); bump(stats.skipped, stopped.reason);
|
|
224
|
+
return { status: 'skipped', reason: stopped.reason };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const config = mergeRecordings(sandboxConfig, recordings);
|
|
228
|
+
const started = performance.now();
|
|
229
|
+
stats.executed += 1;
|
|
230
|
+
const result = await execute({ governor, organisationId, source, input: { input: request.input, version: verified.content }, config, timeoutMs });
|
|
231
|
+
const durationMs = Math.round(performance.now() - started);
|
|
232
|
+
const incidents = new Set();
|
|
233
|
+
const completed = result.status === 'completed';
|
|
234
|
+
if (!completed) incidents.add(result.reason === 'timeout' ? 'candidate_timeout' : 'candidate_failed');
|
|
235
|
+
const candidateOutput = completed ? (result.output ?? null) : null;
|
|
236
|
+
const journal = completed ? (result.journal ?? []) : [];
|
|
237
|
+
const writes = journal.filter((entry) => entry.kind === 'write');
|
|
238
|
+
|
|
239
|
+
const scores = await scoreRun({ input: request.input, candidateOutput, completed, stableOutput: stableResult.output }, incidents);
|
|
240
|
+
let cost = budget.costPerRun;
|
|
241
|
+
if (budget.costOf) {
|
|
242
|
+
try {
|
|
243
|
+
const value = budget.costOf({ durationMs, status: completed ? 'completed' : 'failed', writeIntentions: writes.length, output: copy(candidateOutput) });
|
|
244
|
+
if (typeof value === 'number' && Number.isFinite(value) && value >= 0) {
|
|
245
|
+
// costPerRun is the ceiling reserved for a run: a run cannot cost more than the budget set aside for it.
|
|
246
|
+
cost = Math.min(value, budget.costPerRun);
|
|
247
|
+
if (value > budget.costPerRun) incidents.add('cost_over_ceiling');
|
|
248
|
+
} else incidents.add('cost_invalid');
|
|
249
|
+
} catch (_error) { incidents.add('cost_invalid'); }
|
|
250
|
+
}
|
|
251
|
+
const agreement = completed && fingerprintOf(candidateOutput) === fingerprintOf(stableResult.output);
|
|
252
|
+
|
|
253
|
+
// Privacy first: nothing leaves this function unmasked, and a masker failure keeps the run out of the store.
|
|
254
|
+
const maskedInput = await storable(mask, request.input);
|
|
255
|
+
const maskedCandidate = await storable(mask, candidateOutput);
|
|
256
|
+
const maskedStable = await storable(mask, stableResult.output);
|
|
257
|
+
const intentions = [];
|
|
258
|
+
for (const entry of writes) intentions.push({ sequence: entry.sequence, tool: entry.tool, input: (await storable(mask, entry.input)).masked });
|
|
259
|
+
|
|
260
|
+
const record = {
|
|
261
|
+
id: runKey, shadowId, organisationId, userId: request.userId, createdAt: iso(), sampleRate,
|
|
262
|
+
candidate: { ...copy(descriptor), status: completed ? 'completed' : result.reason === 'timeout' ? 'timeout' : 'error', reason: completed ? null : result.reason ?? 'execution_failed',
|
|
263
|
+
output: maskedCandidate.masked, outputFingerprint: maskedCandidate.fingerprint },
|
|
264
|
+
request: { input: maskedInput.masked, fingerprint: maskedInput.fingerprint },
|
|
265
|
+
stable: { output: maskedStable.masked, outputFingerprint: maskedStable.fingerprint, durationMs: stableResult.durationMs },
|
|
266
|
+
scores, agreement, writeIntentions: intentions, toolCalls: { read: journal.length - writes.length, write: writes.length },
|
|
267
|
+
durationMs, cost, stableCost: stableResult.cost, incidents: [...incidents].sort()
|
|
268
|
+
};
|
|
269
|
+
if (control.abandoned) { release(ticket); return { status: 'timeout', reason: 'deadline' }; }
|
|
270
|
+
// The kill switch is read again right before writing: a run finished after the stop is dropped.
|
|
271
|
+
try { await governorReady(binding); } catch (error) {
|
|
272
|
+
release(ticket);
|
|
273
|
+
if (isHalt(error)) { stop('governor_halted'); bump(stats.skipped, 'governor_halted'); return { status: 'skipped', reason: 'governor_halted' }; }
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
const { created } = await store.putShadowRun(record);
|
|
277
|
+
settle(ticket, cost, durationMs);
|
|
278
|
+
if (created) stats.recorded += 1;
|
|
279
|
+
// The acknowledgement carries no output of the candidate: nothing here can be forwarded to a user by mistake.
|
|
280
|
+
return { status: 'recorded', runId: runKey, created };
|
|
281
|
+
} catch (error) {
|
|
282
|
+
release(ticket);
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function prepare(input) {
|
|
288
|
+
const request = input?.request, stableResult = input?.stableResult;
|
|
289
|
+
if (!request || typeof request !== 'object' || !isNonEmptyString(request.id)) invalid('request needs a stable id.');
|
|
290
|
+
if (request.userId != null && !isNonEmptyString(request.userId)) invalid('userId must be a non-empty string.');
|
|
291
|
+
if (!stableResult || typeof stableResult !== 'object' || !Object.hasOwn(stableResult, 'output')) invalid('stableResult needs the output of the stable strategy.');
|
|
292
|
+
const optionalNumber = (value, label) => (value == null ? null : number(value, label, 0));
|
|
293
|
+
const recordings = input.recordings == null ? null : data(input.recordings);
|
|
294
|
+
return {
|
|
295
|
+
request: { id: request.id, userId: request.userId ?? null, input: data(request.input === undefined ? null : request.input) },
|
|
296
|
+
stableResult: { output: data(stableResult.output === undefined ? null : stableResult.output), durationMs: optionalNumber(stableResult.durationMs, 'stableResult.durationMs'), cost: optionalNumber(stableResult.cost, 'stableResult.cost') },
|
|
297
|
+
recordings, runKey: `shadowrun_${fingerprintOf({ shadowId, requestId: request.id }).slice(0, 32)}`
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function observe(input) {
|
|
302
|
+
const immediate = (status, reason) => ({ scheduled: false, reason, done: Promise.resolve({ status, reason }) });
|
|
303
|
+
try {
|
|
304
|
+
stats.seen += 1;
|
|
305
|
+
let prepared;
|
|
306
|
+
try { prepared = prepare(input); } catch (_error) { bump(stats.errors, 'invalid_input'); return immediate('rejected', 'invalid_input'); }
|
|
307
|
+
if (stopped) { bump(stats.skipped, stopped.reason); return immediate('skipped', stopped.reason); }
|
|
308
|
+
if (!sampled(prepared.request.id)) { bump(stats.skipped, 'not_sampled'); return immediate('skipped', 'not_sampled'); }
|
|
309
|
+
if (inflight.has(prepared.runKey)) { bump(stats.skipped, 'duplicate'); return immediate('skipped', 'duplicate'); }
|
|
310
|
+
if (inflight.size >= maxConcurrent) { bump(stats.skipped, 'busy'); return immediate('skipped', 'busy'); }
|
|
311
|
+
stats.sampled += 1;
|
|
312
|
+
inflight.add(prepared.runKey);
|
|
313
|
+
const control = { abandoned: false };
|
|
314
|
+
let timer;
|
|
315
|
+
const deadline = new Promise((resolve) => { timer = setTimeout(() => { control.abandoned = true; resolve({ status: 'timeout', reason: 'deadline' }); }, deadlineMs); timer.unref?.(); });
|
|
316
|
+
// Errors are isolated: whatever happens, `done` resolves and carries no message of the candidate.
|
|
317
|
+
const work = pipeline(prepared, control).catch((error) => {
|
|
318
|
+
if (isHalt(error)) { stop('governor_halted'); bump(stats.skipped, 'governor_halted'); return { status: 'skipped', reason: 'governor_halted' }; }
|
|
319
|
+
const code = typeof error?.code === 'string' ? error.code : 'shadow_failed';
|
|
320
|
+
bump(stats.errors, code);
|
|
321
|
+
return { status: 'error', reason: code };
|
|
322
|
+
});
|
|
323
|
+
const done = Promise.race([work, deadline]).finally(() => { clearTimeout(timer); inflight.delete(prepared.runKey); pending.delete(done); });
|
|
324
|
+
pending.add(done);
|
|
325
|
+
return { scheduled: true, reason: null, done };
|
|
326
|
+
} catch (_error) {
|
|
327
|
+
return immediate('error', 'shadow_failed');
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function flush() { await Promise.allSettled([...pending]); }
|
|
332
|
+
|
|
333
|
+
async function listRuns(input = {}) {
|
|
334
|
+
await governorReady(binding);
|
|
335
|
+
return store.listShadowRuns({ organisationId, shadowId, limit: input.limit });
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async function report(input = {}) {
|
|
339
|
+
await governorReady(binding);
|
|
340
|
+
const reportOptions = { ...defaults, ...(input.minimumSample != null || input.level != null ? statisticsOptions({ ...defaults, ...input }) : {}) };
|
|
341
|
+
const runs = await store.listShadowRuns({ organisationId, shadowId, limit: SHADOW_RUN_LIMIT });
|
|
342
|
+
if (runs.length >= SHADOW_RUN_LIMIT) throw new ConscienceError('evaluation_limit', 'Too many shadow runs for one report: stop this shadow and start a new one.');
|
|
343
|
+
const body = buildReport({ runs, metrics, target, options: reportOptions, limits });
|
|
344
|
+
const result = {
|
|
345
|
+
organisationId, shadowId, candidate: copy(descriptor), generatedAt: iso(), ...body,
|
|
346
|
+
stopped: stopped && { ...stopped },
|
|
347
|
+
provesImprovement: false, causality: 'not_established', applied: false, proposal: null, finding: null
|
|
348
|
+
};
|
|
349
|
+
const digest = fingerprintOf({ conclusion: result.conclusion, reasons: result.reasons, sample: result.sample, metrics: result.metrics, target });
|
|
350
|
+
if (input.propose && result.conclusion === 'promising') {
|
|
351
|
+
const proposedBy = actorOf(input.propose.proposedBy, { kind: 'engine', id: 'shadow-runner' });
|
|
352
|
+
// The recommendation is a proposal like any other: a human decides, and deployChange / promoteChange stay theirs to call.
|
|
353
|
+
result.proposal = await governor.evaluatePolicy({
|
|
354
|
+
organisationId, target: { kind: descriptor.kind, id: `shadow:${descriptor.id}:${descriptor.versionId ?? descriptor.proposalId}` },
|
|
355
|
+
change: { shadowEvidence: { conclusion: result.conclusion, runs: result.sample.runs, reportFingerprint: digest, candidateFingerprint: descriptor.fingerprint, recommendation: 'review_for_gradual_deployment' } },
|
|
356
|
+
justification: { code: 'shadow_promising', target, reasons: result.reasons, interval: result.metrics[target].difference },
|
|
357
|
+
risk: input.propose.risk ?? 'medium', proposedBy, owner: input.propose.owner ?? null
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
if (input.recordedBy) {
|
|
361
|
+
result.finding = await governor.recordFinding({
|
|
362
|
+
organisationId, runId: shadowId, code: `shadow_${result.conclusion}`, severity: result.conclusion === 'regression' ? 'high' : 'info',
|
|
363
|
+
subject: { kind: 'shadow', id: shadowId }, evidenceRefs: result.criticalRegressions.map((c) => c.runId), sampleSize: result.sample.runs,
|
|
364
|
+
metrics: { conclusion: result.conclusion, reasons: result.reasons, reportFingerprint: digest }, recordedBy: actorOf(input.recordedBy)
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return result;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async function eraseSubject({ organisationId: org, userId } = {}) {
|
|
371
|
+
governor.assertSingleOrganisation(organisationId, org);
|
|
372
|
+
if (!isNonEmptyString(userId)) invalid('eraseSubject needs organisationId and userId.');
|
|
373
|
+
return { removed: await store.removeShadowRuns({ organisationId, userId }) };
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return Object.freeze({
|
|
377
|
+
id: shadowId, candidate: descriptor, sampleRate, observe, report, flush, listRuns, eraseSubject,
|
|
378
|
+
eraser: shadowEraser(store),
|
|
379
|
+
stats: () => ({ ...stats, skipped: { ...stats.skipped }, errors: { ...stats.errors }, inflight: inflight.size, stopped: stopped && { ...stopped } }),
|
|
380
|
+
isStopped: () => stopped !== null
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
module.exports = { createShadowRunner };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
* Storage of the shadow mode (P4): one ShadowRun per sampled request. A memory
|
|
4
|
+
* store and a PostgreSQL store with the same contract. Every read and write is
|
|
5
|
+
* scoped by organisation; a run is written once (idempotent by id); runs of one
|
|
6
|
+
* person are removed with `removeShadowRuns` (erasure).
|
|
7
|
+
*/
|
|
8
|
+
const { copy, prefixOf, requireQueryable } = require('../../governor/util');
|
|
9
|
+
const { ConscienceError } = require('../../errors');
|
|
10
|
+
|
|
11
|
+
const MAX_LIMIT = 10000;
|
|
12
|
+
const limitOf = (value) => Math.min(Math.max(Number(value) || 1000, 1), MAX_LIMIT);
|
|
13
|
+
const DEFAULT_PREFIX = 'astratra_shadow';
|
|
14
|
+
const keyOf = (organisationId, id) => `${organisationId}\u0000${id}`;
|
|
15
|
+
const SHADOW_STORE_METHODS = ['putShadowRun', 'getShadowRun', 'listShadowRuns', 'removeShadowRuns'];
|
|
16
|
+
|
|
17
|
+
/** Eraser for createConscience({ erasers: [...] }): removes the shadow runs of a person with the rest of their data. */
|
|
18
|
+
function shadowEraser(store) {
|
|
19
|
+
if (!store || typeof store.removeShadowRuns !== 'function') throw new ConscienceError('store_invalid', 'A shadow store is required.');
|
|
20
|
+
return async ({ organisationId, userId }) => ({ name: 'shadow_runs', count: await store.removeShadowRuns({ organisationId, userId }) });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function createMemoryShadowStore() {
|
|
24
|
+
const runs = new Map();
|
|
25
|
+
return {
|
|
26
|
+
async putShadowRun(record) {
|
|
27
|
+
const key = keyOf(record.organisationId, record.id);
|
|
28
|
+
if (runs.has(key)) return { record: copy(runs.get(key)), created: false };
|
|
29
|
+
runs.set(key, copy(record));
|
|
30
|
+
return { record: copy(record), created: true };
|
|
31
|
+
},
|
|
32
|
+
async getShadowRun(organisationId, id) { return copy(runs.get(keyOf(organisationId, id)) ?? null); },
|
|
33
|
+
async listShadowRuns(filter = {}) {
|
|
34
|
+
return [...runs.values()]
|
|
35
|
+
.filter((row) => row.organisationId === filter.organisationId && (filter.shadowId === undefined || row.shadowId === filter.shadowId)
|
|
36
|
+
&& (filter.userId === undefined || row.userId === filter.userId))
|
|
37
|
+
.sort((a, b) => (a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : a.id < b.id ? -1 : 1))
|
|
38
|
+
.slice(0, limitOf(filter.limit)).map(copy);
|
|
39
|
+
},
|
|
40
|
+
async removeShadowRuns({ organisationId, userId }) {
|
|
41
|
+
let count = 0;
|
|
42
|
+
for (const [key, row] of runs) if (row.organisationId === organisationId && row.userId === userId) { runs.delete(key); count += 1; }
|
|
43
|
+
return count;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** SQL that creates the shadow table. Idempotent. */
|
|
49
|
+
function postgresShadowSchema(options) {
|
|
50
|
+
const p = prefixOf(options, DEFAULT_PREFIX);
|
|
51
|
+
return `CREATE TABLE IF NOT EXISTS "${p}_runs" (
|
|
52
|
+
organisation_id text NOT NULL,
|
|
53
|
+
id text NOT NULL,
|
|
54
|
+
shadow_id text NOT NULL,
|
|
55
|
+
user_id text,
|
|
56
|
+
created_at timestamptz NOT NULL,
|
|
57
|
+
envelope jsonb NOT NULL,
|
|
58
|
+
PRIMARY KEY (organisation_id, id)
|
|
59
|
+
);
|
|
60
|
+
CREATE INDEX IF NOT EXISTS "${p}_runs_shadow_idx" ON "${p}_runs" (organisation_id, shadow_id, created_at);
|
|
61
|
+
CREATE INDEX IF NOT EXISTS "${p}_runs_user_idx" ON "${p}_runs" (organisation_id, user_id);`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const read = (row) => (row ? (typeof row.envelope === 'string' ? JSON.parse(row.envelope) : row.envelope) : null);
|
|
65
|
+
|
|
66
|
+
/** Shadow store on PostgreSQL. `db` is any `{ query(sql, params) }`; every value is a parameter. */
|
|
67
|
+
function createPostgresShadowStore(db, options) {
|
|
68
|
+
requireQueryable(db);
|
|
69
|
+
const p = prefixOf(options, DEFAULT_PREFIX);
|
|
70
|
+
const table = `"${p}_runs"`;
|
|
71
|
+
return {
|
|
72
|
+
async putShadowRun(record) {
|
|
73
|
+
const inserted = await db.query(
|
|
74
|
+
`INSERT INTO ${table} (organisation_id, id, shadow_id, user_id, created_at, envelope)
|
|
75
|
+
VALUES ($1, $2, $3, $4, $5::timestamptz, $6::jsonb) ON CONFLICT (organisation_id, id) DO NOTHING RETURNING envelope`,
|
|
76
|
+
[record.organisationId, record.id, record.shadowId, record.userId ?? null, record.createdAt, JSON.stringify(record)]);
|
|
77
|
+
if (inserted.rows.length) return { record: read(inserted.rows[0]), created: true };
|
|
78
|
+
return { record: await this.getShadowRun(record.organisationId, record.id), created: false };
|
|
79
|
+
},
|
|
80
|
+
async getShadowRun(organisationId, id) {
|
|
81
|
+
return read((await db.query(`SELECT envelope FROM ${table} WHERE organisation_id = $1 AND id = $2`, [organisationId, id])).rows[0]);
|
|
82
|
+
},
|
|
83
|
+
async listShadowRuns(filter = {}) {
|
|
84
|
+
const params = [filter.organisationId];
|
|
85
|
+
const parts = ['organisation_id = $1'];
|
|
86
|
+
if (filter.shadowId !== undefined) { params.push(filter.shadowId); parts.push(`shadow_id = $${params.length}`); }
|
|
87
|
+
if (filter.userId !== undefined) { if (filter.userId === null) parts.push('user_id IS NULL'); else { params.push(filter.userId); parts.push(`user_id = $${params.length}`); } }
|
|
88
|
+
params.push(limitOf(filter.limit));
|
|
89
|
+
return (await db.query(`SELECT envelope FROM ${table} WHERE ${parts.join(' AND ')} ORDER BY created_at ASC, id ASC LIMIT $${params.length}`, params)).rows.map(read);
|
|
90
|
+
},
|
|
91
|
+
async removeShadowRuns({ organisationId, userId }) {
|
|
92
|
+
const result = await db.query(`DELETE FROM ${table} WHERE organisation_id = $1 AND user_id = $2`, [organisationId, userId]);
|
|
93
|
+
return result.rowCount ?? 0;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = { createMemoryShadowStore, createPostgresShadowStore, postgresShadowSchema, shadowEraser, SHADOW_STORE_METHODS, SHADOW_RUN_LIMIT: MAX_LIMIT };
|