humemory 0.2.7 → 0.2.9

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 (102) hide show
  1. package/AGENTS.md +14 -7
  2. package/CHANGELOG.md +23 -0
  3. package/README.md +66 -12
  4. package/dist/agent/kimi-import.d.ts +19 -0
  5. package/dist/agent/kimi-import.js +111 -0
  6. package/dist/agent/kimi-session-parser.d.ts +3 -0
  7. package/dist/agent/kimi-session-parser.js +102 -0
  8. package/dist/agent/maintenance-queue.d.ts +2 -1
  9. package/dist/agent/maintenance-queue.js +36 -62
  10. package/dist/agent/maintenance-runner.d.ts +17 -4
  11. package/dist/agent/maintenance-runner.js +45 -10
  12. package/dist/agent/mcp-client-setup.d.ts +16 -0
  13. package/dist/agent/mcp-client-setup.js +108 -0
  14. package/dist/agent/opencode-import.d.ts +22 -0
  15. package/dist/agent/opencode-import.js +71 -0
  16. package/dist/agent/opencode-session-parser.d.ts +3 -0
  17. package/dist/agent/opencode-session-parser.js +35 -0
  18. package/dist/agent/session-parser.d.ts +8 -0
  19. package/dist/agent/session-parser.js +18 -0
  20. package/dist/agent/source-registry.js +1 -1
  21. package/dist/api/memory-routes.js +1 -1
  22. package/dist/api/server.js +3 -2
  23. package/dist/cli/index.js +117 -6
  24. package/dist/core/cues.d.ts +1 -0
  25. package/dist/core/cues.js +19 -7
  26. package/dist/core/doctor.d.ts +28 -0
  27. package/dist/core/doctor.js +320 -0
  28. package/dist/core/embeddings.d.ts +0 -10
  29. package/dist/core/embeddings.js +2 -1
  30. package/dist/core/hybrid.d.ts +7 -3
  31. package/dist/core/hybrid.js +7 -3
  32. package/dist/core/llm-generator.d.ts +2 -10
  33. package/dist/core/paths.d.ts +12 -0
  34. package/dist/core/paths.js +36 -0
  35. package/dist/core/project-path.d.ts +3 -0
  36. package/dist/core/project-path.js +14 -0
  37. package/dist/core/search.d.ts +1 -1
  38. package/dist/core/search.js +7 -9
  39. package/dist/core/types.d.ts +13 -1
  40. package/dist/index.d.ts +5 -0
  41. package/dist/index.js +5 -0
  42. package/dist/store/sqlite.d.ts +37 -0
  43. package/dist/store/sqlite.js +94 -42
  44. package/docs/PORTABILITY.md +84 -0
  45. package/package.json +5 -3
  46. package/public/app/{index-95ffty7j.js → index-n2qg9hyr.js} +6 -6
  47. package/public/app/index.html +1 -1
  48. package/scripts/consolidate.js +4 -4
  49. package/scripts/hook-post-commit.ts +2 -1
  50. package/scripts/hook-session-start.ts +2 -1
  51. package/scripts/hook-session.ts +2 -1
  52. package/scripts/maintenance-worker.ts +7 -2
  53. package/src/agent/claude-hook.ts +126 -0
  54. package/src/agent/codex-import.ts +129 -0
  55. package/src/agent/codex-rollout-parser.ts +121 -0
  56. package/src/agent/commit-closer.ts +173 -0
  57. package/src/agent/kimi-import.ts +128 -0
  58. package/src/agent/kimi-session-parser.ts +127 -0
  59. package/src/agent/learning-extractor.ts +128 -0
  60. package/src/agent/maintenance-queue.ts +544 -0
  61. package/src/agent/maintenance-runner.ts +322 -0
  62. package/src/agent/maintenance-state.ts +168 -0
  63. package/src/agent/mcp-client-setup.ts +125 -0
  64. package/src/agent/opencode-import.ts +96 -0
  65. package/src/agent/opencode-session-parser.ts +53 -0
  66. package/src/agent/session-context.ts +302 -0
  67. package/src/agent/session-parser.ts +155 -0
  68. package/src/agent/source-registry.ts +135 -0
  69. package/src/api/cors.ts +42 -0
  70. package/src/api/dream-routes.ts +46 -0
  71. package/src/api/errors.ts +45 -0
  72. package/src/api/intentions-routes.ts +348 -0
  73. package/src/api/limits.ts +90 -0
  74. package/src/api/maintenance-routes.ts +73 -0
  75. package/src/api/memory-routes.ts +391 -0
  76. package/src/api/scripts-routes.ts +176 -0
  77. package/src/api/security-headers.ts +54 -0
  78. package/src/api/server.ts +292 -0
  79. package/src/cli/index.ts +1195 -0
  80. package/src/core/clock.ts +64 -0
  81. package/src/core/cue-arg.ts +79 -0
  82. package/src/core/cues.ts +511 -0
  83. package/src/core/decay.ts +198 -0
  84. package/src/core/doctor.ts +362 -0
  85. package/src/core/dreamer.ts +494 -0
  86. package/src/core/embeddings.ts +157 -0
  87. package/src/core/event-bus.ts +100 -0
  88. package/src/core/hybrid.ts +82 -0
  89. package/src/core/llm-cli-client.ts +118 -0
  90. package/src/core/llm-generator.ts +90 -0
  91. package/src/core/paths.ts +38 -0
  92. package/src/core/project-path.ts +14 -0
  93. package/src/core/sanitize.ts +116 -0
  94. package/src/core/scripts.ts +112 -0
  95. package/src/core/search.ts +189 -0
  96. package/src/core/trust.ts +67 -0
  97. package/src/core/types.ts +393 -0
  98. package/src/core/vector-clusterer.ts +131 -0
  99. package/src/index.ts +40 -0
  100. package/src/mcp/HTTP_TRANSPORT.md +359 -0
  101. package/src/mcp/server.ts +202 -0
  102. package/src/store/sqlite.ts +1958 -0
