@remnic/core 9.69.0 → 9.69.2

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 (190) hide show
  1. package/dist/access-admin-ops-surface.js +18 -18
  2. package/dist/access-authorization-probe.js +19 -19
  3. package/dist/access-boundary.js +18 -18
  4. package/dist/access-cli.js +76 -76
  5. package/dist/access-extraction-force-flush.js +18 -18
  6. package/dist/access-http-query.js +20 -20
  7. package/dist/access-http.js +34 -34
  8. package/dist/access-identity-continuity-surface.js +18 -18
  9. package/dist/access-lcm-surface.js +18 -18
  10. package/dist/access-mcp.js +25 -25
  11. package/dist/access-namespace-preflight.js +18 -18
  12. package/dist/access-observe-write-surface.js +18 -18
  13. package/dist/access-operations-batch.js +19 -19
  14. package/dist/access-operations.js +24 -24
  15. package/dist/access-recall-concurrency.js +18 -18
  16. package/dist/access-recall-response.js +18 -18
  17. package/dist/access-recall-surface.js +18 -18
  18. package/dist/access-service.js +18 -18
  19. package/dist/access-surface-catalog.js +20 -20
  20. package/dist/adapters/index.js +4 -4
  21. package/dist/adapters/registry.js +2 -2
  22. package/dist/briefing.js +10 -10
  23. package/dist/causal-consolidation.js +11 -11
  24. package/dist/{chunk-4PEZMGZD.js → chunk-42JAVCED.js} +2 -2
  25. package/dist/{chunk-QOE3EM5Z.js → chunk-42LQMCEE.js} +8 -8
  26. package/dist/{chunk-PIBQIYF2.js → chunk-4EQBBIFH.js} +2 -2
  27. package/dist/{chunk-DJ7TZLGU.js → chunk-4FRP3R44.js} +2 -2
  28. package/dist/{chunk-25P3LW6U.js → chunk-5H5TKPBA.js} +2 -2
  29. package/dist/{chunk-L7MJJAG2.js → chunk-6FLNUDEO.js} +2 -2
  30. package/dist/{chunk-2MHMHF76.js → chunk-6LDZNG3T.js} +2 -2
  31. package/dist/{chunk-KAY2C6VX.js → chunk-7ATQD2GP.js} +2 -2
  32. package/dist/{chunk-P342D5BP.js → chunk-AEZCLP3C.js} +2 -2
  33. package/dist/{chunk-4PMPOZK3.js → chunk-AF67CHFU.js} +2 -2
  34. package/dist/{chunk-YYAWJRGL.js → chunk-AI6TT4Y3.js} +2 -2
  35. package/dist/{chunk-VLSUKXCJ.js → chunk-B6D3O5X2.js} +3 -3
  36. package/dist/{chunk-5UI7WQA6.js → chunk-C7KYMSL5.js} +2 -2
  37. package/dist/{chunk-BS5BD3DF.js → chunk-CCKIHXU5.js} +2 -2
  38. package/dist/{chunk-KJUH3QIQ.js → chunk-DKTIW44X.js} +2 -2
  39. package/dist/{chunk-3IRH5MYM.js → chunk-DMR6GVTW.js} +5 -5
  40. package/dist/{chunk-USQAH6PN.js → chunk-F6B3RUR4.js} +2 -2
  41. package/dist/{chunk-XZBXINXZ.js → chunk-GIC6BASP.js} +4 -4
  42. package/dist/{chunk-BGD3T743.js → chunk-GPTZFSMQ.js} +2 -2
  43. package/dist/{chunk-327JMA2N.js → chunk-IJCC5TSX.js} +2 -2
  44. package/dist/{chunk-NYQHD7H4.js → chunk-J7RZ4NJO.js} +11 -11
  45. package/dist/{chunk-6FUMA6AA.js → chunk-JAQXUGLN.js} +2 -2
  46. package/dist/{chunk-RKNJBZ55.js → chunk-JBPKEARU.js} +4 -4
  47. package/dist/{chunk-FOT5GP2U.js → chunk-JC5GCR3K.js} +2 -2
  48. package/dist/{chunk-363VZTVH.js → chunk-JU5SBSV7.js} +2 -2
  49. package/dist/{chunk-WQCET3N3.js → chunk-KXJ3IDD5.js} +2 -2
  50. package/dist/{chunk-YFK64FYK.js → chunk-LSGQRUTX.js} +2 -2
  51. package/dist/{chunk-X2NFFBTO.js → chunk-LVFI5LG7.js} +10 -10
  52. package/dist/{chunk-ABPCF4MW.js → chunk-LW5DFCGW.js} +1 -1
  53. package/dist/{chunk-KRNFJKMH.js → chunk-M73TXJJW.js} +2 -2
  54. package/dist/{chunk-RACZB5HT.js → chunk-N37X5RU7.js} +4 -4
  55. package/dist/{chunk-AG46P24C.js → chunk-NNBZS3DY.js} +4 -4
  56. package/dist/{chunk-HDQQRRCZ.js → chunk-PSFQ4HDM.js} +3 -3
  57. package/dist/{chunk-D35XLYQ7.js → chunk-RGCF4XNH.js} +9 -9
  58. package/dist/{chunk-F25CL3IQ.js → chunk-RJ7NOBKA.js} +2 -2
  59. package/dist/{chunk-A7NMS5II.js → chunk-RSPIDEY3.js} +3 -3
  60. package/dist/{chunk-ND6NL2YR.js → chunk-S7WPER23.js} +2 -2
  61. package/dist/{chunk-52GQNSLC.js → chunk-SB7PMYWV.js} +7 -7
  62. package/dist/{chunk-WSYACIKD.js → chunk-THWUR7JO.js} +2 -2
  63. package/dist/{chunk-BZ26443Y.js → chunk-TL2THUFH.js} +101 -53
  64. package/dist/chunk-TL2THUFH.js.map +1 -0
  65. package/dist/{chunk-XW3G4BJI.js → chunk-TV52QFXK.js} +7 -7
  66. package/dist/{chunk-ERKFG4RV.js → chunk-UXEAJ765.js} +34 -34
  67. package/dist/{chunk-ZVW724ZK.js → chunk-WFHBC4BQ.js} +2 -2
  68. package/dist/{chunk-7KG5V4VV.js → chunk-WJPSCTZY.js} +2 -2
  69. package/dist/{chunk-BHH736UQ.js → chunk-WKZSKFZC.js} +3 -3
  70. package/dist/{chunk-NFITGYMQ.js → chunk-XCVGQKTR.js} +3 -3
  71. package/dist/{chunk-BTRY6UH3.js → chunk-YJLGCUKZ.js} +2 -2
  72. package/dist/{chunk-Z2FXSE5P.js → chunk-YQPVXUJG.js} +2 -2
  73. package/dist/{chunk-ZDJMCXC7.js → chunk-ZBGODUSY.js} +12 -12
  74. package/dist/{chunk-XPDWNWH6.js → chunk-ZMVMYZMX.js} +3 -3
  75. package/dist/{chunk-I6T4SYFW.js → chunk-ZOMXROJ6.js} +3 -3
  76. package/dist/cli.js +50 -50
  77. package/dist/coding/export-okf-codegraph.js +10 -10
  78. package/dist/compounding/engine.js +10 -10
  79. package/dist/connectors/codex-materialize-runner.js +10 -10
  80. package/dist/connectors/index.js +10 -10
  81. package/dist/convergence-refresh.js +12 -12
  82. package/dist/corpus-watermark.js +12 -12
  83. package/dist/entity-retrieval.js +10 -10
  84. package/dist/external-wiki-access.js +19 -19
  85. package/dist/extraction.js +10 -10
  86. package/dist/harmonic-retrieval.js +1 -1
  87. package/dist/index.d.ts +22 -1
  88. package/dist/index.js +94 -88
  89. package/dist/lcm/engine.js +4 -4
  90. package/dist/lcm/index.js +13 -13
  91. package/dist/location/index.js +12 -12
  92. package/dist/maintenance/memory-governance.js +10 -10
  93. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +10 -10
  94. package/dist/maintenance/rebuild-memory-projection.js +11 -11
  95. package/dist/namespaces/migrate.js +11 -11
  96. package/dist/namespaces/search.js +7 -7
  97. package/dist/namespaces/storage.js +10 -10
  98. package/dist/operator-doctor-corpus.js +13 -13
  99. package/dist/operator-toolkit.js +18 -18
  100. package/dist/orchestration/maintenance.js +18 -18
  101. package/dist/orchestrator.js +76 -76
  102. package/dist/recall-deep-expand-select.d.ts +23 -0
  103. package/dist/recall-deep-expand-select.js +50 -0
  104. package/dist/recall-deep-expand-select.js.map +1 -0
  105. package/dist/recall-navigate-disclosure.d.ts +44 -0
  106. package/dist/recall-navigate-disclosure.js +30 -0
  107. package/dist/recall-navigate-disclosure.js.map +1 -0
  108. package/dist/recall-state-view-packet.d.ts +30 -0
  109. package/dist/recall-state-view-packet.js +69 -0
  110. package/dist/recall-state-view-packet.js.map +1 -0
  111. package/dist/search/factory.js +6 -6
  112. package/dist/search/index.js +10 -10
  113. package/dist/search/lancedb-backend.js +2 -2
  114. package/dist/search/meilisearch-backend.js +2 -2
  115. package/dist/search/orama-backend.js +2 -2
  116. package/dist/semantic-consolidation.js +11 -11
  117. package/dist/semantic-rule-promotion.js +10 -10
  118. package/dist/semantic-rule-verifier.js +10 -10
  119. package/dist/source-agent-qualifier.js +10 -10
  120. package/dist/standup/build.js +10 -10
  121. package/dist/storage.js +9 -9
  122. package/dist/support-passport/index.js +21 -21
  123. package/dist/transfer/export-okf.js +11 -11
  124. package/dist/verified-recall.js +10 -10
  125. package/dist/who-knows-cli.js +11 -11
  126. package/dist/who-knows.js +10 -10
  127. package/package.json +2 -2
  128. package/src/activity/timeline/index.ts +1 -0
  129. package/src/activity/timeline/week-recurring.test.ts +177 -0
  130. package/src/activity/timeline/week-recurring.ts +88 -0
  131. package/src/dedup/index.ts +7 -0
  132. package/src/dedup/merge-provenance.test.ts +185 -0
  133. package/src/dedup/merge-provenance.ts +151 -0
  134. package/src/recall-deep-expand-select.test.ts +163 -0
  135. package/src/recall-deep-expand-select.ts +65 -0
  136. package/src/recall-navigate-disclosure.test.ts +128 -0
  137. package/src/recall-navigate-disclosure.ts +45 -0
  138. package/src/recall-state-view-packet.test.ts +158 -0
  139. package/src/recall-state-view-packet.ts +128 -0
  140. package/dist/chunk-BZ26443Y.js.map +0 -1
  141. /package/dist/{chunk-4PEZMGZD.js.map → chunk-42JAVCED.js.map} +0 -0
  142. /package/dist/{chunk-QOE3EM5Z.js.map → chunk-42LQMCEE.js.map} +0 -0
  143. /package/dist/{chunk-PIBQIYF2.js.map → chunk-4EQBBIFH.js.map} +0 -0
  144. /package/dist/{chunk-DJ7TZLGU.js.map → chunk-4FRP3R44.js.map} +0 -0
  145. /package/dist/{chunk-25P3LW6U.js.map → chunk-5H5TKPBA.js.map} +0 -0
  146. /package/dist/{chunk-L7MJJAG2.js.map → chunk-6FLNUDEO.js.map} +0 -0
  147. /package/dist/{chunk-2MHMHF76.js.map → chunk-6LDZNG3T.js.map} +0 -0
  148. /package/dist/{chunk-KAY2C6VX.js.map → chunk-7ATQD2GP.js.map} +0 -0
  149. /package/dist/{chunk-P342D5BP.js.map → chunk-AEZCLP3C.js.map} +0 -0
  150. /package/dist/{chunk-4PMPOZK3.js.map → chunk-AF67CHFU.js.map} +0 -0
  151. /package/dist/{chunk-YYAWJRGL.js.map → chunk-AI6TT4Y3.js.map} +0 -0
  152. /package/dist/{chunk-VLSUKXCJ.js.map → chunk-B6D3O5X2.js.map} +0 -0
  153. /package/dist/{chunk-5UI7WQA6.js.map → chunk-C7KYMSL5.js.map} +0 -0
  154. /package/dist/{chunk-BS5BD3DF.js.map → chunk-CCKIHXU5.js.map} +0 -0
  155. /package/dist/{chunk-KJUH3QIQ.js.map → chunk-DKTIW44X.js.map} +0 -0
  156. /package/dist/{chunk-3IRH5MYM.js.map → chunk-DMR6GVTW.js.map} +0 -0
  157. /package/dist/{chunk-USQAH6PN.js.map → chunk-F6B3RUR4.js.map} +0 -0
  158. /package/dist/{chunk-XZBXINXZ.js.map → chunk-GIC6BASP.js.map} +0 -0
  159. /package/dist/{chunk-BGD3T743.js.map → chunk-GPTZFSMQ.js.map} +0 -0
  160. /package/dist/{chunk-327JMA2N.js.map → chunk-IJCC5TSX.js.map} +0 -0
  161. /package/dist/{chunk-NYQHD7H4.js.map → chunk-J7RZ4NJO.js.map} +0 -0
  162. /package/dist/{chunk-6FUMA6AA.js.map → chunk-JAQXUGLN.js.map} +0 -0
  163. /package/dist/{chunk-RKNJBZ55.js.map → chunk-JBPKEARU.js.map} +0 -0
  164. /package/dist/{chunk-FOT5GP2U.js.map → chunk-JC5GCR3K.js.map} +0 -0
  165. /package/dist/{chunk-363VZTVH.js.map → chunk-JU5SBSV7.js.map} +0 -0
  166. /package/dist/{chunk-WQCET3N3.js.map → chunk-KXJ3IDD5.js.map} +0 -0
  167. /package/dist/{chunk-YFK64FYK.js.map → chunk-LSGQRUTX.js.map} +0 -0
  168. /package/dist/{chunk-X2NFFBTO.js.map → chunk-LVFI5LG7.js.map} +0 -0
  169. /package/dist/{chunk-ABPCF4MW.js.map → chunk-LW5DFCGW.js.map} +0 -0
  170. /package/dist/{chunk-KRNFJKMH.js.map → chunk-M73TXJJW.js.map} +0 -0
  171. /package/dist/{chunk-RACZB5HT.js.map → chunk-N37X5RU7.js.map} +0 -0
  172. /package/dist/{chunk-AG46P24C.js.map → chunk-NNBZS3DY.js.map} +0 -0
  173. /package/dist/{chunk-HDQQRRCZ.js.map → chunk-PSFQ4HDM.js.map} +0 -0
  174. /package/dist/{chunk-D35XLYQ7.js.map → chunk-RGCF4XNH.js.map} +0 -0
  175. /package/dist/{chunk-F25CL3IQ.js.map → chunk-RJ7NOBKA.js.map} +0 -0
  176. /package/dist/{chunk-A7NMS5II.js.map → chunk-RSPIDEY3.js.map} +0 -0
  177. /package/dist/{chunk-ND6NL2YR.js.map → chunk-S7WPER23.js.map} +0 -0
  178. /package/dist/{chunk-52GQNSLC.js.map → chunk-SB7PMYWV.js.map} +0 -0
  179. /package/dist/{chunk-WSYACIKD.js.map → chunk-THWUR7JO.js.map} +0 -0
  180. /package/dist/{chunk-XW3G4BJI.js.map → chunk-TV52QFXK.js.map} +0 -0
  181. /package/dist/{chunk-ERKFG4RV.js.map → chunk-UXEAJ765.js.map} +0 -0
  182. /package/dist/{chunk-ZVW724ZK.js.map → chunk-WFHBC4BQ.js.map} +0 -0
  183. /package/dist/{chunk-7KG5V4VV.js.map → chunk-WJPSCTZY.js.map} +0 -0
  184. /package/dist/{chunk-BHH736UQ.js.map → chunk-WKZSKFZC.js.map} +0 -0
  185. /package/dist/{chunk-NFITGYMQ.js.map → chunk-XCVGQKTR.js.map} +0 -0
  186. /package/dist/{chunk-BTRY6UH3.js.map → chunk-YJLGCUKZ.js.map} +0 -0
  187. /package/dist/{chunk-Z2FXSE5P.js.map → chunk-YQPVXUJG.js.map} +0 -0
  188. /package/dist/{chunk-ZDJMCXC7.js.map → chunk-ZBGODUSY.js.map} +0 -0
  189. /package/dist/{chunk-XPDWNWH6.js.map → chunk-ZMVMYZMX.js.map} +0 -0
  190. /package/dist/{chunk-I6T4SYFW.js.map → chunk-ZOMXROJ6.js.map} +0 -0
