@dcyfr/ai 3.0.1 → 3.0.2
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 +11 -30
- package/README.md +211 -138
- package/dist/.tsbuildinfo +1 -1
- package/dist/ai/core/provider-registry.d.ts +8 -0
- package/dist/ai/core/provider-registry.d.ts.map +1 -1
- package/dist/ai/core/provider-registry.js +17 -2
- package/dist/ai/core/provider-registry.js.map +1 -1
- package/dist/ai/metacognition/config.d.ts +41 -0
- package/dist/ai/metacognition/config.d.ts.map +1 -0
- package/dist/ai/metacognition/config.js +51 -0
- package/dist/ai/metacognition/config.js.map +1 -0
- package/dist/ai/metacognition/governance.d.ts +68 -0
- package/dist/ai/metacognition/governance.d.ts.map +1 -0
- package/dist/ai/metacognition/governance.js +118 -0
- package/dist/ai/metacognition/governance.js.map +1 -0
- package/dist/ai/metacognition/index.d.ts +24 -0
- package/dist/ai/metacognition/index.d.ts.map +1 -0
- package/dist/ai/metacognition/index.js +18 -0
- package/dist/ai/metacognition/index.js.map +1 -0
- package/dist/ai/metacognition/ledger.d.ts +121 -0
- package/dist/ai/metacognition/ledger.d.ts.map +1 -0
- package/dist/ai/metacognition/ledger.js +268 -0
- package/dist/ai/metacognition/ledger.js.map +1 -0
- package/dist/ai/metacognition/runtime.d.ts +205 -0
- package/dist/ai/metacognition/runtime.d.ts.map +1 -0
- package/dist/ai/metacognition/runtime.js +391 -0
- package/dist/ai/metacognition/runtime.js.map +1 -0
- package/dist/ai/metacognition/telemetry.d.ts +144 -0
- package/dist/ai/metacognition/telemetry.d.ts.map +1 -0
- package/dist/ai/metacognition/telemetry.js +149 -0
- package/dist/ai/metacognition/telemetry.js.map +1 -0
- package/dist/ai/metacognition/transfer.d.ts +153 -0
- package/dist/ai/metacognition/transfer.d.ts.map +1 -0
- package/dist/ai/metacognition/transfer.js +182 -0
- package/dist/ai/metacognition/transfer.js.map +1 -0
- package/dist/ai/metacognition/types.d.ts +302 -0
- package/dist/ai/metacognition/types.d.ts.map +1 -0
- package/dist/ai/metacognition/types.js +79 -0
- package/dist/ai/metacognition/types.js.map +1 -0
- package/dist/ai/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/ai/runtime/agent-runtime.js.map +1 -1
- package/dist/ai/src/integrations/linear/index.d.ts +19 -0
- package/dist/ai/src/integrations/linear/index.d.ts.map +1 -0
- package/dist/ai/src/integrations/linear/index.js +20 -0
- package/dist/ai/src/integrations/linear/index.js.map +1 -0
- package/dist/ai/src/integrations/linear/issue-mapper.d.ts +93 -0
- package/dist/ai/src/integrations/linear/issue-mapper.d.ts.map +1 -0
- package/dist/ai/src/integrations/linear/issue-mapper.js +186 -0
- package/dist/ai/src/integrations/linear/issue-mapper.js.map +1 -0
- package/dist/ai/src/integrations/linear/linear-client.d.ts +199 -0
- package/dist/ai/src/integrations/linear/linear-client.d.ts.map +1 -0
- package/dist/ai/src/integrations/linear/linear-client.js +300 -0
- package/dist/ai/src/integrations/linear/linear-client.js.map +1 -0
- package/dist/ai/src/runtime/agent-runtime.js +1 -1
- package/dist/ai/src/runtime/agent-runtime.js.map +1 -1
- package/dist/ai/src/security/prompt-scan-worker.d.ts +63 -0
- package/dist/ai/src/security/prompt-scan-worker.d.ts.map +1 -0
- package/dist/ai/src/security/prompt-scan-worker.js +174 -0
- package/dist/ai/src/security/prompt-scan-worker.js.map +1 -0
- package/dist/ai/src/telemetry/delegation-telemetry.d.ts +10 -0
- package/dist/ai/src/telemetry/delegation-telemetry.d.ts.map +1 -1
- package/dist/ai/src/telemetry/delegation-telemetry.js +23 -0
- package/dist/ai/src/telemetry/delegation-telemetry.js.map +1 -1
- package/package.json +25 -15
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.d.ts","sourceRoot":"","sources":["../../../packages/ai/metacognition/ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,C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;AAMD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,aAAa,EAAE,yBAAyB,CAAC;IACzC,WAAW,EAAE,OAAO,CAAC;CACtB;AAQD;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,WAAW,EAAE,GACrB,eAAe,CA4CjB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,eAAe,GAAG;IAC9D,OAAO,EAAE,WAAW,CAAC;IACrB,WAAW,EAAE,WAAW,CAAC;IACzB,oBAAoB,EAAE,MAAM,CAAC;CAC9B,GAAG,IAAI,CAeP;AAED,sEAAsE;AACtE,qBAAa,kBAAmB,SAAQ,KAAK;aAEzB,UAAU,EAAE,MAAM;gBAAlB,UAAU,EAAE,MAAM,EAClC,OAAO,EAAE,MAAM;CAKlB"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Improvement Ledger — Append-Only Schema and Serializers
|
|
3
|
+
* TLP:AMBER - Internal Use Only
|
|
4
|
+
*
|
|
5
|
+
* Implements the append-only improvement ledger with:
|
|
6
|
+
* - Typed schema for all lifecycle event entries
|
|
7
|
+
* - JSON serializers / deserializers with schema version stamping
|
|
8
|
+
* - File-backed persistent ledger (newline-delimited JSON, one entry per line)
|
|
9
|
+
* - Lineage helpers (task 2.2) for audit reconstruction
|
|
10
|
+
*
|
|
11
|
+
* The file-backed ledger appends atomically per entry; it never rewrites
|
|
12
|
+
* existing lines, ensuring immutability of prior entries.
|
|
13
|
+
*
|
|
14
|
+
* @module ai/metacognition/ledger
|
|
15
|
+
*/
|
|
16
|
+
import { readFileSync, appendFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
17
|
+
import { readFile, appendFile, mkdir } from 'node:fs/promises';
|
|
18
|
+
import { dirname } from 'node:path';
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Serialized record format (on-disk / over-wire schema)
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
/** Schema version for the serialized ledger record format. */
|
|
23
|
+
export const LEDGER_RECORD_SCHEMA_VERSION = '1.0';
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Serializers
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
/**
|
|
28
|
+
* Serialize a LedgerEntry to a newline-delimited JSON record.
|
|
29
|
+
* Each record is a single line; the newline is included in the output.
|
|
30
|
+
*/
|
|
31
|
+
export function serializeLedgerEntry(entry) {
|
|
32
|
+
const envelope = {
|
|
33
|
+
schema: LEDGER_RECORD_SCHEMA_VERSION,
|
|
34
|
+
written_at: new Date().toISOString(),
|
|
35
|
+
entry,
|
|
36
|
+
};
|
|
37
|
+
return JSON.stringify(envelope) + '\n';
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Deserialize a single newline-delimited JSON line to a LedgerEntry.
|
|
41
|
+
* Returns null if the line is empty or cannot be parsed.
|
|
42
|
+
*
|
|
43
|
+
* @throws {LedgerDeserializationError} if the line parses but the schema is unrecognized.
|
|
44
|
+
*/
|
|
45
|
+
export function deserializeLedgerEntry(line) {
|
|
46
|
+
const trimmed = line.trim();
|
|
47
|
+
if (trimmed === '')
|
|
48
|
+
return null;
|
|
49
|
+
let envelope;
|
|
50
|
+
try {
|
|
51
|
+
envelope = JSON.parse(trimmed);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw new LedgerDeserializationError(`Invalid JSON in ledger line: ${trimmed.slice(0, 80)}`);
|
|
55
|
+
}
|
|
56
|
+
if (typeof envelope !== 'object' || envelope === null) {
|
|
57
|
+
throw new LedgerDeserializationError('Ledger record must be a JSON object');
|
|
58
|
+
}
|
|
59
|
+
const rec = envelope;
|
|
60
|
+
if (rec['schema'] !== LEDGER_RECORD_SCHEMA_VERSION) {
|
|
61
|
+
throw new LedgerDeserializationError(`Unsupported ledger record schema version: ${String(rec['schema'])}`);
|
|
62
|
+
}
|
|
63
|
+
if (typeof rec['entry'] !== 'object' || rec['entry'] === null) {
|
|
64
|
+
throw new LedgerDeserializationError('Ledger record missing "entry" field');
|
|
65
|
+
}
|
|
66
|
+
return rec['entry'];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Deserialize all entries from a newline-delimited JSON string.
|
|
70
|
+
* Skips blank lines; throws on malformed non-blank lines.
|
|
71
|
+
*/
|
|
72
|
+
export function deserializeAllEntries(ndjson) {
|
|
73
|
+
return ndjson
|
|
74
|
+
.split('\n')
|
|
75
|
+
.map((line, i) => {
|
|
76
|
+
try {
|
|
77
|
+
return deserializeLedgerEntry(line);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
throw new LedgerDeserializationError(`Error at line ${i + 1}: ${err instanceof Error ? err.message : String(err)}`);
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.filter((e) => e !== null);
|
|
84
|
+
}
|
|
85
|
+
/** Thrown when a ledger record cannot be deserialized. */
|
|
86
|
+
export class LedgerDeserializationError extends Error {
|
|
87
|
+
constructor(message) {
|
|
88
|
+
super(message);
|
|
89
|
+
this.name = 'LedgerDeserializationError';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// File-backed persistent ledger
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
/**
|
|
96
|
+
* File-backed append-only ledger.
|
|
97
|
+
*
|
|
98
|
+
* Storage format: newline-delimited JSON (NDJSON), one LedgerRecordEnvelope per line.
|
|
99
|
+
* Each `append()` call writes exactly one line; prior lines are never modified.
|
|
100
|
+
*
|
|
101
|
+
* The file is read fully on each query (suitable for low-volume policy improvement
|
|
102
|
+
* workloads; not designed for high-throughput streaming use cases).
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const ledger = new FileLedger('/var/lib/dcyfr/metacog-ledger.ndjson');
|
|
107
|
+
* await ledger.append(entry);
|
|
108
|
+
* const entries = await ledger.getEntriesForProposal('proposal-id');
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export class FileLedger {
|
|
112
|
+
filePath;
|
|
113
|
+
constructor(filePath) {
|
|
114
|
+
this.filePath = filePath;
|
|
115
|
+
// Ensure parent directory exists
|
|
116
|
+
const dir = dirname(filePath);
|
|
117
|
+
if (!existsSync(dir)) {
|
|
118
|
+
mkdirSync(dir, { recursive: true });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async append(entry) {
|
|
122
|
+
// Idempotent: check if entry_id already present before writing
|
|
123
|
+
const existing = await this.getLatestEntry(entry.proposal_id);
|
|
124
|
+
if (existing) {
|
|
125
|
+
// Scan all entries for exact entry_id match
|
|
126
|
+
const all = await this.readAllEntries();
|
|
127
|
+
if (all.some((e) => e.entry_id === entry.entry_id))
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const line = serializeLedgerEntry(entry);
|
|
131
|
+
const dir = dirname(this.filePath);
|
|
132
|
+
await mkdir(dir, { recursive: true });
|
|
133
|
+
await appendFile(this.filePath, line, { encoding: 'utf8' });
|
|
134
|
+
}
|
|
135
|
+
async getEntriesForProposal(proposalId) {
|
|
136
|
+
const all = await this.readAllEntries();
|
|
137
|
+
return all.filter((e) => e.proposal_id === proposalId);
|
|
138
|
+
}
|
|
139
|
+
async getLatestEntry(proposalId) {
|
|
140
|
+
const entries = await this.getEntriesForProposal(proposalId);
|
|
141
|
+
return entries[entries.length - 1] ?? null;
|
|
142
|
+
}
|
|
143
|
+
async readAllEntries() {
|
|
144
|
+
const dir = dirname(this.filePath);
|
|
145
|
+
await mkdir(dir, { recursive: true });
|
|
146
|
+
try {
|
|
147
|
+
const content = await readFile(this.filePath, { encoding: 'utf8' });
|
|
148
|
+
return deserializeAllEntries(content);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
if (isNodeError(err) && err.code === 'ENOENT')
|
|
152
|
+
return [];
|
|
153
|
+
throw err;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Synchronous file-backed ledger.
|
|
159
|
+
* For use in environments where async is not available (e.g. process exit hooks).
|
|
160
|
+
*/
|
|
161
|
+
export class SyncFileLedger {
|
|
162
|
+
filePath;
|
|
163
|
+
constructor(filePath) {
|
|
164
|
+
this.filePath = filePath;
|
|
165
|
+
const dir = dirname(filePath);
|
|
166
|
+
if (!existsSync(dir)) {
|
|
167
|
+
mkdirSync(dir, { recursive: true });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
append(entry) {
|
|
171
|
+
const line = serializeLedgerEntry(entry);
|
|
172
|
+
appendFileSync(this.filePath, line, { encoding: 'utf8' });
|
|
173
|
+
}
|
|
174
|
+
readAll() {
|
|
175
|
+
if (!existsSync(this.filePath))
|
|
176
|
+
return [];
|
|
177
|
+
const content = readFileSync(this.filePath, { encoding: 'utf8' });
|
|
178
|
+
return deserializeAllEntries(content);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const TERMINAL_STATES = new Set([
|
|
182
|
+
'applied',
|
|
183
|
+
'rolled_back',
|
|
184
|
+
'rejected',
|
|
185
|
+
]);
|
|
186
|
+
/**
|
|
187
|
+
* Reconstruct the full ordered lifecycle for a proposal from a set of entries.
|
|
188
|
+
*
|
|
189
|
+
* Entries are ordered by following the `previous_entry_id` chain from the
|
|
190
|
+
* root (where `previous_entry_id === null`) to the latest entry.
|
|
191
|
+
* Falls back to insertion order if the chain cannot be fully resolved.
|
|
192
|
+
*
|
|
193
|
+
* @throws {LedgerLineageError} if the chain is broken (gap in previous_entry_id).
|
|
194
|
+
*/
|
|
195
|
+
export function reconstructLineage(proposalId, entries) {
|
|
196
|
+
if (entries.length === 0) {
|
|
197
|
+
throw new LedgerLineageError(proposalId, 'No entries found for proposal');
|
|
198
|
+
}
|
|
199
|
+
// Build id → entry map
|
|
200
|
+
const byId = new Map();
|
|
201
|
+
for (const e of entries)
|
|
202
|
+
byId.set(e.entry_id, e);
|
|
203
|
+
// Find the root (previous_entry_id === null)
|
|
204
|
+
const roots = entries.filter((e) => e.previous_entry_id === null);
|
|
205
|
+
if (roots.length === 0) {
|
|
206
|
+
throw new LedgerLineageError(proposalId, 'No root entry found (no entry with previous_entry_id=null)');
|
|
207
|
+
}
|
|
208
|
+
if (roots.length > 1) {
|
|
209
|
+
throw new LedgerLineageError(proposalId, `Multiple root entries found: ${roots.map((r) => r.entry_id).join(', ')}`);
|
|
210
|
+
}
|
|
211
|
+
// Walk the chain
|
|
212
|
+
const ordered = [];
|
|
213
|
+
let current = roots[0];
|
|
214
|
+
const visited = new Set();
|
|
215
|
+
while (current) {
|
|
216
|
+
if (visited.has(current.entry_id)) {
|
|
217
|
+
throw new LedgerLineageError(proposalId, `Cycle detected at entry ${current.entry_id}`);
|
|
218
|
+
}
|
|
219
|
+
visited.add(current.entry_id);
|
|
220
|
+
ordered.push(current);
|
|
221
|
+
// Find the next entry that points back to this one
|
|
222
|
+
const next = entries.find((e) => e.previous_entry_id === current.entry_id && !visited.has(e.entry_id));
|
|
223
|
+
current = next;
|
|
224
|
+
}
|
|
225
|
+
const latest = ordered[ordered.length - 1];
|
|
226
|
+
return {
|
|
227
|
+
proposal_id: proposalId,
|
|
228
|
+
entries: ordered,
|
|
229
|
+
current_state: latest.state,
|
|
230
|
+
is_terminal: TERMINAL_STATES.has(latest.state),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Reconstruct the rollback trail for a proposal.
|
|
235
|
+
* Returns the applied entry and the rolled_back entry, or null if no rollback occurred.
|
|
236
|
+
*/
|
|
237
|
+
export function extractRollbackTrail(lineage) {
|
|
238
|
+
const rolledBack = lineage.entries.find((e) => e.state === 'rolled_back');
|
|
239
|
+
if (!rolledBack)
|
|
240
|
+
return null;
|
|
241
|
+
const applied = lineage.entries.find((e) => e.state === 'applied');
|
|
242
|
+
if (!applied)
|
|
243
|
+
return null;
|
|
244
|
+
const payload = rolledBack.payload;
|
|
245
|
+
if (payload.kind !== 'rolled_back')
|
|
246
|
+
return null;
|
|
247
|
+
return {
|
|
248
|
+
applied,
|
|
249
|
+
rolled_back: rolledBack,
|
|
250
|
+
restored_snapshot_id: payload.restored_snapshot_id,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/** Thrown when lineage reconstruction fails due to a broken chain. */
|
|
254
|
+
export class LedgerLineageError extends Error {
|
|
255
|
+
proposalId;
|
|
256
|
+
constructor(proposalId, message) {
|
|
257
|
+
super(`Lineage error for proposal ${proposalId}: ${message}`);
|
|
258
|
+
this.proposalId = proposalId;
|
|
259
|
+
this.name = 'LedgerLineageError';
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
// Helpers
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
function isNodeError(err) {
|
|
266
|
+
return typeof err === 'object' && err !== null && 'code' in err;
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../packages/ai/metacognition/ledger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIpC,8EAA8E;AAC9E,wDAAwD;AACxD,8EAA8E;AAE9E,8DAA8D;AAC9D,MAAM,CAAC,MAAM,4BAA4B,GAAG,KAAc,CAAC;AAgB3D,8EAA8E;AAC9E,cAAc;AACd,8EAA8E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,oBAAoB,EAAE,OAAO,CAAC,oBAAoB;KACnD,CAAC;AACJ,CAAC;AAED,sEAAsE;AACtE,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAEzB;IADlB,YACkB,UAAkB,EAClC,OAAe;QAEf,KAAK,CAAC,8BAA8B,UAAU,KAAK,OAAO,EAAE,CAAC,CAAC;QAH9C,eAAU,GAAV,UAAU,CAAQ;QAIlC,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E,SAAS,WAAW,CAAC,GAAY;IAC/B,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,IAAI,MAAM,IAAI,GAAG,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metacognitive Improvement Runtime — Lifecycle State Machine
|
|
3
|
+
* TLP:AMBER - Internal Use Only
|
|
4
|
+
*
|
|
5
|
+
* Implements the governed lifecycle state machine for improvement proposals:
|
|
6
|
+
* propose → evaluate → approve → apply → (rollback)
|
|
7
|
+
*
|
|
8
|
+
* Enforcement rules:
|
|
9
|
+
* - Schema version must be compatible before any proposal is processed.
|
|
10
|
+
* - `production_direct` scope requires at least `third_party_audit`.
|
|
11
|
+
* - TLP:RED context requires `human_required`.
|
|
12
|
+
* - State transitions are validated; invalid transitions throw.
|
|
13
|
+
* - All lifecycle events are written to the injected ImprovementLedger.
|
|
14
|
+
*
|
|
15
|
+
* Feature flag: controlled by `MetacognitiveRuntimeConfig.enabled`.
|
|
16
|
+
* When disabled the runtime is a structural no-op (see task 1.3 for env wiring).
|
|
17
|
+
*
|
|
18
|
+
* @module ai/metacognition/runtime
|
|
19
|
+
* @version 1.0.0
|
|
20
|
+
*/
|
|
21
|
+
import type { VerificationPolicy, VerificationResult } from '../types/delegation-contracts.js';
|
|
22
|
+
import type { ImprovementLifecycleState, ImprovementProposal, ImprovementEvaluationResult, MetacognitiveRuntimeConfig, PolicySnapshot } from './types.js';
|
|
23
|
+
/** A single entry in the improvement ledger. */
|
|
24
|
+
export interface LedgerEntry {
|
|
25
|
+
readonly entry_id: string;
|
|
26
|
+
readonly proposal_id: string;
|
|
27
|
+
readonly state: ImprovementLifecycleState;
|
|
28
|
+
readonly actor: string;
|
|
29
|
+
readonly timestamp: string;
|
|
30
|
+
/** ID of the previous ledger entry for this proposal (null for first). */
|
|
31
|
+
readonly previous_entry_id: string | null;
|
|
32
|
+
readonly payload: LedgerEntryPayload;
|
|
33
|
+
}
|
|
34
|
+
/** Discriminated union payload per lifecycle state. */
|
|
35
|
+
export type LedgerEntryPayload = {
|
|
36
|
+
kind: 'proposed';
|
|
37
|
+
proposal: ImprovementProposal;
|
|
38
|
+
source_snapshot: PolicySnapshot;
|
|
39
|
+
} | {
|
|
40
|
+
kind: 'evaluated';
|
|
41
|
+
result: ImprovementEvaluationResult;
|
|
42
|
+
} | {
|
|
43
|
+
kind: 'approved';
|
|
44
|
+
verification_result: VerificationResult;
|
|
45
|
+
approved_by: string;
|
|
46
|
+
} | {
|
|
47
|
+
kind: 'rejected';
|
|
48
|
+
reason: string;
|
|
49
|
+
at_state: ImprovementLifecycleState;
|
|
50
|
+
} | {
|
|
51
|
+
kind: 'applied';
|
|
52
|
+
result_snapshot: PolicySnapshot;
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'rolled_back';
|
|
55
|
+
reason: string;
|
|
56
|
+
restored_snapshot_id: string;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Minimal ledger interface consumed by the runtime.
|
|
60
|
+
* Full implementation (append-only persistence, lineage queries) is in task 2.1.
|
|
61
|
+
*/
|
|
62
|
+
export interface ImprovementLedger {
|
|
63
|
+
/** Append a new entry. Must be idempotent on duplicate entry_ids. */
|
|
64
|
+
append(entry: LedgerEntry): Promise<void>;
|
|
65
|
+
/** Return all entries for a proposal in insertion order. */
|
|
66
|
+
getEntriesForProposal(proposal_id: string): Promise<LedgerEntry[]>;
|
|
67
|
+
/** Return the latest entry for a proposal, or null. */
|
|
68
|
+
getLatestEntry(proposal_id: string): Promise<LedgerEntry | null>;
|
|
69
|
+
}
|
|
70
|
+
/** Thrown when an invalid lifecycle transition is attempted. */
|
|
71
|
+
export declare class InvalidLifecycleTransitionError extends Error {
|
|
72
|
+
readonly proposal_id: string;
|
|
73
|
+
readonly from: ImprovementLifecycleState;
|
|
74
|
+
readonly to: ImprovementLifecycleState;
|
|
75
|
+
constructor(proposal_id: string, from: ImprovementLifecycleState, to: ImprovementLifecycleState);
|
|
76
|
+
}
|
|
77
|
+
/** Thrown when a policy snapshot fails schema version compatibility check. */
|
|
78
|
+
export declare class SchemaIncompatibleError extends Error {
|
|
79
|
+
readonly snapshot_id: string;
|
|
80
|
+
readonly snapshot_version: string;
|
|
81
|
+
readonly runtime_version: string;
|
|
82
|
+
constructor(snapshot_id: string, snapshot_version: string, runtime_version: string);
|
|
83
|
+
}
|
|
84
|
+
/** Thrown when an approve request fails governance threshold checks. */
|
|
85
|
+
export declare class GovernanceViolationError extends Error {
|
|
86
|
+
readonly proposal_id: string;
|
|
87
|
+
readonly provided: VerificationPolicy;
|
|
88
|
+
readonly required: VerificationPolicy;
|
|
89
|
+
readonly reason: string;
|
|
90
|
+
constructor(proposal_id: string, provided: VerificationPolicy, required: VerificationPolicy, reason: string);
|
|
91
|
+
}
|
|
92
|
+
/** Thrown when the runtime is disabled and a proposal is submitted. */
|
|
93
|
+
export declare class RuntimeDisabledError extends Error {
|
|
94
|
+
constructor();
|
|
95
|
+
}
|
|
96
|
+
export type IdGenerator = () => string;
|
|
97
|
+
/**
|
|
98
|
+
* Governed runtime for the improvement lifecycle state machine.
|
|
99
|
+
*
|
|
100
|
+
* Usage:
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const runtime = new MetacognitiveImprovementRuntime(config, ledger);
|
|
103
|
+
* await runtime.propose(snapshot, proposal);
|
|
104
|
+
* await runtime.evaluate(proposal_id, evaluationResult);
|
|
105
|
+
* await runtime.approve(proposal_id, verificationResult, 'human-reviewer-1');
|
|
106
|
+
* await runtime.apply(proposal_id, resultSnapshot);
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare class MetacognitiveImprovementRuntime {
|
|
110
|
+
private readonly config;
|
|
111
|
+
private readonly ledger;
|
|
112
|
+
private readonly generateId;
|
|
113
|
+
constructor(config: MetacognitiveRuntimeConfig, ledger: ImprovementLedger, generateId?: IdGenerator);
|
|
114
|
+
/**
|
|
115
|
+
* Submit an improvement proposal.
|
|
116
|
+
*
|
|
117
|
+
* Validates:
|
|
118
|
+
* 1. Runtime is enabled.
|
|
119
|
+
* 2. Source snapshot schema version is compatible with this runtime.
|
|
120
|
+
*
|
|
121
|
+
* On success appends a `proposed` ledger entry.
|
|
122
|
+
*
|
|
123
|
+
* @throws {RuntimeDisabledError} if the runtime is disabled.
|
|
124
|
+
* @throws {SchemaIncompatibleError} if the snapshot schema is incompatible.
|
|
125
|
+
*/
|
|
126
|
+
propose(sourceSnapshot: PolicySnapshot, proposal: ImprovementProposal): Promise<LedgerEntry>;
|
|
127
|
+
/**
|
|
128
|
+
* Record the evaluation result for a proposal.
|
|
129
|
+
*
|
|
130
|
+
* Transitions `proposed → evaluated` (pass) or `proposed → rejected` (fail).
|
|
131
|
+
* Rejection occurs when the evaluation did not pass.
|
|
132
|
+
*
|
|
133
|
+
* @throws {InvalidLifecycleTransitionError} if proposal is not in `proposed` state.
|
|
134
|
+
*/
|
|
135
|
+
evaluate(proposalId: string, result: ImprovementEvaluationResult, evaluatedBy: string): Promise<LedgerEntry>;
|
|
136
|
+
/**
|
|
137
|
+
* Approve an evaluated proposal.
|
|
138
|
+
*
|
|
139
|
+
* Transitions `evaluated → approved`.
|
|
140
|
+
*
|
|
141
|
+
* Governance enforcement:
|
|
142
|
+
* - Resolves the required verification policy from the proposal's context.
|
|
143
|
+
* - Rejects if `verificationResult.verification_method` does not meet the
|
|
144
|
+
* required policy strength.
|
|
145
|
+
* - TLP:RED always requires `human_required`.
|
|
146
|
+
* - `production_direct` scope requires at minimum `third_party_audit`.
|
|
147
|
+
*
|
|
148
|
+
* @throws {InvalidLifecycleTransitionError} if proposal is not in `evaluated` state.
|
|
149
|
+
* @throws {GovernanceViolationError} if the verification policy is insufficient.
|
|
150
|
+
*/
|
|
151
|
+
approve(proposalId: string, verificationResult: VerificationResult, approvedBy: string): Promise<LedgerEntry>;
|
|
152
|
+
/**
|
|
153
|
+
* Apply an approved proposal, recording the resulting policy snapshot.
|
|
154
|
+
*
|
|
155
|
+
* Transitions `approved → applied`.
|
|
156
|
+
*
|
|
157
|
+
* @throws {InvalidLifecycleTransitionError} if proposal is not in `approved` state.
|
|
158
|
+
*/
|
|
159
|
+
apply(proposalId: string, resultSnapshot: PolicySnapshot, appliedBy: string): Promise<LedgerEntry>;
|
|
160
|
+
/**
|
|
161
|
+
* Roll back an applied proposal to a prior snapshot.
|
|
162
|
+
*
|
|
163
|
+
* Transitions `applied → rolled_back`.
|
|
164
|
+
*
|
|
165
|
+
* @throws {InvalidLifecycleTransitionError} if proposal is not in `applied` state.
|
|
166
|
+
*/
|
|
167
|
+
rollback(proposalId: string, reason: string, restoredSnapshotId: string, rolledBackBy: string): Promise<LedgerEntry>;
|
|
168
|
+
/**
|
|
169
|
+
* Return the current lifecycle state of a proposal, or null if unknown.
|
|
170
|
+
*/
|
|
171
|
+
getState(proposalId: string): Promise<ImprovementLifecycleState | null>;
|
|
172
|
+
private assertEnabled;
|
|
173
|
+
private assertSchemaCompatible;
|
|
174
|
+
private assertValidTransition;
|
|
175
|
+
private requireLatestEntry;
|
|
176
|
+
private requireProposedEntry;
|
|
177
|
+
private buildGovernanceViolationReason;
|
|
178
|
+
/**
|
|
179
|
+
* Assert governance config invariants at construction time.
|
|
180
|
+
* Prevents misconfiguration that would weaken production safety gates.
|
|
181
|
+
*/
|
|
182
|
+
private assertGovernanceInvariants;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Simple in-memory ImprovementLedger.
|
|
186
|
+
* Used in tests and non-production shadow mode.
|
|
187
|
+
* Not suitable for production (no persistence, no durability guarantees).
|
|
188
|
+
*/
|
|
189
|
+
export declare class InMemoryImprovementLedger implements ImprovementLedger {
|
|
190
|
+
private entries;
|
|
191
|
+
append(entry: LedgerEntry): Promise<void>;
|
|
192
|
+
getEntriesForProposal(proposalId: string): Promise<LedgerEntry[]>;
|
|
193
|
+
getLatestEntry(proposalId: string): Promise<LedgerEntry | null>;
|
|
194
|
+
/** Test helper: clear all entries. */
|
|
195
|
+
clear(): void;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Create a MetacognitiveImprovementRuntime with the default governance config.
|
|
199
|
+
*/
|
|
200
|
+
export declare function createMetacognitiveRuntime(options?: {
|
|
201
|
+
enabled?: boolean;
|
|
202
|
+
ledger?: ImprovementLedger;
|
|
203
|
+
generateId?: IdGenerator;
|
|
204
|
+
}): MetacognitiveImprovementRuntime;
|
|
205
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../packages/ai/metacognition/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAC/F,OAAO,KAAK,EACV,yBAAyB,EACzB,mBAAmB,EACnB,2BAA2B,EAC3B,0BAA0B,EAC1B,cAAc,EACf,MAAM,YAAY,CAAC;AAepB,gDAAgD;AAChD,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,yBAAyB,CAAC;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC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