@lssm/example.kb-update-pipeline 0.0.0-canary-20251213172311
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.log +23 -0
- package/CHANGELOG.md +11 -0
- package/README.md +18 -0
- package/dist/contracts/index.js +1 -0
- package/dist/contracts/pipeline.js +1 -0
- package/dist/docs/index.js +1 -0
- package/dist/docs/kb-update-pipeline.docblock.js +19 -0
- package/dist/entities/index.js +1 -0
- package/dist/entities/models.js +1 -0
- package/dist/events.js +1 -0
- package/dist/example.js +1 -0
- package/dist/feature.js +1 -0
- package/dist/handlers/index.js +1 -0
- package/dist/handlers/memory.handlers.js +1 -0
- package/dist/index.js +1 -0
- package/example.ts +1 -0
- package/package.json +66 -0
- package/src/contracts/index.ts +3 -0
- package/src/contracts/pipeline.ts +146 -0
- package/src/docs/index.ts +3 -0
- package/src/docs/kb-update-pipeline.docblock.ts +31 -0
- package/src/entities/index.ts +3 -0
- package/src/entities/models.ts +45 -0
- package/src/events.ts +92 -0
- package/src/example.ts +29 -0
- package/src/feature.ts +39 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/memory.handlers.test.ts +80 -0
- package/src/handlers/memory.handlers.ts +180 -0
- package/src/index.ts +15 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.js +9 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createPipelineMemoryHandlers, createPipelineMemoryStore } from './memory.handlers';
|
|
4
|
+
|
|
5
|
+
describe('@lssm/example.kb-update-pipeline memory handlers', () => {
|
|
6
|
+
it('high-risk change cannot be approved by curator role', async () => {
|
|
7
|
+
const store = createPipelineMemoryStore();
|
|
8
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
9
|
+
|
|
10
|
+
store.candidates.set('c1', {
|
|
11
|
+
id: 'c1',
|
|
12
|
+
sourceDocumentId: 'EU_src1',
|
|
13
|
+
detectedAt: new Date(),
|
|
14
|
+
diffSummary: 'Change detected',
|
|
15
|
+
riskLevel: 'high',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c1' });
|
|
19
|
+
await expect(
|
|
20
|
+
pipeline.submitDecision({
|
|
21
|
+
reviewTaskId: review.id,
|
|
22
|
+
decision: 'approve',
|
|
23
|
+
decidedBy: 'u_curator',
|
|
24
|
+
decidedByRole: 'curator',
|
|
25
|
+
})
|
|
26
|
+
).rejects.toThrow('FORBIDDEN_ROLE');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('fires a notification when review is requested', async () => {
|
|
30
|
+
const store = createPipelineMemoryStore();
|
|
31
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
32
|
+
|
|
33
|
+
store.candidates.set('c2', {
|
|
34
|
+
id: 'c2',
|
|
35
|
+
sourceDocumentId: 'EU_src2',
|
|
36
|
+
detectedAt: new Date(),
|
|
37
|
+
diffSummary: 'Minor change',
|
|
38
|
+
riskLevel: 'low',
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c2' });
|
|
42
|
+
expect(store.notifications.length).toBe(1);
|
|
43
|
+
expect(store.notifications[0]?.reviewTaskId).toBe(review.id);
|
|
44
|
+
expect(store.notifications[0]?.assignedRole).toBe('curator');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('publishing fails if any included rule versions are not approved', async () => {
|
|
48
|
+
const store = createPipelineMemoryStore();
|
|
49
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
50
|
+
|
|
51
|
+
store.candidates.set('c3', {
|
|
52
|
+
id: 'c3',
|
|
53
|
+
sourceDocumentId: 'EU_src3',
|
|
54
|
+
detectedAt: new Date(),
|
|
55
|
+
diffSummary: 'Change',
|
|
56
|
+
riskLevel: 'low',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c3' });
|
|
60
|
+
await pipeline.proposeRulePatch({
|
|
61
|
+
changeCandidateId: 'c3',
|
|
62
|
+
proposedRuleVersionIds: ['rv1', 'rv2'],
|
|
63
|
+
});
|
|
64
|
+
await pipeline.submitDecision({
|
|
65
|
+
reviewTaskId: review.id,
|
|
66
|
+
decision: 'approve',
|
|
67
|
+
decidedBy: 'u_curator',
|
|
68
|
+
decidedByRole: 'curator',
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// only approve one of the proposed rule versions
|
|
72
|
+
await pipeline.markRuleVersionApproved({ ruleVersionId: 'rv1' });
|
|
73
|
+
|
|
74
|
+
await expect(
|
|
75
|
+
pipeline.publishIfReady({ jurisdiction: 'EU' })
|
|
76
|
+
).rejects.toThrow('NOT_READY');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
type ChangeCandidate = {
|
|
2
|
+
id: string;
|
|
3
|
+
sourceDocumentId: string;
|
|
4
|
+
detectedAt: Date;
|
|
5
|
+
diffSummary: string;
|
|
6
|
+
riskLevel: 'low' | 'medium' | 'high';
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type ReviewTask = {
|
|
10
|
+
id: string;
|
|
11
|
+
changeCandidateId: string;
|
|
12
|
+
status: 'open' | 'decided';
|
|
13
|
+
assignedRole: 'curator' | 'expert';
|
|
14
|
+
decision?: 'approve' | 'reject';
|
|
15
|
+
decidedAt?: Date;
|
|
16
|
+
decidedBy?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface PipelineMemoryStore {
|
|
20
|
+
candidates: Map<string, ChangeCandidate>;
|
|
21
|
+
reviewTasks: Map<string, ReviewTask>;
|
|
22
|
+
proposedRuleVersionIdsByCandidate: Map<string, string[]>;
|
|
23
|
+
approvedRuleVersionIds: Set<string>;
|
|
24
|
+
notifications: Array<{
|
|
25
|
+
kind: 'kb.review.requested';
|
|
26
|
+
reviewTaskId: string;
|
|
27
|
+
changeCandidateId: string;
|
|
28
|
+
assignedRole: 'curator' | 'expert';
|
|
29
|
+
createdAt: Date;
|
|
30
|
+
}>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createPipelineMemoryStore(): PipelineMemoryStore {
|
|
34
|
+
return {
|
|
35
|
+
candidates: new Map(),
|
|
36
|
+
reviewTasks: new Map(),
|
|
37
|
+
proposedRuleVersionIdsByCandidate: new Map(),
|
|
38
|
+
approvedRuleVersionIds: new Set(),
|
|
39
|
+
notifications: [],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function stableId(prefix: string, value: string): string {
|
|
44
|
+
return `${prefix}_${value.replace(/[^a-zA-Z0-9_-]/g, '_')}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PipelineMemoryHandlers {
|
|
48
|
+
runWatch(input: { jurisdiction: string }): Promise<{ candidates: ChangeCandidate[] }>;
|
|
49
|
+
createReviewTask(input: { changeCandidateId: string }): Promise<ReviewTask>;
|
|
50
|
+
proposeRulePatch(input: {
|
|
51
|
+
changeCandidateId: string;
|
|
52
|
+
proposedRuleVersionIds: string[];
|
|
53
|
+
}): Promise<{ proposedRuleVersionIds: string[] }>;
|
|
54
|
+
markRuleVersionApproved(input: { ruleVersionId: string }): Promise<{ ruleVersionId: string }>;
|
|
55
|
+
submitDecision(input: {
|
|
56
|
+
reviewTaskId: string;
|
|
57
|
+
decision: 'approve' | 'reject';
|
|
58
|
+
decidedBy: string;
|
|
59
|
+
decidedByRole: 'curator' | 'expert';
|
|
60
|
+
}): Promise<ReviewTask>;
|
|
61
|
+
publishIfReady(input: { jurisdiction: string }): Promise<{ published: boolean; reason?: string }>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function createPipelineMemoryHandlers(
|
|
65
|
+
store: PipelineMemoryStore
|
|
66
|
+
): PipelineMemoryHandlers {
|
|
67
|
+
async function runWatch(input: { jurisdiction: string }) {
|
|
68
|
+
// demo: always returns empty unless caller pre-seeds candidates
|
|
69
|
+
const candidates = [...store.candidates.values()].filter(
|
|
70
|
+
(c) => c.sourceDocumentId.startsWith(`${input.jurisdiction}_`) || true
|
|
71
|
+
);
|
|
72
|
+
return { candidates };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function createReviewTask(input: { changeCandidateId: string }) {
|
|
76
|
+
const candidate = store.candidates.get(input.changeCandidateId);
|
|
77
|
+
if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
|
|
78
|
+
const assignedRole = candidate.riskLevel === 'high' ? 'expert' : 'curator';
|
|
79
|
+
const id = stableId('review', input.changeCandidateId);
|
|
80
|
+
const task: ReviewTask = {
|
|
81
|
+
id,
|
|
82
|
+
changeCandidateId: input.changeCandidateId,
|
|
83
|
+
status: 'open',
|
|
84
|
+
assignedRole,
|
|
85
|
+
decision: undefined,
|
|
86
|
+
decidedAt: undefined,
|
|
87
|
+
decidedBy: undefined,
|
|
88
|
+
};
|
|
89
|
+
store.reviewTasks.set(id, task);
|
|
90
|
+
store.notifications.push({
|
|
91
|
+
kind: 'kb.review.requested',
|
|
92
|
+
reviewTaskId: id,
|
|
93
|
+
changeCandidateId: input.changeCandidateId,
|
|
94
|
+
assignedRole,
|
|
95
|
+
createdAt: new Date(),
|
|
96
|
+
});
|
|
97
|
+
return task;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function proposeRulePatch(input: {
|
|
101
|
+
changeCandidateId: string;
|
|
102
|
+
proposedRuleVersionIds: string[];
|
|
103
|
+
}): Promise<{ proposedRuleVersionIds: string[] }> {
|
|
104
|
+
if (!store.candidates.has(input.changeCandidateId)) {
|
|
105
|
+
throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
|
|
106
|
+
}
|
|
107
|
+
store.proposedRuleVersionIdsByCandidate.set(
|
|
108
|
+
input.changeCandidateId,
|
|
109
|
+
[...input.proposedRuleVersionIds]
|
|
110
|
+
);
|
|
111
|
+
return { proposedRuleVersionIds: [...input.proposedRuleVersionIds] };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function markRuleVersionApproved(input: {
|
|
115
|
+
ruleVersionId: string;
|
|
116
|
+
}): Promise<{ ruleVersionId: string }> {
|
|
117
|
+
store.approvedRuleVersionIds.add(input.ruleVersionId);
|
|
118
|
+
return { ruleVersionId: input.ruleVersionId };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function submitDecision(input: {
|
|
122
|
+
reviewTaskId: string;
|
|
123
|
+
decision: 'approve' | 'reject';
|
|
124
|
+
decidedBy: string;
|
|
125
|
+
decidedByRole: 'curator' | 'expert';
|
|
126
|
+
}) {
|
|
127
|
+
const task = store.reviewTasks.get(input.reviewTaskId);
|
|
128
|
+
if (!task) throw new Error('REVIEW_TASK_NOT_FOUND');
|
|
129
|
+
const candidate = store.candidates.get(task.changeCandidateId);
|
|
130
|
+
if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
|
|
131
|
+
if (candidate.riskLevel === 'high' && input.decision === 'approve') {
|
|
132
|
+
if (input.decidedByRole !== 'expert') throw new Error('FORBIDDEN_ROLE');
|
|
133
|
+
}
|
|
134
|
+
const decided: ReviewTask = {
|
|
135
|
+
...task,
|
|
136
|
+
status: 'decided',
|
|
137
|
+
decision: input.decision,
|
|
138
|
+
decidedAt: new Date(),
|
|
139
|
+
decidedBy: input.decidedBy,
|
|
140
|
+
};
|
|
141
|
+
store.reviewTasks.set(decided.id, decided);
|
|
142
|
+
return decided;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function publishIfReady(_input: { jurisdiction: string }) {
|
|
146
|
+
const openTasks = [...store.reviewTasks.values()].filter(
|
|
147
|
+
(t) => t.status !== 'decided'
|
|
148
|
+
);
|
|
149
|
+
if (openTasks.length) {
|
|
150
|
+
throw new Error('NOT_READY');
|
|
151
|
+
}
|
|
152
|
+
const rejected = [...store.reviewTasks.values()].some(
|
|
153
|
+
(t) => t.decision === 'reject'
|
|
154
|
+
);
|
|
155
|
+
if (rejected) return { published: false, reason: 'REJECTED' };
|
|
156
|
+
|
|
157
|
+
// Ensure every proposed rule version is approved before publishing.
|
|
158
|
+
for (const task of store.reviewTasks.values()) {
|
|
159
|
+
if (task.decision !== 'approve') continue;
|
|
160
|
+
const proposed =
|
|
161
|
+
store.proposedRuleVersionIdsByCandidate.get(task.changeCandidateId) ?? [];
|
|
162
|
+
const unapproved = proposed.filter((id) => !store.approvedRuleVersionIds.has(id));
|
|
163
|
+
if (unapproved.length) {
|
|
164
|
+
throw new Error('NOT_READY');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { published: true };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
runWatch,
|
|
172
|
+
createReviewTask,
|
|
173
|
+
proposeRulePatch,
|
|
174
|
+
markRuleVersionApproved,
|
|
175
|
+
submitDecision,
|
|
176
|
+
publishIfReady,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KB Update Pipeline Example
|
|
3
|
+
*
|
|
4
|
+
* Automation proposes KB updates; humans verify; everything audited and notified.
|
|
5
|
+
*/
|
|
6
|
+
export * from './entities';
|
|
7
|
+
export * from './contracts';
|
|
8
|
+
export * from './events';
|
|
9
|
+
export * from './handlers';
|
|
10
|
+
export * from './feature';
|
|
11
|
+
export { default as example } from './example';
|
|
12
|
+
|
|
13
|
+
import './docs';
|
|
14
|
+
|
|
15
|
+
|