@lh8ppl/claude-memory-kit 0.2.2 → 0.2.3

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.
@@ -26,9 +26,9 @@ import { weeklyCurate } from './weekly-curate.mjs';
26
26
  import { autoPersona } from './auto-persona.mjs';
27
27
  import { exportPersona, importPersona } from './persona-portability.mjs';
28
28
  import { setNativeAutoMemory, nativeMemoryInstallNote } from './native-memory.mjs';
29
- import { writeFact } from './write-fact.mjs';
30
- import { buildRichFactBody, slugifyFact } from './rich-fact.mjs';
31
- import { createHash } from 'node:crypto';
29
+ import { rememberRich, richFactTitle, nonProjectTierNote } from './remember-core.mjs';
30
+ import { getObservations, citeLink, buildTimeline, recentActivity } from './read-core.mjs';
31
+ import { readHookStdin } from './read-hook-stdin.mjs';
32
32
  import { runLazyCompress } from './lazy-compress.mjs';
33
33
  import { runDoctor } from './doctor.mjs';
34
34
  import { importAnthropicMemory } from './import-anthropic-memory.mjs';
@@ -61,7 +61,7 @@ import { resolveReviewQueue } from './review-queue.mjs';
61
61
  import { createInterface } from 'node:readline';
62
62
  import { resolve as resolvePath, join, basename } from 'node:path';
63
63
 
64
- const NOTICE_PREFIX = 'not yet implemented in v0.1.0';
64
+ const NOTICE_PREFIX = 'not yet implemented';
65
65
 
66
66
  /**
67
67
  * Real `cmk install` action — wired in Task 3, extended in Task 4 with
@@ -323,6 +323,99 @@ function runSearch(queryParts, options) {
323
323
  }
324
324
  }
325
325
 
326
+ // --- Read verbs (Task 108b — CLI parity with the MCP read tools) ------
327
+ //
328
+ // `cmk get` / `timeline` / `cite` / `recent-activity` mirror the MCP tools
329
+ // mk_get / mk_timeline / mk_cite / mk_recent_activity by calling the SAME
330
+ // shared read cores (read-core.mjs) — identical results from CLI + MCP
331
+ // (ADR-0014). cite is pure (no DB); the rest open the index + reindex first
332
+ // (same fresh-install freshness guard as `cmk search`).
333
+
334
+ // `deps` (projectRoot / log / logError) are injection seams: production passes
335
+ // nothing (defaults to cwd + console), in-process tests pass a temp projectRoot
336
+ // + captured loggers so the glue is covered WITHOUT a subprocess (the D-86
337
+ // lesson — real-binary tests don't contribute line coverage). Exported for the
338
+ // unit tests.
339
+
340
+ /** Open the index DB, refresh it (best-effort), run `fn(db)`, always close. */
341
+ export function withReadDb(fn, deps = {}) {
342
+ const projectRoot = deps.projectRoot ?? resolvePath(process.cwd());
343
+ const userDir =
344
+ deps.userDir ?? process.env.MEMORY_KIT_USER_DIR ?? join(homedir(), '.claude-memory-kit');
345
+ const logError = deps.logError ?? console.error;
346
+ const db = openIndexDb({ projectRoot });
347
+ try {
348
+ try {
349
+ reindexBoot({ projectRoot, userDir, db });
350
+ } catch (err) {
351
+ logError(`cmk: index refresh failed (${err?.message ?? err}); using the existing index.`);
352
+ }
353
+ return fn(db);
354
+ } finally {
355
+ db.close();
356
+ }
357
+ }
358
+
359
+ export function runGet(ids, _options = {}, _command, deps = {}) {
360
+ const log = deps.log ?? console.log;
361
+ const list = Array.isArray(ids) ? ids : [ids];
362
+ const rows = withReadDb((db) => getObservations(db, list), deps);
363
+ log(JSON.stringify(rows, null, 2));
364
+ // All-missing/invalid → exit 2 (lets a script tell "nothing matched" from a hit).
365
+ if (rows.length > 0 && rows.every((r) => r.error)) process.exitCode = 2;
366
+ }
367
+
368
+ export function runCite(id, _options = {}, _command, deps = {}) {
369
+ const log = deps.log ?? console.log;
370
+ const logError = deps.logError ?? console.error;
371
+ const r = citeLink(id);
372
+ if (!r.ok) {
373
+ logError(`cmk cite: ${r.error}`);
374
+ process.exitCode = 2;
375
+ return;
376
+ }
377
+ log(r.link);
378
+ }
379
+
380
+ export function runTimeline(anchor, options = {}, _command, deps = {}) {
381
+ const log = deps.log ?? console.log;
382
+ const logError = deps.logError ?? console.error;
383
+ const r = withReadDb(
384
+ (db) =>
385
+ buildTimeline(db, {
386
+ anchor,
387
+ depthBefore: options.before !== undefined ? Number(options.before) : 5,
388
+ depthAfter: options.after !== undefined ? Number(options.after) : 5,
389
+ }),
390
+ deps,
391
+ );
392
+ if (!r.ok) {
393
+ logError(`cmk timeline: ${r.error}`);
394
+ process.exitCode = 2;
395
+ return;
396
+ }
397
+ log(JSON.stringify(r.timeline, null, 2));
398
+ }
399
+
400
+ export function runRecentActivity(options = {}, _command, deps = {}) {
401
+ const log = deps.log ?? console.log;
402
+ const logError = deps.logError ?? console.error;
403
+ const r = withReadDb(
404
+ (db) =>
405
+ recentActivity(db, {
406
+ window: options.window ?? '24h',
407
+ limit: options.limit !== undefined ? Number(options.limit) : 20,
408
+ }),
409
+ deps,
410
+ );
411
+ if (!r.ok) {
412
+ logError(`cmk recent-activity: ${r.error}`);
413
+ process.exitCode = 2;
414
+ return;
415
+ }
416
+ log(JSON.stringify(r.rows, null, 2));
417
+ }
418
+
326
419
  /**
327
420
  * `cmk reindex` — three modes.
328
421
  *
@@ -365,57 +458,27 @@ function runSearch(queryParts, options) {
365
458
  * carries injection seams for testing.
366
459
  */
