counterparts 0.3.49 → 0.3.52

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 (95) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/README.md +18 -10
  3. package/docs/QUICKSTART.md +46 -12
  4. package/docs/manual.md +2 -1
  5. package/docs/module-map.md +3 -0
  6. package/package.json +3 -2
  7. package/src/adapters/claude-code/NOTES.md +43 -0
  8. package/src/adapters/claude-code/child.ts +23 -2
  9. package/src/adapters/claude-code/doctor-growth.ts +269 -0
  10. package/src/adapters/claude-code/doctor-host.ts +19 -1
  11. package/src/adapters/claude-code/doctor-recall-steps.ts +84 -0
  12. package/src/adapters/claude-code/doctor.ts +26 -3
  13. package/src/adapters/claude-code/hooks.ts +7 -1
  14. package/src/adapters/cli/INTERFACE-GAPS.md +18 -0
  15. package/src/adapters/cli/NOTES.md +24 -0
  16. package/src/adapters/cli/commands-dashboard.ts +9 -1
  17. package/src/adapters/cli/commands-project.ts +2 -1
  18. package/src/adapters/cli/commands-readings.ts +19 -8
  19. package/src/adapters/cli/commands-verify.ts +1 -1
  20. package/src/adapters/cli/commands.ts +4 -2
  21. package/src/adapters/cli/export.ts +2 -1
  22. package/src/adapters/cli/install.ts +14 -6
  23. package/src/adapters/cli/mechanisms.ts +1 -1
  24. package/src/adapters/cli/wire.ts +22 -6
  25. package/src/adapters/dashboard/activity.ts +6 -3
  26. package/src/adapters/dashboard/registries.ts +11 -0
  27. package/src/adapters/dashboard/web/flow.ts +3 -0
  28. package/src/adapters/dashboard/web/lanes.ts +4 -0
  29. package/src/adapters/dashboard/web/narrate.ts +23 -0
  30. package/src/adapters/dashboard/web/views/activity.ts +4 -2
  31. package/src/adapters/dashboard/web/views/flow-view.ts +6 -0
  32. package/src/adapters/dashboard/web/views/memory.ts +13 -0
  33. package/src/adapters/dashboard/web/views/shared.ts +2 -1
  34. package/src/adapters/fired.ts +113 -11
  35. package/src/adapters/host-wiring.ts +3 -1
  36. package/src/adapters/lifecycle.ts +36 -9
  37. package/src/adapters/mcp/CONTRACT.md +22 -1
  38. package/src/adapters/mcp/server-args.ts +51 -0
  39. package/src/adapters/mcp/server-session-end.ts +8 -2
  40. package/src/adapters/mcp/server-words.ts +14 -1
  41. package/src/adapters/mcp/server.ts +117 -7
  42. package/src/adapters/mcp/tools.ts +4 -3
  43. package/src/adapters/plugin.ts +1 -0
  44. package/src/adapters/presence.ts +74 -0
  45. package/src/adapters/runtime.ts +258 -8
  46. package/src/adapters/scopes.ts +3 -2
  47. package/src/adapters/spawn.ts +3 -0
  48. package/src/adapters/update-check.ts +1 -1
  49. package/src/adapters/windows-shims.mjs +102 -0
  50. package/src/core/close-hint.ts +297 -0
  51. package/src/core/counterpart-boundary.ts +15 -0
  52. package/src/core/counterpart-deposit.ts +24 -3
  53. package/src/core/counterpart-events.ts +14 -0
  54. package/src/core/counterpart-types.ts +6 -0
  55. package/src/core/counterpart-wake-shape.ts +88 -19
  56. package/src/core/counterpart.ts +143 -7
  57. package/src/core/dream/index.ts +1 -1
  58. package/src/core/dream/reflect.ts +54 -34
  59. package/src/core/encode/NOTES.md +55 -0
  60. package/src/core/encode/secrets.ts +477 -3
  61. package/src/core/event-rollup.ts +175 -0
  62. package/src/core/handoff/CONTRACT.md +7 -0
  63. package/src/core/interference.ts +496 -0
  64. package/src/core/open-close.ts +583 -0
  65. package/src/core/prospective/CONTRACT.md +24 -0
  66. package/src/core/prospective/derive.ts +22 -1
  67. package/src/core/prospective/index.ts +115 -1
  68. package/src/core/recall/CONTRACT.md +9 -2
  69. package/src/core/recall/NOTES.md +99 -0
  70. package/src/core/recall/activate.ts +83 -6
  71. package/src/core/recall/index.ts +75 -5
  72. package/src/core/recall/probe.ts +134 -0
  73. package/src/core/recall/steps.ts +84 -0
  74. package/src/core/recall/tunables.ts +11 -0
  75. package/src/core/reconsolidation.ts +14 -4
  76. package/src/core/remember/CONTRACT.md +2 -1
  77. package/src/core/remember/NOTES.md +15 -0
  78. package/src/core/remember/spans.ts +44 -6
  79. package/src/core/self/NOTES.md +21 -0
  80. package/src/core/self/work.ts +9 -5
  81. package/src/core/sleep/CONTRACT.md +36 -2
  82. package/src/core/sleep/NOTES.md +35 -0
  83. package/src/core/sleep/close.ts +77 -0
  84. package/src/core/sleep/cycle.ts +44 -0
  85. package/src/core/sleep/index.ts +4 -0
  86. package/src/core/sleep/interfere.ts +73 -0
  87. package/src/core/sleep/log.ts +27 -2
  88. package/src/core/sleep/tunables.ts +19 -0
  89. package/src/core/sleep/types.ts +20 -0
  90. package/src/core/store/index.ts +157 -10
  91. package/src/core/store/operational.ts +22 -0
  92. package/src/core/store/owner-op-seam.ts +16 -0
  93. package/src/core/store/reads-history.ts +173 -7
  94. package/src/core/store/reads-memories.ts +253 -4
  95. package/src/core/store/shapes.ts +8 -0
