@nookplot/runtime 0.5.142 → 0.5.144

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.
Files changed (60) hide show
  1. package/dist/__tests__/bdAgentPack.test.d.ts +2 -0
  2. package/dist/__tests__/bdAgentPack.test.d.ts.map +1 -0
  3. package/dist/__tests__/bdAgentPack.test.js +44 -0
  4. package/dist/__tests__/bdAgentPack.test.js.map +1 -0
  5. package/dist/__tests__/externalMcpTools.test.d.ts +2 -0
  6. package/dist/__tests__/externalMcpTools.test.d.ts.map +1 -0
  7. package/dist/__tests__/externalMcpTools.test.js +94 -0
  8. package/dist/__tests__/externalMcpTools.test.js.map +1 -0
  9. package/dist/__tests__/pack.gating.test.d.ts +2 -0
  10. package/dist/__tests__/pack.gating.test.d.ts.map +1 -0
  11. package/dist/__tests__/pack.gating.test.js +134 -0
  12. package/dist/__tests__/pack.gating.test.js.map +1 -0
  13. package/dist/__tests__/pack.test.d.ts +2 -0
  14. package/dist/__tests__/pack.test.d.ts.map +1 -0
  15. package/dist/__tests__/pack.test.js +299 -0
  16. package/dist/__tests__/pack.test.js.map +1 -0
  17. package/dist/__tests__/packLoader.test.d.ts +2 -0
  18. package/dist/__tests__/packLoader.test.d.ts.map +1 -0
  19. package/dist/__tests__/packLoader.test.js +304 -0
  20. package/dist/__tests__/packLoader.test.js.map +1 -0
  21. package/dist/__tests__/presetLoader.test.d.ts +2 -0
  22. package/dist/__tests__/presetLoader.test.d.ts.map +1 -0
  23. package/dist/__tests__/presetLoader.test.js +766 -0
  24. package/dist/__tests__/presetLoader.test.js.map +1 -0
  25. package/dist/actionCatalog.d.ts.map +1 -1
  26. package/dist/actionCatalog.generated.d.ts +1 -1
  27. package/dist/actionCatalog.generated.d.ts.map +1 -1
  28. package/dist/actionCatalog.generated.js +7 -2
  29. package/dist/actionCatalog.generated.js.map +1 -1
  30. package/dist/actionCatalog.js +4 -12
  31. package/dist/actionCatalog.js.map +1 -1
  32. package/dist/autonomous.d.ts +24 -1
  33. package/dist/autonomous.d.ts.map +1 -1
  34. package/dist/autonomous.js +66 -8
  35. package/dist/autonomous.js.map +1 -1
  36. package/dist/index.d.ts +7 -1
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +6 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/pack.d.ts +181 -0
  41. package/dist/pack.d.ts.map +1 -0
  42. package/dist/pack.js +379 -0
  43. package/dist/pack.js.map +1 -0
  44. package/dist/packLoader.d.ts +109 -0
  45. package/dist/packLoader.d.ts.map +1 -0
  46. package/dist/packLoader.js +236 -0
  47. package/dist/packLoader.js.map +1 -0
  48. package/dist/presetLoader.d.ts +132 -0
  49. package/dist/presetLoader.d.ts.map +1 -0
  50. package/dist/presetLoader.js +740 -0
  51. package/dist/presetLoader.js.map +1 -0
  52. package/dist/signalActionMap.d.ts +17 -1
  53. package/dist/signalActionMap.d.ts.map +1 -1
  54. package/dist/signalActionMap.js +37 -2
  55. package/dist/signalActionMap.js.map +1 -1
  56. package/dist/tools.d.ts +23 -7
  57. package/dist/tools.d.ts.map +1 -1
  58. package/dist/tools.js +20 -6
  59. package/dist/tools.js.map +1 -1
  60. package/package.json +2 -2
