@fabersep/memory 0.4.0 → 0.5.1
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/CHANGELOG.md +14 -0
- package/README.md +62 -110
- package/dist/src/cli.js +41 -1
- package/dist/src/config.js +1 -0
- package/dist/src/direct-save.d.ts +53 -0
- package/dist/src/direct-save.js +226 -0
- package/dist/src/doctor.js +4 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/init.d.ts +1 -1
- package/dist/src/init.js +1 -1
- package/dist/src/mcp.d.ts +47 -2
- package/dist/src/mcp.js +294 -143
- package/dist/src/metrics.d.ts +4 -2
- package/dist/src/proposals.d.ts +10 -3
- package/dist/src/proposals.js +14 -2
- package/dist/src/stats.d.ts +8 -0
- package/dist/src/stats.js +16 -2
- package/dist/src/types.d.ts +12 -0
- package/dist/src/web-assets.js +33 -0
- package/dist/src/web.js +39 -1
- package/package.json +1 -1
package/dist/src/mcp.js
CHANGED
|
@@ -5,13 +5,21 @@ import { getCompiledL0Path, getHomeDir, loadConfig } from './config.js';
|
|
|
5
5
|
import { compileGlobalL0 } from './compiler.js';
|
|
6
6
|
import { getHeadSha } from './git.js';
|
|
7
7
|
import { formatHygieneErrors, scanHygiene } from './hygiene.js';
|
|
8
|
-
import { getUnpushedReminder, isReadOnlyRoot, READ_ONLY_MESSAGE } from './personal-remote.js';
|
|
8
|
+
import { getAheadInfo, getPushMode, getUnpushedReminder, isReadOnlyRoot, pullRebaseBeforeCommit, pushPersonalRoot, READ_ONLY_MESSAGE, } from './personal-remote.js';
|
|
9
9
|
import { appendMetric, MAX_GET_FOUND_IDS } from './metrics.js';
|
|
10
|
-
import { createProposal, reconcileAwaitingCommitProposals } from './proposals.js';
|
|
10
|
+
import { buildProposal, CACHE_GUIDANCE_NOTE, createProposal, reconcileAwaitingCommitProposals, } from './proposals.js';
|
|
11
|
+
import { DIRECT_SAVE_SESSION_CAP, directSavePersonal, followRebasedDirectSave, isDirectUserSaveOn, MAX_USER_QUOTE_CHARS, } from './direct-save.js';
|
|
11
12
|
import { loadAllScopes, resolveProjectRoot } from './store.js';
|
|
12
13
|
import { syncAllRoots } from './sync.js';
|
|
13
14
|
import { getPackageVersion } from './version.js';
|
|
14
15
|
import { DEFAULT_SIMILARITY_HINT_FLOOR, VALID_TYPES, } from './types.js';
|
|
16
|
+
const STAGED_REASON_TEXT = {
|
|
17
|
+
scope: 'only personal memories are saved directly; this one waits for review',
|
|
18
|
+
disabled: 'direct save is off (faber-memory config set-direct-save on); this one waits for review',
|
|
19
|
+
session_cap: 'the direct-save limit for this session is reached; this one waits for review',
|
|
20
|
+
l0_budget: 'the startup index is full; trim it, then approve this one',
|
|
21
|
+
compile_error: 'the startup index did not compile with this entry; it waits for review',
|
|
22
|
+
};
|
|
15
23
|
export const MCP_TOOLS = [
|
|
16
24
|
{
|
|
17
25
|
name: 'memory_search',
|
|
@@ -39,7 +47,8 @@ export const MCP_TOOLS = [
|
|
|
39
47
|
},
|
|
40
48
|
{
|
|
41
49
|
name: 'memory_propose',
|
|
42
|
-
description: 'Propose a durable rule, decision, lesson or preference you learned (or a fix to an existing entry). Goes to pre-memory for human approval; never edits active memory.'
|
|
50
|
+
description: 'Propose a durable rule, decision, lesson or preference you learned (or a fix to an existing entry). Goes to pre-memory for human approval; never edits active memory. ' +
|
|
51
|
+
'Only when the user explicitly asks you to remember or add something, pass source "user" and their exact words in user_quote: a personal memory is then saved at once and listed in the startup index from the next session; tell the user what was saved. Things you decide to save yourself use the default source.',
|
|
43
52
|
inputSchema: {
|
|
44
53
|
type: 'object',
|
|
45
54
|
properties: {
|
|
@@ -51,9 +60,15 @@ export const MCP_TOOLS = [
|
|
|
51
60
|
target_id: { type: 'string' },
|
|
52
61
|
entry_id: { type: 'string' },
|
|
53
62
|
tags: { type: 'array', items: { type: 'string' } },
|
|
54
|
-
l0: {
|
|
63
|
+
l0: {
|
|
64
|
+
type: 'string',
|
|
65
|
+
enum: ['none', 'line', 'body'],
|
|
66
|
+
description: '"line": listed in the startup index every session (rules and preferences that must always hold). "none": found only by search (details looked up when needed).',
|
|
67
|
+
},
|
|
55
68
|
force_new: { type: 'boolean' },
|
|
56
69
|
reason: { type: 'string' },
|
|
70
|
+
source: { type: 'string', enum: ['agent', 'user'], default: 'agent' },
|
|
71
|
+
user_quote: { type: 'string', description: 'The user\'s exact words asking for this memory (required when source is "user").' },
|
|
57
72
|
},
|
|
58
73
|
required: ['action', 'scope', 'summary', 'type', 'body'],
|
|
59
74
|
},
|
|
@@ -70,6 +85,9 @@ export class FaberMemoryMcpServer {
|
|
|
70
85
|
startupWarnings = [];
|
|
71
86
|
unpushedReminderShown = false;
|
|
72
87
|
startupPullPromise = null;
|
|
88
|
+
directSaveCount = 0;
|
|
89
|
+
/** Keeps stdio responses in request order while a direct save awaits pull/push. */
|
|
90
|
+
queue = Promise.resolve();
|
|
73
91
|
constructor(options = {}) {
|
|
74
92
|
this.homeDir = options.homeDir || getHomeDir();
|
|
75
93
|
this.cwd = options.cwd || process.cwd();
|
|
@@ -124,10 +142,10 @@ export class FaberMemoryMcpServer {
|
|
|
124
142
|
}
|
|
125
143
|
return { config, loaded: this.lastLoaded };
|
|
126
144
|
}
|
|
127
|
-
drainWarnings(loaded) {
|
|
145
|
+
drainWarnings(loaded, withReminder = true) {
|
|
128
146
|
const warnings = [...this.startupWarnings, ...loaded.uncommittedWarnings, ...loaded.shadowWarnings];
|
|
129
147
|
this.startupWarnings = [];
|
|
130
|
-
if (!this.unpushedReminderShown) {
|
|
148
|
+
if (withReminder && !this.unpushedReminderShown) {
|
|
131
149
|
// Feature 03 FR-03: once per server process, never in L0 or the tool list.
|
|
132
150
|
this.unpushedReminderShown = true;
|
|
133
151
|
const reminder = getUnpushedReminder(loadConfig(this.homeDir), this.homeDir);
|
|
@@ -230,91 +248,111 @@ export class FaberMemoryMcpServer {
|
|
|
230
248
|
};
|
|
231
249
|
}
|
|
232
250
|
if (name === 'memory_propose') {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
251
|
+
return this.handlePropose(args).response;
|
|
252
|
+
}
|
|
253
|
+
return errorResponse(`Unknown tool: ${name}`);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* 06 FR-01..FR-08 (and 01 FR-05): validate, then stage in pre-memory, or, for a
|
|
257
|
+
* user-dictated personal memory, save it directly. Pull and push are done by
|
|
258
|
+
* `handleToolCallAsync`; this synchronous path keeps a direct save local.
|
|
259
|
+
*/
|
|
260
|
+
handlePropose(args, extraWarnings = [], checkOnly = false) {
|
|
261
|
+
const { config, loaded } = this.refreshIndexIfNeeded();
|
|
262
|
+
const action = String(args.action || '');
|
|
263
|
+
const scope = String(args.scope || '');
|
|
264
|
+
const summary = typeof args.summary === 'string' ? args.summary.trim() : '';
|
|
265
|
+
const type = String(args.type || '');
|
|
266
|
+
const body = typeof args.body === 'string' ? args.body.trim() : '';
|
|
267
|
+
const targetId = typeof args.target_id === 'string' ? args.target_id.trim() : undefined;
|
|
268
|
+
const entryId = typeof args.entry_id === 'string' ? args.entry_id.trim() : undefined;
|
|
269
|
+
const tags = Array.isArray(args.tags) ? args.tags.map(String) : [];
|
|
270
|
+
let l0 = typeof args.l0 === 'string' ? args.l0 : undefined;
|
|
271
|
+
const forceNew = Boolean(args.force_new);
|
|
272
|
+
const reason = typeof args.reason === 'string' ? args.reason.trim() : undefined;
|
|
273
|
+
const rawSource = args.source === undefined || args.source === null ? 'agent' : String(args.source);
|
|
274
|
+
const source = rawSource === 'user' ? 'user' : 'agent';
|
|
275
|
+
const userQuote = source === 'user' && typeof args.user_quote === 'string' ? args.user_quote.trim() : '';
|
|
276
|
+
const metricScope = ['team', 'personal', 'project'].includes(scope)
|
|
277
|
+
? scope
|
|
278
|
+
: 'personal';
|
|
279
|
+
const metricAction = ['create', 'update', 'supersede'].includes(action)
|
|
280
|
+
? action
|
|
281
|
+
: 'create';
|
|
282
|
+
const recordPropose = (outcome, hygieneCategory) => {
|
|
283
|
+
// A check-only pass (before the pull) records nothing; the real pass does.
|
|
284
|
+
if (checkOnly)
|
|
285
|
+
return;
|
|
286
|
+
appendMetric({
|
|
287
|
+
type: 'propose',
|
|
288
|
+
ts: new Date().toISOString(),
|
|
289
|
+
source: 'mcp',
|
|
290
|
+
scope: metricScope,
|
|
291
|
+
action: metricAction,
|
|
292
|
+
outcome,
|
|
293
|
+
...(hygieneCategory ? { hygiene_category: hygieneCategory } : {}),
|
|
294
|
+
origin: source,
|
|
295
|
+
}, this.homeDir);
|
|
296
|
+
};
|
|
297
|
+
const fail = (message, extra) => {
|
|
298
|
+
recordPropose('validation_error');
|
|
299
|
+
return { response: errorResponse(message, extra) };
|
|
300
|
+
};
|
|
301
|
+
if (rawSource !== 'agent' && rawSource !== 'user') {
|
|
302
|
+
return fail('Invalid source; expected "agent" or "user".');
|
|
303
|
+
}
|
|
304
|
+
if (source === 'user' && !userQuote) {
|
|
305
|
+
return fail('user_quote is required when source is "user": pass the user\'s exact words.');
|
|
306
|
+
}
|
|
307
|
+
if (userQuote.length > MAX_USER_QUOTE_CHARS) {
|
|
308
|
+
return fail(`user_quote is too long (max ${MAX_USER_QUOTE_CHARS} characters).`);
|
|
309
|
+
}
|
|
310
|
+
if (!['create', 'update', 'supersede'].includes(action)) {
|
|
311
|
+
return fail('Invalid action; expected create, update, or supersede.');
|
|
312
|
+
}
|
|
313
|
+
if (!['team', 'personal', 'project'].includes(scope)) {
|
|
314
|
+
return fail('Invalid scope; expected team, personal, or project.');
|
|
315
|
+
}
|
|
316
|
+
if (!summary)
|
|
317
|
+
return fail('summary is required.');
|
|
318
|
+
if (!VALID_TYPES.includes(type)) {
|
|
319
|
+
return fail(`Invalid type; expected one of: ${VALID_TYPES.join(', ')}`);
|
|
320
|
+
}
|
|
321
|
+
if (!body)
|
|
322
|
+
return fail('body is required.');
|
|
323
|
+
if (scope === 'personal' && isReadOnlyRoot(config.roots.personal)) {
|
|
324
|
+
return fail(READ_ONLY_MESSAGE);
|
|
325
|
+
}
|
|
326
|
+
// Validate target_id for update / supersede (AC-08c1, AC-08c2)
|
|
327
|
+
if (action === 'update' || action === 'supersede') {
|
|
328
|
+
if (!targetId)
|
|
329
|
+
return fail(`target_id is required when action is "${action}".`);
|
|
330
|
+
const exists = loaded.activeEntries.some((e) => e.id === targetId && e.scope === scope) ||
|
|
331
|
+
loaded.activeEntries.some((e) => e.id === targetId);
|
|
332
|
+
if (!exists)
|
|
333
|
+
return fail(`Target entry "${targetId}" not found in active memory.`);
|
|
334
|
+
}
|
|
335
|
+
// Hygiene scan (AC-20a; 06 FR-02 adds the quote)
|
|
336
|
+
const hygieneFindings = scanHygiene(`${summary}\n${body}${userQuote ? `\n${userQuote}` : ''}`);
|
|
337
|
+
if (hygieneFindings.length > 0) {
|
|
338
|
+
const hasSecret = hygieneFindings.some((f) => f.category === 'secret');
|
|
339
|
+
recordPropose('hygiene_blocked', hasSecret ? 'secret' : 'machine-path');
|
|
340
|
+
return {
|
|
341
|
+
response: errorResponse(`Hygiene scan rejected proposal:\n${formatHygieneErrors(hygieneFindings)}`, { findings: hygieneFindings }),
|
|
261
342
|
};
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
recordPropose('validation_error');
|
|
268
|
-
return errorResponse('Invalid scope; expected team, personal, or project.');
|
|
269
|
-
}
|
|
270
|
-
if (!summary) {
|
|
271
|
-
recordPropose('validation_error');
|
|
272
|
-
return errorResponse('summary is required.');
|
|
273
|
-
}
|
|
274
|
-
if (!VALID_TYPES.includes(type)) {
|
|
275
|
-
recordPropose('validation_error');
|
|
276
|
-
return errorResponse(`Invalid type; expected one of: ${VALID_TYPES.join(', ')}`);
|
|
277
|
-
}
|
|
278
|
-
if (!body) {
|
|
279
|
-
recordPropose('validation_error');
|
|
280
|
-
return errorResponse('body is required.');
|
|
281
|
-
}
|
|
282
|
-
if (scope === 'personal' && isReadOnlyRoot(config.roots.personal)) {
|
|
283
|
-
recordPropose('validation_error');
|
|
284
|
-
return errorResponse(READ_ONLY_MESSAGE);
|
|
285
|
-
}
|
|
286
|
-
// Validate target_id for update / supersede (AC-08c1, AC-08c2)
|
|
287
|
-
if (action === 'update' || action === 'supersede') {
|
|
288
|
-
if (!targetId) {
|
|
289
|
-
recordPropose('validation_error');
|
|
290
|
-
return errorResponse(`target_id is required when action is "${action}".`);
|
|
291
|
-
}
|
|
292
|
-
const exists = loaded.activeEntries.some((e) => e.id === targetId && e.scope === scope) ||
|
|
293
|
-
loaded.activeEntries.some((e) => e.id === targetId);
|
|
294
|
-
if (!exists) {
|
|
295
|
-
recordPropose('validation_error');
|
|
296
|
-
return errorResponse(`Target entry "${targetId}" not found in active memory.`);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
// Hygiene scan (AC-20a)
|
|
300
|
-
const hygieneFindings = scanHygiene(`${summary}\n${body}`);
|
|
301
|
-
if (hygieneFindings.length > 0) {
|
|
302
|
-
const hasSecret = hygieneFindings.some((f) => f.category === 'secret');
|
|
303
|
-
recordPropose('hygiene_blocked', hasSecret ? 'secret' : 'machine-path');
|
|
304
|
-
return errorResponse(`Hygiene scan rejected proposal:\n${formatHygieneErrors(hygieneFindings)}`, { findings: hygieneFindings });
|
|
343
|
+
}
|
|
344
|
+
// Near-duplicate check on create (AC-10, AC-11)
|
|
345
|
+
if (action === 'create') {
|
|
346
|
+
if (forceNew && !reason) {
|
|
347
|
+
return fail('A non-empty "reason" is required when force_new is true.');
|
|
305
348
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const pending = reconcileAwaitingCommitProposals(this.homeDir);
|
|
314
|
-
const matches = findNearDuplicates(summary, body, loaded.activeEntries, pending, config.similarityThreshold);
|
|
315
|
-
if (matches.length > 0) {
|
|
316
|
-
recordPropose('duplicate_blocked');
|
|
317
|
-
return errorResponse('Near-duplicate entry found. Use action "update" or "supersede", or pass force_new: true with a reason.', {
|
|
349
|
+
if (!forceNew) {
|
|
350
|
+
const pending = reconcileAwaitingCommitProposals(this.homeDir);
|
|
351
|
+
const matches = findNearDuplicates(summary, body, loaded.activeEntries, pending, config.similarityThreshold);
|
|
352
|
+
if (matches.length > 0) {
|
|
353
|
+
recordPropose('duplicate_blocked');
|
|
354
|
+
return {
|
|
355
|
+
response: errorResponse('Near-duplicate entry found. Use action "update" or "supersede", or pass force_new: true with a reason.', {
|
|
318
356
|
matches: matches.map((m) => ({
|
|
319
357
|
id: m.id,
|
|
320
358
|
scope: m.scope,
|
|
@@ -322,61 +360,166 @@ export class FaberMemoryMcpServer {
|
|
|
322
360
|
pending: m.pending,
|
|
323
361
|
similarity: m.similarity,
|
|
324
362
|
})),
|
|
325
|
-
})
|
|
326
|
-
}
|
|
363
|
+
}),
|
|
364
|
+
};
|
|
327
365
|
}
|
|
328
366
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
367
|
+
}
|
|
368
|
+
// 06 FR-03: a rule the user dictates always reaches the startup index.
|
|
369
|
+
let l0Note;
|
|
370
|
+
if (source === 'user' && scope !== 'project' && l0 !== 'body' && l0 !== 'line') {
|
|
371
|
+
l0Note = `l0 set to "line" (was ${l0 ? `"${l0}"` : 'not set'}): a memory the user asks for is listed in the startup index.`;
|
|
372
|
+
l0 = 'line';
|
|
373
|
+
}
|
|
374
|
+
else if (source === 'agent' &&
|
|
375
|
+
scope !== 'project' &&
|
|
376
|
+
l0 === 'none' &&
|
|
377
|
+
(type === 'rule' || type === 'preference')) {
|
|
378
|
+
// 06 FR-03 amendment: a rule or preference must reach the startup index to be applied.
|
|
379
|
+
l0Note = `l0 set to "line" (was "none"): a ${type} is listed in the startup index so it holds in every session; the reviewer can lower it.`;
|
|
380
|
+
l0 = 'line';
|
|
381
|
+
}
|
|
382
|
+
let stagedReason;
|
|
383
|
+
if (source === 'user') {
|
|
384
|
+
stagedReason = this.directSaveBlocker(config, scope);
|
|
385
|
+
}
|
|
386
|
+
if (checkOnly) {
|
|
387
|
+
return { response: jsonResponse({}), checksPassed: true, directEligible: source === 'user' && !stagedReason };
|
|
388
|
+
}
|
|
389
|
+
const input = {
|
|
390
|
+
scope,
|
|
391
|
+
action,
|
|
392
|
+
entryId,
|
|
393
|
+
targetId,
|
|
394
|
+
summary,
|
|
395
|
+
type,
|
|
396
|
+
tags,
|
|
397
|
+
l0,
|
|
398
|
+
body,
|
|
399
|
+
forceNew,
|
|
400
|
+
reason,
|
|
401
|
+
projectRootPath: scope === 'project' ? loaded.projectResolution.projectRoot : undefined,
|
|
402
|
+
...(source === 'user' ? { source, userQuote } : {}),
|
|
403
|
+
};
|
|
404
|
+
try {
|
|
405
|
+
let stagedErrors = [];
|
|
406
|
+
if (source === 'user' && !stagedReason) {
|
|
407
|
+
const proposal = buildProposal(config, input);
|
|
408
|
+
const saved = directSavePersonal(config, proposal, this.homeDir);
|
|
409
|
+
if (saved.ok) {
|
|
410
|
+
this.directSaveCount++;
|
|
411
|
+
recordPropose('direct_saved');
|
|
412
|
+
this.lastHeadSignature = '';
|
|
413
|
+
// The unpushed reminder is decided after the push step (03 FR-03, 06 FR-02).
|
|
414
|
+
const warnings = [...extraWarnings, ...this.drainWarnings(loaded, false)];
|
|
415
|
+
this.unpushedReminderShown = false;
|
|
416
|
+
const payload = {
|
|
417
|
+
status: 'saved',
|
|
418
|
+
entry_id: proposal.entryId,
|
|
419
|
+
scope: 'personal',
|
|
420
|
+
commit: saved.commit.slice(0, 7),
|
|
421
|
+
l0: proposal.l0,
|
|
422
|
+
...(l0Note ? { l0_note: l0Note } : {}),
|
|
423
|
+
push: 'local',
|
|
424
|
+
note: CACHE_GUIDANCE_NOTE,
|
|
425
|
+
tell_user: `Saved to your personal memory: "${proposal.summary}". It is in the startup index from your next session. ` +
|
|
426
|
+
`To undo: faber-memory undo ${proposal.entryId}`,
|
|
427
|
+
warnings,
|
|
428
|
+
};
|
|
429
|
+
return { response: jsonResponse(payload), payload, directSaved: true, commit: saved.commit };
|
|
430
|
+
}
|
|
431
|
+
stagedReason = saved.stagedReason;
|
|
432
|
+
stagedErrors = saved.errors;
|
|
373
433
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
434
|
+
const proposal = createProposal(config, { ...input, ...(stagedReason ? { stagedReason } : {}) }, this.homeDir);
|
|
435
|
+
recordPropose('staged');
|
|
436
|
+
// 01 FR-04 amendment: say what the similarity check found, without blocking.
|
|
437
|
+
const similar = action === 'create'
|
|
438
|
+
? findRelatedEntries(summary, body, loaded.activeEntries, reconcileAwaitingCommitProposals(this.homeDir).filter((p) => p.id !== proposal.id), config.similarityHintFloor ?? DEFAULT_SIMILARITY_HINT_FLOOR, config.similarityThreshold).map((m) => ({ id: m.id, scope: m.scope, summary: m.summary, pending: m.pending, similarity: m.similarity }))
|
|
439
|
+
: [];
|
|
440
|
+
this.lastHeadSignature = '';
|
|
441
|
+
const warnings = [...extraWarnings, ...stagedErrors, ...this.drainWarnings(loaded)];
|
|
442
|
+
const payload = {
|
|
443
|
+
status: 'staged',
|
|
444
|
+
proposal_id: proposal.id,
|
|
445
|
+
entry_id: proposal.entryId,
|
|
446
|
+
scope: proposal.scope,
|
|
447
|
+
...(stagedReason ? { staged_reason: stagedReason, staged_note: STAGED_REASON_TEXT[stagedReason] } : {}),
|
|
448
|
+
...(l0Note ? { l0_note: l0Note } : {}),
|
|
449
|
+
...(action === 'create'
|
|
450
|
+
? {
|
|
451
|
+
similar,
|
|
452
|
+
...(similar.length > 0
|
|
453
|
+
? { similar_note: 'a related entry exists; prefer update if it is the same fact' }
|
|
454
|
+
: {}),
|
|
455
|
+
}
|
|
456
|
+
: {}),
|
|
457
|
+
warnings,
|
|
458
|
+
};
|
|
459
|
+
return { response: jsonResponse(payload), payload };
|
|
460
|
+
}
|
|
461
|
+
catch (err) {
|
|
462
|
+
return fail(err instanceof Error ? err.message : String(err));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
/** 06 FR-02/FR-05/FR-06: why a user-dictated call must be staged, or undefined when it can be saved directly. */
|
|
466
|
+
directSaveBlocker(config, scope) {
|
|
467
|
+
if (scope !== 'personal' || !config.roots.personal)
|
|
468
|
+
return 'scope';
|
|
469
|
+
if (!isDirectUserSaveOn(config))
|
|
470
|
+
return 'disabled';
|
|
471
|
+
if (this.directSaveCount >= DIRECT_SAVE_SESSION_CAP)
|
|
472
|
+
return 'session_cap';
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Like `handleToolCall`, but a user-dictated personal save pulls before it commits
|
|
477
|
+
* (03 FR-04) and pushes per `pushMode` after (03 FR-02, 06 FR-02).
|
|
478
|
+
*/
|
|
479
|
+
async handleToolCallAsync(name, args = {}) {
|
|
480
|
+
if (name !== 'memory_propose' || args.source !== 'user') {
|
|
481
|
+
return this.handleToolCall(name, args);
|
|
482
|
+
}
|
|
483
|
+
const config = loadConfig(this.homeDir);
|
|
484
|
+
const root = config.roots.personal;
|
|
485
|
+
const extraWarnings = [];
|
|
486
|
+
// 06 FR-02: validate, scan and check duplicates first; pull only for a call that will be saved.
|
|
487
|
+
const check = this.handlePropose(args, [], true);
|
|
488
|
+
if (check.checksPassed && check.directEligible && root) {
|
|
489
|
+
// Never pull while the startup sync may still be writing the same repository.
|
|
490
|
+
if (this.startupPullPromise)
|
|
491
|
+
await this.startupPullPromise;
|
|
492
|
+
const pullWarning = await pullRebaseBeforeCommit(root);
|
|
493
|
+
if (pullWarning)
|
|
494
|
+
extraWarnings.push(pullWarning);
|
|
495
|
+
this.lastHeadSignature = '';
|
|
496
|
+
}
|
|
497
|
+
const handled = this.handlePropose(args, extraWarnings);
|
|
498
|
+
if (!handled.directSaved || !handled.payload || !root || !handled.commit)
|
|
499
|
+
return handled.response;
|
|
500
|
+
let push = 'local';
|
|
501
|
+
if (getPushMode(root) === 'auto' && getAheadInfo(root.path).hasRemote) {
|
|
502
|
+
const res = await pushPersonalRoot(root, this.homeDir);
|
|
503
|
+
push = res.ok ? 'pushed' : 'push_failed';
|
|
504
|
+
if (!res.ok)
|
|
505
|
+
handled.payload.warnings.push(res.message);
|
|
506
|
+
// A push may rebase the save onto origin: keep the log, the result and L0 in step.
|
|
507
|
+
const moved = followRebasedDirectSave(config, handled.commit, this.homeDir);
|
|
508
|
+
if (moved) {
|
|
509
|
+
handled.payload.commit = moved.slice(0, 7);
|
|
510
|
+
this.lastHeadSignature = '';
|
|
377
511
|
}
|
|
378
512
|
}
|
|
379
|
-
|
|
513
|
+
handled.payload.push = push;
|
|
514
|
+
return jsonResponse(handled.payload);
|
|
515
|
+
}
|
|
516
|
+
/** JSON-RPC entry for stdio: like `handleJsonRpc`, with `tools/call` via `handleToolCallAsync`. */
|
|
517
|
+
async handleJsonRpcAsync(req) {
|
|
518
|
+
if (req.method !== 'tools/call')
|
|
519
|
+
return this.handleJsonRpc(req);
|
|
520
|
+
const params = req.params || {};
|
|
521
|
+
const res = await this.handleToolCallAsync(String(params.name || ''), params.arguments || {});
|
|
522
|
+
return { jsonrpc: '2.0', id: req.id ?? null, result: res };
|
|
380
523
|
}
|
|
381
524
|
handleJsonRpc(req) {
|
|
382
525
|
if (req.method === 'initialize') {
|
|
@@ -436,10 +579,15 @@ export class FaberMemoryMcpServer {
|
|
|
436
579
|
return;
|
|
437
580
|
try {
|
|
438
581
|
const req = JSON.parse(trimmed);
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
582
|
+
this.queue = this.queue.then(async () => {
|
|
583
|
+
const resp = await this.handleJsonRpcAsync(req).catch((err) => ({
|
|
584
|
+
jsonrpc: '2.0',
|
|
585
|
+
id: req.id ?? null,
|
|
586
|
+
error: { code: -32603, message: err instanceof Error ? err.message : String(err) },
|
|
587
|
+
}));
|
|
588
|
+
if (resp)
|
|
589
|
+
process.stdout.write(JSON.stringify(resp) + '\n');
|
|
590
|
+
});
|
|
443
591
|
}
|
|
444
592
|
catch (err) {
|
|
445
593
|
const errResp = {
|
|
@@ -461,3 +609,6 @@ function errorResponse(message, extra) {
|
|
|
461
609
|
content: [{ type: 'text', text: JSON.stringify({ error: message, ...extra }, null, 2) }],
|
|
462
610
|
};
|
|
463
611
|
}
|
|
612
|
+
function jsonResponse(payload) {
|
|
613
|
+
return { content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }] };
|
|
614
|
+
}
|
package/dist/src/metrics.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ProposalAction, Scope } from './types.js';
|
|
2
2
|
export type CompileTrigger = 'cli' | 'sync' | 'mcp-startup' | 'approve' | 'init';
|
|
3
3
|
export type ProposeSource = 'mcp' | 'import';
|
|
4
|
-
export type ProposeOutcome = 'staged' | 'duplicate_blocked' | 'hygiene_blocked' | 'validation_error';
|
|
5
|
-
export type ReviewOutcome = 'approved_committed' | 'approved_pushed' | 'approved_pr' | 'push_failed' | 'awaiting_commit' | 'rejected';
|
|
4
|
+
export type ProposeOutcome = 'staged' | 'direct_saved' | 'duplicate_blocked' | 'hygiene_blocked' | 'validation_error';
|
|
5
|
+
export type ReviewOutcome = 'approved_committed' | 'approved_pushed' | 'approved_pr' | 'push_failed' | 'awaiting_commit' | 'rejected' | 'undone';
|
|
6
6
|
export interface CompileMetricEvent {
|
|
7
7
|
type: 'compile';
|
|
8
8
|
ts: string;
|
|
@@ -40,6 +40,8 @@ export interface ProposeMetricEvent {
|
|
|
40
40
|
action: ProposalAction;
|
|
41
41
|
outcome: ProposeOutcome;
|
|
42
42
|
hygiene_category?: 'secret' | 'machine-path';
|
|
43
|
+
/** 06 FR-08: who asked for it; absent in older events means "agent". */
|
|
44
|
+
origin?: 'user' | 'agent';
|
|
43
45
|
}
|
|
44
46
|
export interface ReviewOutcomeMetricEvent {
|
|
45
47
|
type: 'review_outcome';
|
package/dist/src/proposals.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { EntryType, L0Mode, Proposal, ProposalAction, Scope, TeamSubmitMode, UserConfig } from './types.js';
|
|
1
|
+
import { EntryType, L0Mode, Proposal, ProposalAction, ProposalSource, StagedReason, Scope, TeamSubmitMode, UserConfig } from './types.js';
|
|
2
2
|
export declare const CACHE_GUIDANCE_NOTE = "Note: Updated L0 applies to new sessions; batching reviews at session end maximizes prompt-cache reuse.";
|
|
3
3
|
export declare function saveProposal(proposal: Proposal, homeDir?: string): string;
|
|
4
4
|
export declare function deleteProposal(proposal: Proposal, homeDir?: string): void;
|
|
5
5
|
export declare function listAllProposals(homeDir?: string): Proposal[];
|
|
6
6
|
export declare function reconcileAwaitingCommitProposals(homeDir?: string): Proposal[];
|
|
7
7
|
export declare function findProposalById(id: string, homeDir?: string): Proposal | null;
|
|
8
|
-
export
|
|
8
|
+
export interface ProposalInput {
|
|
9
9
|
scope: Scope;
|
|
10
10
|
action: ProposalAction;
|
|
11
11
|
entryId?: string;
|
|
@@ -18,8 +18,15 @@ export declare function createProposal(config: UserConfig, input: {
|
|
|
18
18
|
forceNew?: boolean;
|
|
19
19
|
reason?: string;
|
|
20
20
|
projectRootPath?: string;
|
|
21
|
-
|
|
21
|
+
source?: ProposalSource;
|
|
22
|
+
userQuote?: string;
|
|
23
|
+
stagedReason?: StagedReason;
|
|
24
|
+
}
|
|
25
|
+
export declare function createProposal(config: UserConfig, input: ProposalInput, homeDir?: string): Proposal;
|
|
26
|
+
/** Builds a proposal record without writing it (06 FR-02 uses it for a direct save). */
|
|
27
|
+
export declare function buildProposal(config: UserConfig, input: ProposalInput): Proposal;
|
|
22
28
|
export declare function rejectProposal(id: string, homeDir?: string): Proposal;
|
|
29
|
+
export declare function applyProposalFilesToDir(repoDir: string, subDir: string, proposal: Proposal): string[];
|
|
23
30
|
export interface ApproveOutcome {
|
|
24
31
|
ok: boolean;
|
|
25
32
|
approvedIds: string[];
|
package/dist/src/proposals.js
CHANGED
|
@@ -66,6 +66,12 @@ export function findProposalById(id, homeDir = getHomeDir()) {
|
|
|
66
66
|
return all.find((p) => p.id === id) || null;
|
|
67
67
|
}
|
|
68
68
|
export function createProposal(config, input, homeDir = getHomeDir()) {
|
|
69
|
+
const proposal = buildProposal(config, input);
|
|
70
|
+
saveProposal(proposal, homeDir);
|
|
71
|
+
return proposal;
|
|
72
|
+
}
|
|
73
|
+
/** Builds a proposal record without writing it (06 FR-02 uses it for a direct save). */
|
|
74
|
+
export function buildProposal(config, input) {
|
|
69
75
|
let rootUuid;
|
|
70
76
|
if (input.scope === 'personal') {
|
|
71
77
|
if (!config.roots.personal) {
|
|
@@ -107,8 +113,14 @@ export function createProposal(config, input, homeDir = getHomeDir()) {
|
|
|
107
113
|
status: 'pending',
|
|
108
114
|
createdAt: new Date().toISOString(),
|
|
109
115
|
projectRootPath: input.projectRootPath,
|
|
116
|
+
...(input.source === 'user'
|
|
117
|
+
? {
|
|
118
|
+
source: 'user',
|
|
119
|
+
userQuote: input.userQuote,
|
|
120
|
+
...(input.stagedReason ? { stagedReason: input.stagedReason } : {}),
|
|
121
|
+
}
|
|
122
|
+
: {}),
|
|
110
123
|
};
|
|
111
|
-
saveProposal(proposal, homeDir);
|
|
112
124
|
return proposal;
|
|
113
125
|
}
|
|
114
126
|
export function rejectProposal(id, homeDir = getHomeDir()) {
|
|
@@ -125,7 +137,7 @@ export function rejectProposal(id, homeDir = getHomeDir()) {
|
|
|
125
137
|
}, homeDir);
|
|
126
138
|
return proposal;
|
|
127
139
|
}
|
|
128
|
-
function applyProposalFilesToDir(repoDir, subDir, proposal) {
|
|
140
|
+
export function applyProposalFilesToDir(repoDir, subDir, proposal) {
|
|
129
141
|
const touched = [];
|
|
130
142
|
const targetDir = subDir ? path.join(repoDir, subDir) : repoDir;
|
|
131
143
|
fs.mkdirSync(targetDir, { recursive: true });
|
package/dist/src/stats.d.ts
CHANGED
|
@@ -94,6 +94,12 @@ export interface StatsReport {
|
|
|
94
94
|
awaitingCommit: number;
|
|
95
95
|
rejected: number;
|
|
96
96
|
};
|
|
97
|
+
/** 06 FR-08: memories the user dictated. */
|
|
98
|
+
userDictated: {
|
|
99
|
+
directSaved: number;
|
|
100
|
+
staged: number;
|
|
101
|
+
undone: number;
|
|
102
|
+
};
|
|
97
103
|
};
|
|
98
104
|
cache?: CacheAnalysisResult;
|
|
99
105
|
}
|
|
@@ -137,6 +143,8 @@ export interface ShareableBenchmarkJson {
|
|
|
137
143
|
pushFailed: number;
|
|
138
144
|
awaitingCommit: number;
|
|
139
145
|
rejected: number;
|
|
146
|
+
directSaved: number;
|
|
147
|
+
userDictatedStaged: number;
|
|
140
148
|
};
|
|
141
149
|
cache?: Partial<Record<'claude-code' | 'antigravity', {
|
|
142
150
|
sessions: number;
|