@@ -244,11 +244,14 @@ export function planCandidates(store: Store, input: {
244
244
  .map((r) => r.id);
245
245
  }
246
246
 
247
- export function datedMemories(store: Store, from: string, to: string, opts: { archived?: boolean } = {}): DatedMemory[] {
247
+ export function datedMemories(store: Store, from: string, to: string, opts: { archived?: boolean; since?: string } = {}): DatedMemory[] {
248
+ // `since` (round 3 of #780): a lexical floor on the stored date, for a caller
249
+ // that knows nothing older can overlap its window. Absent: every dated row.
248
250
  const rows = store["ops"].all<{ id: string; event_date: string }>(
249
251
  `SELECT id, event_date FROM memories
250
- WHERE event_date IS NOT NULL AND event_date <= ?${opts.archived === true ? "" : " AND archived = 0"}`,
252
+ WHERE event_date IS NOT NULL AND event_date <= ?${opts.since === undefined ? "" : " AND event_date >= ?"}${opts.archived === true ? "" : " AND archived = 0"}`,
251
253
  `${to}~`,
254
+ ...(opts.since === undefined ? [] : [opts.since]),
252
255
  );
253
256
  return rows
254
257
  .filter((r) => calendarOverlaps(r.event_date, from, to))
@@ -414,6 +417,103 @@ export function supersededPairs(store: Store): { id: string; by: string }[] {
414
417
  .map((r) => ({ id: r.id, by: r.by }));
415
418
  }
416
419
 
420
+ /** One live work memory as the interference sweep reads it (`workEventRows`). Columns only. */
421
+ export interface WorkEventRow {
422
+ readonly id: string;
423
+ readonly title: string | null;
424
+ /** The first 121 characters of the words, no more: the headline's room. */
425
+ readonly body: string;
426
+ readonly scope: string;
427
+ readonly kind: string;
428
+ readonly band: string;
429
+ readonly birthDay: number;
430
+ readonly createdAt: number;
431
+ readonly status: string | null;
432
+ readonly saidBy: string | null;
433
+ readonly uses: number;
434
+ readonly protected: boolean;
435
+ readonly promoted: boolean;
436
+ readonly eventDate: string | null;
437
+ readonly meta: string;
438
+ readonly novelty: number | null;
439
+ readonly relevance: number;
440
+ readonly emotional: number;
441
+ readonly predictive: number;
442
+ readonly claimed: number | null;
443
+ readonly belief: boolean;
444
+ /** The fade multiplier a settle (or an earlier night) set; 1 when none. */
445
+ readonly fade: number;
446
+ }
447
+
448
+ /**
449
+ * THE LIVE WORK MEMORIES A SESSION WROTE IN A FOLDER (2026-11-07, FD1 step 3):
450
+ * `about = 'work'`, `source = 'authored'`, a folder, words, not archived or
451
+ * replaced — what the night's interference sweep reads (`interference.ts`),
452
+ * oldest first. One read of columns, and of the words only their first 121
453
+ * characters (the headline's room, `interference.ts#headline`); the sweep's
454
+ * guards judge each row.
455
+ */
456
+ export function workEventRows(store: Store): WorkEventRow[] {
457
+ const v14 = store.hasColumn("memories", "birth_route");
458
+ return store["ops"]
459
+ .all<{
460
+ id: string;
461
+ title: string | null;
462
+ body: string;
463
+ origin_scope: string;
464
+ kind: string;
465
+ band: string;
466
+ birth_day: number;
467
+ created_at: number | null;
468
+ status: string | null;
469
+ said_by: string | null;
470
+ uses: number;
471
+ protected: number;
472
+ promoted_identity: number;
473
+ event_date: string | null;
474
+ meta: string;
475
+ novelty: number | null;
476
+ relevance: number;
477
+ emotional: number;
478
+ predictive: number;
479
+ claimed: number | null;
480
+ birth_route: string | null;
481
+ fade: number | null;
482
+ }>(
483
+ `SELECT id, title, substr(body, 1, 121) AS body, origin_scope, kind, band, birth_day, created_at, status, said_by, uses,
484
+ protected, promoted_identity, event_date, meta, novelty, relevance, emotional, predictive, claimed, fade,
485
+ ${v14 ? "birth_route" : "NULL AS birth_route"}
486
+ FROM memories
487
+ WHERE type = 'memory' AND archived = 0 AND superseded_by IS NULL AND about = 'work'
488
+ AND source = 'authored' AND origin_scope IS NOT NULL AND origin_scope != '' AND body != ''
489
+ ORDER BY birth_day, created_at, id`,
490
+ )
491
+ .map((r) => ({
492
+ id: r.id,
493
+ title: r.title,
494
+ body: r.body,
495
+ scope: r.origin_scope,
496
+ kind: r.kind,
497
+ band: r.band,
498
+ birthDay: r.birth_day,
499
+ createdAt: r.created_at ?? 0,
500
+ status: r.status,
501
+ saidBy: r.said_by,
502
+ uses: r.uses,
503
+ protected: r.protected === 1,
504
+ promoted: r.promoted_identity === 1,
505
+ eventDate: r.event_date,
506
+ meta: r.meta,
507
+ novelty: r.novelty,
508
+ relevance: r.relevance,
509
+ emotional: r.emotional,
510
+ predictive: r.predictive,
511
+ claimed: r.claimed,
512
+ belief: r.birth_route !== null,
513
+ fade: typeof r.fade === "number" && r.fade > 0 && r.fade < 1 ? r.fade : 1,
514
+ }));
515
+ }
516
+
417
517
  /**
418
518
  * `remember/own-plumbing.ts#OWN_PLUMBING_META_KEY` (TC1), spelled here as the
419
519
  * store imports no module above it; a test holds the spellings equal.
@@ -539,8 +639,11 @@ function openTouchedSql(glimpses: boolean): string {
539
639
  */
540
640
  export function openThreadTouches(
541
641
  store: Store,
542
- opts: { glimpses?: boolean; at?: string } = {},
642
+ opts: { glimpses?: boolean; at?: string; ids?: readonly string[] } = {},
543
643
  ): { id: string; touchedDay: number; dated: boolean; asked: boolean; settles: boolean }[] {
644
+ // Only these ids, when given (SO1 step 1b: a write's few matches, not every open item).
645
+ const only = opts.ids === undefined ? null : [...new Set(opts.ids)];
646
+ if (only !== null && only.length === 0) return [];
544
647
  const settles = `CASE WHEN json_valid(m.meta) AND json_type(m.meta, '$.${SETTLES_WHEN_META_KEY}') = 'text'
545
648
  AND TRIM(json_extract(m.meta, '$.${SETTLES_WHEN_META_KEY}')) <> '' THEN 1 ELSE 0 END`;
546
649
  const at = opts.at !== undefined && isDay(opts.at) ? opts.at : null;
@@ -554,8 +657,9 @@ export function openThreadTouches(
554
657
  `SELECT m.id AS id, m.event_date AS event_date, m.status AS status, ${openTouchedSql(opts.glimpses === true)} AS touched,
555
658
  ${settles} AS settles, CASE WHEN m.meta LIKE '%"${RECURRING_META}":"%' THEN 1 ELSE 0 END AS recurring
556
659
  FROM memories m
557
- WHERE m.type = 'memory' AND m.archived = 0 AND m.meta LIKE '%"unresolved":true%'
660
+ WHERE m.type = 'memory' AND m.archived = 0 AND m.meta LIKE '%"unresolved":true%'${only === null ? "" : ` AND m.id IN (${only.map(() => "?").join(", ")})`}
558
661
  ORDER BY m.birth_day, m.id`,
662
+ ...(only ?? []),
559
663
  )
560
664
  .map((r) => ({
561
665
  id: r.id,
@@ -566,6 +670,72 @@ export function openThreadTouches(
566
670
  }));
567
671
  }
568
672
 
673
+ /**
674
+ * EVERY OPEN ITEM'S HEAD, IN ONE READ (2026-11-07, SO1 step 1b): what a
675
+ * write's close hint (`core/close-hint.ts`) matches a new memory against —
676
+ * the title, the first `bodyChars` of the words (a headline's worth), the
677
+ * meta (the flag and `settlesWhen`), the folder that wrote it and its
678
+ * status. Live, ordinary memories flagged `unresolved` only (the
679
+ * `memories_open` index's own clause), never the night's own writing, a
680
+ * starter memory, an identity element or a protected row (its close would
681
+ * be refused). Newest first. The caller confirms the flag on the parsed meta.
682
+ */
683
+ export function openItemHeads(
684
+ store: Store,
685
+ bodyChars: number,
686
+ ): {
687
+ id: string;
688
+ type: string;
689
+ title: string | null;
690
+ body: string;
691
+ meta: string;
692
+ scope: string | null;
693
+ origin_scope: string | null;
694
+ origin_ref: string | null;
695
+ source: string | null;
696
+ status: string | null;
697
+ createdAt: number;
698
+ }[] {
699
+ // `memories_open` by name (review of #783): the planner otherwise took
700
+ // `memories_birth` and walked every live row.
701
+ return store["ops"]
702
+ .all<{
703
+ id: string;
704
+ type: string;
705
+ title: string | null;
706
+ body: string;
707
+ meta: string | null;
708
+ origin_scope: string | null;
709
+ origin_ref: string | null;
710
+ source: string | null;
711
+ status: string | null;
712
+ created_at: number | null;
713
+ }>(
714
+ `SELECT m.id AS id, m.type AS type, m.title AS title, substr(m.body, 1, ?) AS body, m.meta AS meta,
715
+ m.origin_scope AS origin_scope, m.origin_ref AS origin_ref, m.source AS source,
716
+ m.status AS status, m.created_at AS created_at
717
+ FROM memories m INDEXED BY memories_open
718
+ WHERE m.archived = 0 AND m.meta LIKE '%"unresolved":true%'
719
+ AND m.type = 'memory' AND m.superseded_by IS NULL AND m.band <> 'identity' AND m.protected = 0
720
+ AND COALESCE(m.source, '') <> 'told' AND NOT ${NIGHTLY_SQL}
721
+ ORDER BY COALESCE(m.created_at, 0) DESC, m.id DESC`,
722
+ Math.max(1, Math.floor(bodyChars)),
723
+ )
724
+ .map((r) => ({
725
+ id: r.id,
726
+ type: r.type,
727
+ title: r.title,
728
+ body: r.body,
729
+ meta: r.meta ?? "{}",
730
+ scope: r.origin_scope,
731
+ origin_scope: r.origin_scope,
732
+ origin_ref: r.origin_ref,
733
+ source: r.source,
734
+ status: r.status,
735
+ createdAt: r.created_at ?? 0,
736
+ }));
737
+ }
738
+
569
739
  /**
570
740
  * THIS SESSION'S OWN OPEN THREADS (2026-10-22; the review of #592, F1): the
571
741
  * open memories it WROTE (`origin_session`) or USED — a return credited to
@@ -803,6 +973,85 @@ export function belowReachCount(store: Store): number {
803
973
  return n;
804
974
  }
805
975
 
976
+ /**
977
+ * WHERE THE LIVE MEMORIES STAND AGAINST THE CURVE (FD1 step 2, 2026-11-07):
978
+ * the population `belowReachCount` reads (live, not a chapter, not exempt from
979
+ * reach), as the last decay pass ranked it (box 3), split three ways: within
980
+ * reach, below reach, and of those under the prune floor (`PHI_PRUNE`, where
981
+ * the night's prune may take a memory after its wait). The identity band
982
+ * counts as within reach, as it does for `belowReachCount`. A row with no
983
+ * ranking yet (born since the last pass) is `unranked`. One pass; read-only.
984
+ */
985
+ export function reachCensus(store: Store): {
986
+ live: number;
987
+ inReach: number;
988
+ belowReach: number;
989
+ underFloor: number;
990
+ unranked: number;
991
+ /** Live rows the prune never examines, by what they are (each has its own exit, or none). */
992
+ exempt: { journal: number; chapterCopies: number; handoffs: number; cards: number; selfPage: number };
993
+ } {
994
+ const ranking = store.rankingAll();
995
+ const exempt = { journal: 0, chapterCopies: 0, handoffs: 0, cards: 0, selfPage: 0 };
996
+ const out = { live: 0, inReach: 0, belowReach: 0, underFloor: 0, unranked: 0, exempt };
997
+ for (const r of store["ops"].all<MemoryRow>("SELECT * FROM memories WHERE archived = 0")) {
998
+ if (r.type === "episode") {
999
+ exempt.journal += 1;
1000
+ continue;
1001
+ }
1002
+ if (reachExempt(r)) {
1003
+ if (r.type === "memory") {
1004
+ exempt.chapterCopies += 1;
1005
+ } else {
1006
+ let role: unknown = null;
1007
+ try {
1008
+ role = (JSON.parse(r.meta) as { role?: unknown } | null)?.role ?? null;
1009
+ } catch {
1010
+ role = null;
1011
+ }
1012
+ if (role === "handoff") exempt.handoffs += 1;
1013
+ else if (role === "entity") exempt.cards += 1;
1014
+ else exempt.selfPage += 1;
1015
+ }
1016
+ continue;
1017
+ }
1018
+ out.live += 1;
1019
+ const rk = ranking.get(r.id);
1020
+ if (rk === undefined) out.unranked += 1;
1021
+ else if (rk.band === "identity" || rk.strength >= PHYSICS_TUNABLES.REACH) out.inReach += 1;
1022
+ else {
1023
+ out.belowReach += 1;
1024
+ if (rk.strength < PHYSICS_TUNABLES.PHI_PRUNE) out.underFloor += 1;
1025
+ }
1026
+ }
1027
+ return out;
1028
+ }
1029
+
1030
+ /**
1031
+ * MEMORIES IN AND OUT OVER A WINDOW (FD1 step 2): of the population
1032
+ * `reachCensus` calls live (not a chapter, not exempt from reach: no chapter
1033
+ * copy, handoff, card or self page, whose churn is not memories coming and
1034
+ * going), rows born on or after lived day `sinceDay`, and rows archived at or
1035
+ * after `sinceAt` (the archive sets `updated_at`): a prune, a merge, a
1036
+ * revision's old words. Only the window's rows are read. Read-only.
1037
+ *
1038
+ * A BIAS, said (review of #776): an archived row has no archive moment of its
1039
+ * own, so "left" reads `updated_at`, and a later write to an already-archived
1040
+ * row (an owner's operation on it) counts it as leaving again. Rare; a column
1041
+ * would fix it and waits for the next batched format change.
1042
+ */
1043
+ export function memoryFlow(store: Store, sinceDay: number, sinceAt: number): { born: number; archived: number } {
1044
+ const counted = (rows: MemoryRow[]): number => rows.filter((r) => !reachExempt(r)).length;
1045
+ const born = counted(store["ops"].all<MemoryRow>("SELECT * FROM memories WHERE birth_day >= ? AND type != 'episode'", sinceDay));
1046
+ const archived = counted(
1047
+ store["ops"].all<MemoryRow>(
1048
+ "SELECT * FROM memories WHERE archived = 1 AND type != 'episode' AND COALESCE(updated_at, 0) >= ?",
1049
+ sinceAt,
1050
+ ),
1051
+ );
1052
+ return { born, archived };
1053
+ }
1054
+
806
1055
  export function returnCounts(store: Store, filter: { sinceAt?: number; sinceDay?: number } = {}): {
807
1056
  awake: number;
808
1057
  dream: number;
@@ -275,6 +275,11 @@ export interface EventPruneReport extends PruneReport {
275
275
  remaining: number;
276
276
  /** The per-pass cap in force, or null when the caller set none. */
277
277
  limit: number | null;
278
+ /** FD1 step 2: the per-turn names' cutoff (`livedDay - shortRetentionDays`) and window. */
279
+ shortCutoffDay: number;
280
+ shortRetentionDays: number;
281
+ /** `events.rolled` rows this pass wrote, one per lived day rolled up. */
282
+ rolled: number;
278
283
  }
279
284
 
280
285
  /** What `eventLog` selects on. `order` defaults to `"asc"` (oldest first). */
@@ -325,6 +330,9 @@ export interface EventLogCensus {
325
330
  eligible: number;
326
331
  /** Latched rows past the window — kept by kind, and counted so "kept" is a number. */
327
332
  latchedPastCutoff: number;
333
+ /** FD1 step 2: `SHORT_RETENTION_NAMES` keep this many lived days (`livedDay - shortRetentionDays` is their cutoff); `eligible` counts them past it. */
334
+ shortCutoffDay: number;
335
+ shortRetentionDays: number;
328
336
  }
329
337
 
330
338
  export interface RebuildReport {