@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.
- package/README.md +270 -0
- package/dist/__tests__/attachment-selector.test.d.ts +11 -0
- package/dist/__tests__/attachment-selector.test.d.ts.map +1 -0
- package/dist/__tests__/attachment-selector.test.js +449 -0
- package/dist/__tests__/attachment-selector.test.js.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts +11 -0
- package/dist/__tests__/cache-breakpoints.test.d.ts.map +1 -0
- package/dist/__tests__/cache-breakpoints.test.js +398 -0
- package/dist/__tests__/cache-breakpoints.test.js.map +1 -0
- package/dist/__tests__/codecs.test.d.ts +7 -0
- package/dist/__tests__/codecs.test.d.ts.map +1 -0
- package/dist/__tests__/codecs.test.js +331 -0
- package/dist/__tests__/codecs.test.js.map +1 -0
- package/dist/__tests__/compactor.test.d.ts +11 -0
- package/dist/__tests__/compactor.test.d.ts.map +1 -0
- package/dist/__tests__/compactor.test.js +519 -0
- package/dist/__tests__/compactor.test.js.map +1 -0
- package/dist/__tests__/context-graph.test.d.ts +7 -0
- package/dist/__tests__/context-graph.test.d.ts.map +1 -0
- package/dist/__tests__/context-graph.test.js +262 -0
- package/dist/__tests__/context-graph.test.js.map +1 -0
- package/dist/__tests__/hash.test.d.ts +7 -0
- package/dist/__tests__/hash.test.d.ts.map +1 -0
- package/dist/__tests__/hash.test.js +228 -0
- package/dist/__tests__/hash.test.js.map +1 -0
- package/dist/__tests__/integration.test.d.ts +15 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +728 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/__tests__/kind-order.test.d.ts +7 -0
- package/dist/__tests__/kind-order.test.d.ts.map +1 -0
- package/dist/__tests__/kind-order.test.js +243 -0
- package/dist/__tests__/kind-order.test.js.map +1 -0
- package/dist/__tests__/phase2-integration.test.d.ts +5 -0
- package/dist/__tests__/phase2-integration.test.d.ts.map +1 -0
- package/dist/__tests__/phase2-integration.test.js +222 -0
- package/dist/__tests__/phase2-integration.test.js.map +1 -0
- package/dist/__tests__/queries.test.d.ts +7 -0
- package/dist/__tests__/queries.test.d.ts.map +1 -0
- package/dist/__tests__/queries.test.js +254 -0
- package/dist/__tests__/queries.test.js.map +1 -0
- package/dist/__tests__/token-estimator.test.d.ts +7 -0
- package/dist/__tests__/token-estimator.test.d.ts.map +1 -0
- package/dist/__tests__/token-estimator.test.js +267 -0
- package/dist/__tests__/token-estimator.test.js.map +1 -0
- package/dist/adapters/anthropic-estimator.d.ts +38 -0
- package/dist/adapters/anthropic-estimator.d.ts.map +1 -0
- package/dist/adapters/anthropic-estimator.js +108 -0
- package/dist/adapters/anthropic-estimator.js.map +1 -0
- package/dist/adapters/attachment-resolver.d.ts +96 -0
- package/dist/adapters/attachment-resolver.d.ts.map +1 -0
- package/dist/adapters/attachment-resolver.js +176 -0
- package/dist/adapters/attachment-resolver.js.map +1 -0
- package/dist/adapters/attachment-selector.d.ts +59 -0
- package/dist/adapters/attachment-selector.d.ts.map +1 -0
- package/dist/adapters/attachment-selector.js +163 -0
- package/dist/adapters/attachment-selector.js.map +1 -0
- package/dist/adapters/gemini-estimator.d.ts +27 -0
- package/dist/adapters/gemini-estimator.d.ts.map +1 -0
- package/dist/adapters/gemini-estimator.js +80 -0
- package/dist/adapters/gemini-estimator.js.map +1 -0
- package/dist/adapters/index.d.ts +12 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +28 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/memory-store.d.ts +139 -0
- package/dist/adapters/memory-store.d.ts.map +1 -0
- package/dist/adapters/memory-store.js +187 -0
- package/dist/adapters/memory-store.js.map +1 -0
- package/dist/adapters/openai-estimator.d.ts +35 -0
- package/dist/adapters/openai-estimator.d.ts.map +1 -0
- package/dist/adapters/openai-estimator.js +89 -0
- package/dist/adapters/openai-estimator.js.map +1 -0
- package/dist/adapters/summarizer.d.ts +121 -0
- package/dist/adapters/summarizer.d.ts.map +1 -0
- package/dist/adapters/summarizer.js +121 -0
- package/dist/adapters/summarizer.js.map +1 -0
- package/dist/adapters/token-estimator.d.ts +63 -0
- package/dist/adapters/token-estimator.d.ts.map +1 -0
- package/dist/adapters/token-estimator.js +37 -0
- package/dist/adapters/token-estimator.js.map +1 -0
- package/dist/builder/context-builder.d.ts +186 -0
- package/dist/builder/context-builder.d.ts.map +1 -0
- package/dist/builder/context-builder.js +305 -0
- package/dist/builder/context-builder.js.map +1 -0
- package/dist/builder/context-fork.d.ts +166 -0
- package/dist/builder/context-fork.d.ts.map +1 -0
- package/dist/builder/context-fork.js +282 -0
- package/dist/builder/context-fork.js.map +1 -0
- package/dist/builder/index.d.ts +6 -0
- package/dist/builder/index.d.ts.map +1 -0
- package/dist/builder/index.js +22 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/codecs/base.d.ts +18 -0
- package/dist/codecs/base.d.ts.map +1 -0
- package/dist/codecs/base.js +39 -0
- package/dist/codecs/base.js.map +1 -0
- package/dist/codecs/conversation-history.codec.d.ts +81 -0
- package/dist/codecs/conversation-history.codec.d.ts.map +1 -0
- package/dist/codecs/conversation-history.codec.js +89 -0
- package/dist/codecs/conversation-history.codec.js.map +1 -0
- package/dist/codecs/index.d.ts +31 -0
- package/dist/codecs/index.d.ts.map +1 -0
- package/dist/codecs/index.js +71 -0
- package/dist/codecs/index.js.map +1 -0
- package/dist/codecs/redacted-stub.codec.d.ts +32 -0
- package/dist/codecs/redacted-stub.codec.d.ts.map +1 -0
- package/dist/codecs/redacted-stub.codec.js +64 -0
- package/dist/codecs/redacted-stub.codec.js.map +1 -0
- package/dist/codecs/structured-reference.codec.d.ts +40 -0
- package/dist/codecs/structured-reference.codec.d.ts.map +1 -0
- package/dist/codecs/structured-reference.codec.js +81 -0
- package/dist/codecs/structured-reference.codec.js.map +1 -0
- package/dist/codecs/system-rules.codec.d.ts +32 -0
- package/dist/codecs/system-rules.codec.d.ts.map +1 -0
- package/dist/codecs/system-rules.codec.js +62 -0
- package/dist/codecs/system-rules.codec.js.map +1 -0
- package/dist/codecs/tool-output.codec.d.ts +66 -0
- package/dist/codecs/tool-output.codec.d.ts.map +1 -0
- package/dist/codecs/tool-output.codec.js +95 -0
- package/dist/codecs/tool-output.codec.js.map +1 -0
- package/dist/codecs/tool-schema.codec.d.ts +36 -0
- package/dist/codecs/tool-schema.codec.d.ts.map +1 -0
- package/dist/codecs/tool-schema.codec.js +74 -0
- package/dist/codecs/tool-schema.codec.js.map +1 -0
- package/dist/codecs/unsafe-text.codec.d.ts +28 -0
- package/dist/codecs/unsafe-text.codec.d.ts.map +1 -0
- package/dist/codecs/unsafe-text.codec.js +63 -0
- package/dist/codecs/unsafe-text.codec.js.map +1 -0
- package/dist/graph/context-graph.d.ts +121 -0
- package/dist/graph/context-graph.d.ts.map +1 -0
- package/dist/graph/context-graph.js +166 -0
- package/dist/graph/context-graph.js.map +1 -0
- package/dist/graph/index.d.ts +8 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +24 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/kind-order.d.ts +60 -0
- package/dist/graph/kind-order.d.ts.map +1 -0
- package/dist/graph/kind-order.js +113 -0
- package/dist/graph/kind-order.js.map +1 -0
- package/dist/graph/queries.d.ts +68 -0
- package/dist/graph/queries.d.ts.map +1 -0
- package/dist/graph/queries.js +240 -0
- package/dist/graph/queries.js.map +1 -0
- package/dist/graph/views.d.ts +90 -0
- package/dist/graph/views.d.ts.map +1 -0
- package/dist/graph/views.js +173 -0
- package/dist/graph/views.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline/compactor.d.ts +128 -0
- package/dist/pipeline/compactor.d.ts.map +1 -0
- package/dist/pipeline/compactor.js +346 -0
- package/dist/pipeline/compactor.js.map +1 -0
- package/dist/pipeline/index.d.ts +6 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +22 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/pipeline/summarizer.d.ts +18 -0
- package/dist/pipeline/summarizer.d.ts.map +1 -0
- package/dist/pipeline/summarizer.js +68 -0
- package/dist/pipeline/summarizer.js.map +1 -0
- package/dist/policies/default-policy.d.ts +29 -0
- package/dist/policies/default-policy.d.ts.map +1 -0
- package/dist/policies/default-policy.js +58 -0
- package/dist/policies/default-policy.js.map +1 -0
- package/dist/policies/index.d.ts +5 -0
- package/dist/policies/index.d.ts.map +1 -0
- package/dist/policies/index.js +21 -0
- package/dist/policies/index.js.map +1 -0
- package/dist/providers/anthropic-compiler.d.ts +58 -0
- package/dist/providers/anthropic-compiler.d.ts.map +1 -0
- package/dist/providers/anthropic-compiler.js +182 -0
- package/dist/providers/anthropic-compiler.js.map +1 -0
- package/dist/providers/capabilities.d.ts +54 -0
- package/dist/providers/capabilities.d.ts.map +1 -0
- package/dist/providers/capabilities.js +87 -0
- package/dist/providers/capabilities.js.map +1 -0
- package/dist/providers/gemini-compiler.d.ts +51 -0
- package/dist/providers/gemini-compiler.d.ts.map +1 -0
- package/dist/providers/gemini-compiler.js +206 -0
- package/dist/providers/gemini-compiler.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +24 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compiler.d.ts +46 -0
- package/dist/providers/openai-compiler.d.ts.map +1 -0
- package/dist/providers/openai-compiler.js +149 -0
- package/dist/providers/openai-compiler.js.map +1 -0
- package/dist/types/attachment.d.ts +62 -0
- package/dist/types/attachment.d.ts.map +1 -0
- package/dist/types/attachment.js +6 -0
- package/dist/types/attachment.js.map +1 -0
- package/dist/types/block.d.ts +61 -0
- package/dist/types/block.d.ts.map +1 -0
- package/dist/types/block.js +8 -0
- package/dist/types/block.js.map +1 -0
- package/dist/types/codec.d.ts +58 -0
- package/dist/types/codec.d.ts.map +1 -0
- package/dist/types/codec.js +6 -0
- package/dist/types/codec.js.map +1 -0
- package/dist/types/compiled.d.ts +91 -0
- package/dist/types/compiled.d.ts.map +1 -0
- package/dist/types/compiled.js +6 -0
- package/dist/types/compiled.js.map +1 -0
- package/dist/types/hash.d.ts +24 -0
- package/dist/types/hash.d.ts.map +1 -0
- package/dist/types/hash.js +49 -0
- package/dist/types/hash.js.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +26 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/policy.d.ts +128 -0
- package/dist/types/policy.d.ts.map +1 -0
- package/dist/types/policy.js +55 -0
- package/dist/types/policy.js.map +1 -0
- package/package.json +55 -0
- package/postcss.config.js +4 -0
- package/src/__tests__/attachment-selector.test.ts +559 -0
- package/src/__tests__/cache-breakpoints.test.ts +566 -0
- package/src/__tests__/codecs.test.ts +417 -0
- package/src/__tests__/compactor.test.ts +608 -0
- package/src/__tests__/context-graph.test.ts +383 -0
- package/src/__tests__/hash.test.ts +274 -0
- package/src/__tests__/integration.test.ts +866 -0
- package/src/__tests__/kind-order.test.ts +312 -0
- package/src/__tests__/phase2-integration.test.ts +253 -0
- package/src/__tests__/queries.test.ts +387 -0
- package/src/__tests__/token-estimator.test.ts +326 -0
- package/src/adapters/anthropic-estimator.ts +125 -0
- package/src/adapters/attachment-resolver.ts +295 -0
- package/src/adapters/attachment-selector.ts +218 -0
- package/src/adapters/gemini-estimator.ts +93 -0
- package/src/adapters/index.ts +12 -0
- package/src/adapters/memory-store.ts +299 -0
- package/src/adapters/openai-estimator.ts +105 -0
- package/src/adapters/summarizer.ts +250 -0
- package/src/adapters/token-estimator.ts +74 -0
- package/src/builder/context-builder.ts +467 -0
- package/src/builder/context-fork.ts +471 -0
- package/src/builder/index.ts +6 -0
- package/src/codecs/base.ts +36 -0
- package/src/codecs/conversation-history.codec.ts +108 -0
- package/src/codecs/index.ts +57 -0
- package/src/codecs/redacted-stub.codec.ts +76 -0
- package/src/codecs/structured-reference.codec.ts +96 -0
- package/src/codecs/system-rules.codec.ts +74 -0
- package/src/codecs/tool-output.codec.ts +109 -0
- package/src/codecs/tool-schema.codec.ts +87 -0
- package/src/codecs/unsafe-text.codec.ts +74 -0
- package/src/graph/context-graph.ts +205 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/kind-order.ts +125 -0
- package/src/graph/queries.ts +306 -0
- package/src/graph/views.ts +255 -0
- package/src/index.ts +31 -0
- package/src/pipeline/compactor.ts +563 -0
- package/src/pipeline/index.ts +6 -0
- package/src/pipeline/summarizer.ts +76 -0
- package/src/policies/default-policy.ts +69 -0
- package/src/policies/index.ts +5 -0
- package/src/providers/anthropic-compiler.ts +294 -0
- package/src/providers/capabilities.ts +144 -0
- package/src/providers/gemini-compiler.ts +272 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/openai-compiler.ts +191 -0
- package/src/types/attachment.ts +86 -0
- package/src/types/block.ts +84 -0
- package/src/types/codec.ts +68 -0
- package/src/types/compiled.ts +109 -0
- package/src/types/hash.ts +58 -0
- package/src/types/index.ts +10 -0
- package/src/types/policy.ts +194 -0
- package/tsconfig.json +21 -0
- 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
|
+
});
|