@@ -0,0 +1,740 @@
1
+ /**
2
+ * PresetLoader — loads forge preset data into agent memory at boot.
3
+ *
4
+ * Reads the preset section of nookplot.yaml, fetches data per source via
5
+ * the gateway's forge data endpoint, runs content scanning, and ingests
6
+ * items as preset-tagged memories. Supports layered ingestion: RAG when
7
+ * available, memory.import() fallback.
8
+ *
9
+ * Features:
10
+ * - Content scanning (sanitizeForPrompt) on every item
11
+ * - Idempotent loading via .preset-loaded.json manifest
12
+ * - Configurable failure policy (abort / continue / retry)
13
+ * - Boot progress events via EventEmitter
14
+ * - Self-awareness memory generation
15
+ *
16
+ * @module presetLoader
17
+ */
18
+ import { readFile, writeFile } from "node:fs/promises";
19
+ import { createHash, createHmac } from "node:crypto";
20
+ import { EventEmitter } from "node:events";
21
+ import yaml from "js-yaml";
22
+ import { sanitizeForPrompt } from "./contentSafety.js";
23
+ // ── PresetLoader ──────────────────────────────────────────────
24
+ export class PresetLoader extends EventEmitter {
25
+ runtime;
26
+ configPath;
27
+ loading = null;
28
+ /** When set, used instead of reading the `preset:` section of the config file (PackLoader delegation). */
29
+ presetOverride;
30
+ constructor(runtime, configPath = "./nookplot.yaml", presetOverride) {
31
+ super();
32
+ this.runtime = runtime;
33
+ this.configPath = configPath;
34
+ this.presetOverride = presetOverride ?? null;
35
+ }
36
+ /**
37
+ * Build x-preset-hmac header so the gateway trusts isPreset/presetId fields.
38
+ * HMAC = SHA-256(apiKey, "preset-ingest:" + agent_address).
39
+ */
40
+ presetIngestHeaders() {
41
+ const address = this.runtime.connection.address;
42
+ const apiKey = this.runtime.connection.apiKey;
43
+ if (!address || !apiKey)
44
+ return {};
45
+ const hmac = createHmac("sha256", apiKey).update(`preset-ingest:${address.toLowerCase()}`).digest("hex");
46
+ return { "x-preset-hmac": hmac };
47
+ }
48
+ /**
49
+ * Load all preset data sources. Called once at agent boot.
50
+ * Concurrent calls are coalesced — only the first triggers a fetch,
51
+ * subsequent callers await the same promise (prevents double-pay).
52
+ */
53
+ async load() {
54
+ if (this.loading)
55
+ return this.loading;
56
+ this.loading = this._doLoad();
57
+ try {
58
+ return await this.loading;
59
+ }
60
+ finally {
61
+ this.loading = null;
62
+ }
63
+ }
64
+ async _doLoad() {
65
+ // 1. Parse nookplot.yaml
66
+ const config = await this.readConfig();
67
+ if (!config || !config.sources || config.sources.length === 0) {
68
+ return this.emptyResult();
69
+ }
70
+ // 2. Check idempotent loading (presetId + version + sourceHashes must all match)
71
+ const manifest = await this.readManifest();
72
+ if (manifest && manifest.presetId === config.id && manifest.presetVersion === (config.version ?? 1)) {
73
+ // Compute current source hashes and compare with manifest
74
+ const currentHashes = {};
75
+ if (config.sources) {
76
+ for (let i = 0; i < config.sources.length; i++) {
77
+ const src = config.sources[i];
78
+ const hash = createHash("sha256")
79
+ .update(JSON.stringify(src.config ?? {}))
80
+ .digest("hex");
81
+ currentHashes[`${src.type}:${i}`] = hash;
82
+ }
83
+ }
84
+ const hashesMatch = manifest.sourceHashes &&
85
+ JSON.stringify(manifest.sourceHashes) === JSON.stringify(currentHashes);
86
+ if (hashesMatch) {
87
+ this.emit("progress", { phase: "complete", result: this.manifestToResult(manifest) });
88
+ return this.manifestToResult(manifest);
89
+ }
90
+ // sourceHashes differ — force reload even though version matches
91
+ }
92
+ // 3. Pre-flight cost estimate — abort BEFORE spending credits
93
+ this.emit("progress", { phase: "estimating", message: `Estimating costs for "${config.id}"...` });
94
+ if (typeof config.maxCostNook === "number") {
95
+ try {
96
+ const agentAddress = "agentAddress" in this.runtime ? this.runtime.agentAddress : undefined;
97
+ const qs = agentAddress ? `?agentAddress=${encodeURIComponent(agentAddress)}` : "";
98
+ const estimate = await this.runtime.connection.request("GET", `/v1/forge/presets/${encodeURIComponent(config.id)}/estimate${qs}`);
99
+ if (estimate.estimatedCostNook > config.maxCostNook) {
100
+ const msg = `Estimated cost ${estimate.estimatedCostNook} NOOK exceeds safety cap ${config.maxCostNook} NOOK. Aborting before fetch.`;
101
+ this.emit("progress", { phase: "error", source: "cost", error: msg });
102
+ throw new Error(msg);
103
+ }
104
+ }
105
+ catch (err) {
106
+ // If the error is our own cost-cap abort, re-throw it
107
+ if (err instanceof Error && err.message.includes("Aborting before fetch"))
108
+ throw err;
109
+ // Otherwise the estimate endpoint failed — log and proceed (fetch will do the real spend)
110
+ }
111
+ }
112
+ // 4. Fetch data from gateway
113
+ let fetchResult;
114
+ try {
115
+ fetchResult = await this.runtime.connection.request("POST", "/v1/forge/data/fetch", {
116
+ presetId: config.id,
117
+ sources: config.sources.map((s) => ({ type: s.type, config: s.config })),
118
+ });
119
+ }
120
+ catch (err) {
121
+ const msg = err instanceof Error ? err.message : String(err);
122
+ this.emit("progress", { phase: "error", source: "fetch", error: msg });
123
+ if (config.failurePolicy === "abort") {
124
+ throw new Error(`Preset data fetch failed: ${msg}`);
125
+ }
126
+ return this.emptyResult();
127
+ }
128
+ // 5. Safety cap check (use explicit !== undefined to allow maxCostNook: 0)
129
+ if (typeof config.maxCostNook === "number" && fetchResult.totalCostNook > config.maxCostNook) {
130
+ const msg = `Estimated cost ${fetchResult.totalCostNook} NOOK exceeds safety cap ${config.maxCostNook} NOOK. Aborting.`;
131
+ this.emit("progress", { phase: "error", source: "cost", error: msg });
132
+ throw new Error(msg);
133
+ }
134
+ // 6. Check if RAG / knowledge graph is available
135
+ let ragAvailable = false;
136
+ let knowledgeGraphAvailable = false;
137
+ try {
138
+ await this.runtime.connection.request("GET", "/v1/mining/knowledge/search?q=test&limit=1");
139
+ ragAvailable = true;
140
+ }
141
+ catch {
142
+ // RAG not available
143
+ }
144
+ try {
145
+ // Knowledge graph endpoint returns stats if migration 180 is applied
146
+ await this.runtime.connection.request("POST", "/v1/agents/me/knowledge/ingest", {
147
+ items: [],
148
+ });
149
+ knowledgeGraphAvailable = true;
150
+ }
151
+ catch (err) {
152
+ // 400 = endpoint exists but items empty → KG is available
153
+ // 404 = endpoint doesn't exist → KG not available
154
+ const status = err?.status ?? err?.statusCode;
155
+ if (status === 400) {
156
+ knowledgeGraphAvailable = true;
157
+ }
158
+ }
159
+ // 7. Scan + ingest each source
160
+ const trustLevel = config.trustLevel ?? "verified";
161
+ const sourceResults = [];
162
+ const sourceHashes = {};
163
+ for (let i = 0; i < fetchResult.sources.length; i++) {
164
+ const fetchSource = fetchResult.sources[i];
165
+ const configSource = config.sources[i];
166
+ const label = configSource?.label ?? `${fetchSource.source} source`;
167
+ const kgBatch = [];
168
+ this.emit("progress", {
169
+ phase: "fetching",
170
+ source: label,
171
+ progress: `${fetchSource.itemCount} items`,
172
+ });
173
+ let loaded = 0;
174
+ let blocked = 0;
175
+ let skipped = 0;
176
+ let status = "loaded";
177
+ let error;
178
+ try {
179
+ for (const item of fetchSource.items) {
180
+ // Content scanning
181
+ if (trustLevel !== "raw") {
182
+ const severity = this.assessSeverity(item.content);
183
+ if (severity >= 50) {
184
+ blocked++;
185
+ continue;
186
+ }
187
+ }
188
+ // Sanitize
189
+ const sanitized = sanitizeForPrompt(item.content, 10_000);
190
+ if (!sanitized.trim()) {
191
+ skipped++;
192
+ continue;
193
+ }
194
+ // Compute content hash for dedup
195
+ const contentHash = createHash("sha256").update(sanitized).digest("hex");
196
+ // Ingest: prefer knowledge graph (semantic search), fall back to memory
197
+ try {
198
+ if (knowledgeGraphAvailable) {
199
+ // C3 upgrade: ingest into agent knowledge graph for semantic search
200
+ if (fetchSource.source === "aggregate") {
201
+ const subLoaded = await this.ingestAggregateToKG(sanitized, config.id, configSource?.config?.domainTags ?? []);
202
+ loaded += subLoaded;
203
+ }
204
+ else {
205
+ kgBatch.push({
206
+ contentText: sanitized,
207
+ knowledgeType: this.mapSourceToKnowledgeType(fetchSource.source),
208
+ sourceType: this.mapSourceToKGSourceType(fetchSource.source),
209
+ sourceId: item.metadata?.traceId ?? item.contentHash,
210
+ domain: configSource?.config?.domainTags?.[0] ?? null,
211
+ tags: ["preset", fetchSource.source, ...(configSource?.config?.domainTags ?? [])],
212
+ importance: 0.8,
213
+ confidence: item.metadata?.compositeScore ?? 0.5,
214
+ metadata: { content_hash: contentHash, ...item.metadata },
215
+ isPreset: true,
216
+ presetId: config.id,
217
+ });
218
+ loaded++;
219
+ }
220
+ }
221
+ else {
222
+ // Memory fallback (pre-C3 behavior)
223
+ if (fetchSource.source === "aggregate") {
224
+ const subLoaded = await this.ingestAggregate(sanitized, config.id, configSource?.config?.domainTags ?? []);
225
+ loaded += subLoaded;
226
+ }
227
+ else {
228
+ await this.runtime.memory.storeMemory("semantic", sanitized, {
229
+ tags: ["preset", fetchSource.source, ...(configSource?.config?.domainTags ?? [])],
230
+ source: `preset:${config.id}:${fetchSource.source}`,
231
+ importance: 0.8,
232
+ metadata: {
233
+ is_preset: true,
234
+ preset_id: config.id,
235
+ content_hash: contentHash,
236
+ ...item.metadata,
237
+ },
238
+ });
239
+ loaded++;
240
+ }
241
+ }
242
+ }
243
+ catch {
244
+ skipped++;
245
+ }
246
+ }
247
+ // Flush KG batch if any items accumulated
248
+ if (kgBatch.length > 0) {
249
+ try {
250
+ const batchResult = await this.runtime.connection.request("POST", "/v1/agents/me/knowledge/ingest", { items: kgBatch }, 4, 0, this.presetIngestHeaders());
251
+ // Adjust loaded/skipped counts based on actual ingest result
252
+ const batchSkipped = batchResult.skipped ?? 0;
253
+ if (batchSkipped > 0) {
254
+ loaded -= batchSkipped;
255
+ skipped += batchSkipped;
256
+ }
257
+ }
258
+ catch {
259
+ // If batch ingest fails, fall back to memory for this source
260
+ for (const batchItem of kgBatch) {
261
+ try {
262
+ await this.runtime.memory.storeMemory("semantic", batchItem.contentText, {
263
+ tags: batchItem.tags,
264
+ source: `preset:${config.id}:${fetchSource.source}`,
265
+ importance: batchItem.importance,
266
+ metadata: { is_preset: true, preset_id: config.id, ...batchItem.metadata },
267
+ });
268
+ }
269
+ catch {
270
+ skipped++;
271
+ loaded--;
272
+ }
273
+ }
274
+ }
275
+ }
276
+ if (loaded === 0 && fetchSource.itemCount > 0) {
277
+ status = "failed";
278
+ error = `All ${fetchSource.itemCount} items were blocked or skipped`;
279
+ }
280
+ else if (blocked > 0 || skipped > 0) {
281
+ status = "partial";
282
+ }
283
+ }
284
+ catch (err) {
285
+ status = "failed";
286
+ error = err instanceof Error ? err.message : String(err);
287
+ this.emit("progress", { phase: "error", source: label, error });
288
+ if (config.failurePolicy === "abort") {
289
+ throw new Error(`Source "${label}" failed: ${error}`);
290
+ }
291
+ }
292
+ this.emit("progress", {
293
+ phase: "scanning",
294
+ source: label,
295
+ blocked,
296
+ });
297
+ this.emit("progress", {
298
+ phase: "ingesting",
299
+ source: label,
300
+ method: knowledgeGraphAvailable ? "rag" : ragAvailable ? "rag" : "memory",
301
+ });
302
+ // Source hash for idempotent manifest
303
+ const sourceHash = createHash("sha256")
304
+ .update(JSON.stringify(configSource?.config ?? {}))
305
+ .digest("hex");
306
+ sourceHashes[`${fetchSource.source}:${i}`] = sourceHash;
307
+ sourceResults.push({
308
+ type: fetchSource.source,
309
+ label,
310
+ itemsLoaded: loaded,
311
+ itemsBlocked: blocked,
312
+ itemsSkipped: skipped,
313
+ method: knowledgeGraphAvailable ? "rag" : ragAvailable ? "rag" : "memory",
314
+ status,
315
+ error,
316
+ costNook: fetchSource.costNook,
317
+ });
318
+ }
319
+ // 8. Self-awareness memory
320
+ const totalLoaded = sourceResults.reduce((s, r) => s + r.itemsLoaded, 0);
321
+ if (totalLoaded > 0) {
322
+ // M10: Sanitize config.id to prevent prompt injection via crafted preset IDs
323
+ const safePresetId = (config.id ?? "").replace(/[^\w\s._-]/g, "").slice(0, 100);
324
+ const selfModelContent = [
325
+ `I was forged with preset "${safePresetId}" (v${config.version ?? 1}) on ${new Date().toISOString()}.`,
326
+ `My knowledge includes:`,
327
+ ...sourceResults
328
+ .filter((s) => s.itemsLoaded > 0)
329
+ .map((s) => `- ${s.itemsLoaded} items from ${s.label} (${s.type}, ingested via ${s.method})`),
330
+ knowledgeGraphAvailable
331
+ ? `My knowledge is indexed in my personal knowledge graph with semantic search.`
332
+ : ragAvailable
333
+ ? `I can search my knowledge semantically via RAG.`
334
+ : `I search my knowledge via text matching (RAG not yet available — will upgrade automatically).`,
335
+ ].join("\n");
336
+ try {
337
+ await this.runtime.memory.storeMemory("self_model", selfModelContent, {
338
+ tags: ["preset", "forge", safePresetId],
339
+ source: `preset:${safePresetId}`,
340
+ importance: 0.95,
341
+ metadata: { is_preset: true, preset_id: safePresetId },
342
+ });
343
+ }
344
+ catch {
345
+ // Non-critical — don't fail the load
346
+ }
347
+ }
348
+ // 9. Write manifest for idempotent reboot
349
+ const result = {
350
+ sources: sourceResults,
351
+ totalItems: totalLoaded,
352
+ totalBlocked: sourceResults.reduce((s, r) => s + r.itemsBlocked, 0),
353
+ totalCostNook: fetchResult.totalCostNook,
354
+ ragAvailable: ragAvailable || knowledgeGraphAvailable,
355
+ presetVersion: config.version ?? 1,
356
+ loadedAt: new Date().toISOString(),
357
+ };
358
+ await this.writeManifest({
359
+ presetId: config.id,
360
+ presetVersion: config.version ?? 1,
361
+ loadedAt: result.loadedAt,
362
+ sourceHashes,
363
+ totalItems: result.totalItems,
364
+ costPaid: result.totalCostNook,
365
+ ragAvailable: result.ragAvailable,
366
+ });
367
+ this.emit("progress", { phase: "complete", result });
368
+ return result;
369
+ }
370
+ /**
371
+ * Check if preset data was already loaded (idempotent reboot check).
372
+ */
373
+ async isLoaded() {
374
+ // M11: Check manifest matches current config (not just existence)
375
+ const manifest = await this.readManifest();
376
+ if (!manifest)
377
+ return false;
378
+ const config = await this.readConfig();
379
+ if (!config)
380
+ return false;
381
+ return manifest.presetId === config.id && manifest.presetVersion === (config.version ?? 1);
382
+ }
383
+ // ── Knowledge Graph Helpers (C3) ────────────────────────────
384
+ /**
385
+ * Map preset source type to knowledge graph knowledge_type.
386
+ */
387
+ mapSourceToKnowledgeType(source) {
388
+ switch (source) {
389
+ case "mining": return "fact";
390
+ case "bundle": return "fact";
391
+ case "aggregate": return "synthesis";
392
+ case "memory": return "experience";
393
+ case "reppo": return "fact";
394
+ default: return "fact";
395
+ }
396
+ }
397
+ /**
398
+ * Map preset source type to knowledge graph source_type.
399
+ */
400
+ mapSourceToKGSourceType(source) {
401
+ switch (source) {
402
+ case "mining": return "mining";
403
+ case "bundle": return "import";
404
+ case "aggregate": return "aggregation";
405
+ case "memory": return "import";
406
+ case "reppo": return "import";
407
+ default: return "preset";
408
+ }
409
+ }
410
+ /**
411
+ * Ingest a KnowledgeAggregateV1 into the knowledge graph as structured items.
412
+ * Same decomposition as ingestAggregate but targets the KG endpoint.
413
+ */
414
+ async ingestAggregateToKG(content, presetId, domainTags) {
415
+ let aggregate;
416
+ try {
417
+ aggregate = JSON.parse(content);
418
+ }
419
+ catch {
420
+ // Not valid JSON — single item
421
+ try {
422
+ await this.runtime.connection.request("POST", "/v1/agents/me/knowledge", {
423
+ contentText: content,
424
+ knowledgeType: "synthesis",
425
+ sourceType: "aggregation",
426
+ domain: domainTags[0] ?? null,
427
+ tags: ["preset", "aggregate", ...domainTags],
428
+ importance: 0.8,
429
+ isPreset: true,
430
+ presetId,
431
+ });
432
+ return 1;
433
+ }
434
+ catch {
435
+ return 0;
436
+ }
437
+ }
438
+ const items = [];
439
+ const tags = aggregate.tags ?? domainTags;
440
+ const domain = aggregate.domain ?? domainTags[0] ?? null;
441
+ // Helper: scan + sanitize each sub-item before ingestion
442
+ const scanAndSanitize = (text) => {
443
+ if (this.assessSeverity(text) >= 50)
444
+ return null; // blocked
445
+ const sanitized = sanitizeForPrompt(text, 10_000);
446
+ return sanitized.trim() || null;
447
+ };
448
+ // Synthesis → synthesis type
449
+ const synthesis = aggregate.synthesis;
450
+ if (synthesis?.narrative) {
451
+ const cleaned = scanAndSanitize(synthesis.narrative);
452
+ if (cleaned) {
453
+ items.push({
454
+ contentText: cleaned,
455
+ knowledgeType: "synthesis",
456
+ sourceType: "aggregation",
457
+ domain,
458
+ tags: ["preset", "aggregate", ...tags],
459
+ importance: 0.9,
460
+ confidence: 0.8,
461
+ metadata: { scope: synthesis.scope, limitations: synthesis.limitations },
462
+ isPreset: true,
463
+ presetId,
464
+ });
465
+ }
466
+ }
467
+ // Key insights → insight type
468
+ const insights = aggregate.keyInsights ?? [];
469
+ for (const insight of insights) {
470
+ if (typeof insight.insight !== "string")
471
+ continue;
472
+ const cleaned = scanAndSanitize(insight.insight);
473
+ if (!cleaned)
474
+ continue;
475
+ const confidence = typeof insight.confidence === "number" ? insight.confidence : 0.7;
476
+ items.push({
477
+ contentText: cleaned,
478
+ knowledgeType: "insight",
479
+ sourceType: "aggregation",
480
+ domain,
481
+ tags: ["preset", "aggregate", "insight", ...(insight.tags ?? [])],
482
+ importance: Math.max(0.6, confidence),
483
+ confidence,
484
+ metadata: {
485
+ supportingTraces: Array.isArray(insight.supportingTraceIds) ? insight.supportingTraceIds.length : 0,
486
+ },
487
+ isPreset: true,
488
+ presetId,
489
+ });
490
+ }
491
+ // Reasoning patterns → pattern type
492
+ const patterns = aggregate.reasoningPatterns ?? [];
493
+ for (const pattern of patterns) {
494
+ if (typeof pattern.pattern !== "string")
495
+ continue;
496
+ const cleaned = scanAndSanitize(pattern.pattern);
497
+ if (!cleaned)
498
+ continue;
499
+ items.push({
500
+ contentText: cleaned,
501
+ knowledgeType: "pattern",
502
+ sourceType: "aggregation",
503
+ domain,
504
+ tags: ["preset", "aggregate", "reasoning_pattern", ...tags],
505
+ importance: 0.7,
506
+ confidence: 0.6,
507
+ isPreset: true,
508
+ presetId,
509
+ });
510
+ }
511
+ // Contradictions → single insight with low confidence
512
+ const contradictions = aggregate.contradictions ?? [];
513
+ if (contradictions.length > 0) {
514
+ const summary = contradictions.map((c) => `Contested: "${c.claim_a}" vs "${c.claim_b}"${c.resolution ? ` — likely: ${c.resolution}` : ""}`).join("\n");
515
+ const cleaned = scanAndSanitize(summary);
516
+ if (cleaned) {
517
+ items.push({
518
+ contentText: cleaned,
519
+ knowledgeType: "insight",
520
+ sourceType: "aggregation",
521
+ domain,
522
+ tags: ["preset", "aggregate", "uncertainty"],
523
+ importance: 0.75,
524
+ confidence: 0.3,
525
+ isPreset: true,
526
+ presetId,
527
+ });
528
+ }
529
+ }
530
+ if (items.length === 0)
531
+ return 0;
532
+ try {
533
+ const result = await this.runtime.connection.request("POST", "/v1/agents/me/knowledge/ingest", { items }, 4, 0, this.presetIngestHeaders());
534
+ return result.ingested;
535
+ }
536
+ catch {
537
+ // Fall back to memory-based aggregate ingestion
538
+ return this.ingestAggregate(content, presetId, domainTags);
539
+ }
540
+ }
541
+ // ── Aggregate Ingestion (E8) ────────────────────────────────
542
+ /**
543
+ * Ingest a KnowledgeAggregateV1 as structured memories.
544
+ * Uses 5-7x fewer memory slots than equivalent raw traces:
545
+ * - Synthesis → semantic memory (importance 0.9)
546
+ * - Each keyInsight → procedural memory (confidence-weighted)
547
+ * - Each reasoning pattern → procedural memory
548
+ * - Contradictions → self_model memory
549
+ *
550
+ * Returns the number of memories stored.
551
+ */
552
+ async ingestAggregate(content, presetId, domainTags) {
553
+ let aggregate;
554
+ try {
555
+ aggregate = JSON.parse(content);
556
+ }
557
+ catch {
558
+ // Not valid JSON — fall back to generic memory
559
+ await this.runtime.memory.storeMemory("semantic", content, {
560
+ tags: ["preset", "aggregate", ...domainTags],
561
+ source: `preset:${presetId}:aggregate`,
562
+ importance: 0.8,
563
+ metadata: { is_preset: true, preset_id: presetId },
564
+ });
565
+ return 1;
566
+ }
567
+ let stored = 0;
568
+ const tags = aggregate.tags ?? domainTags;
569
+ const baseMeta = { is_preset: true, preset_id: presetId };
570
+ // Helper: scan + sanitize sub-items before ingestion
571
+ const scanAndSanitize = (text) => {
572
+ if (this.assessSeverity(text) >= 50)
573
+ return null;
574
+ const sanitized = sanitizeForPrompt(text, 10_000);
575
+ return sanitized.trim() || null;
576
+ };
577
+ // 1. Synthesis → semantic memory (high importance)
578
+ const synthesis = aggregate.synthesis;
579
+ if (synthesis?.narrative) {
580
+ const cleaned = scanAndSanitize(synthesis.narrative);
581
+ if (cleaned) {
582
+ try {
583
+ await this.runtime.memory.storeMemory("semantic", cleaned, {
584
+ tags: ["preset", "aggregate", ...tags],
585
+ source: `preset:${presetId}:aggregate:synthesis`,
586
+ importance: 0.9,
587
+ metadata: { ...baseMeta, scope: synthesis.scope, limitations: synthesis.limitations },
588
+ });
589
+ stored++;
590
+ }
591
+ catch { /* non-critical */ }
592
+ }
593
+ }
594
+ // 2. Each keyInsight → procedural memory (confidence-weighted)
595
+ const insights = aggregate.keyInsights ?? [];
596
+ for (const insight of insights) {
597
+ if (typeof insight.insight !== "string")
598
+ continue;
599
+ const cleaned = scanAndSanitize(insight.insight);
600
+ if (!cleaned)
601
+ continue;
602
+ try {
603
+ const confidence = typeof insight.confidence === "number" ? insight.confidence : 0.7;
604
+ await this.runtime.memory.storeMemory("procedural", cleaned, {
605
+ tags: ["preset", "aggregate", "insight", ...(insight.tags ?? [])],
606
+ source: `preset:${presetId}:aggregate:insight`,
607
+ importance: Math.max(0.6, confidence),
608
+ metadata: {
609
+ ...baseMeta,
610
+ confidence,
611
+ supportingTraces: Array.isArray(insight.supportingTraceIds) ? insight.supportingTraceIds.length : 0,
612
+ },
613
+ });
614
+ stored++;
615
+ }
616
+ catch { /* non-critical */ }
617
+ }
618
+ // 3. Reasoning patterns → procedural memory
619
+ const patterns = aggregate.reasoningPatterns ?? [];
620
+ for (const pattern of patterns) {
621
+ if (typeof pattern.pattern !== "string")
622
+ continue;
623
+ const cleaned = scanAndSanitize(pattern.pattern);
624
+ if (!cleaned)
625
+ continue;
626
+ try {
627
+ await this.runtime.memory.storeMemory("procedural", cleaned, {
628
+ tags: ["preset", "aggregate", "reasoning_pattern", ...tags],
629
+ source: `preset:${presetId}:aggregate:pattern`,
630
+ importance: 0.7,
631
+ metadata: baseMeta,
632
+ });
633
+ stored++;
634
+ }
635
+ catch { /* non-critical */ }
636
+ }
637
+ // 4. Contradictions → self_model (agent should know what's contested)
638
+ const contradictions = aggregate.contradictions ?? [];
639
+ if (contradictions.length > 0) {
640
+ const summary = contradictions.map((c) => `Contested: "${c.claim_a}" vs "${c.claim_b}"${c.resolution ? ` — likely: ${c.resolution}` : ""}`).join("\n");
641
+ const cleaned = scanAndSanitize(summary);
642
+ if (cleaned) {
643
+ try {
644
+ await this.runtime.memory.storeMemory("self_model", cleaned, {
645
+ tags: ["preset", "aggregate", "uncertainty"],
646
+ source: `preset:${presetId}:aggregate:contradictions`,
647
+ importance: 0.75,
648
+ metadata: baseMeta,
649
+ });
650
+ stored++;
651
+ }
652
+ catch { /* non-critical */ }
653
+ }
654
+ }
655
+ return stored;
656
+ }
657
+ // ── Private Helpers ─────────────────────────────────────────
658
+ async readConfig() {
659
+ if (this.presetOverride) {
660
+ return this.presetOverride.id ? this.presetOverride : null;
661
+ }
662
+ try {
663
+ const raw = await readFile(this.configPath, "utf-8");
664
+ const doc = yaml.load(raw);
665
+ const preset = doc?.preset;
666
+ if (!preset?.id)
667
+ return null;
668
+ return preset;
669
+ }
670
+ catch {
671
+ return null;
672
+ }
673
+ }
674
+ async readManifest() {
675
+ try {
676
+ const raw = await readFile(".preset-loaded.json", "utf-8");
677
+ return JSON.parse(raw);
678
+ }
679
+ catch {
680
+ return null;
681
+ }
682
+ }
683
+ async writeManifest(manifest) {
684
+ try {
685
+ await writeFile(".preset-loaded.json", JSON.stringify(manifest, null, 2));
686
+ }
687
+ catch {
688
+ // Non-critical
689
+ }
690
+ }
691
+ /**
692
+ * Lightweight client-side severity assessment.
693
+ * Full ContentScanner runs server-side; this is a first pass.
694
+ */
695
+ assessSeverity(content) {
696
+ if (!content)
697
+ return 0;
698
+ let score = 0;
699
+ const lower = content.toLowerCase();
700
+ // Prompt injection patterns
701
+ if (/ignore\s+(all\s+)?(previous|above)\s+(instructions|prompts)/i.test(content))
702
+ score += 40;
703
+ if (/you\s+are\s+now\s+(a|an|the)/i.test(content))
704
+ score += 30;
705
+ if (/<\s*\/?\s*(system|assistant|user)\s*>/i.test(content))
706
+ score += 25;
707
+ if (/---\s*END\s+OF\s+(SYSTEM\s+)?(PROMPT|INSTRUCTIONS)\s*---/i.test(content))
708
+ score += 35;
709
+ // Credential harvesting
710
+ if (/private.?key|api.?key|secret|password/i.test(content) && /send|share|tell|reveal/i.test(content))
711
+ score += 50;
712
+ // Data exfiltration
713
+ if (/fetch\s*\(|curl\s|wget\s|http:\/\/\d/i.test(content))
714
+ score += 20;
715
+ return Math.min(score, 100);
716
+ }
717
+ emptyResult() {
718
+ return {
719
+ sources: [],
720
+ totalItems: 0,
721
+ totalBlocked: 0,
722
+ totalCostNook: 0,
723
+ ragAvailable: false,
724
+ presetVersion: 0,
725
+ loadedAt: new Date().toISOString(),
726
+ };
727
+ }
728
+ manifestToResult(manifest) {
729
+ return {
730
+ sources: [],
731
+ totalItems: manifest.totalItems,
732
+ totalBlocked: 0,
733
+ totalCostNook: manifest.costPaid,
734
+ ragAvailable: manifest.ragAvailable ?? false,
735
+ presetVersion: manifest.presetVersion,
736
+ loadedAt: manifest.loadedAt,
737
+ };
738
+ }
739
+ }
740
+ //# sourceMappingURL=presetLoader.js.map