@agentmemory/agentmemory 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("iii-sdk", () => ({
|
|
4
|
+
getContext: () => ({
|
|
5
|
+
logger: { info: vi.fn(), error: vi.fn(), warn: vi.fn() },
|
|
6
|
+
}),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
import { registerSentinelsFunction } from "../src/functions/sentinels.js";
|
|
10
|
+
import { registerActionsFunction } from "../src/functions/actions.js";
|
|
11
|
+
import type { Action, ActionEdge, Sentinel } from "../src/types.js";
|
|
12
|
+
|
|
13
|
+
function mockKV() {
|
|
14
|
+
const store = new Map<string, Map<string, unknown>>();
|
|
15
|
+
return {
|
|
16
|
+
get: async <T>(scope: string, key: string): Promise<T | null> => {
|
|
17
|
+
return (store.get(scope)?.get(key) as T) ?? null;
|
|
18
|
+
},
|
|
19
|
+
set: async <T>(scope: string, key: string, data: T): Promise<T> => {
|
|
20
|
+
if (!store.has(scope)) store.set(scope, new Map());
|
|
21
|
+
store.get(scope)!.set(key, data);
|
|
22
|
+
return data;
|
|
23
|
+
},
|
|
24
|
+
delete: async (scope: string, key: string): Promise<void> => {
|
|
25
|
+
store.get(scope)?.delete(key);
|
|
26
|
+
},
|
|
27
|
+
list: async <T>(scope: string): Promise<T[]> => {
|
|
28
|
+
const entries = store.get(scope);
|
|
29
|
+
return entries ? (Array.from(entries.values()) as T[]) : [];
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function mockSdk() {
|
|
35
|
+
const functions = new Map<string, Function>();
|
|
36
|
+
return {
|
|
37
|
+
registerFunction: (opts: { id: string }, handler: Function) => {
|
|
38
|
+
functions.set(opts.id, handler);
|
|
39
|
+
},
|
|
40
|
+
registerTrigger: () => {},
|
|
41
|
+
trigger: async (id: string, data: unknown) => {
|
|
42
|
+
const fn = functions.get(id);
|
|
43
|
+
if (!fn) throw new Error(`No function: ${id}`);
|
|
44
|
+
return fn(data);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe("Sentinels Functions", () => {
|
|
50
|
+
let sdk: ReturnType<typeof mockSdk>;
|
|
51
|
+
let kv: ReturnType<typeof mockKV>;
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
sdk = mockSdk();
|
|
55
|
+
kv = mockKV();
|
|
56
|
+
registerSentinelsFunction(sdk as never, kv as never);
|
|
57
|
+
registerActionsFunction(sdk as never, kv as never);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("mem::sentinel-create", () => {
|
|
61
|
+
it("creates a webhook sentinel with valid config", async () => {
|
|
62
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
63
|
+
name: "deploy-hook",
|
|
64
|
+
type: "webhook",
|
|
65
|
+
config: { path: "/hooks/deploy" },
|
|
66
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
67
|
+
|
|
68
|
+
expect(result.success).toBe(true);
|
|
69
|
+
expect(result.sentinel.id).toMatch(/^snl_/);
|
|
70
|
+
expect(result.sentinel.name).toBe("deploy-hook");
|
|
71
|
+
expect(result.sentinel.type).toBe("webhook");
|
|
72
|
+
expect(result.sentinel.status).toBe("watching");
|
|
73
|
+
expect(result.sentinel.config).toEqual({ path: "/hooks/deploy" });
|
|
74
|
+
expect(result.sentinel.linkedActionIds).toEqual([]);
|
|
75
|
+
expect(result.sentinel.createdAt).toBeDefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("creates a timer sentinel with valid config", async () => {
|
|
79
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
80
|
+
name: "timeout-check",
|
|
81
|
+
type: "timer",
|
|
82
|
+
config: { durationMs: 5000 },
|
|
83
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
84
|
+
|
|
85
|
+
expect(result.success).toBe(true);
|
|
86
|
+
expect(result.sentinel.type).toBe("timer");
|
|
87
|
+
expect(result.sentinel.status).toBe("watching");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("creates a threshold sentinel with valid config", async () => {
|
|
91
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
92
|
+
name: "high-calls",
|
|
93
|
+
type: "threshold",
|
|
94
|
+
config: { metric: "api_calls", operator: "gt", value: 100 },
|
|
95
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
96
|
+
|
|
97
|
+
expect(result.success).toBe(true);
|
|
98
|
+
expect(result.sentinel.type).toBe("threshold");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("creates a pattern sentinel with valid config", async () => {
|
|
102
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
103
|
+
name: "error-watcher",
|
|
104
|
+
type: "pattern",
|
|
105
|
+
config: { pattern: "error|fail" },
|
|
106
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
107
|
+
|
|
108
|
+
expect(result.success).toBe(true);
|
|
109
|
+
expect(result.sentinel.type).toBe("pattern");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("creates an approval sentinel without config", async () => {
|
|
113
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
114
|
+
name: "needs-approval",
|
|
115
|
+
type: "approval",
|
|
116
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
117
|
+
|
|
118
|
+
expect(result.success).toBe(true);
|
|
119
|
+
expect(result.sentinel.type).toBe("approval");
|
|
120
|
+
expect(result.sentinel.config).toEqual({});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("creates a custom sentinel without config", async () => {
|
|
124
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
125
|
+
name: "custom-gate",
|
|
126
|
+
type: "custom",
|
|
127
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
128
|
+
|
|
129
|
+
expect(result.success).toBe(true);
|
|
130
|
+
expect(result.sentinel.type).toBe("custom");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("returns error when name is missing", async () => {
|
|
134
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
135
|
+
type: "webhook",
|
|
136
|
+
config: { path: "/hooks/deploy" },
|
|
137
|
+
})) as { success: boolean; error: string };
|
|
138
|
+
|
|
139
|
+
expect(result.success).toBe(false);
|
|
140
|
+
expect(result.error).toContain("name is required");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("returns error for invalid type", async () => {
|
|
144
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
145
|
+
name: "bad-type",
|
|
146
|
+
type: "invalid_type",
|
|
147
|
+
})) as { success: boolean; error: string };
|
|
148
|
+
|
|
149
|
+
expect(result.success).toBe(false);
|
|
150
|
+
expect(result.error).toContain("type must be one of");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("returns error for timer config missing durationMs", async () => {
|
|
154
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
155
|
+
name: "bad-timer",
|
|
156
|
+
type: "timer",
|
|
157
|
+
config: {},
|
|
158
|
+
})) as { success: boolean; error: string };
|
|
159
|
+
|
|
160
|
+
expect(result.success).toBe(false);
|
|
161
|
+
expect(result.error).toContain("positive durationMs");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("returns error for timer config with negative durationMs", async () => {
|
|
165
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
166
|
+
name: "neg-timer",
|
|
167
|
+
type: "timer",
|
|
168
|
+
config: { durationMs: -100 },
|
|
169
|
+
})) as { success: boolean; error: string };
|
|
170
|
+
|
|
171
|
+
expect(result.success).toBe(false);
|
|
172
|
+
expect(result.error).toContain("positive durationMs");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("returns error for threshold config missing metric", async () => {
|
|
176
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
177
|
+
name: "bad-threshold",
|
|
178
|
+
type: "threshold",
|
|
179
|
+
config: { operator: "gt", value: 10 },
|
|
180
|
+
})) as { success: boolean; error: string };
|
|
181
|
+
|
|
182
|
+
expect(result.success).toBe(false);
|
|
183
|
+
expect(result.error).toContain("threshold config requires");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("returns error for threshold config with invalid operator", async () => {
|
|
187
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
188
|
+
name: "bad-op",
|
|
189
|
+
type: "threshold",
|
|
190
|
+
config: { metric: "calls", operator: "gte", value: 10 },
|
|
191
|
+
})) as { success: boolean; error: string };
|
|
192
|
+
|
|
193
|
+
expect(result.success).toBe(false);
|
|
194
|
+
expect(result.error).toContain("threshold config requires");
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("returns error for threshold config missing value", async () => {
|
|
198
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
199
|
+
name: "no-val",
|
|
200
|
+
type: "threshold",
|
|
201
|
+
config: { metric: "calls", operator: "gt" },
|
|
202
|
+
})) as { success: boolean; error: string };
|
|
203
|
+
|
|
204
|
+
expect(result.success).toBe(false);
|
|
205
|
+
expect(result.error).toContain("threshold config requires");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("returns error for pattern config missing pattern", async () => {
|
|
209
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
210
|
+
name: "bad-pattern",
|
|
211
|
+
type: "pattern",
|
|
212
|
+
config: {},
|
|
213
|
+
})) as { success: boolean; error: string };
|
|
214
|
+
|
|
215
|
+
expect(result.success).toBe(false);
|
|
216
|
+
expect(result.error).toContain("pattern config requires");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("returns error for webhook config missing path", async () => {
|
|
220
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
221
|
+
name: "bad-webhook",
|
|
222
|
+
type: "webhook",
|
|
223
|
+
config: {},
|
|
224
|
+
})) as { success: boolean; error: string };
|
|
225
|
+
|
|
226
|
+
expect(result.success).toBe(false);
|
|
227
|
+
expect(result.error).toContain("webhook config requires");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("creates gated_by edges for linkedActionIds", async () => {
|
|
231
|
+
const action = (await sdk.trigger("mem::action-create", {
|
|
232
|
+
title: "Gated task",
|
|
233
|
+
})) as { success: boolean; action: Action };
|
|
234
|
+
|
|
235
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
236
|
+
name: "gate-sentinel",
|
|
237
|
+
type: "approval",
|
|
238
|
+
linkedActionIds: [action.action.id],
|
|
239
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
240
|
+
|
|
241
|
+
expect(result.success).toBe(true);
|
|
242
|
+
expect(result.sentinel.linkedActionIds).toEqual([action.action.id]);
|
|
243
|
+
|
|
244
|
+
const edges = await kv.list<ActionEdge>("mem:action-edges");
|
|
245
|
+
const gatedEdges = edges.filter(
|
|
246
|
+
(e) =>
|
|
247
|
+
e.type === "gated_by" &&
|
|
248
|
+
e.sourceActionId === action.action.id &&
|
|
249
|
+
e.targetActionId === result.sentinel.id,
|
|
250
|
+
);
|
|
251
|
+
expect(gatedEdges.length).toBe(1);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("returns error for non-existent linkedActionId", async () => {
|
|
255
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
256
|
+
name: "bad-link",
|
|
257
|
+
type: "approval",
|
|
258
|
+
linkedActionIds: ["nonexistent_action"],
|
|
259
|
+
})) as { success: boolean; error: string };
|
|
260
|
+
|
|
261
|
+
expect(result.success).toBe(false);
|
|
262
|
+
expect(result.error).toContain("linked action not found");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("sets expiresAt when expiresInMs is provided", async () => {
|
|
266
|
+
const result = (await sdk.trigger("mem::sentinel-create", {
|
|
267
|
+
name: "expiring",
|
|
268
|
+
type: "custom",
|
|
269
|
+
expiresInMs: 60000,
|
|
270
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
271
|
+
|
|
272
|
+
expect(result.success).toBe(true);
|
|
273
|
+
expect(result.sentinel.expiresAt).toBeDefined();
|
|
274
|
+
const created = new Date(result.sentinel.createdAt).getTime();
|
|
275
|
+
const expires = new Date(result.sentinel.expiresAt!).getTime();
|
|
276
|
+
expect(expires - created).toBeCloseTo(60000, -2);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("mem::sentinel-trigger", () => {
|
|
281
|
+
it("triggers a watching sentinel", async () => {
|
|
282
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
283
|
+
name: "trigger-me",
|
|
284
|
+
type: "approval",
|
|
285
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
286
|
+
|
|
287
|
+
const result = (await sdk.trigger("mem::sentinel-trigger", {
|
|
288
|
+
sentinelId: sentinel.sentinel.id,
|
|
289
|
+
result: { approvedBy: "admin" },
|
|
290
|
+
})) as { success: boolean; sentinel: Sentinel; unblockedCount: number };
|
|
291
|
+
|
|
292
|
+
expect(result.success).toBe(true);
|
|
293
|
+
expect(result.sentinel.status).toBe("triggered");
|
|
294
|
+
expect(result.sentinel.triggeredAt).toBeDefined();
|
|
295
|
+
expect(result.sentinel.result).toEqual({ approvedBy: "admin" });
|
|
296
|
+
expect(result.unblockedCount).toBe(0);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("returns error when triggering already-triggered sentinel", async () => {
|
|
300
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
301
|
+
name: "already-fired",
|
|
302
|
+
type: "custom",
|
|
303
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
304
|
+
|
|
305
|
+
await sdk.trigger("mem::sentinel-trigger", {
|
|
306
|
+
sentinelId: sentinel.sentinel.id,
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const result = (await sdk.trigger("mem::sentinel-trigger", {
|
|
310
|
+
sentinelId: sentinel.sentinel.id,
|
|
311
|
+
})) as { success: boolean; error: string };
|
|
312
|
+
|
|
313
|
+
expect(result.success).toBe(false);
|
|
314
|
+
expect(result.error).toContain("already triggered");
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("returns error for non-existent sentinel", async () => {
|
|
318
|
+
const result = (await sdk.trigger("mem::sentinel-trigger", {
|
|
319
|
+
sentinelId: "nonexistent_sentinel",
|
|
320
|
+
})) as { success: boolean; error: string };
|
|
321
|
+
|
|
322
|
+
expect(result.success).toBe(false);
|
|
323
|
+
expect(result.error).toContain("sentinel not found");
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("returns error when sentinelId is missing", async () => {
|
|
327
|
+
const result = (await sdk.trigger("mem::sentinel-trigger", {})) as {
|
|
328
|
+
success: boolean;
|
|
329
|
+
error: string;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
expect(result.success).toBe(false);
|
|
333
|
+
expect(result.error).toContain("sentinelId is required");
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("unblocks gated actions when triggered", async () => {
|
|
337
|
+
const action = (await sdk.trigger("mem::action-create", {
|
|
338
|
+
title: "Blocked task",
|
|
339
|
+
})) as { success: boolean; action: Action };
|
|
340
|
+
|
|
341
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
342
|
+
name: "gate",
|
|
343
|
+
type: "approval",
|
|
344
|
+
linkedActionIds: [action.action.id],
|
|
345
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
346
|
+
|
|
347
|
+
await sdk.trigger("mem::action-update", {
|
|
348
|
+
actionId: action.action.id,
|
|
349
|
+
status: "blocked",
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
const result = (await sdk.trigger("mem::sentinel-trigger", {
|
|
353
|
+
sentinelId: sentinel.sentinel.id,
|
|
354
|
+
})) as { success: boolean; sentinel: Sentinel; unblockedCount: number };
|
|
355
|
+
|
|
356
|
+
expect(result.success).toBe(true);
|
|
357
|
+
expect(result.unblockedCount).toBe(1);
|
|
358
|
+
|
|
359
|
+
const updated = (await sdk.trigger("mem::action-get", {
|
|
360
|
+
actionId: action.action.id,
|
|
361
|
+
})) as { success: boolean; action: Action };
|
|
362
|
+
|
|
363
|
+
expect(updated.action.status).toBe("pending");
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
describe("mem::sentinel-cancel", () => {
|
|
368
|
+
it("cancels a watching sentinel", async () => {
|
|
369
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
370
|
+
name: "cancel-me",
|
|
371
|
+
type: "custom",
|
|
372
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
373
|
+
|
|
374
|
+
const result = (await sdk.trigger("mem::sentinel-cancel", {
|
|
375
|
+
sentinelId: sentinel.sentinel.id,
|
|
376
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
377
|
+
|
|
378
|
+
expect(result.success).toBe(true);
|
|
379
|
+
expect(result.sentinel.status).toBe("cancelled");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("returns error when cancelling non-watching sentinel", async () => {
|
|
383
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
384
|
+
name: "already-triggered",
|
|
385
|
+
type: "custom",
|
|
386
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
387
|
+
|
|
388
|
+
await sdk.trigger("mem::sentinel-trigger", {
|
|
389
|
+
sentinelId: sentinel.sentinel.id,
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const result = (await sdk.trigger("mem::sentinel-cancel", {
|
|
393
|
+
sentinelId: sentinel.sentinel.id,
|
|
394
|
+
})) as { success: boolean; error: string };
|
|
395
|
+
|
|
396
|
+
expect(result.success).toBe(false);
|
|
397
|
+
expect(result.error).toContain("cannot cancel sentinel with status");
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it("returns error for non-existent sentinel", async () => {
|
|
401
|
+
const result = (await sdk.trigger("mem::sentinel-cancel", {
|
|
402
|
+
sentinelId: "nonexistent_sentinel",
|
|
403
|
+
})) as { success: boolean; error: string };
|
|
404
|
+
|
|
405
|
+
expect(result.success).toBe(false);
|
|
406
|
+
expect(result.error).toContain("sentinel not found");
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it("returns error when sentinelId is missing", async () => {
|
|
410
|
+
const result = (await sdk.trigger("mem::sentinel-cancel", {})) as {
|
|
411
|
+
success: boolean;
|
|
412
|
+
error: string;
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
expect(result.success).toBe(false);
|
|
416
|
+
expect(result.error).toContain("sentinelId is required");
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe("mem::sentinel-list", () => {
|
|
421
|
+
beforeEach(async () => {
|
|
422
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
423
|
+
name: "webhook-1",
|
|
424
|
+
type: "webhook",
|
|
425
|
+
config: { path: "/a" },
|
|
426
|
+
});
|
|
427
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
428
|
+
name: "timer-1",
|
|
429
|
+
type: "timer",
|
|
430
|
+
config: { durationMs: 1000 },
|
|
431
|
+
});
|
|
432
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
433
|
+
name: "approval-1",
|
|
434
|
+
type: "approval",
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it("returns all sentinels", async () => {
|
|
439
|
+
const result = (await sdk.trigger("mem::sentinel-list", {})) as {
|
|
440
|
+
success: boolean;
|
|
441
|
+
sentinels: Sentinel[];
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
expect(result.success).toBe(true);
|
|
445
|
+
expect(result.sentinels.length).toBe(3);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it("filters by status", async () => {
|
|
449
|
+
const all = (await sdk.trigger("mem::sentinel-list", {})) as {
|
|
450
|
+
sentinels: Sentinel[];
|
|
451
|
+
};
|
|
452
|
+
await sdk.trigger("mem::sentinel-trigger", {
|
|
453
|
+
sentinelId: all.sentinels[0].id,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
const result = (await sdk.trigger("mem::sentinel-list", {
|
|
457
|
+
status: "triggered",
|
|
458
|
+
})) as { success: boolean; sentinels: Sentinel[] };
|
|
459
|
+
|
|
460
|
+
expect(result.success).toBe(true);
|
|
461
|
+
expect(result.sentinels.length).toBe(1);
|
|
462
|
+
expect(result.sentinels[0].status).toBe("triggered");
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("filters by type", async () => {
|
|
466
|
+
const result = (await sdk.trigger("mem::sentinel-list", {
|
|
467
|
+
type: "webhook",
|
|
468
|
+
})) as { success: boolean; sentinels: Sentinel[] };
|
|
469
|
+
|
|
470
|
+
expect(result.success).toBe(true);
|
|
471
|
+
expect(result.sentinels.length).toBe(1);
|
|
472
|
+
expect(result.sentinels[0].type).toBe("webhook");
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it("filters by both status and type", async () => {
|
|
476
|
+
const result = (await sdk.trigger("mem::sentinel-list", {
|
|
477
|
+
status: "watching",
|
|
478
|
+
type: "approval",
|
|
479
|
+
})) as { success: boolean; sentinels: Sentinel[] };
|
|
480
|
+
|
|
481
|
+
expect(result.success).toBe(true);
|
|
482
|
+
expect(result.sentinels.length).toBe(1);
|
|
483
|
+
expect(result.sentinels[0].type).toBe("approval");
|
|
484
|
+
expect(result.sentinels[0].status).toBe("watching");
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
describe("mem::sentinel-expire", () => {
|
|
489
|
+
it("expires sentinels past their expiresAt", async () => {
|
|
490
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
491
|
+
name: "will-expire",
|
|
492
|
+
type: "custom",
|
|
493
|
+
expiresInMs: 1,
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
497
|
+
|
|
498
|
+
const result = (await sdk.trigger("mem::sentinel-expire", {})) as {
|
|
499
|
+
success: boolean;
|
|
500
|
+
expired: number;
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
expect(result.success).toBe(true);
|
|
504
|
+
expect(result.expired).toBe(1);
|
|
505
|
+
|
|
506
|
+
const list = (await sdk.trigger("mem::sentinel-list", {
|
|
507
|
+
status: "expired",
|
|
508
|
+
})) as { sentinels: Sentinel[] };
|
|
509
|
+
expect(list.sentinels.length).toBe(1);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("skips sentinels that have not expired", async () => {
|
|
513
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
514
|
+
name: "not-expired",
|
|
515
|
+
type: "custom",
|
|
516
|
+
expiresInMs: 600000,
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
const result = (await sdk.trigger("mem::sentinel-expire", {})) as {
|
|
520
|
+
success: boolean;
|
|
521
|
+
expired: number;
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
expect(result.success).toBe(true);
|
|
525
|
+
expect(result.expired).toBe(0);
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it("skips sentinels without expiresAt", async () => {
|
|
529
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
530
|
+
name: "no-expiry",
|
|
531
|
+
type: "custom",
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
const result = (await sdk.trigger("mem::sentinel-expire", {})) as {
|
|
535
|
+
success: boolean;
|
|
536
|
+
expired: number;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
expect(result.success).toBe(true);
|
|
540
|
+
expect(result.expired).toBe(0);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("skips non-watching sentinels even if expired", async () => {
|
|
544
|
+
const sentinel = (await sdk.trigger("mem::sentinel-create", {
|
|
545
|
+
name: "already-cancelled",
|
|
546
|
+
type: "custom",
|
|
547
|
+
expiresInMs: 1,
|
|
548
|
+
})) as { success: boolean; sentinel: Sentinel };
|
|
549
|
+
|
|
550
|
+
await sdk.trigger("mem::sentinel-cancel", {
|
|
551
|
+
sentinelId: sentinel.sentinel.id,
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
555
|
+
|
|
556
|
+
const result = (await sdk.trigger("mem::sentinel-expire", {})) as {
|
|
557
|
+
success: boolean;
|
|
558
|
+
expired: number;
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
expect(result.success).toBe(true);
|
|
562
|
+
expect(result.expired).toBe(0);
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
describe("mem::sentinel-check", () => {
|
|
567
|
+
it("triggers threshold sentinel when condition is met", async () => {
|
|
568
|
+
await kv.set("mem:metrics", "api_calls", {
|
|
569
|
+
totalCalls: 150,
|
|
570
|
+
errorCount: 0,
|
|
571
|
+
avgDurationMs: 50,
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
575
|
+
name: "high-traffic",
|
|
576
|
+
type: "threshold",
|
|
577
|
+
config: { metric: "api_calls", operator: "gt", value: 100 },
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
const result = (await sdk.trigger("mem::sentinel-check", {})) as {
|
|
581
|
+
success: boolean;
|
|
582
|
+
triggered: string[];
|
|
583
|
+
checkedCount: number;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
expect(result.success).toBe(true);
|
|
587
|
+
expect(result.triggered.length).toBe(1);
|
|
588
|
+
expect(result.checkedCount).toBe(1);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it("does not trigger threshold sentinel when condition is not met", async () => {
|
|
592
|
+
await kv.set("mem:metrics", "api_calls", {
|
|
593
|
+
totalCalls: 50,
|
|
594
|
+
errorCount: 0,
|
|
595
|
+
avgDurationMs: 50,
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
await sdk.trigger("mem::sentinel-create", {
|
|
599
|
+
name: "low-traffic",
|
|
600
|
+
type: "threshold",
|
|
601
|
+
config: { metric: "api_calls", operator: "gt", value: 100 },
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
const result = (await sdk.trigger("mem::sentinel-check", {})) as {
|
|
605
|
+
success: boolean;
|
|
606
|
+
triggered: string[];
|
|
607
|
+
checkedCount: number;
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
expect(result.success).toBe(true);
|
|
611
|
+
expect(result.triggered.length).toBe(0);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("returns empty triggered list when no active sentinels", async () => {
|
|
615
|
+
const result = (await sdk.trigger("mem::sentinel-check", {})) as {
|
|
616
|
+
success: boolean;
|
|
617
|
+
triggered: string[];
|
|
618
|
+
checkedCount: number;
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
expect(result.success).toBe(true);
|
|
622
|
+
expect(result.triggered).toEqual([]);
|
|
623
|
+
expect(result.checkedCount).toBe(0);
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
});
|