@diyor28/context 1.0.0

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 (280) hide show
  1. package/README.md +270 -0
  2. package/dist/__tests__/attachment-selector.test.d.ts +11 -0
  3. package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
  4. package/dist/__tests__/attachment-selector.test.js +449 -0
  5. package/dist/__tests__/attachment-selector.test.js.map +1 -0
  6. package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
  7. package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
  8. package/dist/__tests__/cache-breakpoints.test.js +398 -0
  9. package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
  10. package/dist/__tests__/codecs.test.d.ts +7 -0
  11. package/dist/__tests__/codecs.test.d.ts.map +1 -0
  12. package/dist/__tests__/codecs.test.js +331 -0
  13. package/dist/__tests__/codecs.test.js.map +1 -0
  14. package/dist/__tests__/compactor.test.d.ts +11 -0
  15. package/dist/__tests__/compactor.test.d.ts.map +1 -0
  16. package/dist/__tests__/compactor.test.js +519 -0
  17. package/dist/__tests__/compactor.test.js.map +1 -0
  18. package/dist/__tests__/context-graph.test.d.ts +7 -0
  19. package/dist/__tests__/context-graph.test.d.ts.map +1 -0
  20. package/dist/__tests__/context-graph.test.js +262 -0
  21. package/dist/__tests__/context-graph.test.js.map +1 -0
  22. package/dist/__tests__/hash.test.d.ts +7 -0
  23. package/dist/__tests__/hash.test.d.ts.map +1 -0
  24. package/dist/__tests__/hash.test.js +228 -0
  25. package/dist/__tests__/hash.test.js.map +1 -0
  26. package/dist/__tests__/integration.test.d.ts +15 -0
  27. package/dist/__tests__/integration.test.d.ts.map +1 -0
  28. package/dist/__tests__/integration.test.js +728 -0
  29. package/dist/__tests__/integration.test.js.map +1 -0
  30. package/dist/__tests__/kind-order.test.d.ts +7 -0
  31. package/dist/__tests__/kind-order.test.d.ts.map +1 -0
  32. package/dist/__tests__/kind-order.test.js +243 -0
  33. package/dist/__tests__/kind-order.test.js.map +1 -0
  34. package/dist/__tests__/phase2-integration.test.d.ts +5 -0
  35. package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
  36. package/dist/__tests__/phase2-integration.test.js +222 -0
  37. package/dist/__tests__/phase2-integration.test.js.map +1 -0
  38. package/dist/__tests__/queries.test.d.ts +7 -0
  39. package/dist/__tests__/queries.test.d.ts.map +1 -0
  40. package/dist/__tests__/queries.test.js +254 -0
  41. package/dist/__tests__/queries.test.js.map +1 -0
  42. package/dist/__tests__/token-estimator.test.d.ts +7 -0
  43. package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
  44. package/dist/__tests__/token-estimator.test.js +267 -0
  45. package/dist/__tests__/token-estimator.test.js.map +1 -0
  46. package/dist/adapters/anthropic-estimator.d.ts +38 -0
  47. package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
  48. package/dist/adapters/anthropic-estimator.js +108 -0
  49. package/dist/adapters/anthropic-estimator.js.map +1 -0
  50. package/dist/adapters/attachment-resolver.d.ts +96 -0
  51. package/dist/adapters/attachment-resolver.d.ts.map +1 -0
  52. package/dist/adapters/attachment-resolver.js +176 -0
  53. package/dist/adapters/attachment-resolver.js.map +1 -0
  54. package/dist/adapters/attachment-selector.d.ts +59 -0
  55. package/dist/adapters/attachment-selector.d.ts.map +1 -0
  56. package/dist/adapters/attachment-selector.js +163 -0
  57. package/dist/adapters/attachment-selector.js.map +1 -0
  58. package/dist/adapters/gemini-estimator.d.ts +27 -0
  59. package/dist/adapters/gemini-estimator.d.ts.map +1 -0
  60. package/dist/adapters/gemini-estimator.js +80 -0
  61. package/dist/adapters/gemini-estimator.js.map +1 -0
  62. package/dist/adapters/index.d.ts +12 -0
  63. package/dist/adapters/index.d.ts.map +1 -0
  64. package/dist/adapters/index.js +28 -0
  65. package/dist/adapters/index.js.map +1 -0
  66. package/dist/adapters/memory-store.d.ts +139 -0
  67. package/dist/adapters/memory-store.d.ts.map +1 -0
  68. package/dist/adapters/memory-store.js +187 -0
  69. package/dist/adapters/memory-store.js.map +1 -0
  70. package/dist/adapters/openai-estimator.d.ts +35 -0
  71. package/dist/adapters/openai-estimator.d.ts.map +1 -0
  72. package/dist/adapters/openai-estimator.js +89 -0
  73. package/dist/adapters/openai-estimator.js.map +1 -0
  74. package/dist/adapters/summarizer.d.ts +121 -0
  75. package/dist/adapters/summarizer.d.ts.map +1 -0
  76. package/dist/adapters/summarizer.js +121 -0
  77. package/dist/adapters/summarizer.js.map +1 -0
  78. package/dist/adapters/token-estimator.d.ts +63 -0
  79. package/dist/adapters/token-estimator.d.ts.map +1 -0
  80. package/dist/adapters/token-estimator.js +37 -0
  81. package/dist/adapters/token-estimator.js.map +1 -0
  82. package/dist/builder/context-builder.d.ts +186 -0
  83. package/dist/builder/context-builder.d.ts.map +1 -0
  84. package/dist/builder/context-builder.js +305 -0
  85. package/dist/builder/context-builder.js.map +1 -0
  86. package/dist/builder/context-fork.d.ts +166 -0
  87. package/dist/builder/context-fork.d.ts.map +1 -0
  88. package/dist/builder/context-fork.js +282 -0
  89. package/dist/builder/context-fork.js.map +1 -0
  90. package/dist/builder/index.d.ts +6 -0
  91. package/dist/builder/index.d.ts.map +1 -0
  92. package/dist/builder/index.js +22 -0
  93. package/dist/builder/index.js.map +1 -0
  94. package/dist/codecs/base.d.ts +18 -0
  95. package/dist/codecs/base.d.ts.map +1 -0
  96. package/dist/codecs/base.js +39 -0
  97. package/dist/codecs/base.js.map +1 -0
  98. package/dist/codecs/conversation-history.codec.d.ts +81 -0
  99. package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
  100. package/dist/codecs/conversation-history.codec.js +89 -0
  101. package/dist/codecs/conversation-history.codec.js.map +1 -0
  102. package/dist/codecs/index.d.ts +31 -0
  103. package/dist/codecs/index.d.ts.map +1 -0
  104. package/dist/codecs/index.js +71 -0
  105. package/dist/codecs/index.js.map +1 -0
  106. package/dist/codecs/redacted-stub.codec.d.ts +32 -0
  107. package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
  108. package/dist/codecs/redacted-stub.codec.js +64 -0
  109. package/dist/codecs/redacted-stub.codec.js.map +1 -0
  110. package/dist/codecs/structured-reference.codec.d.ts +40 -0
  111. package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
  112. package/dist/codecs/structured-reference.codec.js +81 -0
  113. package/dist/codecs/structured-reference.codec.js.map +1 -0
  114. package/dist/codecs/system-rules.codec.d.ts +32 -0
  115. package/dist/codecs/system-rules.codec.d.ts.map +1 -0
  116. package/dist/codecs/system-rules.codec.js +62 -0
  117. package/dist/codecs/system-rules.codec.js.map +1 -0
  118. package/dist/codecs/tool-output.codec.d.ts +66 -0
  119. package/dist/codecs/tool-output.codec.d.ts.map +1 -0
  120. package/dist/codecs/tool-output.codec.js +95 -0
  121. package/dist/codecs/tool-output.codec.js.map +1 -0
  122. package/dist/codecs/tool-schema.codec.d.ts +36 -0
  123. package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
  124. package/dist/codecs/tool-schema.codec.js +74 -0
  125. package/dist/codecs/tool-schema.codec.js.map +1 -0
  126. package/dist/codecs/unsafe-text.codec.d.ts +28 -0
  127. package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
  128. package/dist/codecs/unsafe-text.codec.js +63 -0
  129. package/dist/codecs/unsafe-text.codec.js.map +1 -0
  130. package/dist/graph/context-graph.d.ts +121 -0
  131. package/dist/graph/context-graph.d.ts.map +1 -0
  132. package/dist/graph/context-graph.js +166 -0
  133. package/dist/graph/context-graph.js.map +1 -0
  134. package/dist/graph/index.d.ts +8 -0
  135. package/dist/graph/index.d.ts.map +1 -0
  136. package/dist/graph/index.js +24 -0
  137. package/dist/graph/index.js.map +1 -0
  138. package/dist/graph/kind-order.d.ts +60 -0
  139. package/dist/graph/kind-order.d.ts.map +1 -0
  140. package/dist/graph/kind-order.js +113 -0
  141. package/dist/graph/kind-order.js.map +1 -0
  142. package/dist/graph/queries.d.ts +68 -0
  143. package/dist/graph/queries.d.ts.map +1 -0
  144. package/dist/graph/queries.js +240 -0
  145. package/dist/graph/queries.js.map +1 -0
  146. package/dist/graph/views.d.ts +90 -0
  147. package/dist/graph/views.d.ts.map +1 -0
  148. package/dist/graph/views.js +173 -0
  149. package/dist/graph/views.js.map +1 -0
  150. package/dist/index.d.ts +16 -0
  151. package/dist/index.d.ts.map +1 -0
  152. package/dist/index.js +40 -0
  153. package/dist/index.js.map +1 -0
  154. package/dist/pipeline/compactor.d.ts +128 -0
  155. package/dist/pipeline/compactor.d.ts.map +1 -0
  156. package/dist/pipeline/compactor.js +346 -0
  157. package/dist/pipeline/compactor.js.map +1 -0
  158. package/dist/pipeline/index.d.ts +6 -0
  159. package/dist/pipeline/index.d.ts.map +1 -0
  160. package/dist/pipeline/index.js +22 -0
  161. package/dist/pipeline/index.js.map +1 -0
  162. package/dist/pipeline/summarizer.d.ts +18 -0
  163. package/dist/pipeline/summarizer.d.ts.map +1 -0
  164. package/dist/pipeline/summarizer.js +68 -0
  165. package/dist/pipeline/summarizer.js.map +1 -0
  166. package/dist/policies/default-policy.d.ts +29 -0
  167. package/dist/policies/default-policy.d.ts.map +1 -0
  168. package/dist/policies/default-policy.js +58 -0
  169. package/dist/policies/default-policy.js.map +1 -0
  170. package/dist/policies/index.d.ts +5 -0
  171. package/dist/policies/index.d.ts.map +1 -0
  172. package/dist/policies/index.js +21 -0
  173. package/dist/policies/index.js.map +1 -0
  174. package/dist/providers/anthropic-compiler.d.ts +58 -0
  175. package/dist/providers/anthropic-compiler.d.ts.map +1 -0
  176. package/dist/providers/anthropic-compiler.js +182 -0
  177. package/dist/providers/anthropic-compiler.js.map +1 -0
  178. package/dist/providers/capabilities.d.ts +54 -0
  179. package/dist/providers/capabilities.d.ts.map +1 -0
  180. package/dist/providers/capabilities.js +87 -0
  181. package/dist/providers/capabilities.js.map +1 -0
  182. package/dist/providers/gemini-compiler.d.ts +51 -0
  183. package/dist/providers/gemini-compiler.d.ts.map +1 -0
  184. package/dist/providers/gemini-compiler.js +206 -0
  185. package/dist/providers/gemini-compiler.js.map +1 -0
  186. package/dist/providers/index.d.ts +8 -0
  187. package/dist/providers/index.d.ts.map +1 -0
  188. package/dist/providers/index.js +24 -0
  189. package/dist/providers/index.js.map +1 -0
  190. package/dist/providers/openai-compiler.d.ts +46 -0
  191. package/dist/providers/openai-compiler.d.ts.map +1 -0
  192. package/dist/providers/openai-compiler.js +149 -0
  193. package/dist/providers/openai-compiler.js.map +1 -0
  194. package/dist/types/attachment.d.ts +62 -0
  195. package/dist/types/attachment.d.ts.map +1 -0
  196. package/dist/types/attachment.js +6 -0
  197. package/dist/types/attachment.js.map +1 -0
  198. package/dist/types/block.d.ts +61 -0
  199. package/dist/types/block.d.ts.map +1 -0
  200. package/dist/types/block.js +8 -0
  201. package/dist/types/block.js.map +1 -0
  202. package/dist/types/codec.d.ts +58 -0
  203. package/dist/types/codec.d.ts.map +1 -0
  204. package/dist/types/codec.js +6 -0
  205. package/dist/types/codec.js.map +1 -0
  206. package/dist/types/compiled.d.ts +91 -0
  207. package/dist/types/compiled.d.ts.map +1 -0
  208. package/dist/types/compiled.js +6 -0
  209. package/dist/types/compiled.js.map +1 -0
  210. package/dist/types/hash.d.ts +24 -0
  211. package/dist/types/hash.d.ts.map +1 -0
  212. package/dist/types/hash.js +49 -0
  213. package/dist/types/hash.js.map +1 -0
  214. package/dist/types/index.d.ts +10 -0
  215. package/dist/types/index.d.ts.map +1 -0
  216. package/dist/types/index.js +26 -0
  217. package/dist/types/index.js.map +1 -0
  218. package/dist/types/policy.d.ts +128 -0
  219. package/dist/types/policy.d.ts.map +1 -0
  220. package/dist/types/policy.js +55 -0
  221. package/dist/types/policy.js.map +1 -0
  222. package/package.json +55 -0
  223. package/postcss.config.js +4 -0
  224. package/src/__tests__/attachment-selector.test.ts +559 -0
  225. package/src/__tests__/cache-breakpoints.test.ts +566 -0
  226. package/src/__tests__/codecs.test.ts +417 -0
  227. package/src/__tests__/compactor.test.ts +608 -0
  228. package/src/__tests__/context-graph.test.ts +383 -0
  229. package/src/__tests__/hash.test.ts +274 -0
  230. package/src/__tests__/integration.test.ts +866 -0
  231. package/src/__tests__/kind-order.test.ts +312 -0
  232. package/src/__tests__/phase2-integration.test.ts +253 -0
  233. package/src/__tests__/queries.test.ts +387 -0
  234. package/src/__tests__/token-estimator.test.ts +326 -0
  235. package/src/adapters/anthropic-estimator.ts +125 -0
  236. package/src/adapters/attachment-resolver.ts +295 -0
  237. package/src/adapters/attachment-selector.ts +218 -0
  238. package/src/adapters/gemini-estimator.ts +93 -0
  239. package/src/adapters/index.ts +12 -0
  240. package/src/adapters/memory-store.ts +299 -0
  241. package/src/adapters/openai-estimator.ts +105 -0
  242. package/src/adapters/summarizer.ts +250 -0
  243. package/src/adapters/token-estimator.ts +74 -0
  244. package/src/builder/context-builder.ts +467 -0
  245. package/src/builder/context-fork.ts +471 -0
  246. package/src/builder/index.ts +6 -0
  247. package/src/codecs/base.ts +36 -0
  248. package/src/codecs/conversation-history.codec.ts +108 -0
  249. package/src/codecs/index.ts +57 -0
  250. package/src/codecs/redacted-stub.codec.ts +76 -0
  251. package/src/codecs/structured-reference.codec.ts +96 -0
  252. package/src/codecs/system-rules.codec.ts +74 -0
  253. package/src/codecs/tool-output.codec.ts +109 -0
  254. package/src/codecs/tool-schema.codec.ts +87 -0
  255. package/src/codecs/unsafe-text.codec.ts +74 -0
  256. package/src/graph/context-graph.ts +205 -0
  257. package/src/graph/index.ts +8 -0
  258. package/src/graph/kind-order.ts +125 -0
  259. package/src/graph/queries.ts +306 -0
  260. package/src/graph/views.ts +255 -0
  261. package/src/index.ts +31 -0
  262. package/src/pipeline/compactor.ts +563 -0
  263. package/src/pipeline/index.ts +6 -0
  264. package/src/pipeline/summarizer.ts +76 -0
  265. package/src/policies/default-policy.ts +69 -0
  266. package/src/policies/index.ts +5 -0
  267. package/src/providers/anthropic-compiler.ts +294 -0
  268. package/src/providers/capabilities.ts +144 -0
  269. package/src/providers/gemini-compiler.ts +272 -0
  270. package/src/providers/index.ts +8 -0
  271. package/src/providers/openai-compiler.ts +191 -0
  272. package/src/types/attachment.ts +86 -0
  273. package/src/types/block.ts +84 -0
  274. package/src/types/codec.ts +68 -0
  275. package/src/types/compiled.ts +109 -0
  276. package/src/types/hash.ts +58 -0
  277. package/src/types/index.ts +10 -0
  278. package/src/types/policy.ts +194 -0
  279. package/tsconfig.json +21 -0
  280. package/vitest.config.ts +21 -0
