counterparts 0.3.12 → 0.3.13

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 (116) hide show
  1. package/CHANGELOG.md +184 -0
  2. package/docs/module-map.md +1 -1
  3. package/package.json +1 -1
  4. package/src/adapters/claude-code/CONTRACT.md +36 -10
  5. package/src/adapters/claude-code/INTERFACE-GAPS.md +116 -16
  6. package/src/adapters/claude-code/NOTES.md +131 -0
  7. package/src/adapters/claude-code/bin/hook.ts +35 -2
  8. package/src/adapters/claude-code/doctor.ts +155 -21
  9. package/src/adapters/claude-code/hooks.ts +49 -7
  10. package/src/adapters/claude-code/index.ts +11 -0
  11. package/src/adapters/claude-code/night-run.ts +133 -12
  12. package/src/adapters/claude-code/transcript.ts +268 -1
  13. package/src/adapters/cli/CONTRACT.md +5 -0
  14. package/src/adapters/cli/INTERFACE-GAPS.md +8 -4
  15. package/src/adapters/cli/NOTES.md +111 -0
  16. package/src/adapters/cli/commands.ts +65 -14
  17. package/src/adapters/cli/dream-core.ts +57 -42
  18. package/src/adapters/cli/export.ts +320 -21
  19. package/src/adapters/cli/index.ts +1 -0
  20. package/src/adapters/cli/install.ts +39 -7
  21. package/src/adapters/cli/mechanisms.ts +7 -1
  22. package/src/adapters/cli/uninstall.ts +13 -5
  23. package/src/adapters/cli/wire.ts +189 -63
  24. package/src/adapters/dashboard/INTERFACE-GAPS.md +10 -1
  25. package/src/adapters/dashboard/NOTES.md +79 -0
  26. package/src/adapters/dashboard/registries.ts +10 -2
  27. package/src/adapters/dashboard/web/README.md +19 -14
  28. package/src/adapters/dashboard/web/actions.ts +23 -1
  29. package/src/adapters/dashboard/web/flow.ts +4 -1
  30. package/src/adapters/dashboard/web/lanes.ts +3 -0
  31. package/src/adapters/dashboard/web/narrate.ts +30 -2
  32. package/src/adapters/dashboard/web/pages/flow/sections/fired.js +1 -0
  33. package/src/adapters/dashboard/web/pages/memories/memories.css +17 -4
  34. package/src/adapters/dashboard/web/pages/memories/row.js +37 -21
  35. package/src/adapters/dashboard/web/pages/memories/sections/search.js +279 -60
  36. package/src/adapters/dashboard/web/pages/memories/state.js +2 -1
  37. package/src/adapters/dashboard/web/server.ts +0 -5
  38. package/src/adapters/dashboard/web/shared/memory-marks.css +7 -0
  39. package/src/adapters/dashboard/web/shared/memory-marks.js +33 -0
  40. package/src/adapters/dashboard/web/shared/memory-modal.js +5 -3
  41. package/src/adapters/dashboard/web/views/health.ts +1 -1
  42. package/src/adapters/dashboard/web/views/search.ts +4 -45
  43. package/src/adapters/dashboard/web/views.ts +2 -2
  44. package/src/adapters/fired.ts +410 -25
  45. package/src/adapters/host-wiring.ts +300 -0
  46. package/src/adapters/lifecycle.ts +14 -0
  47. package/src/adapters/log/index.ts +2 -0
  48. package/src/adapters/mcp/CONTRACT.md +33 -3
  49. package/src/adapters/mcp/INTERFACE-GAPS.md +24 -2
  50. package/src/adapters/mcp/NOTES.md +136 -0
  51. package/src/adapters/mcp/bin/serve.ts +30 -0
  52. package/src/adapters/mcp/facts.ts +13 -1
  53. package/src/adapters/mcp/meaning.ts +208 -13
  54. package/src/adapters/mcp/server.ts +87 -15
  55. package/src/adapters/mcp/stdio.ts +3 -1
  56. package/src/adapters/mcp/stood-down.ts +51 -0
  57. package/src/adapters/mcp/tools.ts +27 -7
  58. package/src/adapters/plugin-run.sh +133 -0
  59. package/src/adapters/plugin.ts +329 -0
  60. package/src/core/associate/CONTRACT.md +2 -1
  61. package/src/core/associate/INTERFACE-GAPS.md +6 -1
  62. package/src/core/associate/NOTES.md +45 -0
  63. package/src/core/briefing.ts +12 -3
  64. package/src/core/contradictions.ts +184 -0
  65. package/src/core/counterpart.ts +451 -18
  66. package/src/core/dream/CONTRACT.md +5 -2
  67. package/src/core/dream/INTERFACE-GAPS.md +29 -6
  68. package/src/core/dream/NOTES.md +21 -0
  69. package/src/core/dream/index.ts +33 -1
  70. package/src/core/dream/reflect.ts +20 -5
  71. package/src/core/encode/INTERFACE-GAPS.md +3 -1
  72. package/src/core/fit/INTERFACE-GAPS.md +8 -2
  73. package/src/core/fit/NOTES.md +5 -2
  74. package/src/core/fit/index.ts +4 -1
  75. package/src/core/handoff/CONTRACT.md +37 -2
  76. package/src/core/handoff/INTERFACE-GAPS.md +10 -0
  77. package/src/core/handoff/NOTES.md +66 -0
  78. package/src/core/handoff/index.ts +214 -12
  79. package/src/core/mint.ts +8 -2
  80. package/src/core/physics/CONTRACT.md +11 -1
  81. package/src/core/physics/NOTES.md +30 -0
  82. package/src/core/physics/index.ts +26 -4
  83. package/src/core/prospective/CONTRACT.md +45 -6
  84. package/src/core/prospective/INTERFACE-GAPS.md +4 -0
  85. package/src/core/prospective/NOTES.md +138 -0
  86. package/src/core/prospective/derive.ts +33 -5
  87. package/src/core/prospective/index.ts +90 -5
  88. package/src/core/prospective/windows.ts +72 -1
  89. package/src/core/recall/CONTRACT.md +6 -2
  90. package/src/core/recall/INTERFACE-GAPS.md +28 -4
  91. package/src/core/recall/NOTES.md +57 -1
  92. package/src/core/recall/probe.ts +58 -0
  93. package/src/core/recall/session.ts +27 -3
  94. package/src/core/recall/time-ask.ts +116 -18
  95. package/src/core/remember/CONTRACT.md +4 -1
  96. package/src/core/remember/INTERFACE-GAPS.md +9 -1
  97. package/src/core/remember/proposals.ts +38 -5
  98. package/src/core/revision.ts +5 -0
  99. package/src/core/schemas/CONTRACT.md +13 -1
  100. package/src/core/schemas/INTERFACE-GAPS.md +20 -2
  101. package/src/core/schemas/NOTES.md +127 -0
  102. package/src/core/schemas/types.ts +2 -0
  103. package/src/core/self/CONTRACT.md +15 -4
  104. package/src/core/self/INTERFACE-GAPS.md +7 -2
  105. package/src/core/self/NOTES.md +49 -0
  106. package/src/core/self/briefing.ts +12 -2
  107. package/src/core/self/index.ts +82 -8
  108. package/src/core/self/page.ts +88 -0
  109. package/src/core/sleep/INTERFACE-GAPS.md +22 -3
  110. package/src/core/sleep/NOTES.md +11 -0
  111. package/src/core/sleep/prune.ts +6 -2
  112. package/src/core/store/NOTES.md +14 -0
  113. package/src/core/store/index.ts +107 -0
  114. package/src/core/time.ts +152 -0
  115. package/tools/install-loop/run.sh +48 -12
  116. package/src/adapters/dashboard/web/pages/memories/fold.js +0 -53