367
460
  export function runRememberRich(text, options = {}, deps = {}) {
368
- const projectRoot = deps.projectRoot ?? resolvePath(process.cwd());
369
461
  const log = deps.log ?? console.log;
370
462
  const logError = deps.logError ?? console.error;
371
- const write = deps.writeFact ?? writeFact;
372
463
 
373
- // M2: rich capture writes the project tier (P) in v0.1.x same deferral as
374
- // the terse path + mk_remember (U/L need per-tier scratchpad routing, design
375
- // §16). Terse mode ERRORS on a non-P --tier; rich mode notes it and proceeds
376
- // (a no-write surprise is worse than a captured-to-P note). Surface it so the
377
- // divergence isn't silent.
464
+ // Non-P --tier: capture at the project tier (P) + surface the note (don't
465
+ // silently honor it, don't hard-error). ONE shared note across CLI + MCP so
466
+ // the message can't drift (D-102 / Task 121).
378
467
  if (options.tier && options.tier !== 'P') {
379
- log(
380
- `cmk remember: --tier '${options.tier}' is v0.1.x — rich capture writes the project tier (P) for now.`,
381
- );
468
+ log(`cmk remember: ${nonProjectTierNote(options.tier)}`);
382
469
  }
383
470
 
384
- const headline = String(text).trim();
385
- const title = (options.title && String(options.title).trim()) || headline.split('\n')[0].slice(0, 80);
386
- const body = buildRichFactBody({ text: headline, why: options.why, how: options.how });
387
- const related = options.links
388
- ? String(options.links).split(',').map((s) => s.trim()).filter(Boolean)
389
- : undefined;
390
-
391
- const r = write({
392
- tier: 'P',
393
- type: options.type ?? 'feedback',
394
- slug: slugifyFact(title),
395
- title,
396
- body,
397
- writeSource: 'user-explicit',
398
- trust: options.trust ?? 'high',
399
- sourceFile: 'user-explicit',
400
- sourceLine: 1,
401
- // Content fingerprint for provenance/dedup — NOT a security context.
402
- // Matches the kit's sha1-of-content convention (memory-write.mjs,
403
- // index-rebuild.mjs); writeFact dedups by content-addressed id, this is
404
- // just the source_sha1 provenance field. // NOSONAR
405
- sourceSha1: createHash('sha1').update(body).digest('hex'), // NOSONAR
406
-
407
- related,
408
- projectRoot,
409
- });
471
+ // The write is the shared core (remember-core.rememberRich) — the SAME one the
472
+ // MCP `mk_remember` rich path calls, so both surfaces emit identical fact files
473
+ // (ADR-0014). This wrapper only formats the CLI's messages from the result.
474
+ const r = rememberRich(text, options, deps);
410
475
 
411
476
  if (r.action === 'error') {
412
- // M1: a collision means a fact file with this title (→ slug) already exists
413
- // but with different content (different id). Give an actionable hint rather
414
- // than the raw "refusing overwrite" — the user almost certainly wants to
415
- // edit the existing fact or pick a new --title.
416
477
  if (r.errorCategory === 'collision') {
478
+ // M1: a same-title / different-content collision — actionable hint over
479
+ // the raw "refusing overwrite".
417
480
  logError(
418
- `cmk remember: a fact titled "${title}" already exists with different content. ` +
481
+ `cmk remember: a fact titled "${richFactTitle(text, options)}" already exists with different content. ` +
419
482
  `Edit it directly, or capture under a new --title.`,
420
483
  );
421
484
  return r;
@@ -431,11 +494,128 @@ export function runRememberRich(text, options = {}, deps = {}) {
431
494
  return r;
432
495
  }
433
496
 
434
- function runRemember(textParts, options) {
435
- const projectRoot = resolvePath(process.cwd());
497
+ /**
498
+ * Task 108.2 (108a) — parse a structured fact from the off-shell input channel
499
+ * (`--from-file <path>` or `--json` stdin). PURE + dependency-injected (the CLI
500
+ * passes real fs readers; tests pass fakes) so every parse/validate/allowlist
501
+ * branch is covered IN-PROCESS — the real-binary subprocess tests prove the CLI
502
+ * wiring but don't contribute line coverage.
503
+ *
504
+ * @param {object} options - subcommand options (fromFile/json + the rich flags).
505
+ * @param {object} deps
506
+ * @param {(path:string)=>string} deps.readFile - read a file to a string (throws on error).
507
+ * @param {()=>string} deps.readStdin - read stdin to a string ('' for TTY/empty).
508
+ * @returns {{ok:true,channel:string,fields:object,ignored:string[]}
509
+ * | {ok:false,channel:string,error:string,ignored:string[]}}
510
+ */
511
+ export function parseFactInput(options, { readFile, readStdin } = {}) {
512
+ const channel = options.fromFile ? '--from-file' : '--json';
513
+ // --from-file/--json are self-contained (the JSON is the whole fact); rich /
514
+ // terse flags passed alongside are ignored — surfaced so they aren't dropped silently.
515
+ const ignored = ['why', 'how', 'type', 'title', 'links', 'tier', 'trust', 'section']
516
+ .filter((k) => options[k] != null)
517
+ .map((k) => '--' + k);
518
+ const fail = (error) => ({ ok: false, channel, error, ignored });
519
+
520
+ let raw;
521
+ if (options.fromFile) {
522
+ try {
523
+ raw = readFile(options.fromFile);
524
+ } catch (e) {
525
+ return fail(`${channel} could not read ${options.fromFile}: ${e.message}`);
526
+ }
527
+ } else {
528
+ // '' = interactive TTY or empty pipe (read-hook-stdin returns '' for a TTY).
529
+ raw = readStdin();
530
+ if (!raw || !raw.trim()) {
531
+ return fail('--json expects a JSON object on stdin (pipe it in, or use --from-file).');
532
+ }
533
+ }
534
+
535
+ // Bound the input so a pathological file can't burn Poison_Guard regex time
536
+ // (the M1 concern that capped mk_remember). 64 KB is generous for one fact.
537
+ const MAX_INPUT_BYTES = 64 * 1024;
538
+ if (Buffer.byteLength(raw, 'utf8') > MAX_INPUT_BYTES) {
539
+ return fail(`${channel} fact is too large (max ${MAX_INPUT_BYTES / 1024} KB). Split it into smaller facts.`);
540
+ }
541
+
542
+ let parsed;
543
+ try {
544
+ parsed = JSON.parse(raw);
545
+ } catch (e) {
546
+ return fail(`${channel} could not parse JSON: ${e.message}`);
547
+ }
548
+ if (
549
+ !parsed ||
550
+ typeof parsed !== 'object' ||
551
+ Array.isArray(parsed) ||
552
+ typeof parsed.text !== 'string' ||
553
+ !parsed.text.trim()
554
+ ) {
555
+ return fail(`${channel} JSON must be an object with a non-empty "text" field.`);
556
+ }
557
+
558
+ // Allowlist the honored fields — NEVER forward the raw parsed object. A crafted
559
+ // JSON must not reach a field runRememberRich might read; provenance
560
+ // (write_source / source_file) stays hardcoded user-explicit in runRememberRich.
561
+ return {
562
+ ok: true,
563
+ channel,
564
+ ignored,
565
+ fields: {
566
+ text: parsed.text,
567
+ why: parsed.why,
568
+ how: parsed.how,
569
+ type: parsed.type,
570
+ title: parsed.title,
571
+ links: parsed.links,
572
+ tier: parsed.tier,
573
+ trust: parsed.trust,
574
+ },
575
+ };
576
+ }
577
+
578
+ export function runRemember(textParts, options, deps = {}) {
579
+ const projectRoot = deps.projectRoot ?? resolvePath(process.cwd());
436
580
  const userDir =
437
- process.env.MEMORY_KIT_USER_DIR ?? join(homedir(), '.claude-memory-kit');
581
+ deps.userDir ?? process.env.MEMORY_KIT_USER_DIR ?? join(homedir(), '.claude-memory-kit');
582
+ const log = deps.log ?? console.log;
583
+ const logError = deps.logError ?? console.error;
584
+
585
+ // Task 108.2 (108a) — structured off-shell input. `--from-file`/`--json` carry
586
+ // the fact as a JSON object from a FILE or STDIN, so rich content (backticks,
587
+ // $(), quotes, newlines) never rides the shell command line — the D-81 fix.
588
+ // The parse/validate/allowlist lives in the pure parseFactInput() helper.
589
+ if (options?.fromFile || options?.json) {
590
+ const parsed = parseFactInput(options, {
591
+ readFile: (p) => readFileSync(p, 'utf8'),
592
+ readStdin: () => readHookStdin({ isTTY: process.stdin.isTTY }),
593
+ });
594
+ if (parsed.ignored.length) {
595
+ logError(
596
+ `cmk remember: ${parsed.channel} is self-contained — ignoring ${parsed.ignored.join(', ')} (put these in the JSON instead).`,
597
+ );
598
+ }
599
+ if (!parsed.ok) {
600
+ logError(`cmk remember: ${parsed.error}`);
601
+ process.exitCode = 2;
602
+ return;
603
+ }
604
+ runRememberRich(parsed.fields.text, parsed.fields, { projectRoot, log, logError });
605
+ return;
606
+ }
607
+
438
608
  const text = Array.isArray(textParts) ? textParts.join(' ') : textParts;
609
+ // Bare `cmk remember` — no positional text and no input channel. The positional
610
+ // arg is optional now (for --from-file/--json), so guard explicitly instead of
611
+ // falling through to a vague empty-text write error.
612
+ if (!text || !String(text).trim()) {
613
+ logError(
614
+ 'cmk remember: provide a fact to remember, or use --from-file <json> / --json (stdin).',
615
+ );
616
+ process.exitCode = 2;
617
+ return;
618
+ }
439
619
  // Rich mode: any of --why/--how/--type/--title/--links → write a real fact
440
620
  // file (the F1 fix) instead of a terse MEMORY.md bullet. M3: --trust and
441
621
  // --section are intentionally NOT triggers — --trust is shared by both forms
@@ -445,15 +625,15 @@ function runRemember(textParts, options) {
445
625
  runRememberRich(text, options, { projectRoot });
446
626
  return;
447
627
  }
448
- const tier = options?.tier ?? 'P';
449
- if (tier !== 'P') {
450
- console.error(
451
- `cmk remember: tier '${tier}' not yet supported v0.1.0 writes the project tier (P). ` +
452
- 'For machine-only config, edit context.local/machine-paths.md directly (v0.1.x will add --tier routing).',
453
- );
454
- process.exitCode = 2;
455
- return;
628
+ // Non-P --tier: capture at P + note (consistent with the rich path + the MCP
629
+ // tool D-102). A fact becomes cross-project via `cmk lessons promote`, not a
630
+ // direct tier write (direct U/L routing is the deferred feature in design §16.40). We do NOT
631
+ // hard-error losing the capture to an error is worse than landing it at P.
632
+ const requestedTier = options?.tier ?? 'P';
633
+ if (requestedTier !== 'P') {
634
+ log(`cmk remember: ${nonProjectTierNote(requestedTier)}`);
456
635
  }
636
+ const tier = 'P';
457
637
  const trust = options?.trust ?? 'high';
458
638
  const section = options?.section ?? 'Active Threads';
459
639
  const r = memoryWrite({
@@ -547,6 +727,11 @@ function runForget(idOrQuery, options /* , command */) {
547
727
  const result = forgetAction({
548
728
  idOrQuery,
549
729
  projectRoot,
730
+ // Pass the resolved userDir (same source as `cmk search`) so forget's
731
+ // in-band reindex covers all three tiers and its orphan-prune fires
732
+ // IMMEDIATELY (Task 110) — without it the prune is skipped here and only
733
+ // self-heals on the next search. Also lets forget tombstone U-tier facts.
734
+ userDir: resolveUserDir(),
550
735
  reason: options.reason,
551
736
  deletedBy: options.deletedBy,
552
737
  yes: true,
@@ -667,11 +852,19 @@ export async function runPersonaGenerate(opts = {}) {
667
852
  try {
668
853
  const backend =
669
854
  opts.backend ?? new (await import('./compressor.mjs')).HaikuViaAnthropicApi();
670
- const r = await autoPersona({ projectRoot, userDir, backend });
855
+ // Task 111 (F-2): `cmk persona generate` is an explicit one-shot with NO outer
856
+ // hook ceiling (unlike the 60s-bounded SessionEnd path), so it gives the Haiku
857
+ // classifier generous headroom — the whole-project facts sweep is a heavier
858
+ // call than a session summary, and the user is willing to wait for the command
859
+ // they ran. The corpus is byte-capped (PERSONA_CORPUS_BYTES) so this can't run
860
+ // unbounded. Overridable via opts.timeoutMs.
861
+ const r = await autoPersona({ projectRoot, userDir, backend, timeoutMs: opts.timeoutMs ?? 120_000 });
671
862
  if (r.action === 'error') {
672
- logError(
673
- `cmk persona generate: error (${r.errorCategory ?? 'unknown'})${(r.errors && r.errors.length) ? `: ${r.errors.join('; ')}` : ''}`,
674
- );
863
+ const detail = (r.errors && r.errors.length) ? `: ${r.errors.join('; ')}` : '';
864
+ const hint = /did not return within/.test(detail)
865
+ ? ' — the Haiku classifier timed out; this is usually a transient API slowdown. Re-run `cmk persona generate` (the weekly curate pass also retries it automatically).'
866
+ : '';
867
+ logError(`cmk persona generate: error (${r.errorCategory ?? 'unknown'})${detail}${hint}`);
675
868
  return;
676
869
  }
677
870
  const promoted = r.promoted?.length ?? 0;
@@ -795,9 +988,9 @@ function runRegisterCrons(options /* , command */) {
795
988
  // Helper: quote a path for the platform's cron-line shell.
796
989
  // Linux + macOS: double-quote (the cron line is single-quoted around the
797
990
  // whole `echo '...'`; double-quotes inside are safe).
798
- // Windows: the schtasks /TR value is already double-quoted by registerCron,
799
- // with `\"` escaping for inner quotesregisterCron's existing
800
- // escapedCommand handles this.
991
+ // Windows: registerCron execs schtasks with an args array (no shell), so this
992
+ // double-quoted /TR value is delivered verbatimno escaping needed (Task 109
993
+ // / D-83). macOS strips these wrapping quotes before building the plist.
801
994
  const quote = (s) => `"${s}"`;
802
995
 
803
996
  const jobs = [
@@ -988,40 +1181,44 @@ async function runRollCli(options /* , command */) {
988
1181
  }
989
1182
  }
990
1183
 
991
- async function runImportAnthropicMemory(options /* , command */) {
992
- const projectRoot = resolvePath(process.cwd());
1184
+ // Task 114 (F-13): dep-injectable (projectRoot / harnessRoot / log / logError) so
1185
+ // the real-import CLI path is verifiable on real input WITHOUT touching the user's
1186
+ // ~/.claude. Defaults are unchanged for production. Returns the core result.
1187
+ export async function runImportAnthropicMemory(options = {}) {
1188
+ const projectRoot = options?.projectRoot ?? resolvePath(process.cwd());
1189
+ const log = options?.log ?? console.log;
1190
+ const logError = options?.logError ?? console.error;
993
1191
  const dryRun = options?.dryRun === true;
994
1192
  const acceptAll = options?.yes === true;
1193
+ // options.importFn is a test seam (default = the real core) so the error +
1194
+ // catch branches below — unreachable via normal input — are coverable.
1195
+ const importFn = options?.importFn ?? importAnthropicMemory;
995
1196
  try {
996
- // I1 fix (skill-review 2026-05-28): userDir was unused, dropped.
997
- const r = await importAnthropicMemory({ projectRoot, dryRun, acceptAll });
1197
+ const r = await importFn({ projectRoot, dryRun, acceptAll, harnessRoot: options?.harnessRoot });
998
1198
  if (r.action === 'error') {
999
- console.error(`cmk import-anthropic-memory: error — ${(r.errors ?? []).join('; ')}`);
1199
+ logError(`cmk import-anthropic-memory: error — ${(r.errors ?? []).join('; ')}`);
1000
1200
  process.exitCode = 2;
1001
- return;
1201
+ return r;
1002
1202
  }
1003
1203
  if (r.reason === 'no-source') {
1004
- console.log(`cmk import-anthropic-memory: no Anthropic auto-memory found at ${r.sourcePath}`);
1005
- return;
1204
+ log(`cmk import-anthropic-memory: no Anthropic auto-memory found at ${r.sourcePath}`);
1205
+ return r;
1006
1206
  }
1007
1207
  if (r.mode === 'dry-run') {
1008
- console.log(`cmk import-anthropic-memory: dry-run — ${r.proposals.length} proposal(s), ${r.skipped} duplicate(s) skipped`);
1009
- for (const p of r.proposals) {
1010
- console.log(` + ${p.id}: ${p.text}`);
1011
- }
1012
- return;
1208
+ log(`cmk import-anthropic-memory: dry-run — ${r.proposals.length} proposal(s), ${r.skipped} duplicate(s) skipped`);
1209
+ for (const p of r.proposals) log(` + ${p.id}: ${p.text}`);
1210
+ return r;
1013
1211
  }
1014
1212
  if (r.mode === 'requires-confirmation') {
1015
- console.log(`cmk import-anthropic-memory: ${r.proposals.length} proposal(s) ready to apply.`);
1016
- console.log(' Re-run with --yes to apply, or --dry-run to inspect.');
1017
- for (const p of r.proposals) {
1018
- console.log(` + ${p.id}: ${p.text}`);
1019
- }
1020
- return;
1213
+ log(`cmk import-anthropic-memory: ${r.proposals.length} proposal(s) ready to apply.`);
1214
+ log(' Re-run with --yes to apply, or --dry-run to inspect.');
1215
+ for (const p of r.proposals) log(` + ${p.id}: ${p.text}`);
1216
+ return r;
1021
1217
  }
1022
- console.log(`cmk import-anthropic-memory: applied ${r.accepted} proposal(s), skipped ${r.skipped} duplicate(s)`);
1218
+ log(`cmk import-anthropic-memory: applied ${r.accepted} proposal(s), skipped ${r.skipped} duplicate(s)`);
1219
+ return r;
1023
1220
  } catch (err) {
1024
- console.error(`cmk import-anthropic-memory: unexpected error: ${err?.message ?? err}`);
1221
+ logError(`cmk import-anthropic-memory: unexpected error: ${err?.message ?? err}`);
1025
1222
  process.exitCode = 2;
1026
1223
  }
1027
1224
  }
@@ -1131,8 +1328,12 @@ async function runCompress(options /* , command */) {
1131
1328
 
1132
1329
  async function runMcpDispatch(childName) {
1133
1330
  if (childName === 'serve') {
1134
- const projectRoot = resolvePath(process.cwd());
1135
- const userDir = join(homedir(), '.claude-memory-kit');
1331
+ // Claude Code sets CLAUDE_PROJECT_DIR in the spawned MCP server's environment
1332
+ // to the project root (code.claude.com/docs/en/mcp). Prefer it over cwd so the
1333
+ // server indexes the right project even when Claude Code launches it with a
1334
+ // different working directory. Falls back to cwd for a manual `cmk mcp serve`.
1335
+ const projectRoot = resolvePath(process.env.CLAUDE_PROJECT_DIR ?? process.cwd());
1336
+ const userDir = process.env.MEMORY_KIT_USER_DIR ?? join(homedir(), '.claude-memory-kit');
1136
1337
  // ALL logs to stderr per design §10.1; stdout is reserved for
1137
1338
  // JSON-RPC messages handled by the SDK's StdioServerTransport.
1138
1339
  // Don't console.log() anything before/during the server's run.
@@ -1169,43 +1370,44 @@ async function runQueueDispatch(childName) {
1169
1370
  * canonical kit usage). User-tier / language-tier conflicts queues
1170
1371
  * can be added when the kit's CLI gains explicit `--tier` selection.
1171
1372
  */
1172
- async function runQueueConflicts() {
1173
- const rl = createInterface({ input: process.stdin, output: process.stdout });
1174
- const askOnce = (q) =>
1175
- new Promise((resolve) => {
1176
- rl.question(q, (answer) => resolve(answer));
1177
- });
1178
-
1179
- const VALID_DECISIONS = new Set(['keep-old', 'keep-new', 'merge-both', 'skip']);
1180
-
1181
- const prompter = async ({
1182
- proposedId,
1183
- proposedText,
1184
- proposedTrust,
1185
- existingId,
1186
- existingText,
1187
- existingTrust,
1188
- similarity,
1189
- }) => {
1190
- console.log('');
1191
- console.log('─── pending conflict ──────────────────────────────────────');
1192
- console.log(`existing (${existingId}, trust=${existingTrust}): ${existingText}`);
1193
- console.log(`proposed (${proposedId}, trust=${proposedTrust}): ${proposedText}`);
1194
- console.log(`similarity: ${Number(similarity).toFixed(4)}`);
1373
+ // Task 113 (F-9): conflict prompter LOGIC extracted as a factory over `ask`
1374
+ // (see buildReviewPrompter) so it's unit-testable without stdin.
1375
+ export function buildConflictPrompter({ ask, log }) {
1376
+ const VALID = new Set(['keep-old', 'keep-new', 'merge-both', 'skip']);
1377
+ return async ({ proposedId, proposedText, proposedTrust, existingId, existingText, existingTrust, similarity }) => {
1378
+ log('');
1379
+ log('─── pending conflict ──────────────────────────────────────');
1380
+ log(`existing (${existingId}, trust=${existingTrust}): ${existingText}`);
1381
+ log(`proposed (${proposedId}, trust=${proposedTrust}): ${proposedText}`);
1382
+ log(`similarity: ${Number(similarity).toFixed(4)}`);
1195
1383
  let decision = '';
1196
- while (!VALID_DECISIONS.has(decision)) {
1197
- const answer = await askOnce(
1198
- ` [keep-old / keep-new / merge-both / skip]: `,
1199
- );
1384
+ while (!VALID.has(decision)) {
1385
+ const answer = await ask(` [keep-old / keep-new / merge-both / skip]: `);
1200
1386
  decision = String(answer).trim();
1201
- if (!VALID_DECISIONS.has(decision)) {
1202
- console.log(
1203
- ` unknown answer "${decision}" — please type one of: keep-old, keep-new, merge-both, skip`,
1204
- );
1387
+ if (!VALID.has(decision)) {
1388
+ log(` unknown answer "${decision}" — please type one of: keep-old, keep-new, merge-both, skip`);
1205
1389
  }
1206
1390
  }
1207
1391
  return decision;
1208
1392
  };
1393
+ }
1394
+
1395
+ // Task 113 (F-9): dep-injectable (see runQueueReview). Defaults unchanged for prod;
1396
+ // a test injects { projectRoot, prompter, log, logError } to drive a real keep-old
1397
+ // / keep-new / merge-both resolution end-to-end without stdin. Returns the result.
1398
+ export async function runQueueConflicts(opts = {}) {
1399
+ const projectRoot = opts.projectRoot ?? process.cwd();
1400
+ const log = opts.log ?? console.log;
1401
+ const logError = opts.logError ?? console.error;
1402
+
1403
+ let rl = null;
1404
+ let prompter = opts.prompter;
1405
+ if (!prompter) {
1406
+ rl = createInterface({ input: process.stdin, output: process.stdout });
1407
+ const askOnce = (q) =>
1408
+ new Promise((resolve) => rl.question(q, (answer) => resolve(answer)));
1409
+ prompter = buildConflictPrompter({ ask: askOnce, log });
1410
+ }
1209
1411
 
1210
1412
  // merge-both wiring (Task 25b — closes Task 25's cross-layer
1211
1413
  // composition gap). The proposed bullet from the conflict queue
@@ -1251,34 +1453,35 @@ async function runQueueConflicts() {
1251
1453
  idB: proposedId,
1252
1454
  });
1253
1455
  if (result.action === 'error') {
1254
- console.error(
1456
+ logError(
1255
1457
  `cmk queue conflicts: merge-both for ${existingId} + ${proposedId} failed: ${result.errors.join('; ')}`,
1256
1458
  );
1257
1459
  } else {
1258
- console.log(
1259
- ` merge-both → ${existingId} + ${proposedId} merged into ${result.id}`,
1260
- );
1460
+ log(` merge-both → ${existingId} + ${proposedId} merged into ${result.id}`);
1261
1461
  }
1262
1462
  };
1263
1463
 
1464
+ // opts.resolve test seam (default = the real resolver) — see runQueueReview.
1465
+ const resolve = opts.resolve ?? resolveConflictQueue;
1264
1466
  try {
1265
- const result = await resolveConflictQueue({
1467
+ const result = await resolve({
1266
1468
  tier: 'P',
1267
- projectRoot: process.cwd(),
1469
+ projectRoot,
1268
1470
  prompter,
1269
1471
  mergeFn,
1270
1472
  });
1271
1473
  if (result.action === 'error') {
1272
- for (const e of result.errors) console.error(`cmk queue conflicts: ${e}`);
1474
+ for (const e of result.errors) logError(`cmk queue conflicts: ${e}`);
1273
1475
  process.exitCode = 2;
1274
- return;
1476
+ return result;
1275
1477
  }
1276
- console.log('');
1277
- console.log(
1478
+ log('');
1479
+ log(
1278
1480
  `cmk queue conflicts: ${result.resolved} resolved (${result.kept_old} kept-old, ${result.kept_new} kept-new, ${result.merged} merged), ${result.skipped} skipped`,
1279
1481
  );
1482
+ return result;
1280
1483
  } finally {
1281
- rl.close();
1484
+ if (rl) rl.close();
1282
1485
  }
1283
1486
  }
1284
1487
 
@@ -1291,59 +1494,74 @@ async function runQueueConflicts() {
1291
1494
  *
1292
1495
  * Resolves the PROJECT tier's review queue (the canonical kit usage).
1293
1496
  */
1294
- async function runQueueReview() {
1295
- const rl = createInterface({ input: process.stdin, output: process.stdout });
1296
- const askOnce = (q) =>
1297
- new Promise((resolve) => {
1298
- rl.question(q, (answer) => resolve(answer));
1299
- });
1300
-
1301
- const VALID_DECISIONS = new Set(['promote', 'discard', 'skip']);
1302
-
1303
- const prompter = async ({ id, text, ts, provenance }) => {
1304
- console.log('');
1305
- console.log('─── pending review ────────────────────────────────────────');
1306
- console.log(`id: ${id}`);
1307
- console.log(`ts: ${ts}`);
1308
- console.log(`text: ${text}`);
1309
- if (provenance) console.log(`prov: ${provenance.trim()}`);
1497
+ // Task 113 (F-9): the interactive prompter LOGIC (formatting + the validate-retry
1498
+ // loop) extracted as a factory over an injectable `ask`, so it's unit-testable
1499
+ // without a real readline/stdin — only the 2-line createInterface wrapper in the
1500
+ // runner stays an uncovered shim.
1501
+ export function buildReviewPrompter({ ask, log }) {
1502
+ const VALID = new Set(['promote', 'discard', 'skip']);
1503
+ return async ({ id, text, ts, provenance }) => {
1504
+ log('');
1505
+ log('─── pending review ────────────────────────────────────────');
1506
+ log(`id: ${id}`);
1507
+ log(`ts: ${ts}`);
1508
+ log(`text: ${text}`);
1509
+ if (provenance) log(`prov: ${provenance.trim()}`);
1310
1510
  let decision = '';
1311
- while (!VALID_DECISIONS.has(decision)) {
1312
- const answer = await askOnce(` [promote / discard / skip]: `);
1511
+ while (!VALID.has(decision)) {
1512
+ const answer = await ask(` [promote / discard / skip]: `);
1313
1513
  decision = String(answer).trim();
1314
- if (!VALID_DECISIONS.has(decision)) {
1315
- console.log(
1316
- ` unknown answer "${decision}" — please type one of: promote, discard, skip`,
1317
- );
1514
+ if (!VALID.has(decision)) {
1515
+ log(` unknown answer "${decision}" — please type one of: promote, discard, skip`);
1318
1516
  }
1319
1517
  }
1320
1518
  return decision;
1321
1519
  };
1520
+ }
1521
+
1522
+ // Task 113 (F-9): dep-injectable so the CLI resolution path is verifiable on REAL
1523
+ // queued items. Defaults (readline over stdin + cwd + console) are unchanged for
1524
+ // production; a test injects { projectRoot, prompter, log, logError } to drive a
1525
+ // real promote/discard end-to-end without stdin. Returns the resolver result.
1526
+ export async function runQueueReview(opts = {}) {
1527
+ const projectRoot = opts.projectRoot ?? process.cwd();
1528
+ const log = opts.log ?? console.log;
1529
+ const logError = opts.logError ?? console.error;
1322
1530
 
1531
+ // Build the interactive prompter only when the caller didn't inject one — so a
1532
+ // test never opens a real readline on stdin.
1533
+ let rl = null;
1534
+ let prompter = opts.prompter;
1535
+ if (!prompter) {
1536
+ rl = createInterface({ input: process.stdin, output: process.stdout });
1537
+ const askOnce = (q) =>
1538
+ new Promise((resolve) => rl.question(q, (answer) => resolve(answer)));
1539
+ prompter = buildReviewPrompter({ ask: askOnce, log });
1540
+ }
1541
+
1542
+ // opts.resolve is a test seam (default = the real resolver) so the error-
1543
+ // handling branches below — unreachable via normal input since tier/prompter
1544
+ // are always valid here — are coverable (Task 113).
1545
+ const resolve = opts.resolve ?? resolveReviewQueue;
1323
1546
  try {
1324
- const result = await resolveReviewQueue({
1325
- tier: 'P',
1326
- projectRoot: process.cwd(),
1327
- prompter,
1328
- });
1547
+ const result = await resolve({ tier: 'P', projectRoot, prompter });
1329
1548
  if (result.action === 'error') {
1330
- for (const e of result.errors) console.error(`cmk queue review: ${e}`);
1549
+ for (const e of result.errors) logError(`cmk queue review: ${e}`);
1331
1550
  process.exitCode = 2;
1332
- return;
1551
+ return result;
1333
1552
  }
1334
- console.log('');
1335
- console.log(
1553
+ log('');
1554
+ log(
1336
1555
  `cmk queue review: ${result.promoted} promoted, ${result.discarded} discarded, ${result.skipped} skipped${result.errors && result.errors.length ? `, ${result.errors.length} errored` : ''}`,
1337
1556
  );
1338
1557
  if (result.errors && result.errors.length) {
1339
1558
  for (const err of result.errors) {
1340
- console.error(
1341
- ` error on ${err.id} (${err.decision}): ${err.errors.join('; ')}`,
1342
- );
1559
+ logError(` error on ${err.id} (${err.decision}): ${err.errors.join('; ')}`);
1343
1560
  }
1344
1561
  }
1562
+ return result;
1345
1563
  } finally {
1346
- rl.close();
1564
+ if (rl) rl.close();
1347
1565
  }
1348
1566
  }
1349
1567
 
@@ -1412,7 +1630,7 @@ export const subcommands = [
1412
1630
  name: 'remember',
1413
1631
  description: 'capture a durable fact (Poison_Guard + home-path abstraction). Terse → a MEMORY.md bullet; RICH (--why/--how/--type) → a granular fact file with rationale (the safe way to capture richly).',
1414
1632
  milestone: 24,
1415
- argSpec: [{ flags: '<text...>', description: 'the fact to remember' }],
1633
+ argSpec: [{ flags: '[text...]', description: 'the fact to remember (omit when using --from-file)' }],
1416
1634
  optionSpec: [
1417
1635
  { flags: '--tier <tier>', description: 'P (default; U/L are v0.1.x)' },
1418
1636
  { flags: '--trust <level>', description: 'high | medium | low (default: high)' },
@@ -1423,6 +1641,8 @@ export const subcommands = [
1423
1641
  { flags: '--type <type>', description: 'rich: feedback | project | reference | user (default: feedback)' },
1424
1642
  { flags: '--title <text>', description: 'rich: a short title (also the fact-file slug)' },
1425
1643
  { flags: '--links <a,b>', description: 'rich: related fact names for [[cross-links]]' },
1644
+ { flags: '--from-file <path>', description: 'rich: read the fact as a JSON object from a file — shell-safe (content never touches argv; the safe way to capture backtick/quote-heavy Why/How). JSON keys: text (required), why, how, type, title, links. Self-contained — other flags are ignored.' },
1645
+ { flags: '--json', description: 'rich: read the fact as a JSON object from stdin (pipe-safe, shell-safe) — same JSON keys as --from-file' },
1426
1646
  ],
1427
1647
  action: runRemember,
1428
1648
  },
@@ -1441,6 +1661,41 @@ export const subcommands = [
1441
1661
  ],
1442
1662
  action: runSearch,
1443
1663
  },
1664
+ {
1665
+ name: 'get',
1666
+ description: 'fetch full observation bodies + provenance by ID (parity with the mk_get MCP tool)',
1667
+ milestone: 108,
1668
+ argSpec: [{ flags: '<ids...>', description: 'one or more citation IDs (e.g. P-S79MJHFN)' }],
1669
+ action: runGet,
1670
+ },
1671
+ {
1672
+ name: 'timeline',
1673
+ description: 'sequential context around an anchor observation — N before + N after (mk_timeline parity)',
1674
+ milestone: 108,
1675
+ argSpec: [{ flags: '<anchor>', description: 'citation ID to anchor the timeline on' }],
1676
+ optionSpec: [
1677
+ { flags: '--before <n>', description: 'observations before the anchor (default: 5)' },
1678
+ { flags: '--after <n>', description: 'observations after the anchor (default: 5)' },
1679
+ ],
1680
+ action: runTimeline,
1681
+ },
1682
+ {
1683
+ name: 'cite',
1684
+ description: 'render the canonical Markdown citation link for an observation (mk_cite parity)',
1685
+ milestone: 108,
1686
+ argSpec: [{ flags: '<id>', description: 'citation ID' }],
1687
+ action: runCite,
1688
+ },
1689
+ {
1690
+ name: 'recent-activity',
1691
+ description: 'list recent observation changes within a time window (mk_recent_activity parity)',
1692
+ milestone: 108,
1693
+ optionSpec: [
1694
+ { flags: '--window <w>', description: '1h | 24h | 7d (default: 24h)' },
1695
+ { flags: '--limit <n>', description: 'max results (default: 20)' },
1696
+ ],
1697
+ action: runRecentActivity,
1698
+ },
1444
1699
  {
1445
1700
  name: 'reindex',
1446
1701
  description: 'rebuild the markdown INDEX.md pointer index for the project tier',