@@ -0,0 +1,202 @@
1
+ /**
2
+ * MCP server — Phase 6.2.
3
+ *
4
+ * Exposes the shared humemory store over the Model Context Protocol so any
5
+ * MCP-capable agent (Claude, Codex, Kimi, OpenCode) encodes, searches and
6
+ * recalls against the SAME database, with the same decay curve — one unified
7
+ * memory instead of one memory per tool.
8
+ *
9
+ * Attribution: every write path carries the calling agent, taken from
10
+ * `$HUMEMORY_AGENT` in this process's environment — set by the client's MCP
11
+ * config, one server process per client. It is deliberately NOT a tool
12
+ * argument: the model filling in its own name is a claim, and in an injection
13
+ * scenario the model is precisely the untrusted party. stdio gives us a real
14
+ * identity per process; using it is the whole point of doing attribution at
15
+ * this boundary.
16
+ *
17
+ * That identity feeds the Phase 6.0.1 trust layer: cross-agent recall earns
18
+ * the `reused` verification, cross-agent recurrence feeds the dreamer.
19
+ *
20
+ * Run: `pnpm mcp` (stdio transport — register it in the client's MCP config).
21
+ */
22
+
23
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
24
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
25
+ import { z } from 'zod';
26
+ import { SQLiteStore } from '../store/sqlite.js';
27
+ import { trustScore } from '../core/trust.js';
28
+ import { sanitizeTrace, wrapUntrusted } from '../core/sanitize.js';
29
+
30
+ const store = new SQLiteStore(process.env.HUMEMORY_DB);
31
+
32
+ /**
33
+ * This process's agent identity. Not overridable per call — see the header.
34
+ * Register one server per agent in the client's MCP config:
35
+ * { "humemory": { "command": "bun", "args": ["run", "src/mcp/server.ts"],
36
+ * "env": { "HUMEMORY_AGENT": "claude" } } }
37
+ */
38
+ const callingAgent = process.env.HUMEMORY_AGENT ?? 'unknown';
39
+
40
+ const server = new McpServer({
41
+ name: 'humemory',
42
+ version: '6.2.0',
43
+ });
44
+
45
+ /** Renders a memory for tool output — sanitized, never raw prompt material. */
46
+ function show(m: any): string {
47
+ const trust = trustScore(m);
48
+ const text = sanitizeTrace(m.content, 300).text;
49
+ return [
50
+ `id: ${m.id}`,
51
+ `level: L${m.currentLevel} · saillance: ${m.saillance} · trust: ${trust}`,
52
+ `source: ${m.source ?? 'agent'}${m.agent ? ` (${m.agent})` : ''}${m.verified ? ` · verified:${m.verificationReason}` : ''}`,
53
+ `content: ${text}`,
54
+ ].join('\n');
55
+ }
56
+
57
+ server.registerTool(
58
+ 'humemory_add',
59
+ {
60
+ description: 'Encode a new memory trace (decision, bug fix, lesson learned) into the shared store',
61
+ inputSchema: {
62
+ content: z.string().describe('What to remember'),
63
+ directory: z.string().optional().describe('Project path (defaults to cwd)'),
64
+ keywords: z.array(z.string()).optional(),
65
+ memoryType: z.enum(['episodic', 'semantic', 'procedural']).optional(),
66
+ },
67
+ },
68
+ async ({ content, directory, keywords, memoryType }) => {
69
+ const m = await store.add({
70
+ content,
71
+ directory: directory ?? process.cwd(),
72
+ day: new Date().toISOString().split('T')[0],
73
+ keywords: keywords ?? [],
74
+ sessionId: 'mcp',
75
+ memoryType: memoryType ?? 'semantic',
76
+ source: 'agent',
77
+ agent: callingAgent,
78
+ });
79
+ return { content: [{ type: 'text', text: `encoded:\n${show(m)}` }] };
80
+ }
81
+ );
82
+
83
+ server.registerTool(
84
+ 'humemory_search',
85
+ {
86
+ description: 'Inverse search — degraded layers first, like a human memory',
87
+ inputSchema: {
88
+ query: z.string(),
89
+ directory: z.string().optional(),
90
+ limit: z.number().optional(),
91
+ },
92
+ },
93
+ async ({ query, directory, limit }) => {
94
+ const results = await store.search({ query, directory, limit: limit ?? 5 });
95
+ if (!results.length) return { content: [{ type: 'text', text: 'no trace' }] };
96
+ return {
97
+ content: [
98
+ {
99
+ type: 'text',
100
+ text: results.map((r) => `match@L${r.matchLevel} score ${r.score}\n${show(r.memory)}`).join('\n---\n'),
101
+ },
102
+ ],
103
+ };
104
+ }
105
+ );
106
+
107
+ server.registerTool(
108
+ 'humemory_recall',
109
+ {
110
+ description: 'Recall a trace — reinforces it; a cross-agent recall earns the `reused` verification',
111
+ inputSchema: {
112
+ id: z.string(),
113
+ },
114
+ },
115
+ async ({ id }) => {
116
+ // identityTrusted: this agent id came from the process environment, not
117
+ // from the caller — so a cross-agent recall here is evidence, not a claim.
118
+ const m = await store.recall(id, callingAgent, { identityTrusted: true });
119
+ return { content: [{ type: 'text', text: `recalled:\n${show(m)}` }] };
120
+ }
121
+ );
122
+
123
+ server.registerTool(
124
+ 'humemory_intent_add',
125
+ {
126
+ description: 'Arm an open loop (prospective memory) with an optional time/event cue',
127
+ inputSchema: {
128
+ content: z.string(),
129
+ directory: z.string().optional(),
130
+ cron: z.string().optional().describe('5-field cron, e.g. "0 9 * * 1"'),
131
+ at: z.string().optional().describe('ISO datetime for a one-shot cue'),
132
+ },
133
+ },
134
+ async ({ content, directory, cron, at }) => {
135
+ const cues = [];
136
+ if (cron) cues.push({ kind: 'time' as const, cron });
137
+ else if (at) cues.push({ kind: 'time' as const, at });
138
+ const i = await store.addIntention(
139
+ {
140
+ content,
141
+ directory: directory ?? process.cwd(),
142
+ source: 'agent',
143
+ agent: callingAgent,
144
+ },
145
+ cues
146
+ );
147
+ // Echoes the caller's own content back into the tool result, which lands
148
+ // in the agent's context like any other tool output — sanitized the same
149
+ // way a recalled trace would be (SECURITY_AUDIT.md H-03).
150
+ const s = sanitizeTrace(i.content);
151
+ return { content: [{ type: 'text', text: `loop armed: ${i.id}\n${s.text}` }] };
152
+ }
153
+ );
154
+
155
+ server.registerTool(
156
+ 'humemory_intent_close',
157
+ {
158
+ description: 'Close an open loop by id (or short loop-XXXX id)',
159
+ inputSchema: { id: z.string() },
160
+ },
161
+ async ({ id }) => {
162
+ let target = await store.getIntention(id);
163
+ if (!target) {
164
+ const armed = await store.listIntentions({ status: ['armed', 'fired'], limit: 500 });
165
+ const { matchIntentionByShortId } = await import('../core/cues.js');
166
+ target = matchIntentionByShortId(armed, id.replace(/^loop-/, '')) ?? null;
167
+ }
168
+ if (!target) return { content: [{ type: 'text', text: `no open loop matches ${id}` }] };
169
+ await store.updateIntentionStatus(target.id, 'closed', {});
170
+ return { content: [{ type: 'text', text: `closed: ${target.id}\n${target.content}` }] };
171
+ }
172
+ );
173
+
174
+ server.registerTool(
175
+ 'humemory_dreams',
176
+ {
177
+ description: 'List pending dream proposals (cross-session patterns awaiting human review)',
178
+ inputSchema: {},
179
+ },
180
+ async () => {
181
+ const pending = await store.listDreamProposals({ status: 'pending' });
182
+ if (!pending.length) return { content: [{ type: 'text', text: 'no dream pending' }] };
183
+ // Dream proposals are synthesized cross-session patterns, never
184
+ // human-verified at this stage — wrapped like any other unverified
185
+ // recalled content (SECURITY_AUDIT.md H-03), not returned raw.
186
+ return {
187
+ content: [
188
+ {
189
+ type: 'text',
190
+ text: pending
191
+ .map((p) => {
192
+ const s = sanitizeTrace(p.payload.slice(0, 400));
193
+ return `[${p.kind}] ${p.id} — confidence ${p.confidence}\n${wrapUntrusted(s.text, { id: p.id })}`;
194
+ })
195
+ .join('\n---\n'),
196
+ },
197
+ ],
198
+ };
199
+ }
200
+ );
201
+
202
+ await server.connect(new StdioServerTransport());