@@ -0,0 +1,158 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+
4
+ import { buildStateEvidencePackets } from "./recall-state-view-packet.js";
5
+ import type { StateViewEntry } from "./recall-state-view-packet.js";
6
+
7
+ test("two-step predecessor chain is ordered nearest to head first", () => {
8
+ const result = buildStateEvidencePackets([
9
+ { memoryId: "head", stateLabel: "current" },
10
+ { memoryId: "old-2", stateLabel: "historical", supersededById: "old-1" },
11
+ { memoryId: "old-1", stateLabel: "historical", supersededById: "head" },
12
+ ]);
13
+ assert.deepEqual(result, {
14
+ packets: [{ headId: "head", historyIds: ["old-1", "old-2"] }],
15
+ orphanHistoryIds: [],
16
+ });
17
+ });
18
+
19
+ test("two independent packets never interleave", () => {
20
+ const result = buildStateEvidencePackets([
21
+ { memoryId: "b-head", stateLabel: "transition" },
22
+ { memoryId: "a-head", stateLabel: "current" },
23
+ { memoryId: "b-old", stateLabel: "historical", supersededById: "b-head" },
24
+ { memoryId: "a-old", stateLabel: "historical", supersededById: "a-head" },
25
+ ]);
26
+ assert.deepEqual(result, {
27
+ packets: [
28
+ { headId: "a-head", historyIds: ["a-old"] },
29
+ { headId: "b-head", historyIds: ["b-old"] },
30
+ ],
31
+ orphanHistoryIds: [],
32
+ });
33
+ });
34
+
35
+ test("historical entry pointing at an absent successor becomes an orphan", () => {
36
+ const result = buildStateEvidencePackets([
37
+ { memoryId: "head", stateLabel: "current" },
38
+ { memoryId: "dangling", stateLabel: "historical", supersededById: "missing" },
39
+ ]);
40
+ assert.deepEqual(result, {
41
+ packets: [{ headId: "head", historyIds: [] }],
42
+ orphanHistoryIds: ["dangling"],
43
+ });
44
+ });
45
+
46
+ test("historical entry with no supersededById becomes an orphan", () => {
47
+ const result = buildStateEvidencePackets([
48
+ { memoryId: "rootless", stateLabel: "historical" },
49
+ ]);
50
+ assert.deepEqual(result, {
51
+ packets: [],
52
+ orphanHistoryIds: ["rootless"],
53
+ });
54
+ });
55
+
56
+ test("supersededById cycle terminates and yields orphans", () => {
57
+ const result = buildStateEvidencePackets([
58
+ { memoryId: "head", stateLabel: "current" },
59
+ { memoryId: "loop-a", stateLabel: "historical", supersededById: "loop-b" },
60
+ { memoryId: "loop-b", stateLabel: "historical", supersededById: "loop-a" },
61
+ { memoryId: "into-loop", stateLabel: "historical", supersededById: "loop-a" },
62
+ ]);
63
+ assert.deepEqual(result, {
64
+ packets: [{ headId: "head", historyIds: [] }],
65
+ orphanHistoryIds: ["into-loop", "loop-a", "loop-b"],
66
+ });
67
+ });
68
+
69
+ test("lone current entry yields a packet with empty history", () => {
70
+ const result = buildStateEvidencePackets([
71
+ { memoryId: "solo", stateLabel: "current" },
72
+ ]);
73
+ assert.deepEqual(result, {
74
+ packets: [{ headId: "solo", historyIds: [] }],
75
+ orphanHistoryIds: [],
76
+ });
77
+ });
78
+
79
+ test("unknown state label throws TypeError listing the allowed values", () => {
80
+ assert.throws(
81
+ () =>
82
+ buildStateEvidencePackets([
83
+ { memoryId: "x", stateLabel: "archived" as StateViewEntry["stateLabel"] },
84
+ ]),
85
+ (error: unknown) => {
86
+ assert.ok(error instanceof TypeError);
87
+ const message = String((error as Error).message);
88
+ assert.match(message, /state label/);
89
+ assert.ok(message.includes('"current"'));
90
+ assert.ok(message.includes('"historical"'));
91
+ assert.ok(message.includes('"transition"'));
92
+ return true;
93
+ },
94
+ );
95
+ });
96
+
97
+ test("blank memoryId is ignored", () => {
98
+ const result = buildStateEvidencePackets([
99
+ { memoryId: "", stateLabel: "historical", supersededById: "head" },
100
+ { memoryId: " ", stateLabel: "current" },
101
+ { memoryId: "head", stateLabel: "current" },
102
+ ]);
103
+ assert.deepEqual(result, {
104
+ packets: [{ headId: "head", historyIds: [] }],
105
+ orphanHistoryIds: [],
106
+ });
107
+ });
108
+
109
+ test("duplicate memoryId keeps the first entry", () => {
110
+ const result = buildStateEvidencePackets([
111
+ { memoryId: "dup", stateLabel: "current" },
112
+ { memoryId: "dup", stateLabel: "historical", supersededById: "other" },
113
+ { memoryId: "old", stateLabel: "historical", supersededById: "dup" },
114
+ ]);
115
+ assert.deepEqual(result, {
116
+ packets: [{ headId: "dup", historyIds: ["old"] }],
117
+ orphanHistoryIds: [],
118
+ });
119
+ });
120
+
121
+ test("shuffled input produces a deep-equal result", () => {
122
+ const entries: StateViewEntry[] = [
123
+ { memoryId: "h2", stateLabel: "transition" },
124
+ { memoryId: "h1", stateLabel: "current" },
125
+ { memoryId: "a1", stateLabel: "historical", supersededById: "h1" },
126
+ { memoryId: "a2", stateLabel: "historical", supersededById: "a1" },
127
+ { memoryId: "b1", stateLabel: "historical", supersededById: "h2" },
128
+ { memoryId: "lost", stateLabel: "historical" },
129
+ ];
130
+ const expected = {
131
+ packets: [
132
+ { headId: "h1", historyIds: ["a1", "a2"] },
133
+ { headId: "h2", historyIds: ["b1"] },
134
+ ],
135
+ orphanHistoryIds: ["lost"],
136
+ };
137
+ assert.deepEqual(buildStateEvidencePackets(entries), expected);
138
+ const byId = new Map(entries.map((entry) => [entry.memoryId, entry]));
139
+ const orders = [
140
+ ["lost", "b1", "a2", "a1", "h2", "h1"],
141
+ ["h2", "lost", "h1", "b1", "a2", "a1"],
142
+ ["a2", "h1", "lost", "h2", "a1", "b1"],
143
+ ];
144
+ for (const order of orders) {
145
+ const shuffled = order.map((id) => byId.get(id) as StateViewEntry);
146
+ assert.deepEqual(buildStateEvidencePackets(shuffled), expected);
147
+ }
148
+ });
149
+
150
+ test("input entries are not mutated", () => {
151
+ const entries: StateViewEntry[] = [
152
+ { memoryId: "head", stateLabel: "current" },
153
+ { memoryId: "old", stateLabel: "historical", supersededById: "head" },
154
+ ];
155
+ const snapshot = structuredClone(entries);
156
+ buildStateEvidencePackets(entries);
157
+ assert.deepEqual(entries, snapshot);
158
+ });
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Recall state-view evidence packets (issue #1952).
3
+ *
4
+ * Groups labeled state-view entries into per-head evidence packets so
5
+ * superseded records stay visible as ordered history instead of being
6
+ * hidden or interleaved with another packet's entries. Pure. Surfaces wait.
7
+ */
8
+ import type { StateLabel } from "./recall-state-view.js";
9
+
10
+ export type { StateLabel };
11
+
12
+ export interface StateViewEntry {
13
+ memoryId: string;
14
+ stateLabel: StateLabel;
15
+ /** Id of the entry that superseded this one, when any. */
16
+ supersededById?: string;
17
+ }
18
+
19
+ export interface StateEvidencePacket {
20
+ /** The current (or transition) entry this packet is about. */
21
+ headId: string;
22
+ /** Predecessors, nearest to head first. Never interleaved with other packets. */
23
+ historyIds: string[];
24
+ }
25
+
26
+ export interface StateEvidencePackets {
27
+ packets: StateEvidencePacket[];
28
+ /** Historical entries whose successor is absent from the input. */
29
+ orphanHistoryIds: string[];
30
+ }
31
+
32
+ const STATE_LABELS: readonly StateLabel[] = ["current", "historical", "transition"];
33
+
34
+ interface HeadResolution {
35
+ headId: string;
36
+ depth: number;
37
+ }
38
+
39
+ interface Attachment {
40
+ headId: string;
41
+ historyId: string;
42
+ depth: number;
43
+ }
44
+
45
+ /**
46
+ * Walk supersededById forward through intermediate historical entries and
47
+ * stop at the first current/transition successor. Returns undefined when the
48
+ * walk dead-ends (no or blank successor, successor id absent from the input)
49
+ * or revisits an id (cycle), so callers treat the entry as an orphan.
50
+ */
51
+ function resolveHead(
52
+ entry: StateViewEntry,
53
+ byId: ReadonlyMap<string, StateViewEntry>,
54
+ ): HeadResolution | undefined {
55
+ const visited = new Set<string>();
56
+ let current = entry;
57
+ let depth = 0;
58
+ for (;;) {
59
+ const successorId = current.supersededById;
60
+ if (successorId === undefined || successorId.trim() === "") return undefined;
61
+ const successor = byId.get(successorId);
62
+ if (successor === undefined) return undefined;
63
+ depth += 1;
64
+ if (successor.stateLabel !== "historical") return { headId: successorId, depth };
65
+ if (visited.has(successorId)) return undefined;
66
+ visited.add(successorId);
67
+ current = successor;
68
+ }
69
+ }
70
+
71
+ function compareStrings(a: string, b: string): number {
72
+ return a < b ? -1 : a > b ? 1 : 0;
73
+ }
74
+
75
+ function compareAttachments(a: Attachment, b: Attachment): number {
76
+ if (a.headId !== b.headId) return compareStrings(a.headId, b.headId);
77
+ if (a.depth !== b.depth) return a.depth < b.depth ? -1 : 1;
78
+ return compareStrings(a.historyId, b.historyId);
79
+ }
80
+
81
+ export function buildStateEvidencePackets(
82
+ entries: readonly StateViewEntry[],
83
+ ): StateEvidencePackets {
84
+ // Validate every label before any skip, dedup, or walk.
85
+ for (const entry of entries) {
86
+ if (!(STATE_LABELS as readonly string[]).includes(entry.stateLabel)) {
87
+ throw new TypeError(
88
+ `unknown state label ${JSON.stringify(entry.stateLabel)}; expected one of "current", "historical", "transition"`,
89
+ );
90
+ }
91
+ }
92
+
93
+ // Blank ids are ignored; on duplicate ids the first entry wins.
94
+ const byId = new Map<string, StateViewEntry>();
95
+ for (const entry of entries) {
96
+ if (typeof entry.memoryId !== "string" || entry.memoryId.trim() === "") continue;
97
+ if (!byId.has(entry.memoryId)) byId.set(entry.memoryId, entry);
98
+ }
99
+
100
+ // ponytail: per-entry chain walks are O(n^2) on one long chain; recall-sized
101
+ // result sets stay tiny, memoize if packet building ever moves offline.
102
+ const packets: StateEvidencePacket[] = [];
103
+ const packetByHead = new Map<string, StateEvidencePacket>();
104
+ const attachments: Attachment[] = [];
105
+ const orphanHistoryIds: string[] = [];
106
+
107
+ for (const [id, entry] of byId) {
108
+ if (entry.stateLabel !== "historical") {
109
+ const packet: StateEvidencePacket = { headId: id, historyIds: [] };
110
+ packetByHead.set(id, packet);
111
+ packets.push(packet);
112
+ continue;
113
+ }
114
+ const head = resolveHead(entry, byId);
115
+ if (head === undefined) orphanHistoryIds.push(id);
116
+ else attachments.push({ headId: head.headId, historyId: id, depth: head.depth });
117
+ }
118
+
119
+ attachments.sort(compareAttachments);
120
+ for (const attachment of attachments) {
121
+ const packet = packetByHead.get(attachment.headId);
122
+ if (packet) packet.historyIds.push(attachment.historyId);
123
+ }
124
+
125
+ packets.sort((a, b) => compareStrings(a.headId, b.headId));
126
+ orphanHistoryIds.sort(compareStrings);
127
+ return { packets, orphanHistoryIds };
128
+ }