@contractspec/example.policy-safe-knowledge-assistant 1.44.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/.turbo/turbo-build$colon$bundle.log +35 -0
- package/.turbo/turbo-build.log +36 -0
- package/CHANGELOG.md +365 -0
- package/IMPLEMENTATION_SKETCH.md +40 -0
- package/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/docs/index.d.ts +1 -0
- package/dist/docs/index.js +1 -0
- package/dist/docs/policy-safe-knowledge-assistant.docblock.d.ts +1 -0
- package/dist/docs/policy-safe-knowledge-assistant.docblock.js +35 -0
- package/dist/docs/policy-safe-knowledge-assistant.docblock.js.map +1 -0
- package/dist/example.d.ts +35 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/example.js +47 -0
- package/dist/example.js.map +1 -0
- package/dist/feature.d.ts +7 -0
- package/dist/feature.d.ts.map +1 -0
- package/dist/feature.js +148 -0
- package/dist/feature.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/orchestrator/buildAnswer.d.ts +53 -0
- package/dist/orchestrator/buildAnswer.d.ts.map +1 -0
- package/dist/orchestrator/buildAnswer.js +77 -0
- package/dist/orchestrator/buildAnswer.js.map +1 -0
- package/dist/seed/fixtures.d.ts +35 -0
- package/dist/seed/fixtures.d.ts.map +1 -0
- package/dist/seed/fixtures.js +34 -0
- package/dist/seed/fixtures.js.map +1 -0
- package/dist/seed/index.d.ts +2 -0
- package/dist/seed/index.js +3 -0
- package/package.json +77 -0
- package/src/docs/index.ts +1 -0
- package/src/docs/policy-safe-knowledge-assistant.docblock.ts +28 -0
- package/src/example.ts +29 -0
- package/src/feature.ts +58 -0
- package/src/index.ts +9 -0
- package/src/integration.test.ts +108 -0
- package/src/orchestrator/buildAnswer.ts +122 -0
- package/src/seed/fixtures.ts +31 -0
- package/src/seed/index.ts +1 -0
- package/tsconfig.json +19 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.js +17 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createMemoryKbHandlers,
|
|
5
|
+
createMemoryKbStore,
|
|
6
|
+
} from '@contractspec/example.versioned-knowledge-base/handlers/memory.handlers';
|
|
7
|
+
import {
|
|
8
|
+
createPipelineMemoryHandlers,
|
|
9
|
+
createPipelineMemoryStore,
|
|
10
|
+
} from '@contractspec/example.kb-update-pipeline/handlers/memory.handlers';
|
|
11
|
+
|
|
12
|
+
import { buildPolicySafeAnswer } from './orchestrator/buildAnswer';
|
|
13
|
+
import { DEMO_FIXTURES } from './seed/fixtures';
|
|
14
|
+
|
|
15
|
+
describe('@contractspec/example.policy-safe-knowledge-assistant integration', () => {
|
|
16
|
+
it('answers cite latest snapshot; after pipeline change + publish, answers cite new snapshot', async () => {
|
|
17
|
+
const kbStore = createMemoryKbStore();
|
|
18
|
+
const kb = createMemoryKbHandlers(kbStore);
|
|
19
|
+
|
|
20
|
+
const pipelineStore = createPipelineMemoryStore();
|
|
21
|
+
const pipeline = createPipelineMemoryHandlers(pipelineStore);
|
|
22
|
+
|
|
23
|
+
// Seed rules
|
|
24
|
+
await kb.createRule(DEMO_FIXTURES.rules.EU_RULE_TAX);
|
|
25
|
+
|
|
26
|
+
// Publish initial snapshot (EU v1)
|
|
27
|
+
const rv1 = await kb.upsertRuleVersion({
|
|
28
|
+
ruleId: DEMO_FIXTURES.rules.EU_RULE_TAX.id,
|
|
29
|
+
content: 'EU: Reporting obligations v1',
|
|
30
|
+
sourceRefs: [{ sourceDocumentId: 'src_eu_v1', excerpt: 'v1 excerpt' }],
|
|
31
|
+
});
|
|
32
|
+
await kb.approveRuleVersion({
|
|
33
|
+
ruleVersionId: rv1.id,
|
|
34
|
+
approver: 'expert_1',
|
|
35
|
+
});
|
|
36
|
+
const snap1 = await kb.publishSnapshot({
|
|
37
|
+
jurisdiction: 'EU',
|
|
38
|
+
asOfDate: new Date('2026-01-01T00:00:00.000Z'),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const envelopeBase = {
|
|
42
|
+
traceId: 'trace_1',
|
|
43
|
+
locale: 'en-GB',
|
|
44
|
+
regulatoryContext: { jurisdiction: 'EU' },
|
|
45
|
+
allowedScope: 'education_only' as const,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const a1 = await buildPolicySafeAnswer({
|
|
49
|
+
envelope: { ...envelopeBase, kbSnapshotId: snap1.id },
|
|
50
|
+
question: 'reporting obligations',
|
|
51
|
+
kbSearch: kb.search,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(a1.refused).not.toBeTrue();
|
|
55
|
+
expect(a1.citations.length).toBeGreaterThan(0);
|
|
56
|
+
expect(a1.citations[0]?.kbSnapshotId).toBe(snap1.id);
|
|
57
|
+
|
|
58
|
+
// Simulate regulatory change via pipeline: create candidate, review, propose patch
|
|
59
|
+
pipelineStore.candidates.set('cand_1', {
|
|
60
|
+
id: 'cand_1',
|
|
61
|
+
sourceDocumentId: 'EU_src_change',
|
|
62
|
+
detectedAt: new Date('2026-02-01T00:00:00.000Z'),
|
|
63
|
+
diffSummary: 'Updated obligations',
|
|
64
|
+
riskLevel: 'high',
|
|
65
|
+
});
|
|
66
|
+
const review = await pipeline.createReviewTask({
|
|
67
|
+
changeCandidateId: 'cand_1',
|
|
68
|
+
});
|
|
69
|
+
await pipeline.submitDecision({
|
|
70
|
+
reviewTaskId: review.id,
|
|
71
|
+
decision: 'approve',
|
|
72
|
+
decidedBy: 'expert_2',
|
|
73
|
+
decidedByRole: 'expert',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Create + approve new KB rule version
|
|
77
|
+
const rv2 = await kb.upsertRuleVersion({
|
|
78
|
+
ruleId: DEMO_FIXTURES.rules.EU_RULE_TAX.id,
|
|
79
|
+
content: 'EU: Reporting obligations v2 (updated)',
|
|
80
|
+
sourceRefs: [{ sourceDocumentId: 'src_eu_v2', excerpt: 'v2 excerpt' }],
|
|
81
|
+
});
|
|
82
|
+
await kb.approveRuleVersion({
|
|
83
|
+
ruleVersionId: rv2.id,
|
|
84
|
+
approver: 'expert_2',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Link pipeline proposal to the actual KB rule version id, then mark it approved
|
|
88
|
+
await pipeline.proposeRulePatch({
|
|
89
|
+
changeCandidateId: 'cand_1',
|
|
90
|
+
proposedRuleVersionIds: [rv2.id],
|
|
91
|
+
});
|
|
92
|
+
await pipeline.markRuleVersionApproved({ ruleVersionId: rv2.id });
|
|
93
|
+
await pipeline.publishIfReady({ jurisdiction: 'EU' });
|
|
94
|
+
|
|
95
|
+
const snap2 = await kb.publishSnapshot({
|
|
96
|
+
jurisdiction: 'EU',
|
|
97
|
+
asOfDate: new Date('2026-02-01T00:00:00.000Z'),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const a2 = await buildPolicySafeAnswer({
|
|
101
|
+
envelope: { ...envelopeBase, kbSnapshotId: snap2.id },
|
|
102
|
+
question: 'updated obligations',
|
|
103
|
+
kbSearch: kb.search,
|
|
104
|
+
});
|
|
105
|
+
expect(a2.refused).not.toBeTrue();
|
|
106
|
+
expect(a2.citations[0]?.kbSnapshotId).toBe(snap2.id);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import {
|
|
2
|
+
enforceAllowedScope,
|
|
3
|
+
enforceCitations,
|
|
4
|
+
validateEnvelope,
|
|
5
|
+
} from '@contractspec/example.locale-jurisdiction-gate/policy/guard';
|
|
6
|
+
|
|
7
|
+
type AllowedScope = 'education_only' | 'generic_info' | 'escalation_required';
|
|
8
|
+
|
|
9
|
+
export interface AssistantAnswerIR {
|
|
10
|
+
locale: string;
|
|
11
|
+
jurisdiction: string;
|
|
12
|
+
allowedScope: AllowedScope;
|
|
13
|
+
sections: { heading: string; body: string }[];
|
|
14
|
+
citations: {
|
|
15
|
+
kbSnapshotId: string;
|
|
16
|
+
sourceType: string;
|
|
17
|
+
sourceId: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
excerpt?: string;
|
|
20
|
+
}[];
|
|
21
|
+
disclaimers?: string[];
|
|
22
|
+
riskFlags?: string[];
|
|
23
|
+
refused?: boolean;
|
|
24
|
+
refusalReason?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface BuildAnswerInput {
|
|
28
|
+
envelope: {
|
|
29
|
+
traceId: string;
|
|
30
|
+
locale: string;
|
|
31
|
+
kbSnapshotId: string;
|
|
32
|
+
allowedScope: AllowedScope;
|
|
33
|
+
regulatoryContext: { jurisdiction: string };
|
|
34
|
+
};
|
|
35
|
+
question: string;
|
|
36
|
+
kbSearch: (input: {
|
|
37
|
+
snapshotId: string;
|
|
38
|
+
jurisdiction: string;
|
|
39
|
+
query: string;
|
|
40
|
+
}) => Promise<{ items: { ruleVersionId: string; excerpt?: string }[] }>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Build a policy-safe assistant answer derived from KB search results.
|
|
45
|
+
*
|
|
46
|
+
* Deterministic: no LLM calls; if search yields no results, it refuses.
|
|
47
|
+
*/
|
|
48
|
+
export async function buildPolicySafeAnswer(
|
|
49
|
+
input: BuildAnswerInput
|
|
50
|
+
): Promise<AssistantAnswerIR> {
|
|
51
|
+
const env = validateEnvelope(input.envelope);
|
|
52
|
+
if (!env.ok) {
|
|
53
|
+
return {
|
|
54
|
+
locale: input.envelope.locale ?? 'en-GB',
|
|
55
|
+
jurisdiction: input.envelope.regulatoryContext?.jurisdiction ?? 'UNKNOWN',
|
|
56
|
+
allowedScope: input.envelope.allowedScope ?? 'education_only',
|
|
57
|
+
sections: [{ heading: 'Request blocked', body: env.error.message }],
|
|
58
|
+
citations: [],
|
|
59
|
+
disclaimers: ['This system refuses to answer without a valid envelope.'],
|
|
60
|
+
riskFlags: [env.error.code],
|
|
61
|
+
refused: true,
|
|
62
|
+
refusalReason: env.error.code,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const results = await input.kbSearch({
|
|
67
|
+
snapshotId: env.value.kbSnapshotId,
|
|
68
|
+
jurisdiction: env.value.regulatoryContext?.jurisdiction ?? 'UNKNOWN',
|
|
69
|
+
query: input.question,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const citations = results.items.map((item) => ({
|
|
73
|
+
kbSnapshotId: env.value.kbSnapshotId,
|
|
74
|
+
sourceType: 'ruleVersion',
|
|
75
|
+
sourceId: item.ruleVersionId,
|
|
76
|
+
title: 'Curated rule version',
|
|
77
|
+
excerpt: item.excerpt,
|
|
78
|
+
}));
|
|
79
|
+
|
|
80
|
+
const draft: AssistantAnswerIR = {
|
|
81
|
+
locale: env.value.locale,
|
|
82
|
+
jurisdiction: env.value.regulatoryContext?.jurisdiction ?? 'UNKNOWN',
|
|
83
|
+
allowedScope: env.value.allowedScope,
|
|
84
|
+
sections: [
|
|
85
|
+
{
|
|
86
|
+
heading: 'Answer (KB-derived)',
|
|
87
|
+
body:
|
|
88
|
+
results.items.length > 0
|
|
89
|
+
? `This answer is derived from ${results.items.length} curated rule version(s) in the referenced snapshot.`
|
|
90
|
+
: 'No curated knowledge found in the referenced snapshot.',
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
citations,
|
|
94
|
+
disclaimers: ['Educational demo only.'],
|
|
95
|
+
riskFlags: [],
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const scope = enforceAllowedScope(env.value.allowedScope, draft);
|
|
99
|
+
if (!scope.ok) {
|
|
100
|
+
return {
|
|
101
|
+
...draft,
|
|
102
|
+
sections: [{ heading: 'Escalation required', body: scope.error.message }],
|
|
103
|
+
refused: true,
|
|
104
|
+
refusalReason: scope.error.code,
|
|
105
|
+
riskFlags: [...(draft.riskFlags ?? []), scope.error.code],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const cited = enforceCitations(draft);
|
|
110
|
+
if (!cited.ok) {
|
|
111
|
+
return {
|
|
112
|
+
...draft,
|
|
113
|
+
sections: [{ heading: 'Request blocked', body: cited.error.message }],
|
|
114
|
+
citations: [],
|
|
115
|
+
refused: true,
|
|
116
|
+
refusalReason: cited.error.code,
|
|
117
|
+
riskFlags: [...(draft.riskFlags ?? []), cited.error.code],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return draft;
|
|
122
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const DEMO_FIXTURES = {
|
|
2
|
+
jurisdictions: ['EU', 'FR'] as const,
|
|
3
|
+
locales: ['en-GB', 'fr-FR'] as const,
|
|
4
|
+
demoOrgId: 'org_demo',
|
|
5
|
+
demoUserId: 'user_demo',
|
|
6
|
+
sources: {
|
|
7
|
+
EU_SOURCE_1: {
|
|
8
|
+
jurisdiction: 'EU',
|
|
9
|
+
authority: 'DemoAuthority',
|
|
10
|
+
title: 'EU Demo Source v1',
|
|
11
|
+
fetchedAt: new Date('2026-01-01T00:00:00.000Z'),
|
|
12
|
+
hash: 'hash_eu_v1',
|
|
13
|
+
fileId: 'file_eu_v1',
|
|
14
|
+
},
|
|
15
|
+
EU_SOURCE_2: {
|
|
16
|
+
jurisdiction: 'EU',
|
|
17
|
+
authority: 'DemoAuthority',
|
|
18
|
+
title: 'EU Demo Source v2',
|
|
19
|
+
fetchedAt: new Date('2026-02-01T00:00:00.000Z'),
|
|
20
|
+
hash: 'hash_eu_v2',
|
|
21
|
+
fileId: 'file_eu_v2',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
rules: {
|
|
25
|
+
EU_RULE_TAX: {
|
|
26
|
+
id: 'rule_eu_tax',
|
|
27
|
+
jurisdiction: 'EU',
|
|
28
|
+
topicKey: 'tax_reporting',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
} as const;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fixtures';
|
package/tsconfig.json
ADDED