@@ -0,0 +1,312 @@
1
+ /**
2
+ * Unit tests for KIND_ORDER enforcement.
3
+ *
4
+ * Tests block ordering, comparison, sorting, and validation.
5
+ */
6
+
7
+ import { describe, it, expect } from 'vitest';
8
+ import {
9
+ KIND_ORDER,
10
+ getKindIndex,
11
+ compareKinds,
12
+ sortBlocksByKind,
13
+ validateBlockOrder,
14
+ groupBlocksByKind,
15
+ isValidKind,
16
+ } from '../graph/kind-order.js';
17
+ import type { ContextBlock, BlockKind } from '../types/block.js';
18
+
19
+ describe('KIND_ORDER', () => {
20
+ describe('KIND_ORDER constant', () => {
21
+ it('should define correct order', () => {
22
+ expect(KIND_ORDER).toEqual([
23
+ 'pinned',
24
+ 'reference',
25
+ 'memory',
26
+ 'state',
27
+ 'tool_output',
28
+ 'history',
29
+ 'turn',
30
+ ]);
31
+ });
32
+
33
+ it('should be immutable', () => {
34
+ expect(Object.isFrozen(KIND_ORDER)).toBe(true);
35
+ });
36
+
37
+ it('should have 7 kinds', () => {
38
+ expect(KIND_ORDER).toHaveLength(7);
39
+ });
40
+ });
41
+
42
+ describe('getKindIndex', () => {
43
+ it('should return correct index for each kind', () => {
44
+ expect(getKindIndex('pinned')).toBe(0);
45
+ expect(getKindIndex('reference')).toBe(1);
46
+ expect(getKindIndex('memory')).toBe(2);
47
+ expect(getKindIndex('state')).toBe(3);
48
+ expect(getKindIndex('tool_output')).toBe(4);
49
+ expect(getKindIndex('history')).toBe(5);
50
+ expect(getKindIndex('turn')).toBe(6);
51
+ });
52
+
53
+ it('should return -1 for invalid kind', () => {
54
+ expect(getKindIndex('invalid' as BlockKind)).toBe(-1);
55
+ });
56
+ });
57
+
58
+ describe('compareKinds', () => {
59
+ it('should return negative when a < b', () => {
60
+ expect(compareKinds('pinned', 'memory')).toBeLessThan(0);
61
+ expect(compareKinds('reference', 'history')).toBeLessThan(0);
62
+ });
63
+
64
+ it('should return positive when a > b', () => {
65
+ expect(compareKinds('memory', 'pinned')).toBeGreaterThan(0);
66
+ expect(compareKinds('turn', 'state')).toBeGreaterThan(0);
67
+ });
68
+
69
+ it('should return zero when a == b', () => {
70
+ expect(compareKinds('pinned', 'pinned')).toBe(0);
71
+ expect(compareKinds('memory', 'memory')).toBe(0);
72
+ });
73
+
74
+ it('should throw for invalid kind', () => {
75
+ expect(() => compareKinds('invalid' as BlockKind, 'pinned')).toThrow(
76
+ 'Invalid block kind: invalid'
77
+ );
78
+ expect(() => compareKinds('pinned', 'invalid' as BlockKind)).toThrow(
79
+ 'Invalid block kind: invalid'
80
+ );
81
+ });
82
+ });
83
+
84
+ describe('sortBlocksByKind', () => {
85
+ it('should sort blocks by KIND_ORDER', () => {
86
+ const blocks: ContextBlock<string>[] = [
87
+ createBlock('hash3', 'history', 'block 3'),
88
+ createBlock('hash1', 'pinned', 'block 1'),
89
+ createBlock('hash2', 'memory', 'block 2'),
90
+ ];
91
+
92
+ const sorted = sortBlocksByKind(blocks);
93
+
94
+ expect(sorted).toHaveLength(3);
95
+ expect(sorted[0].meta.kind).toBe('pinned');
96
+ expect(sorted[1].meta.kind).toBe('memory');
97
+ expect(sorted[2].meta.kind).toBe('history');
98
+ });
99
+
100
+ it('should preserve relative order within same kind (stable sort)', () => {
101
+ const blocks: ContextBlock<string>[] = [
102
+ createBlock('hash1', 'memory', 'block 1'),
103
+ createBlock('hash2', 'memory', 'block 2'),
104
+ createBlock('hash3', 'memory', 'block 3'),
105
+ ];
106
+
107
+ const sorted = sortBlocksByKind(blocks);
108
+
109
+ expect(sorted[0].blockHash).toBe('hash1');
110
+ expect(sorted[1].blockHash).toBe('hash2');
111
+ expect(sorted[2].blockHash).toBe('hash3');
112
+ });
113
+
114
+ it('should not mutate original array', () => {
115
+ const blocks: ContextBlock<string>[] = [
116
+ createBlock('hash2', 'history', 'block 2'),
117
+ createBlock('hash1', 'pinned', 'block 1'),
118
+ ];
119
+
120
+ const originalOrder = blocks.map((b) => b.blockHash);
121
+ const sorted = sortBlocksByKind(blocks);
122
+
123
+ // Original should be unchanged
124
+ expect(blocks.map((b) => b.blockHash)).toEqual(originalOrder);
125
+ // Sorted should be different
126
+ expect(sorted.map((b) => b.blockHash)).toEqual(['hash1', 'hash2']);
127
+ });
128
+
129
+ it('should handle empty array', () => {
130
+ const sorted = sortBlocksByKind([]);
131
+
132
+ expect(sorted).toEqual([]);
133
+ });
134
+
135
+ it('should handle single block', () => {
136
+ const blocks = [createBlock('hash1', 'pinned', 'block 1')];
137
+ const sorted = sortBlocksByKind(blocks);
138
+
139
+ expect(sorted).toHaveLength(1);
140
+ expect(sorted[0].blockHash).toBe('hash1');
141
+ });
142
+
143
+ it('should sort all kinds correctly', () => {
144
+ const blocks: ContextBlock<string>[] = [
145
+ createBlock('hash7', 'turn', 'block 7'),
146
+ createBlock('hash5', 'tool_output', 'block 5'),
147
+ createBlock('hash3', 'memory', 'block 3'),
148
+ createBlock('hash1', 'pinned', 'block 1'),
149
+ createBlock('hash6', 'history', 'block 6'),
150
+ createBlock('hash4', 'state', 'block 4'),
151
+ createBlock('hash2', 'reference', 'block 2'),
152
+ ];
153
+
154
+ const sorted = sortBlocksByKind(blocks);
155
+
156
+ expect(sorted.map((b) => b.meta.kind)).toEqual([
157
+ 'pinned',
158
+ 'reference',
159
+ 'memory',
160
+ 'state',
161
+ 'tool_output',
162
+ 'history',
163
+ 'turn',
164
+ ]);
165
+ });
166
+ });
167
+
168
+ describe('validateBlockOrder', () => {
169
+ it('should pass for correctly ordered blocks', () => {
170
+ const blocks: ContextBlock<string>[] = [
171
+ createBlock('hash1', 'pinned', 'block 1'),
172
+ createBlock('hash2', 'memory', 'block 2'),
173
+ createBlock('hash3', 'history', 'block 3'),
174
+ ];
175
+
176
+ expect(() => validateBlockOrder(blocks)).not.toThrow();
177
+ });
178
+
179
+ it('should pass for blocks of same kind', () => {
180
+ const blocks: ContextBlock<string>[] = [
181
+ createBlock('hash1', 'memory', 'block 1'),
182
+ createBlock('hash2', 'memory', 'block 2'),
183
+ createBlock('hash3', 'memory', 'block 3'),
184
+ ];
185
+
186
+ expect(() => validateBlockOrder(blocks)).not.toThrow();
187
+ });
188
+
189
+ it('should throw for incorrectly ordered blocks', () => {
190
+ const blocks: ContextBlock<string>[] = [
191
+ createBlock('hash1', 'memory', 'block 1'),
192
+ createBlock('hash2', 'pinned', 'block 2'), // Wrong order!
193
+ ];
194
+
195
+ expect(() => validateBlockOrder(blocks)).toThrow(
196
+ 'Blocks not sorted by KIND_ORDER'
197
+ );
198
+ });
199
+
200
+ it('should throw with descriptive error message', () => {
201
+ const blocks: ContextBlock<string>[] = [
202
+ createBlock('hash1', 'history', 'block 1'),
203
+ createBlock('hash2', 'memory', 'block 2'),
204
+ ];
205
+
206
+ expect(() => validateBlockOrder(blocks)).toThrow(
207
+ 'history (index 0) comes before memory (index 1)'
208
+ );
209
+ });
210
+
211
+ it('should pass for empty array', () => {
212
+ expect(() => validateBlockOrder([])).not.toThrow();
213
+ });
214
+
215
+ it('should pass for single block', () => {
216
+ const blocks = [createBlock('hash1', 'pinned', 'block 1')];
217
+
218
+ expect(() => validateBlockOrder(blocks)).not.toThrow();
219
+ });
220
+ });
221
+
222
+ describe('groupBlocksByKind', () => {
223
+ it('should group blocks by kind', () => {
224
+ const blocks: ContextBlock<string>[] = [
225
+ createBlock('hash1', 'pinned', 'block 1'),
226
+ createBlock('hash2', 'memory', 'block 2'),
227
+ createBlock('hash3', 'pinned', 'block 3'),
228
+ createBlock('hash4', 'history', 'block 4'),
229
+ ];
230
+
231
+ const groups = groupBlocksByKind(blocks);
232
+
233
+ expect(groups.size).toBe(3);
234
+ expect(groups.get('pinned')).toHaveLength(2);
235
+ expect(groups.get('memory')).toHaveLength(1);
236
+ expect(groups.get('history')).toHaveLength(1);
237
+ });
238
+
239
+ it('should preserve block order within groups', () => {
240
+ const blocks: ContextBlock<string>[] = [
241
+ createBlock('hash1', 'memory', 'block 1'),
242
+ createBlock('hash2', 'memory', 'block 2'),
243
+ createBlock('hash3', 'memory', 'block 3'),
244
+ ];
245
+
246
+ const groups = groupBlocksByKind(blocks);
247
+ const memoryBlocks = groups.get('memory')!;
248
+
249
+ expect(memoryBlocks.map((b) => b.blockHash)).toEqual([
250
+ 'hash1',
251
+ 'hash2',
252
+ 'hash3',
253
+ ]);
254
+ });
255
+
256
+ it('should handle empty array', () => {
257
+ const groups = groupBlocksByKind([]);
258
+
259
+ expect(groups.size).toBe(0);
260
+ });
261
+
262
+ it('should handle single kind', () => {
263
+ const blocks: ContextBlock<string>[] = [
264
+ createBlock('hash1', 'pinned', 'block 1'),
265
+ createBlock('hash2', 'pinned', 'block 2'),
266
+ ];
267
+
268
+ const groups = groupBlocksByKind(blocks);
269
+
270
+ expect(groups.size).toBe(1);
271
+ expect(groups.get('pinned')).toHaveLength(2);
272
+ });
273
+ });
274
+
275
+ describe('isValidKind', () => {
276
+ it('should return true for valid kinds', () => {
277
+ expect(isValidKind('pinned')).toBe(true);
278
+ expect(isValidKind('reference')).toBe(true);
279
+ expect(isValidKind('memory')).toBe(true);
280
+ expect(isValidKind('state')).toBe(true);
281
+ expect(isValidKind('tool_output')).toBe(true);
282
+ expect(isValidKind('history')).toBe(true);
283
+ expect(isValidKind('turn')).toBe(true);
284
+ });
285
+
286
+ it('should return false for invalid kinds', () => {
287
+ expect(isValidKind('invalid')).toBe(false);
288
+ expect(isValidKind('')).toBe(false);
289
+ expect(isValidKind('PINNED')).toBe(false);
290
+ expect(isValidKind('tool-output')).toBe(false);
291
+ });
292
+ });
293
+ });
294
+
295
+ // Helper to create test blocks
296
+ function createBlock(
297
+ hash: string,
298
+ kind: BlockKind,
299
+ payload: string
300
+ ): ContextBlock<string> {
301
+ return {
302
+ blockHash: hash,
303
+ meta: {
304
+ kind,
305
+ sensitivity: 'public',
306
+ codecId: 'test',
307
+ codecVersion: '1.0.0',
308
+ createdAt: Date.now(),
309
+ },
310
+ payload,
311
+ };
312
+ }
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Integration tests for Phase 2: Context Graph & Token Estimation
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import { ContextGraph } from '../graph/context-graph.js';
7
+ import type { ContextBlock } from '../types/block.js';
8
+ import type { BlockQuery } from '../graph/queries.js';
9
+ import { OpenAITokenEstimator } from '../adapters/openai-estimator.js';
10
+
11
+ describe('Phase 2 Integration', () => {
12
+ describe('ContextGraph', () => {
13
+ it('should add and retrieve blocks', () => {
14
+ const graph = new ContextGraph();
15
+
16
+ const block: ContextBlock<string> = {
17
+ blockHash: 'hash1',
18
+ meta: {
19
+ kind: 'pinned',
20
+ sensitivity: 'public',
21
+ codecId: 'test',
22
+ codecVersion: '1.0.0',
23
+ createdAt: Date.now(),
24
+ },
25
+ payload: 'test content',
26
+ };
27
+
28
+ graph.addBlock(block);
29
+
30
+ expect(graph.hasBlock('hash1')).toBe(true);
31
+ expect(graph.getBlock('hash1')).toEqual(block);
32
+ expect(graph.getBlockCount()).toBe(1);
33
+ });
34
+
35
+ it('should track derivation edges', () => {
36
+ const graph = new ContextGraph();
37
+
38
+ const parent: ContextBlock<string> = {
39
+ blockHash: 'parent',
40
+ meta: {
41
+ kind: 'pinned',
42
+ sensitivity: 'public',
43
+ codecId: 'test',
44
+ codecVersion: '1.0.0',
45
+ createdAt: Date.now(),
46
+ },
47
+ payload: 'parent content',
48
+ };
49
+
50
+ const child: ContextBlock<string> = {
51
+ blockHash: 'child',
52
+ meta: {
53
+ kind: 'memory',
54
+ sensitivity: 'public',
55
+ codecId: 'test',
56
+ codecVersion: '1.0.0',
57
+ createdAt: Date.now(),
58
+ },
59
+ payload: 'child content',
60
+ };
61
+
62
+ graph.addBlock(parent);
63
+ graph.addBlock(child, [{ blockHash: 'parent' }]);
64
+
65
+ const derivedFrom = graph.getDerivedFrom('child');
66
+ expect(derivedFrom).toHaveLength(1);
67
+ expect(derivedFrom[0].blockHash).toBe('parent');
68
+ });
69
+
70
+ it('should select blocks by query', () => {
71
+ const graph = new ContextGraph();
72
+
73
+ const block1: ContextBlock<string> = {
74
+ blockHash: 'hash1',
75
+ meta: {
76
+ kind: 'pinned',
77
+ sensitivity: 'public',
78
+ codecId: 'test',
79
+ codecVersion: '1.0.0',
80
+ createdAt: Date.now(),
81
+ tags: ['important'],
82
+ },
83
+ payload: 'block 1',
84
+ };
85
+
86
+ const block2: ContextBlock<string> = {
87
+ blockHash: 'hash2',
88
+ meta: {
89
+ kind: 'memory',
90
+ sensitivity: 'internal',
91
+ codecId: 'test',
92
+ codecVersion: '1.0.0',
93
+ createdAt: Date.now(),
94
+ tags: ['memory'],
95
+ },
96
+ payload: 'block 2',
97
+ };
98
+
99
+ graph.addBlock(block1);
100
+ graph.addBlock(block2);
101
+
102
+ // Query by kind
103
+ const pinnedBlocks = graph.select({ kinds: ['pinned'] });
104
+ expect(pinnedBlocks).toHaveLength(1);
105
+ expect(pinnedBlocks[0].blockHash).toBe('hash1');
106
+
107
+ // Query by tags
108
+ const taggedBlocks = graph.select({ tags: ['memory'] });
109
+ expect(taggedBlocks).toHaveLength(1);
110
+ expect(taggedBlocks[0].blockHash).toBe('hash2');
111
+
112
+ // Query by sensitivity
113
+ const publicBlocks = graph.select({ maxSensitivity: 'public' });
114
+ expect(publicBlocks).toHaveLength(1);
115
+ expect(publicBlocks[0].blockHash).toBe('hash1');
116
+ });
117
+ });
118
+
119
+ describe('ContextView', () => {
120
+ it('should create deterministically ordered view', async () => {
121
+ const graph = new ContextGraph();
122
+
123
+ // Add blocks in random order
124
+ const blocks: ContextBlock<string>[] = [
125
+ {
126
+ blockHash: 'hash-history',
127
+ meta: {
128
+ kind: 'history',
129
+ sensitivity: 'public',
130
+ codecId: 'test',
131
+ codecVersion: '1.0.0',
132
+ createdAt: Date.now(),
133
+ },
134
+ payload: 'history',
135
+ },
136
+ {
137
+ blockHash: 'hash-pinned',
138
+ meta: {
139
+ kind: 'pinned',
140
+ sensitivity: 'public',
141
+ codecId: 'test',
142
+ codecVersion: '1.0.0',
143
+ createdAt: Date.now(),
144
+ },
145
+ payload: 'pinned',
146
+ },
147
+ {
148
+ blockHash: 'hash-memory',
149
+ meta: {
150
+ kind: 'memory',
151
+ sensitivity: 'public',
152
+ codecId: 'test',
153
+ codecVersion: '1.0.0',
154
+ createdAt: Date.now(),
155
+ },
156
+ payload: 'memory',
157
+ },
158
+ ];
159
+
160
+ blocks.forEach((block) => graph.addBlock(block));
161
+
162
+ const view = await graph.createView({});
163
+
164
+ // Verify KIND_ORDER (pinned → reference → memory → state → tool_output → history → turn)
165
+ expect(view.blocks).toHaveLength(3);
166
+ expect(view.blocks[0].meta.kind).toBe('pinned');
167
+ expect(view.blocks[1].meta.kind).toBe('memory');
168
+ expect(view.blocks[2].meta.kind).toBe('history');
169
+
170
+ // Verify stable prefix hash is computed
171
+ expect(view.stablePrefixHash).toBeDefined();
172
+ expect(view.stablePrefixHash.length).toBeGreaterThan(0);
173
+ });
174
+
175
+ it('should apply token budget', async () => {
176
+ const graph = new ContextGraph();
177
+
178
+ // Add blocks
179
+ const blocks: ContextBlock<string>[] = [
180
+ {
181
+ blockHash: 'hash1',
182
+ meta: {
183
+ kind: 'pinned',
184
+ sensitivity: 'public',
185
+ codecId: 'test',
186
+ codecVersion: '1.0.0',
187
+ createdAt: Date.now(),
188
+ },
189
+ payload: 'a'.repeat(100), // ~25 tokens
190
+ },
191
+ {
192
+ blockHash: 'hash2',
193
+ meta: {
194
+ kind: 'memory',
195
+ sensitivity: 'public',
196
+ codecId: 'test',
197
+ codecVersion: '1.0.0',
198
+ createdAt: Date.now(),
199
+ },
200
+ payload: 'b'.repeat(100), // ~25 tokens
201
+ },
202
+ {
203
+ blockHash: 'hash3',
204
+ meta: {
205
+ kind: 'history',
206
+ sensitivity: 'public',
207
+ codecId: 'test',
208
+ codecVersion: '1.0.0',
209
+ createdAt: Date.now(),
210
+ },
211
+ payload: 'c'.repeat(100), // ~25 tokens
212
+ },
213
+ ];
214
+
215
+ blocks.forEach((block) => graph.addBlock(block));
216
+
217
+ // Create view with token budget
218
+ const estimator = new OpenAITokenEstimator('gpt-4');
219
+ const view = await graph.createView({
220
+ tokenEstimator: estimator,
221
+ maxTokens: 50, // Only first 2 blocks should fit
222
+ });
223
+
224
+ // Verify truncation
225
+ expect(view.tokenEstimate).toBeDefined();
226
+ expect(view.tokenEstimate!.truncated).toBe(true);
227
+ expect(view.blocks.length).toBeLessThan(3);
228
+ });
229
+ });
230
+
231
+ describe('TokenEstimator', () => {
232
+ it('should estimate tokens using OpenAI estimator', async () => {
233
+ const estimator = new OpenAITokenEstimator('gpt-4');
234
+
235
+ const block: ContextBlock<string> = {
236
+ blockHash: 'hash1',
237
+ meta: {
238
+ kind: 'pinned',
239
+ sensitivity: 'public',
240
+ codecId: 'test',
241
+ codecVersion: '1.0.0',
242
+ createdAt: Date.now(),
243
+ },
244
+ payload: 'Hello, world!',
245
+ };
246
+
247
+ const estimate = await estimator.estimateBlock(block);
248
+
249
+ expect(estimate.tokens).toBeGreaterThan(0);
250
+ expect(estimate.confidence).toBe('high');
251
+ });
252
+ });
253
+ });