@askexenow/exe-os 0.9.270 → 0.9.272
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/dist/active-agent-BDYXURXQ.js +26 -0
- package/dist/active-agent-YWBGAKGU.js +25 -0
- package/dist/agentic-ontology-56VHSVS3.js +25 -0
- package/dist/backfill-metadata-G46ABBVR.js +597 -0
- package/dist/backfill-metadata-VAV27KJK.js +597 -0
- package/dist/behaviors-USUTDXVA.js +25 -0
- package/dist/bin/agentic-ontology-backfill.js +5 -5
- package/dist/bin/agentic-reflection-backfill.js +6 -6
- package/dist/bin/agentic-semantic-label.js +5 -5
- package/dist/bin/backfill-conversations.js +4 -4
- package/dist/bin/backfill-responses.js +4 -4
- package/dist/bin/backfill-vectors.js +5 -5
- package/dist/bin/bulk-sync-postgres.js +6 -6
- package/dist/bin/cc-doctor.js +4 -4
- package/dist/bin/cleanup-stale-review-tasks.js +10 -10
- package/dist/bin/cli.js +16 -16
- package/dist/bin/exe-agent-config.js +3 -3
- package/dist/bin/exe-agent.js +4 -4
- package/dist/bin/exe-assign.js +5 -5
- package/dist/bin/exe-boot.js +17 -17
- package/dist/bin/exe-call.js +4 -4
- package/dist/bin/exe-cloud.js +4 -4
- package/dist/bin/exe-dispatch.js +10 -10
- package/dist/bin/exe-doctor.js +1 -1
- package/dist/bin/exe-export-behaviors.js +7 -7
- package/dist/bin/exe-forget.js +6 -6
- package/dist/bin/exe-gateway.js +7 -7
- package/dist/bin/exe-healthcheck.js +4 -4
- package/dist/bin/exe-heartbeat.js +10 -10
- package/dist/bin/exe-kill.js +13 -13
- package/dist/bin/exe-launch-agent.js +37 -19
- package/dist/bin/exe-new-employee.js +6 -6
- package/dist/bin/exe-pending-messages.js +11 -11
- package/dist/bin/exe-pending-notifications.js +10 -10
- package/dist/bin/exe-pending-reviews.js +10 -10
- package/dist/bin/exe-rename.js +4 -4
- package/dist/bin/exe-review.js +12 -12
- package/dist/bin/exe-search.js +5 -5
- package/dist/bin/exe-session-cleanup.js +15 -15
- package/dist/bin/exe-settings.js +5 -5
- package/dist/bin/exe-start-codex.js +11 -11
- package/dist/bin/exe-start-opencode.js +8 -8
- package/dist/bin/exe-status.js +11 -11
- package/dist/bin/exe-team.js +3 -3
- package/dist/bin/git-sweep.js +11 -11
- package/dist/bin/graph-backfill.js +4 -4
- package/dist/bin/graph-export.js +5 -5
- package/dist/bin/import-history.js +7 -7
- package/dist/bin/install.js +6 -6
- package/dist/bin/intercom-check.js +4 -4
- package/dist/bin/mcp-sessions.js +2 -2
- package/dist/bin/orchestration-metrics.js +4 -4
- package/dist/bin/postgres-agentic-reflection-backfill.js +2 -2
- package/dist/bin/postgres-agentic-semantic-backfill.js +1 -1
- package/dist/bin/scan-tasks.js +10 -10
- package/dist/bin/setup.js +1 -1
- package/dist/bin/shard-migrate.js +4 -4
- package/dist/bin/stack-update.js +10 -3
- package/dist/bin/vps-health-gate.js +1 -1
- package/dist/capacity-monitor-IFVRCIM7.js +49 -0
- package/dist/capacity-monitor-Q47GBDSY.js +49 -0
- package/dist/catchup-brief-RP4QHXNT.js +151 -0
- package/dist/catchup-brief-TKA6TEK4.js +151 -0
- package/dist/chunk-23KJ2LXY.js +58 -0
- package/dist/chunk-2NQQP3FF.js +630 -0
- package/dist/chunk-3FU5I3KV.js +526 -0
- package/dist/chunk-46IEEKPU.js +13696 -0
- package/dist/chunk-57UAFTO2.js +3958 -0
- package/dist/chunk-5AS622MM.js +3958 -0
- package/dist/chunk-62DEE65H.js +371 -0
- package/dist/chunk-64T6DFSS.js +447 -0
- package/dist/chunk-6BWDP63Z.js +197 -0
- package/dist/chunk-6GPYL7TX.js +214 -0
- package/dist/chunk-6KWLUVFL.js +54 -0
- package/dist/chunk-6N5ISWBF.js +1148 -0
- package/dist/chunk-6OD7PVMC.js +333 -0
- package/dist/chunk-7ET5CYTD.js +382 -0
- package/dist/chunk-7IWLKR6N.js +76 -0
- package/dist/chunk-7OEUOJL5.js +1021 -0
- package/dist/chunk-AIRJTKDK.js +204 -0
- package/dist/chunk-ATJ3NXDP.js +244 -0
- package/dist/chunk-B5IS7LE4.js +128 -0
- package/dist/chunk-BKINEQVI.js +244 -0
- package/dist/chunk-BOJV6NI3.js +128 -0
- package/dist/chunk-BPHWI6N2.js +284 -0
- package/dist/chunk-BXCQWWJP.js +185 -0
- package/dist/chunk-BZ6K7AY3.js +50 -0
- package/dist/chunk-C54KIFLS.js +214 -0
- package/dist/chunk-C6OYEJJI.js +260 -0
- package/dist/chunk-CHBHR5W6.js +3556 -0
- package/dist/chunk-D2T3272U.js +171 -0
- package/dist/chunk-DCHEIVGT.js +221 -0
- package/dist/chunk-DHIBLMSP.js +30 -0
- package/dist/chunk-E2AF2WYY.js +346 -0
- package/dist/chunk-E2KZEZZW.js +1090 -0
- package/dist/chunk-E4KWB4WM.js +348 -0
- package/dist/chunk-ENU7URWK.js +1073 -0
- package/dist/chunk-EZ7KAZMC.js +132 -0
- package/dist/chunk-F4FSSHR4.js +1073 -0
- package/dist/chunk-FPXU56FG.js +346 -0
- package/dist/chunk-FY7HHR5I.js +128 -0
- package/dist/chunk-G2S2UMU4.js +159 -0
- package/dist/chunk-G33BHQCO.js +70 -0
- package/dist/chunk-GJQTL7RX.js +127 -0
- package/dist/chunk-GLCKDEM2.js +97 -0
- package/dist/chunk-GMA34SXV.js +240 -0
- package/dist/chunk-GVAVEBYR.js +2091 -0
- package/dist/chunk-HOSJTLBQ.js +513 -0
- package/dist/chunk-IBMTSEZC.js +230 -0
- package/dist/chunk-IC6HVAS3.js +56 -0
- package/dist/chunk-IDFJNO44.js +1051 -0
- package/dist/chunk-ILFJMEY5.js +97 -0
- package/dist/chunk-ISQAOSL3.js +1921 -0
- package/dist/chunk-J6V2DCZK.js +382 -0
- package/dist/chunk-JP4CLFLR.js +1148 -0
- package/dist/chunk-JTIOZHWG.js +58 -0
- package/dist/chunk-KDICWAYV.js +1345 -0
- package/dist/chunk-KOBIB6WG.js +159 -0
- package/dist/chunk-KQFDDQB6.js +13696 -0
- package/dist/chunk-KZNSOHCB.js +280 -0
- package/dist/chunk-LVMBYP3C.js +171 -0
- package/dist/chunk-M2WQW5NC.js +227 -0
- package/dist/chunk-MY6SP5NZ.js +551 -0
- package/dist/chunk-N2ACW2ZG.js +363 -0
- package/dist/chunk-NSMJDATI.js +495 -0
- package/dist/chunk-NSQ5JE23.js +1090 -0
- package/dist/chunk-NZL567WG.js +81 -0
- package/dist/chunk-O5OMH6LI.js +244 -0
- package/dist/chunk-O7YO7E2G.js +1512 -0
- package/dist/chunk-OBUV3W7L.js +163 -0
- package/dist/chunk-OLDS7LJN.js +495 -0
- package/dist/chunk-OO2I22RX.js +38 -0
- package/dist/chunk-OPUUT33V.js +447 -0
- package/dist/chunk-OT3VMTKB.js +50 -0
- package/dist/chunk-P6RVIOVA.js +157 -0
- package/dist/chunk-PUA5564C.js +210 -0
- package/dist/chunk-PUQLKLQX.js +731 -0
- package/dist/chunk-QROKS65G.js +76 -0
- package/dist/chunk-R54I2N2T.js +818 -0
- package/dist/chunk-RCFYQHUP.js +818 -0
- package/dist/chunk-RJTND4YS.js +284 -0
- package/dist/chunk-SBLHQMMZ.js +81 -0
- package/dist/chunk-SG2ANG5C.js +123 -0
- package/dist/chunk-SVFNKSZV.js +333 -0
- package/dist/chunk-TAQT2DC7.js +330 -0
- package/dist/chunk-TB7HFW7M.js +127 -0
- package/dist/chunk-UUKDAIH2.js +731 -0
- package/dist/chunk-V6VEFEEH.js +1345 -0
- package/dist/chunk-VIO2ALGH.js +290 -0
- package/dist/chunk-VKCUSNJW.js +377 -0
- package/dist/chunk-VRPPJFIQ.js +1921 -0
- package/dist/chunk-WP3PVBBP.js +204 -0
- package/dist/chunk-WQEUY7DC.js +129 -0
- package/dist/chunk-WXMXUKCA.js +262 -0
- package/dist/chunk-X2WBH2IO.js +297 -0
- package/dist/chunk-X33TSJNO.js +394 -0
- package/dist/chunk-X7MMI2UI.js +89 -0
- package/dist/chunk-XG3BQZIK.js +85 -0
- package/dist/chunk-XIKBIAOS.js +75 -0
- package/dist/chunk-XPEB545Q.js +54 -0
- package/dist/chunk-XWH2MLWS.js +330 -0
- package/dist/chunk-YH7V73XW.js +89 -0
- package/dist/chunk-YMLM5D65.js +135 -0
- package/dist/chunk-YNJPRQ6J.js +377 -0
- package/dist/chunk-YSNEHBI6.js +551 -0
- package/dist/chunk-ZD6BMW2K.js +33 -0
- package/dist/chunk-ZKG5IYCG.js +668 -0
- package/dist/chunk-ZU4K7ZNX.js +197 -0
- package/dist/co-activation-HZMJC34P.js +72 -0
- package/dist/co-occurrence-AVYXRV4L.js +74 -0
- package/dist/core-memory-NPJCVUMF.js +110 -0
- package/dist/core-memory-OKGXL33Z.js +110 -0
- package/dist/crdt-sync-ZCH55JNR.js +33 -0
- package/dist/crm-webhook-6OMVUUGR.js +10 -0
- package/dist/crm-webhook-UCWF3XDB.js +10 -0
- package/dist/cto-delegation-gate-JFZFZGC2.js +206 -0
- package/dist/cto-delegation-gate-K32M4GVM.js +206 -0
- package/dist/daemon-orchestration-2Q7BYOHC.js +135 -0
- package/dist/daemon-orchestration-4RJ2CZJL.js +135 -0
- package/dist/db-backup-5GA2YFDX.js +33 -0
- package/dist/dreaming-I6KXO6E2.js +32 -0
- package/dist/dreaming-NJBK5ILR.js +32 -0
- package/dist/exe-drift-VSMIMHL4.js +68 -0
- package/dist/exe-export-DVHHIA6Y.js +73 -0
- package/dist/exe-export-GIVQDENS.js +73 -0
- package/dist/exe-import-7N46LSMQ.js +76 -0
- package/dist/exe-import-FINYUV5T.js +76 -0
- package/dist/exe-key-H45JY44F.js +579 -0
- package/dist/exe-key-MAEQGTB7.js +579 -0
- package/dist/exe-snapshot-3TEM3BFD.js +164 -0
- package/dist/exe-snapshot-HECGUHL3.js +164 -0
- package/dist/fast-db-init-3CNTADVO.js +7 -0
- package/dist/fast-db-init-HXCS2AP5.js +7 -0
- package/dist/gateway/index.js +8 -8
- package/dist/git-staleness-YCEBBIVK.js +110 -0
- package/dist/git-task-sweep-H34STRNT.js +40 -0
- package/dist/git-task-sweep-YL7NLDCK.js +40 -0
- package/dist/global-procedures-IHZM6C2K.js +20 -0
- package/dist/graph-auto-extract-RZQ3MHP2.js +162 -0
- package/dist/hooks/bug-report-worker.js +12 -12
- package/dist/hooks/codex-stop-task-finalizer.js +12 -12
- package/dist/hooks/commit-complete.js +12 -12
- package/dist/hooks/error-recall.js +6 -6
- package/dist/hooks/exe-heartbeat-hook.js +3 -3
- package/dist/hooks/ingest.js +6 -6
- package/dist/hooks/instructions-loaded.js +4 -4
- package/dist/hooks/manifest.json +19 -19
- package/dist/hooks/notification.js +4 -4
- package/dist/hooks/post-compact.js +11 -11
- package/dist/hooks/post-tool-combined.js +5 -5
- package/dist/hooks/pre-compact.js +12 -12
- package/dist/hooks/pre-tool-use.js +15 -15
- package/dist/hooks/prompt-submit.js +21 -21
- package/dist/hooks/session-end.js +16 -16
- package/dist/hooks/session-start.js +10 -10
- package/dist/hooks/stop.js +15 -15
- package/dist/hooks/subagent-stop.js +11 -11
- package/dist/hooks/summary-worker.js +15 -15
- package/dist/index.js +18 -18
- package/dist/installer-4EW5ZDGD.js +296 -0
- package/dist/installer-B2JTQO55.js +38 -0
- package/dist/installer-MIL352T7.js +342 -0
- package/dist/lib/agent-config.js +9 -3
- package/dist/lib/cloud-sync.js +4 -4
- package/dist/lib/consolidation.js +5 -5
- package/dist/lib/database.js +2 -2
- package/dist/lib/db.js +2 -2
- package/dist/lib/employee-templates.js +4 -4
- package/dist/lib/employees.js +2 -2
- package/dist/lib/exe-daemon.js +34 -34
- package/dist/lib/hybrid-search.js +5 -5
- package/dist/lib/identity.js +2 -2
- package/dist/lib/messaging.js +10 -10
- package/dist/lib/reminders.js +3 -3
- package/dist/lib/schedules.js +5 -5
- package/dist/lib/session-registry.js +4 -4
- package/dist/lib/skill-learning.js +4 -4
- package/dist/lib/store.js +4 -4
- package/dist/lib/task-router.js +3 -3
- package/dist/lib/tasks.js +11 -11
- package/dist/lib/tmux-routing.js +9 -9
- package/dist/lib/token-spend.js +3 -3
- package/dist/mcp/register-tools.js +54 -54
- package/dist/mcp/server.js +55 -55
- package/dist/mcp/tools/complete-reminder.js +4 -4
- package/dist/mcp/tools/create-reminder.js +4 -4
- package/dist/mcp/tools/create-task.js +13 -13
- package/dist/mcp/tools/deactivate-behavior.js +5 -5
- package/dist/mcp/tools/list-reminders.js +4 -4
- package/dist/mcp/tools/list-tasks.js +13 -13
- package/dist/mcp/tools/send-message.js +12 -12
- package/dist/mcp/tools/update-task.js +12 -12
- package/dist/mcp-http-config-OJQR246S.js +27 -0
- package/dist/memory-cards-IPULSQFA.js +174 -0
- package/dist/memory-graph-extractor-3TZZOKHY.js +17 -0
- package/dist/memory-poisoning-defense-SGUGR5YJ.js +225 -0
- package/dist/memory-reflection-H3WGCEM6.js +238 -0
- package/dist/notifications-VWPO6NJF.js +45 -0
- package/dist/notifications-WCSRQN2V.js +45 -0
- package/dist/orchestration-events-O5PSDEIO.js +25 -0
- package/dist/orchestrator-RAPEJUOI.js +33 -0
- package/dist/orchestrator-XPG6LJAI.js +33 -0
- package/dist/pipeline-router-5NT6FUC3.js +13 -0
- package/dist/pipeline-router-KSUXONDT.js +13 -0
- package/dist/plan-limits-53NXLNDQ.js +26 -0
- package/dist/project-boot-ITN3FZMM.js +299 -0
- package/dist/projection-worker-27XX5M2W.js +964 -0
- package/dist/reranker-GU7L2PJX.js +19 -0
- package/dist/reranker-TZEXIJAN.js +19 -0
- package/dist/review-polling-FA2J2Q5O.js +124 -0
- package/dist/review-polling-MLS4BQ3N.js +124 -0
- package/dist/runtime/index.js +12 -12
- package/dist/session-events-WWGF3B2N.js +36 -0
- package/dist/session-events-ZHXXAH6B.js +36 -0
- package/dist/session-kill-telemetry-O4TJHHOZ.js +29 -0
- package/dist/session-scope-CQXB7VMH.js +86 -0
- package/dist/session-scope-HHUMJYF6.js +86 -0
- package/dist/setup-wizard-UM2RHSBJ.js +12 -0
- package/dist/skill-refinement-MJPOHYD5.js +157 -0
- package/dist/skill-refinement-NVUBRK22.js +157 -0
- package/dist/stack-release-2KSOYDIV.js +712 -0
- package/dist/stack-release-BAPCXMXW.js +713 -0
- package/dist/stack-release-W4TWTEZP.js +731 -0
- package/dist/stack-update-QQA64STQ.js +52 -0
- package/dist/steward-gate-VLE7OCKO.js +13 -0
- package/dist/task-enforcement-FUHDL6UR.js +391 -0
- package/dist/task-enforcement-QL3K4N3F.js +391 -0
- package/dist/task-scope-TZYMB634.js +35 -0
- package/dist/task-scope-ZVLUBS4C.js +35 -0
- package/dist/tasks-crud-4MSLJWXE.js +77 -0
- package/dist/tasks-crud-HIPXKRKX.js +77 -0
- package/dist/tasks-notify-7JBUNE7R.js +38 -0
- package/dist/tasks-notify-UPIJ3L4O.js +38 -0
- package/dist/tasks-review-5SJSFTUB.js +47 -0
- package/dist/tasks-review-JHSYBR5I.js +47 -0
- package/dist/telemetry-upload-BSGOXGUP.js +739 -0
- package/dist/telemetry-upload-LTX3C5HZ.js +739 -0
- package/dist/token-budget-2CDWQU3Q.js +84 -0
- package/dist/tool-telemetry-7YS7EN7B.js +17 -0
- package/dist/tui/App.js +17 -17
- package/dist/tui-data-GDGBOS6G.js +258 -0
- package/dist/tui-data-VXF2RBVM.js +258 -0
- package/dist/wiki-acl-MJIMXRQV.js +111 -0
- package/dist/worker-gate-WQGTZOSM.js +21 -0
- package/dist/worker-gate-X2YDTKTL.js +21 -0
- package/dist/workflow-engine-CYXRZXBM.js +28 -0
- package/dist/workflow-engine-KMLAXVA4.js +28 -0
- package/dist/worktree-NLSKVRNC.js +26 -0
- package/dist/worktree-sweep-44TMEPLE.js +19 -0
- package/package.json +1 -1
- package/release-notes.json +26 -40
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getClient
|
|
3
|
+
} from "./chunk-CHBHR5W6.js";
|
|
4
|
+
import "./chunk-2I23RPSI.js";
|
|
5
|
+
import "./chunk-C6OYEJJI.js";
|
|
6
|
+
import "./chunk-FXU7JOXK.js";
|
|
7
|
+
import "./chunk-VXIMSRTO.js";
|
|
8
|
+
import "./chunk-LYH5HE24.js";
|
|
9
|
+
import "./chunk-MLKGABMK.js";
|
|
10
|
+
|
|
11
|
+
// src/lib/memory-cards.ts
|
|
12
|
+
import { createHash } from "crypto";
|
|
13
|
+
var MAX_CARDS_PER_MEMORY = 8;
|
|
14
|
+
var MAX_SEGMENT_CHARS = 500;
|
|
15
|
+
var MIN_SEGMENT_CHARS = 20;
|
|
16
|
+
function stableId(memoryId, type, content) {
|
|
17
|
+
return createHash("sha256").update(`${memoryId}:${type}:${content}`).digest("hex").slice(0, 32);
|
|
18
|
+
}
|
|
19
|
+
function cleanText(text) {
|
|
20
|
+
let cleaned = text.replace(
|
|
21
|
+
/```(\w*)\n(.*?)(?:\n[\s\S]*?)```/g,
|
|
22
|
+
(_m, lang, firstLine) => `[code${lang ? `:${lang}` : ""}] ${firstLine.trim()}`
|
|
23
|
+
);
|
|
24
|
+
cleaned = cleaned.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
|
25
|
+
return cleaned;
|
|
26
|
+
}
|
|
27
|
+
function splitSegments(text) {
|
|
28
|
+
const cleaned = cleanText(text);
|
|
29
|
+
const segments = cleaned.split(/(?<=[.!?:;])\s+|\n{2,}|(?<=\))\s+(?=[A-Z])|\s*[|│]\s*/).map((s) => s.trim()).filter((s) => s.length >= MIN_SEGMENT_CHARS && s.length <= MAX_SEGMENT_CHARS);
|
|
30
|
+
if (segments.length === 0 && cleaned.length >= MIN_SEGMENT_CHARS) {
|
|
31
|
+
const lines = cleaned.split(/\n+/).map((l) => l.trim()).filter((l) => l.length >= MIN_SEGMENT_CHARS && l.length <= MAX_SEGMENT_CHARS);
|
|
32
|
+
if (lines.length > 0) return lines;
|
|
33
|
+
if (cleaned.length >= MIN_SEGMENT_CHARS) {
|
|
34
|
+
return [cleaned.slice(0, MAX_SEGMENT_CHARS)];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return segments;
|
|
38
|
+
}
|
|
39
|
+
function inferCardType(sentence, toolName) {
|
|
40
|
+
const lower = sentence.toLowerCase();
|
|
41
|
+
if (toolName === "store_decision" || /\b(decided|decision|adr|approved|rejected)\b/.test(lower)) return "decision";
|
|
42
|
+
if (/\b(prefers|preference|likes|dislikes|wants|doesn't want|does not want)\b/.test(lower)) return "preference";
|
|
43
|
+
if (/\b(changed|updated|replaced|now|no longer|instead|supersedes)\b/.test(lower)) return "belief_update";
|
|
44
|
+
if (toolName && ["Read", "Write", "Edit", "Bash"].includes(toolName)) return "code";
|
|
45
|
+
if (/\b(meeting|deadline|shipped|launched|completed|failed|blocked|assigned|created)\b/.test(lower)) return "event";
|
|
46
|
+
return "fact";
|
|
47
|
+
}
|
|
48
|
+
function extractSubject(sentence, agentId) {
|
|
49
|
+
const explicit = sentence.match(/\b([A-Z][a-zA-Z0-9_-]{2,}(?:\s+[A-Z][a-zA-Z0-9_-]{2,})?)\b/);
|
|
50
|
+
return explicit?.[1] ?? agentId;
|
|
51
|
+
}
|
|
52
|
+
function predicateFor(type) {
|
|
53
|
+
switch (type) {
|
|
54
|
+
case "preference":
|
|
55
|
+
return "prefers";
|
|
56
|
+
case "belief_update":
|
|
57
|
+
return "updated";
|
|
58
|
+
case "decision":
|
|
59
|
+
return "decided";
|
|
60
|
+
case "event":
|
|
61
|
+
return "happened";
|
|
62
|
+
case "code":
|
|
63
|
+
return "implemented";
|
|
64
|
+
default:
|
|
65
|
+
return "states";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function extractMemoryCards(row) {
|
|
69
|
+
const segments = splitSegments(row.raw_text);
|
|
70
|
+
const cards = [];
|
|
71
|
+
for (const sentence of segments) {
|
|
72
|
+
const type = inferCardType(sentence, row.tool_name);
|
|
73
|
+
const subject = extractSubject(sentence, row.agent_id);
|
|
74
|
+
const content = sentence.length > MAX_SEGMENT_CHARS ? `${sentence.slice(0, MAX_SEGMENT_CHARS - 1)}\u2026` : sentence;
|
|
75
|
+
cards.push({
|
|
76
|
+
id: stableId(row.id, type, content),
|
|
77
|
+
memory_id: row.id,
|
|
78
|
+
agent_id: row.agent_id,
|
|
79
|
+
session_id: row.session_id,
|
|
80
|
+
project_name: row.project_name ?? null,
|
|
81
|
+
timestamp: row.timestamp,
|
|
82
|
+
card_type: type,
|
|
83
|
+
subject,
|
|
84
|
+
predicate: predicateFor(type),
|
|
85
|
+
object: content,
|
|
86
|
+
content,
|
|
87
|
+
source_ref: row.id,
|
|
88
|
+
confidence: type === "fact" ? 0.55 : 0.65
|
|
89
|
+
});
|
|
90
|
+
if (cards.length >= MAX_CARDS_PER_MEMORY) break;
|
|
91
|
+
}
|
|
92
|
+
return cards;
|
|
93
|
+
}
|
|
94
|
+
async function insertMemoryCardsForBatch(rows) {
|
|
95
|
+
const cards = rows.flatMap(extractMemoryCards);
|
|
96
|
+
if (cards.length === 0) return 0;
|
|
97
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
98
|
+
const client = getClient();
|
|
99
|
+
const stmts = cards.map((card) => ({
|
|
100
|
+
sql: `INSERT OR IGNORE INTO memory_cards
|
|
101
|
+
(id, memory_id, agent_id, session_id, project_name, timestamp, card_type,
|
|
102
|
+
subject, predicate, object, content, source_ref, confidence, active, created_at)
|
|
103
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?)`,
|
|
104
|
+
args: [
|
|
105
|
+
card.id,
|
|
106
|
+
card.memory_id,
|
|
107
|
+
card.agent_id,
|
|
108
|
+
card.session_id,
|
|
109
|
+
card.project_name,
|
|
110
|
+
card.timestamp,
|
|
111
|
+
card.card_type,
|
|
112
|
+
card.subject,
|
|
113
|
+
card.predicate,
|
|
114
|
+
card.object,
|
|
115
|
+
card.content,
|
|
116
|
+
card.source_ref,
|
|
117
|
+
card.confidence,
|
|
118
|
+
now
|
|
119
|
+
]
|
|
120
|
+
}));
|
|
121
|
+
await client.batch(stmts, "write");
|
|
122
|
+
return cards.length;
|
|
123
|
+
}
|
|
124
|
+
function buildMatchExpr(queryText) {
|
|
125
|
+
const terms = queryText.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_]/g, "")).filter((t) => t.length >= 3).slice(0, 12);
|
|
126
|
+
if (terms.length === 0) return null;
|
|
127
|
+
return terms.map((t) => `${t}*`).join(terms.length >= 3 ? " AND " : " OR ");
|
|
128
|
+
}
|
|
129
|
+
async function searchMemoryCards(queryText, agentId, options) {
|
|
130
|
+
const limit = options?.limit ?? 10;
|
|
131
|
+
const matchExpr = buildMatchExpr(queryText);
|
|
132
|
+
if (!matchExpr) return [];
|
|
133
|
+
let sql = `SELECT c.id, c.memory_id, c.agent_id, c.session_id, c.project_name,
|
|
134
|
+
c.timestamp, c.card_type, c.content, c.source_ref, c.confidence
|
|
135
|
+
FROM memory_cards c
|
|
136
|
+
JOIN memory_cards_fts fts ON c.rowid = fts.rowid
|
|
137
|
+
WHERE memory_cards_fts MATCH ?
|
|
138
|
+
AND c.agent_id = ?
|
|
139
|
+
AND COALESCE(c.active, 1) = 1`;
|
|
140
|
+
const args = [matchExpr, agentId];
|
|
141
|
+
if (options?.projectName) {
|
|
142
|
+
sql += ` AND c.project_name = ?`;
|
|
143
|
+
args.push(options.projectName);
|
|
144
|
+
}
|
|
145
|
+
if (options?.since) {
|
|
146
|
+
sql += ` AND c.timestamp >= ?`;
|
|
147
|
+
args.push(options.since);
|
|
148
|
+
}
|
|
149
|
+
sql += ` ORDER BY rank LIMIT ?`;
|
|
150
|
+
args.push(limit);
|
|
151
|
+
const result = await getClient().execute({ sql, args });
|
|
152
|
+
return result.rows.map((row) => ({
|
|
153
|
+
id: `card:${String(row.id)}`,
|
|
154
|
+
agent_id: String(row.agent_id),
|
|
155
|
+
agent_role: "memory_card",
|
|
156
|
+
session_id: String(row.session_id),
|
|
157
|
+
timestamp: String(row.timestamp),
|
|
158
|
+
tool_name: `memory_card:${String(row.card_type)}`,
|
|
159
|
+
project_name: row.project_name == null ? "" : String(row.project_name),
|
|
160
|
+
has_error: false,
|
|
161
|
+
raw_text: `[${String(row.card_type)}] ${String(row.content)}
|
|
162
|
+
Source memory: ${String(row.source_ref ?? row.memory_id)}`,
|
|
163
|
+
vector: [],
|
|
164
|
+
importance: 6,
|
|
165
|
+
status: "active",
|
|
166
|
+
confidence: Number(row.confidence ?? 0.6),
|
|
167
|
+
last_accessed: String(row.timestamp)
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
export {
|
|
171
|
+
extractMemoryCards,
|
|
172
|
+
insertMemoryCardsForBatch,
|
|
173
|
+
searchMemoryCards
|
|
174
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extractGraphForBatch,
|
|
3
|
+
extractMemoryGraph,
|
|
4
|
+
upsertMemoryGraph
|
|
5
|
+
} from "./chunk-X2WBH2IO.js";
|
|
6
|
+
import "./chunk-CHBHR5W6.js";
|
|
7
|
+
import "./chunk-2I23RPSI.js";
|
|
8
|
+
import "./chunk-C6OYEJJI.js";
|
|
9
|
+
import "./chunk-FXU7JOXK.js";
|
|
10
|
+
import "./chunk-VXIMSRTO.js";
|
|
11
|
+
import "./chunk-LYH5HE24.js";
|
|
12
|
+
import "./chunk-MLKGABMK.js";
|
|
13
|
+
export {
|
|
14
|
+
extractGraphForBatch,
|
|
15
|
+
extractMemoryGraph,
|
|
16
|
+
upsertMemoryGraph
|
|
17
|
+
};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getClient
|
|
3
|
+
} from "./chunk-CHBHR5W6.js";
|
|
4
|
+
import "./chunk-2I23RPSI.js";
|
|
5
|
+
import "./chunk-C6OYEJJI.js";
|
|
6
|
+
import "./chunk-FXU7JOXK.js";
|
|
7
|
+
import {
|
|
8
|
+
loadConfig
|
|
9
|
+
} from "./chunk-VXIMSRTO.js";
|
|
10
|
+
import "./chunk-LYH5HE24.js";
|
|
11
|
+
import "./chunk-MLKGABMK.js";
|
|
12
|
+
|
|
13
|
+
// src/lib/memory-poisoning-defense.ts
|
|
14
|
+
var SYSTEM_TOOLS = /* @__PURE__ */ new Set([
|
|
15
|
+
"session-start",
|
|
16
|
+
"ingest",
|
|
17
|
+
"ingest-worker",
|
|
18
|
+
"consolidation",
|
|
19
|
+
"skill-learning",
|
|
20
|
+
"summary-worker",
|
|
21
|
+
"bug-report-worker",
|
|
22
|
+
"reflection",
|
|
23
|
+
"dreaming-engine",
|
|
24
|
+
"daemon"
|
|
25
|
+
]);
|
|
26
|
+
function computeTrustLevel(toolName, agentId) {
|
|
27
|
+
if (SYSTEM_TOOLS.has(toolName)) return "system";
|
|
28
|
+
if (agentId && (toolName.startsWith("mcp__exe") || toolName === "manual" || toolName === "store_memory")) {
|
|
29
|
+
return "agent";
|
|
30
|
+
}
|
|
31
|
+
if (toolName.includes("webhook") || toolName.includes("pipeline") || toolName.includes("ingest_raw")) {
|
|
32
|
+
return "external";
|
|
33
|
+
}
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
function cosineDistance(a, b) {
|
|
37
|
+
if (a.length !== b.length || a.length === 0) return 1;
|
|
38
|
+
let dotProduct = 0;
|
|
39
|
+
let normA = 0;
|
|
40
|
+
let normB = 0;
|
|
41
|
+
for (let i = 0; i < a.length; i++) {
|
|
42
|
+
dotProduct += a[i] * b[i];
|
|
43
|
+
normA += a[i] * a[i];
|
|
44
|
+
normB += b[i] * b[i];
|
|
45
|
+
}
|
|
46
|
+
const denom = Math.sqrt(normA) * Math.sqrt(normB);
|
|
47
|
+
if (denom === 0) return 1;
|
|
48
|
+
const similarity = dotProduct / denom;
|
|
49
|
+
return 1 - similarity;
|
|
50
|
+
}
|
|
51
|
+
async function getAgentCentroid(agentId, projectName) {
|
|
52
|
+
const client = getClient();
|
|
53
|
+
try {
|
|
54
|
+
const result = await client.execute({
|
|
55
|
+
sql: `SELECT centroid_vector FROM agent_centroids WHERE agent_id = ? AND project_name = ?`,
|
|
56
|
+
args: [agentId, projectName]
|
|
57
|
+
});
|
|
58
|
+
if (result.rows.length === 0 || !result.rows[0].centroid_vector) return null;
|
|
59
|
+
const blob = result.rows[0].centroid_vector;
|
|
60
|
+
if (blob instanceof ArrayBuffer || ArrayBuffer.isView(blob)) {
|
|
61
|
+
return Array.from(new Float32Array(blob instanceof ArrayBuffer ? blob : blob.buffer));
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function checkAnomaly(vector, agentId, projectName, threshold) {
|
|
69
|
+
const centroid = await getAgentCentroid(agentId, projectName);
|
|
70
|
+
if (!centroid) return { anomalous: false, distance: 0 };
|
|
71
|
+
const distance = cosineDistance(vector, centroid);
|
|
72
|
+
const effectiveThreshold = threshold ?? 0.85;
|
|
73
|
+
return { anomalous: distance > effectiveThreshold, distance };
|
|
74
|
+
}
|
|
75
|
+
async function refreshAgentCentroid(agentId, projectName, sampleSize = 100) {
|
|
76
|
+
const client = getClient();
|
|
77
|
+
const result = await client.execute({
|
|
78
|
+
sql: `SELECT vector FROM memories
|
|
79
|
+
WHERE agent_id = ? AND project_name = ? AND vector IS NOT NULL
|
|
80
|
+
AND status = 'active' AND quarantined = 0
|
|
81
|
+
ORDER BY timestamp DESC LIMIT ?`,
|
|
82
|
+
args: [agentId, projectName, sampleSize]
|
|
83
|
+
});
|
|
84
|
+
if (result.rows.length < 5) return;
|
|
85
|
+
let dim = 0;
|
|
86
|
+
const vectors = [];
|
|
87
|
+
for (const row of result.rows) {
|
|
88
|
+
const blob = row.vector;
|
|
89
|
+
if (blob instanceof ArrayBuffer || ArrayBuffer.isView(blob)) {
|
|
90
|
+
const arr = Array.from(new Float32Array(blob instanceof ArrayBuffer ? blob : blob.buffer));
|
|
91
|
+
if (arr.length > 0) {
|
|
92
|
+
dim = arr.length;
|
|
93
|
+
vectors.push(arr);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (vectors.length < 5 || dim === 0) return;
|
|
98
|
+
const centroid = new Float32Array(dim);
|
|
99
|
+
for (const v of vectors) {
|
|
100
|
+
for (let i = 0; i < dim; i++) {
|
|
101
|
+
centroid[i] += v[i] / vectors.length;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const centroidBlob = Buffer.from(centroid.buffer);
|
|
105
|
+
await client.execute({
|
|
106
|
+
sql: `INSERT OR REPLACE INTO agent_centroids (agent_id, project_name, centroid_vector, sample_count, updated_at)
|
|
107
|
+
VALUES (?, ?, ?, ?, ?)`,
|
|
108
|
+
args: [agentId, projectName, centroidBlob, vectors.length, (/* @__PURE__ */ new Date()).toISOString()]
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
async function quarantineMemory(memoryId, reason) {
|
|
112
|
+
const client = getClient();
|
|
113
|
+
await client.execute({
|
|
114
|
+
sql: `UPDATE memories SET quarantined = 1, quarantine_reason = ? WHERE id = ?`,
|
|
115
|
+
args: [reason, memoryId]
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async function approveQuarantined(memoryId) {
|
|
119
|
+
const client = getClient();
|
|
120
|
+
await client.execute({
|
|
121
|
+
sql: `UPDATE memories SET quarantined = 0, quarantine_reason = NULL WHERE id = ?`,
|
|
122
|
+
args: [memoryId]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async function rejectQuarantined(memoryId) {
|
|
126
|
+
const client = getClient();
|
|
127
|
+
await client.execute({
|
|
128
|
+
sql: `UPDATE memories SET status = 'archived', quarantine_reason = COALESCE(quarantine_reason, '') || ' [REJECTED]' WHERE id = ?`,
|
|
129
|
+
args: [memoryId]
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async function listQuarantined(limit = 20) {
|
|
133
|
+
const client = getClient();
|
|
134
|
+
const result = await client.execute({
|
|
135
|
+
sql: `SELECT id, agent_id, raw_text, quarantine_reason, timestamp
|
|
136
|
+
FROM memories
|
|
137
|
+
WHERE quarantined = 1 AND status = 'active'
|
|
138
|
+
ORDER BY timestamp DESC LIMIT ?`,
|
|
139
|
+
args: [limit]
|
|
140
|
+
});
|
|
141
|
+
return result.rows.map((r) => ({
|
|
142
|
+
id: String(r.id),
|
|
143
|
+
agentId: String(r.agent_id),
|
|
144
|
+
rawText: String(r.raw_text).slice(0, 200),
|
|
145
|
+
reason: String(r.quarantine_reason ?? "unknown"),
|
|
146
|
+
createdAt: String(r.timestamp)
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
var _writeCounters = /* @__PURE__ */ new Map();
|
|
150
|
+
var _sessionCounters = /* @__PURE__ */ new Map();
|
|
151
|
+
function checkRateLimit(agentId, sessionId, limits) {
|
|
152
|
+
const perMinute = limits?.perMinute ?? 50;
|
|
153
|
+
const perSession = limits?.perSession ?? 5e3;
|
|
154
|
+
const now = Date.now();
|
|
155
|
+
const minuteKey = `${agentId}:minute`;
|
|
156
|
+
const minuteCounter = _writeCounters.get(minuteKey);
|
|
157
|
+
if (minuteCounter && now - minuteCounter.windowStart < 6e4) {
|
|
158
|
+
if (minuteCounter.count >= perMinute) {
|
|
159
|
+
return { allowed: false, reason: `Rate limit: ${perMinute}/minute exceeded for ${agentId}` };
|
|
160
|
+
}
|
|
161
|
+
minuteCounter.count++;
|
|
162
|
+
} else {
|
|
163
|
+
_writeCounters.set(minuteKey, { count: 1, windowStart: now });
|
|
164
|
+
}
|
|
165
|
+
const sessionCount = (_sessionCounters.get(sessionId) ?? 0) + 1;
|
|
166
|
+
_sessionCounters.set(sessionId, sessionCount);
|
|
167
|
+
if (sessionCount > perSession) {
|
|
168
|
+
return { allowed: false, reason: `Session limit: ${perSession} memories exceeded for session ${sessionId}` };
|
|
169
|
+
}
|
|
170
|
+
return { allowed: true };
|
|
171
|
+
}
|
|
172
|
+
function resetRateLimits() {
|
|
173
|
+
_writeCounters.clear();
|
|
174
|
+
_sessionCounters.clear();
|
|
175
|
+
}
|
|
176
|
+
async function checkMemoryPoisoning(opts) {
|
|
177
|
+
const config = await loadConfig();
|
|
178
|
+
const defenseConfig = config.poisoningDefense;
|
|
179
|
+
if (!defenseConfig?.enabled) {
|
|
180
|
+
return { passed: true, trustLevel: computeTrustLevel(opts.toolName, opts.agentId), quarantined: false };
|
|
181
|
+
}
|
|
182
|
+
const trustLevel = computeTrustLevel(opts.toolName, opts.agentId);
|
|
183
|
+
const rateCheck = checkRateLimit(opts.agentId, opts.sessionId, defenseConfig.rateLimits);
|
|
184
|
+
if (!rateCheck.allowed) {
|
|
185
|
+
await quarantineMemory(opts.memoryId, rateCheck.reason);
|
|
186
|
+
return { passed: false, trustLevel, quarantined: true, reason: rateCheck.reason };
|
|
187
|
+
}
|
|
188
|
+
if (opts.importance >= 8 && trustLevel !== "system") {
|
|
189
|
+
const highImpLimit = defenseConfig.rateLimits?.highImportance ?? 20;
|
|
190
|
+
const sessionHighImp = (_sessionCounters.get(`${opts.sessionId}:highimp`) ?? 0) + 1;
|
|
191
|
+
_sessionCounters.set(`${opts.sessionId}:highimp`, sessionHighImp);
|
|
192
|
+
if (sessionHighImp > highImpLimit) {
|
|
193
|
+
await quarantineMemory(opts.memoryId, `High-importance inflation: ${sessionHighImp} importance>=8 memories in session`);
|
|
194
|
+
return { passed: false, trustLevel, quarantined: true, reason: "importance inflation" };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (opts.vector && opts.vector.length > 0) {
|
|
198
|
+
const threshold = defenseConfig.anomalyThreshold ?? 0.85;
|
|
199
|
+
const anomaly = await checkAnomaly(opts.vector, opts.agentId, opts.projectName, threshold);
|
|
200
|
+
if (anomaly.anomalous) {
|
|
201
|
+
await quarantineMemory(opts.memoryId, `Anomalous vector: distance ${anomaly.distance.toFixed(3)} > threshold ${threshold}`);
|
|
202
|
+
return {
|
|
203
|
+
passed: false,
|
|
204
|
+
trustLevel,
|
|
205
|
+
anomalyDistance: anomaly.distance,
|
|
206
|
+
quarantined: true,
|
|
207
|
+
reason: `anomaly distance ${anomaly.distance.toFixed(3)}`
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return { passed: true, trustLevel, quarantined: false };
|
|
212
|
+
}
|
|
213
|
+
export {
|
|
214
|
+
approveQuarantined,
|
|
215
|
+
checkAnomaly,
|
|
216
|
+
checkMemoryPoisoning,
|
|
217
|
+
checkRateLimit,
|
|
218
|
+
computeTrustLevel,
|
|
219
|
+
getAgentCentroid,
|
|
220
|
+
listQuarantined,
|
|
221
|
+
quarantineMemory,
|
|
222
|
+
refreshAgentCentroid,
|
|
223
|
+
rejectQuarantined,
|
|
224
|
+
resetRateLimits
|
|
225
|
+
};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getClient
|
|
3
|
+
} from "./chunk-CHBHR5W6.js";
|
|
4
|
+
import "./chunk-2I23RPSI.js";
|
|
5
|
+
import "./chunk-C6OYEJJI.js";
|
|
6
|
+
import "./chunk-FXU7JOXK.js";
|
|
7
|
+
import "./chunk-VXIMSRTO.js";
|
|
8
|
+
import "./chunk-LYH5HE24.js";
|
|
9
|
+
import "./chunk-MLKGABMK.js";
|
|
10
|
+
|
|
11
|
+
// src/lib/memory-reflection.ts
|
|
12
|
+
var REFLECTION_SCHEMA = `
|
|
13
|
+
CREATE TABLE IF NOT EXISTS memory_reflections (
|
|
14
|
+
id TEXT PRIMARY KEY,
|
|
15
|
+
agent_id TEXT NOT NULL,
|
|
16
|
+
project_name TEXT,
|
|
17
|
+
insight_type TEXT NOT NULL CHECK (insight_type IN ('pattern', 'contradiction', 'evolution', 'summary')),
|
|
18
|
+
subject TEXT NOT NULL,
|
|
19
|
+
content TEXT NOT NULL,
|
|
20
|
+
source_card_ids TEXT NOT NULL DEFAULT '[]',
|
|
21
|
+
confidence REAL NOT NULL DEFAULT 0.7,
|
|
22
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
23
|
+
created_at TEXT NOT NULL,
|
|
24
|
+
expires_at TEXT
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
CREATE INDEX IF NOT EXISTS idx_memory_reflections_agent
|
|
28
|
+
ON memory_reflections(agent_id, insight_type, created_at DESC);
|
|
29
|
+
|
|
30
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS memory_reflections_fts USING fts5(
|
|
31
|
+
content, subject,
|
|
32
|
+
content=memory_reflections, content_rowid=rowid
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
CREATE TRIGGER IF NOT EXISTS memory_reflections_ai AFTER INSERT ON memory_reflections BEGIN
|
|
36
|
+
INSERT INTO memory_reflections_fts(rowid, content, subject)
|
|
37
|
+
VALUES (new.rowid, new.content, new.subject);
|
|
38
|
+
END;
|
|
39
|
+
|
|
40
|
+
CREATE TRIGGER IF NOT EXISTS memory_reflections_ad AFTER DELETE ON memory_reflections BEGIN
|
|
41
|
+
INSERT INTO memory_reflections_fts(memory_reflections_fts, rowid, content, subject)
|
|
42
|
+
VALUES ('delete', old.rowid, old.content, old.subject);
|
|
43
|
+
END;
|
|
44
|
+
`;
|
|
45
|
+
async function detectContradictions(agentId, projectName) {
|
|
46
|
+
const client = getClient();
|
|
47
|
+
const insights = [];
|
|
48
|
+
let sql = `SELECT subject, GROUP_CONCAT(id) as card_ids,
|
|
49
|
+
GROUP_CONCAT(content, '|||') as contents,
|
|
50
|
+
GROUP_CONCAT(timestamp) as timestamps,
|
|
51
|
+
COUNT(*) as cnt
|
|
52
|
+
FROM memory_cards
|
|
53
|
+
WHERE agent_id = ? AND active = 1 AND subject IS NOT NULL`;
|
|
54
|
+
const args = [agentId];
|
|
55
|
+
if (projectName) {
|
|
56
|
+
sql += " AND project_name = ?";
|
|
57
|
+
args.push(projectName);
|
|
58
|
+
}
|
|
59
|
+
sql += " GROUP BY subject HAVING cnt >= 2 ORDER BY cnt DESC LIMIT 100";
|
|
60
|
+
const result = await client.execute({ sql, args });
|
|
61
|
+
for (const row of result.rows) {
|
|
62
|
+
const subject = row.subject;
|
|
63
|
+
const cardIds = row.card_ids.split(",");
|
|
64
|
+
const contents = row.contents.split("|||");
|
|
65
|
+
const timestamps = row.timestamps.split(",");
|
|
66
|
+
if (contents.length < 2) continue;
|
|
67
|
+
const unique = new Set(contents.map((c) => c.toLowerCase().trim()));
|
|
68
|
+
if (unique.size < 2) continue;
|
|
69
|
+
const pairs = contents.map((c, i) => ({ content: c, ts: timestamps[i], id: cardIds[i] }));
|
|
70
|
+
pairs.sort((a, b) => (a.ts ?? "").localeCompare(b.ts ?? ""));
|
|
71
|
+
const earliest = pairs[0];
|
|
72
|
+
const latest = pairs[pairs.length - 1];
|
|
73
|
+
if (earliest.content.toLowerCase().trim() === latest.content.toLowerCase().trim()) continue;
|
|
74
|
+
const insightContent = `${subject}: changed from "${earliest.content.slice(0, 100)}" to "${latest.content.slice(0, 100)}"`;
|
|
75
|
+
const id = `reflect-evol-${subject.slice(0, 20)}-${Date.now()}`.replace(/[^a-zA-Z0-9-]/g, "_");
|
|
76
|
+
insights.push({
|
|
77
|
+
id,
|
|
78
|
+
agent_id: agentId,
|
|
79
|
+
project_name: projectName ?? null,
|
|
80
|
+
insight_type: "evolution",
|
|
81
|
+
subject,
|
|
82
|
+
content: insightContent,
|
|
83
|
+
source_card_ids: cardIds.slice(0, 10),
|
|
84
|
+
confidence: 0.7,
|
|
85
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return insights;
|
|
89
|
+
}
|
|
90
|
+
async function detectPatterns(agentId, projectName) {
|
|
91
|
+
const client = getClient();
|
|
92
|
+
const insights = [];
|
|
93
|
+
let sql = `SELECT subject, card_type, COUNT(DISTINCT session_id) as sessions,
|
|
94
|
+
COUNT(*) as total, GROUP_CONCAT(id) as card_ids,
|
|
95
|
+
GROUP_CONCAT(content, '|||') as sample_contents
|
|
96
|
+
FROM memory_cards
|
|
97
|
+
WHERE agent_id = ? AND active = 1 AND subject IS NOT NULL`;
|
|
98
|
+
const args = [agentId];
|
|
99
|
+
if (projectName) {
|
|
100
|
+
sql += " AND project_name = ?";
|
|
101
|
+
args.push(projectName);
|
|
102
|
+
}
|
|
103
|
+
sql += " GROUP BY subject, card_type HAVING sessions >= 3 ORDER BY sessions DESC LIMIT 50";
|
|
104
|
+
const result = await client.execute({ sql, args });
|
|
105
|
+
for (const row of result.rows) {
|
|
106
|
+
const subject = row.subject;
|
|
107
|
+
const cardType = row.card_type;
|
|
108
|
+
const sessions = Number(row.sessions);
|
|
109
|
+
const total = Number(row.total);
|
|
110
|
+
const cardIds = row.card_ids.split(",");
|
|
111
|
+
const samples = row.sample_contents.split("|||").slice(0, 3);
|
|
112
|
+
const insightContent = `${subject} appears in ${sessions} sessions (${total} mentions) as ${cardType}. Examples: ${samples.map((s) => `"${s.slice(0, 80)}"`).join("; ")}`;
|
|
113
|
+
const id = `reflect-pattern-${subject.slice(0, 20)}-${Date.now()}`.replace(/[^a-zA-Z0-9-]/g, "_");
|
|
114
|
+
insights.push({
|
|
115
|
+
id,
|
|
116
|
+
agent_id: agentId,
|
|
117
|
+
project_name: projectName ?? null,
|
|
118
|
+
insight_type: "pattern",
|
|
119
|
+
subject,
|
|
120
|
+
content: insightContent,
|
|
121
|
+
source_card_ids: cardIds.slice(0, 10),
|
|
122
|
+
confidence: Math.min(0.5 + sessions * 0.1, 0.95),
|
|
123
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return insights;
|
|
127
|
+
}
|
|
128
|
+
async function generateSummaries(agentId, projectName) {
|
|
129
|
+
const client = getClient();
|
|
130
|
+
const insights = [];
|
|
131
|
+
let sql = `SELECT subject, COUNT(*) as cnt, GROUP_CONCAT(id) as card_ids,
|
|
132
|
+
GROUP_CONCAT(content, '|||') as all_contents,
|
|
133
|
+
MIN(timestamp) as first_seen, MAX(timestamp) as last_seen
|
|
134
|
+
FROM memory_cards
|
|
135
|
+
WHERE agent_id = ? AND active = 1 AND subject IS NOT NULL`;
|
|
136
|
+
const args = [agentId];
|
|
137
|
+
if (projectName) {
|
|
138
|
+
sql += " AND project_name = ?";
|
|
139
|
+
args.push(projectName);
|
|
140
|
+
}
|
|
141
|
+
sql += " GROUP BY subject HAVING cnt >= 5 ORDER BY cnt DESC LIMIT 30";
|
|
142
|
+
const result = await client.execute({ sql, args });
|
|
143
|
+
for (const row of result.rows) {
|
|
144
|
+
const subject = row.subject;
|
|
145
|
+
const cnt = Number(row.cnt);
|
|
146
|
+
const cardIds = row.card_ids.split(",");
|
|
147
|
+
const allContents = (row.sample_contents ?? row.all_contents ?? "").split("|||");
|
|
148
|
+
const firstSeen = row.first_seen;
|
|
149
|
+
const lastSeen = row.last_seen;
|
|
150
|
+
const unique = [...new Set(allContents.map((c) => c.trim()))].filter((c) => c.length > 10).slice(0, 5);
|
|
151
|
+
if (unique.length < 2) continue;
|
|
152
|
+
const insightContent = `${subject} (${cnt} facts, ${firstSeen?.slice(0, 10)} to ${lastSeen?.slice(0, 10)}): ${unique.map((u) => u.slice(0, 80)).join(". ")}`;
|
|
153
|
+
const id = `reflect-summary-${subject.slice(0, 20)}-${Date.now()}`.replace(/[^a-zA-Z0-9-]/g, "_");
|
|
154
|
+
insights.push({
|
|
155
|
+
id,
|
|
156
|
+
agent_id: agentId,
|
|
157
|
+
project_name: projectName ?? null,
|
|
158
|
+
insight_type: "summary",
|
|
159
|
+
subject,
|
|
160
|
+
content: insightContent,
|
|
161
|
+
source_card_ids: cardIds.slice(0, 10),
|
|
162
|
+
confidence: 0.6,
|
|
163
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return insights;
|
|
167
|
+
}
|
|
168
|
+
async function runReflection(agentId, projectName) {
|
|
169
|
+
const [contradictions, patterns, summaries] = await Promise.all([
|
|
170
|
+
detectContradictions(agentId, projectName),
|
|
171
|
+
detectPatterns(agentId, projectName),
|
|
172
|
+
generateSummaries(agentId, projectName)
|
|
173
|
+
]);
|
|
174
|
+
const all = [...contradictions, ...patterns, ...summaries];
|
|
175
|
+
if (all.length === 0) return { contradictions: 0, patterns: 0, summaries: 0 };
|
|
176
|
+
const client = getClient();
|
|
177
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
178
|
+
const stmts = all.map((insight) => ({
|
|
179
|
+
sql: `INSERT OR IGNORE INTO memory_reflections
|
|
180
|
+
(id, agent_id, project_name, insight_type, subject, content,
|
|
181
|
+
source_card_ids, confidence, active, created_at)
|
|
182
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)`,
|
|
183
|
+
args: [
|
|
184
|
+
insight.id,
|
|
185
|
+
insight.agent_id,
|
|
186
|
+
insight.project_name,
|
|
187
|
+
insight.insight_type,
|
|
188
|
+
insight.subject,
|
|
189
|
+
insight.content,
|
|
190
|
+
JSON.stringify(insight.source_card_ids),
|
|
191
|
+
insight.confidence,
|
|
192
|
+
now
|
|
193
|
+
]
|
|
194
|
+
}));
|
|
195
|
+
await client.batch(stmts, "write");
|
|
196
|
+
return {
|
|
197
|
+
contradictions: contradictions.length,
|
|
198
|
+
patterns: patterns.length,
|
|
199
|
+
summaries: summaries.length
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async function searchReflections(queryText, agentId, limit = 5) {
|
|
203
|
+
const terms = queryText.toLowerCase().split(/\s+/).filter((t) => t.length >= 3).map((t) => t.replace(/[^a-z0-9_]/g, "")).filter((t) => t.length >= 3).slice(0, 8);
|
|
204
|
+
if (terms.length === 0) return [];
|
|
205
|
+
const matchExpr = terms.map((t) => `${t}*`).join(terms.length >= 3 ? " AND " : " OR ");
|
|
206
|
+
const client = getClient();
|
|
207
|
+
const result = await client.execute({
|
|
208
|
+
sql: `SELECT r.id, r.agent_id, r.project_name, r.insight_type, r.subject,
|
|
209
|
+
r.content, r.source_card_ids, r.confidence, r.created_at
|
|
210
|
+
FROM memory_reflections r
|
|
211
|
+
JOIN memory_reflections_fts fts ON r.rowid = fts.rowid
|
|
212
|
+
WHERE memory_reflections_fts MATCH ?
|
|
213
|
+
AND r.agent_id = ?
|
|
214
|
+
AND r.active = 1
|
|
215
|
+
ORDER BY rank
|
|
216
|
+
LIMIT ?`,
|
|
217
|
+
args: [matchExpr, agentId, limit]
|
|
218
|
+
});
|
|
219
|
+
return result.rows.map((row) => ({
|
|
220
|
+
id: row.id,
|
|
221
|
+
agent_id: row.agent_id,
|
|
222
|
+
project_name: row.project_name,
|
|
223
|
+
insight_type: row.insight_type,
|
|
224
|
+
subject: row.subject,
|
|
225
|
+
content: row.content,
|
|
226
|
+
source_card_ids: JSON.parse(row.source_card_ids ?? "[]"),
|
|
227
|
+
confidence: Number(row.confidence),
|
|
228
|
+
created_at: row.created_at
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
export {
|
|
232
|
+
REFLECTION_SCHEMA,
|
|
233
|
+
detectContradictions,
|
|
234
|
+
detectPatterns,
|
|
235
|
+
generateSummaries,
|
|
236
|
+
runReflection,
|
|
237
|
+
searchReflections
|
|
238
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cleanupOldNotifications,
|
|
3
|
+
formatNotifications,
|
|
4
|
+
markAsRead,
|
|
5
|
+
markAsReadByTaskFile,
|
|
6
|
+
markDoneTaskNotificationsAsRead,
|
|
7
|
+
migrateJsonNotifications,
|
|
8
|
+
readUnreadNotifications,
|
|
9
|
+
writeNotification
|
|
10
|
+
} from "./chunk-5AS622MM.js";
|
|
11
|
+
import "./chunk-KZNSOHCB.js";
|
|
12
|
+
import "./chunk-5CHYEKMH.js";
|
|
13
|
+
import "./chunk-4JERP7NT.js";
|
|
14
|
+
import "./chunk-OBUV3W7L.js";
|
|
15
|
+
import "./chunk-MVW62NIZ.js";
|
|
16
|
+
import "./chunk-OYNKIAVW.js";
|
|
17
|
+
import "./chunk-CX6GL3ZJ.js";
|
|
18
|
+
import "./chunk-2NQQP3FF.js";
|
|
19
|
+
import "./chunk-EZ7KAZMC.js";
|
|
20
|
+
import "./chunk-NGP6LSV2.js";
|
|
21
|
+
import "./chunk-WQEUY7DC.js";
|
|
22
|
+
import "./chunk-CVYC6DUW.js";
|
|
23
|
+
import "./chunk-OPU3NYOO.js";
|
|
24
|
+
import "./chunk-GJV3WDWM.js";
|
|
25
|
+
import "./chunk-MP2AFCGL.js";
|
|
26
|
+
import "./chunk-G33BHQCO.js";
|
|
27
|
+
import "./chunk-HYZV25LY.js";
|
|
28
|
+
import "./chunk-CHBHR5W6.js";
|
|
29
|
+
import "./chunk-2I23RPSI.js";
|
|
30
|
+
import "./chunk-C6OYEJJI.js";
|
|
31
|
+
import "./chunk-FXU7JOXK.js";
|
|
32
|
+
import "./chunk-MOZ2YQ54.js";
|
|
33
|
+
import "./chunk-VXIMSRTO.js";
|
|
34
|
+
import "./chunk-LYH5HE24.js";
|
|
35
|
+
import "./chunk-MLKGABMK.js";
|
|
36
|
+
export {
|
|
37
|
+
cleanupOldNotifications,
|
|
38
|
+
formatNotifications,
|
|
39
|
+
markAsRead,
|
|
40
|
+
markAsReadByTaskFile,
|
|
41
|
+
markDoneTaskNotificationsAsRead,
|
|
42
|
+
migrateJsonNotifications,
|
|
43
|
+
readUnreadNotifications,
|
|
44
|
+
writeNotification
|
|
45
|
+
};
|