@@ -20,6 +20,8 @@
20
20
  * are already built; only the source was borrowed).
21
21
  */
22
22
  import type { RenderFn } from "./sleep/index.js";
23
+ import { readableRecurrence } from "./time.js";
24
+ import type { Recurrence } from "./time.js";
23
25
 
24
26
  /** Structurally `Self`, so this file imports no class. */
25
27
  export interface BriefingRenderer {
@@ -33,7 +35,9 @@ export interface BriefingRenderer {
33
35
 
34
36
  /** Structurally `Prospective.horizon()`. */
35
37
  export interface HorizonSource {
36
- horizon(input: { at: string; day?: number }): { items: readonly { memoryId: string; eventDate?: string }[] };
38
+ horizon(input: { at: string; day?: number }): {
39
+ items: readonly { memoryId: string; eventDate?: string; anchor?: string; recurring?: Recurrence }[];
40
+ };
37
41
  }
38
42
 
39
43
  export interface RendererOptions {
@@ -60,8 +64,13 @@ export function selfRenderer(self: BriefingRenderer, opts: RendererOptions = {})
60
64
  ? undefined
61
65
  : opts.prospective
62
66
  .horizon({ at: opts.at, day: ctx.day })
63
- // The date it is due rides along, so the line can say it (2026-10-01).
64
- .items.map((i) => (i.eventDate === undefined ? { id: i.memoryId } : { id: i.memoryId, due: i.eventDate }));
67
+ // The date it is due rides along, so the line can say it (2026-10-01),
68
+ // and how often it comes round when it repeats (2026-10-09).
69
+ .items.map((i) => {
70
+ if (i.eventDate === undefined) return { id: i.memoryId };
71
+ const every = i.anchor === undefined || i.recurring === undefined ? "" : readableRecurrence(i.anchor, i.recurring);
72
+ return every === "" ? { id: i.memoryId, due: i.eventDate } : { id: i.memoryId, due: i.eventDate, every };
73
+ });
65
74
  const result = self.boundary({
66
75
  day: ctx.day,
67
76
  budgetBytes: ctx.budgetBytes,
@@ -41,6 +41,21 @@
41
41
  * memories of one just written, above a similarity bar, handed back with the
42
42
  * write so the writer can settle there and then. No model call.
43
43
  *
44
+ * **A write's word is checked before it settles** (`updateRelatedness`,
45
+ * 2026-10-09). A `changed` or `corrected` declared by id moves the old
46
+ * memory on the writer's word alone, and a weaker writer points at the wrong
47
+ * one: in a 10-02 benchmark read, a Haiku writer named an unrelated memory in
48
+ * about two thirds of 149 pairs (a mole removal retired a passport name
49
+ * change). So the pair is read first — by meaning when the embedder can read
50
+ * both, by shared content words when it cannot — and a pair that looks
51
+ * unrelated is HELD: the new memory lands, the old one is not settled and
52
+ * not linked, the writer is shown the old one's words and asked to settle it
53
+ * itself if it meant to (`HELD_HINT`), and the hold is recorded
54
+ * (`CONTRADICTION_HELD_EVENT`). No pair is written: an unrelated pair shown
55
+ * as a disagreement would be noise. `Counterpart#deposit` decides when the
56
+ * guard applies (a close or a redate goes straight through); this file reads
57
+ * the pair. No model call.
58
+ *
44
59
  * Arithmetic and bookkeeping only. Refusals are RETURNED with a reason and a
45
60
  * sentence, never thrown; an observer writes nothing and says so.
46
61
  */
@@ -87,6 +102,21 @@ export const CONTRADICTION_TUNABLES = {
87
102
  EXCERPT_CHARS: 160,
88
103
  /** Most neighbours one `session_end` call lists across all its entries (review of #284, M1). */
89
104
  NEIGHBOURS_PER_CALL: 12,
105
+ /**
106
+ * THE UPDATE GUARD's meaning bar (2026-10-09): the embedder's cosine
107
+ * between a write and the memory it says it changes or corrects, below
108
+ * which the pair looks unrelated and the settle is held. Calibrated on 120
109
+ * labelled pairs (61 related, 59 not: the demo store's script and
110
+ * hand-written life and work facts) under potion-base-8M: at 0.30, 50 of 61
111
+ * related passed and 58 of 59 unrelated were held (the one that passed, at
112
+ * 0.37, is two demo notes about reading screens). Unrelated pairs topped
113
+ * out at 0.29; a short mole/passport pair scored 0.28. CAL.
114
+ */
115
+ UPDATE_COSINE: 0.3,
116
+ /** Characters of a held memory's text shown back to the writer: enough to judge by. */
117
+ HELD_TEXT_CHARS: 600,
118
+ /** Most rows `heldCorrections` reads of each name (holds newest first). */
119
+ HELD_READ_ROWS: 5000,
90
120
  } as const;
91
121
 
92
122
  /** The archive reason a `corrected` memory carries. */
@@ -96,11 +126,17 @@ export const CORRECTED_REASON = "corrected";
96
126
  export const CONTRADICTION_FLAGGED_EVENT = "contradiction.flagged";
97
127
  export const CONTRADICTION_SETTLED_EVENT = "contradiction.settled";
98
128
  export const CONTRADICTION_UNDONE_EVENT = "contradiction.undone";
129
+ /** A write's `updates` that looked unrelated, so nothing was settled (2026-10-09). */
130
+ export const CONTRADICTION_HELD_EVENT = "contradiction.held";
99
131
 
100
132
  /** The one line a write's result carries beside its neighbours. */
101
133
  export const NEIGHBOURS_HINT =
102
134
  "These existing memories are close to what you just wrote. If the new memory changes, corrects or disagrees with one of them, settle it now: note with settle {holds, over, how, why}.";
103
135
 
136
+ /** The same ask, for the memory a write named in `updates` that looked unrelated to it (2026-10-09). */
137
+ export const HELD_HINT =
138
+ "The memory you named in updates looks unrelated to what you just wrote (few words in common, not close in meaning), so it was left as it was: not changed, not corrected, not linked. Its title and text are here. If the new memory does change or correct it, settle it now: note with settle {holds, over, how, why}. If not, do nothing.";
139
+
104
140
  export type SettleRefusal =
105
141
  | "observer"
106
142
  | "how-unknown"
@@ -605,3 +641,151 @@ export function writeNeighbours(
605
641
  excerpt: excerptOf(s.row.body, T.EXCERPT_CHARS),
606
642
  }));
607
643
  }
644
+
645
+ // ── THE UPDATE GUARD (2026-10-09) ───────────────────────────────────────────
646
+
647
+ /**
648
+ * Words every memory shares, so sharing one says nothing: function words,
649
+ * the words a writer uses about writing ("updated", "corrected", "user"),
650
+ * and dates' words. The benchmark harness's list (`tools/longmemeval/store.ts`
651
+ * on `bench/longmemeval`, 10-02), kept as it was.
652
+ */
653
+ const GUARD_STOP: ReadonlySet<string> = new Set(
654
+ (
655
+ "the and for with that this from have has had was were are been being its it's his her hers their theirs they them " +
656
+ "she he him you your yours our ours who what when where which while about into onto over under after before again " +
657
+ "also just very more most some any all each every other another than then there here not but can could would should " +
658
+ "will did does doing done one two three new old now still only same such per via upon user user's users assistant " +
659
+ "said says say mentioned mentions told tell asked asks noted shared memory memories conversation today yesterday " +
660
+ "week weeks month months year years day days time times date dated " +
661
+ "change changed changes changing update updated updates updating correct corrected correction latest current " +
662
+ "january february march april may june july august september october november december " +
663
+ "monday tuesday wednesday thursday friday saturday sunday"
664
+ ).split(" "),
665
+ );
666
+
667
+ /**
668
+ * A text's content words, the benchmark harness's loose rule: letters only
669
+ * (numbers and dates are shared by everything), three or more long, no
670
+ * stopword and nothing in `ignore` (the owner's names, which half the store
671
+ * says), cut to a five-letter stem.
672
+ */
673
+ export function contentWords(text: string, ignore: ReadonlySet<string> = new Set()): Set<string> {
674
+ const out = new Set<string>();
675
+ for (const w of text.toLowerCase().split(/[^a-z']+/)) {
676
+ const t = w.replace(/^'+|'+$/g, "").replace(/'s$/, "");
677
+ if (t.length < 3 || GUARD_STOP.has(t) || ignore.has(t)) continue;
678
+ out.add(t.replace(/(ing|ed|es|s)$/, "").slice(0, 5));
679
+ }
680
+ return out;
681
+ }
682
+
683
+ /** How a write and the memory it `updates` read against each other. */
684
+ export interface UpdateRelatedness {
685
+ /** True: settle as declared. False: hold. */
686
+ readonly related: boolean;
687
+ /** Which reading decided: `meaning` when there were two vectors to compare, `words` otherwise. */
688
+ readonly by: "meaning" | "words";
689
+ /** The embedder's cosine, rounded, when there were two vectors. */
690
+ readonly cosine: number | null;
691
+ /** The content words the two share (stems). */
692
+ readonly shared: readonly string[];
693
+ }
694
+
695
+ /**
696
+ * IS THIS THE MEMORY THE WRITE IS ABOUT? Read by MEANING when both vectors
697
+ * are there: the cosine clears `UPDATE_COSINE` or the pair looks unrelated.
698
+ * The words do not vote then: on the labelled pairs they added nothing but
699
+ * mistakes — as a second requirement they held 20 related pairs where meaning
700
+ * alone held 11, and as a second way in they passed 12 unrelated pairs that
701
+ * shared a common word ("back", "because", "staff"). Read by WORDS when the
702
+ * embedder cannot read both (no embedder, or two widths): related when the
703
+ * two share a content word, or when the old memory has none to share. Pure.
704
+ */
705
+ export function updateRelatedness(input: {
706
+ /** The memory named in `updates`: its title and text. */
707
+ readonly over: string;
708
+ /** The memory being written: its title and text. */
709
+ readonly text: string;
710
+ readonly overVec?: readonly number[] | null;
711
+ readonly textVec?: readonly number[] | null;
712
+ /** Names that say nothing here — the owner's (`sleep/consolidate.ts#ownerNames`), any case. */
713
+ readonly ignoreNames?: readonly string[];
714
+ }): UpdateRelatedness {
715
+ const ignore = new Set((input.ignoreNames ?? []).flatMap((n) => n.toLowerCase().split(/[^a-z']+/)).filter((w) => w.length >= 3));
716
+ const a = contentWords(input.over, ignore);
717
+ const b = contentWords(input.text, ignore);
718
+ const shared = [...a].filter((w) => b.has(w));
719
+ const x = input.overVec ?? null;
720
+ const y = input.textVec ?? null;
721
+ if (x !== null && y !== null && x.length > 0 && x.length === y.length) {
722
+ const cos = cosine(x, y);
723
+ return { related: cos >= CONTRADICTION_TUNABLES.UPDATE_COSINE, by: "meaning", cosine: round(cos), shared };
724
+ }
725
+ return { related: a.size === 0 || shared.length > 0, by: "words", cosine: null, shared };
726
+ }
727
+
728
+ /** A held `updates`, as the write's result carries it: the memory, in its own words. */
729
+ export interface HeldUpdate {
730
+ /** The memory named in `updates`, left as it was. */
731
+ readonly over: string;
732
+ readonly title: string | null;
733
+ /** Its text, cut at `HELD_TEXT_CHARS`; null when this session may not read it (confidential, not the owner's). */
734
+ readonly text: string | null;
735
+ /** The settle the writer declared. */
736
+ readonly how: SettleHow;
737
+ readonly by: UpdateRelatedness["by"];
738
+ readonly cosine: number | null;
739
+ /** How many content words the two share. */
740
+ readonly shared: number;
741
+ }
742
+
743
+ /** A held memory's title and text, shaped for the writer (`HeldUpdate`). */
744
+ export function heldView(row: Pick<MemoryRow, "title" | "body" | "confidential">, owner: boolean): { title: string | null; text: string | null } {
745
+ if (row.confidential === 1 && !owner) return { title: null, text: null };
746
+ const title = row.title === null || row.title.trim().length === 0 ? lineOf({ body: row.body }) || null : row.title;
747
+ return { title, text: excerptOf(row.body, CONTRADICTION_TUNABLES.HELD_TEXT_CHARS) };
748
+ }
749
+
750
+ /**
751
+ * HOW OFTEN THE GUARD FIRED — a reader for doctor or the dashboard, which
752
+ * neither calls yet (owed: `mcp/INTERFACE-GAPS.md`). Holds since `sinceDay`,
753
+ * by which reading, and how many of them the writer settled by hand
754
+ * afterwards (a `contradiction.settled` row naming the same two memories): a
755
+ * hold later settled is most likely a related pair the guard got wrong; one
756
+ * never settled is a wrong pointer caught, or a writer that had no one to
757
+ * read its result (a write-up, the nightly catch-up).
758
+ */
759
+ export function heldCorrections(
760
+ store: Pick<Store, "eventLog">,
761
+ opts: { sinceDay?: number } = {},
762
+ ): { held: number; settledAfter: number; byMeaning: number; byWords: number } {
763
+ const limit = CONTRADICTION_TUNABLES.HELD_READ_ROWS;
764
+ const read = (raw: string | null): Record<string, unknown> => {
765
+ try {
766
+ return JSON.parse(raw ?? "{}") as Record<string, unknown>;
767
+ } catch {
768
+ return {};
769
+ }
770
+ };
771
+ // The newest holds, and every settle from the day of the oldest of them on
772
+ // (`eventLog` reads 500 rows unless told otherwise, oldest first).
773
+ const holds = store
774
+ .eventLog({ name: CONTRADICTION_HELD_EVENT, ...(opts.sinceDay === undefined ? {} : { sinceDay: opts.sinceDay }), order: "desc", limit })
775
+ .map((e) => ({ seq: e.seq, day: e.day, p: read(e.payload) }));
776
+ const from = holds.reduce((d, h) => Math.min(d, h.day), Number.POSITIVE_INFINITY);
777
+ const settles =
778
+ holds.length === 0
779
+ ? []
780
+ : store.eventLog({ name: CONTRADICTION_SETTLED_EVENT, sinceDay: from, limit }).map((e) => ({ seq: e.seq, p: read(e.payload) }));
781
+ let settledAfter = 0;
782
+ for (const h of holds) {
783
+ if (settles.some((s) => s.seq > h.seq && s.p["holds"] === h.p["holds"] && s.p["over"] === h.p["over"])) settledAfter += 1;
784
+ }
785
+ return {
786
+ held: holds.length,
787
+ settledAfter,
788
+ byMeaning: holds.filter((h) => h.p["by"] === "meaning").length,
789
+ byWords: holds.filter((h) => h.p["by"] === "words").length,
790
+ };
791
+ }