@contractspec/example.kb-update-pipeline 3.7.5 → 3.7.7

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 (40) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/AGENTS.md +50 -29
  3. package/CHANGELOG.md +9 -0
  4. package/README.md +59 -12
  5. package/dist/browser/entities/index.js +2 -2
  6. package/dist/browser/entities/models.js +2 -2
  7. package/dist/browser/events.js +1 -1
  8. package/dist/browser/index.js +70 -70
  9. package/dist/browser/operations/index.js +3 -3
  10. package/dist/browser/operations/pipeline.js +3 -3
  11. package/dist/browser/presentations.js +2 -2
  12. package/dist/entities/index.js +2 -2
  13. package/dist/entities/models.js +2 -2
  14. package/dist/events.js +1 -1
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.js +70 -70
  17. package/dist/node/entities/index.js +2 -2
  18. package/dist/node/entities/models.js +2 -2
  19. package/dist/node/events.js +1 -1
  20. package/dist/node/index.js +70 -70
  21. package/dist/node/operations/index.js +3 -3
  22. package/dist/node/operations/pipeline.js +3 -3
  23. package/dist/node/presentations.js +2 -2
  24. package/dist/operations/index.js +3 -3
  25. package/dist/operations/pipeline.js +3 -3
  26. package/dist/presentations.js +2 -2
  27. package/package.json +7 -7
  28. package/src/docs/kb-update-pipeline.docblock.ts +22 -22
  29. package/src/entities/models.ts +36 -36
  30. package/src/events.ts +99 -99
  31. package/src/example.ts +28 -28
  32. package/src/handlers/memory.handlers.test.ts +64 -64
  33. package/src/handlers/memory.handlers.ts +158 -158
  34. package/src/index.ts +2 -2
  35. package/src/kb-update-pipeline.feature.ts +62 -62
  36. package/src/operations/pipeline.ts +125 -125
  37. package/src/presentations.ts +59 -59
  38. package/src/tests/operations.test-spec.ts +72 -72
  39. package/tsconfig.json +7 -17
  40. package/tsdown.config.js +7 -13
@@ -1,186 +1,186 @@
1
1
  interface ChangeCandidate {
2
- id: string;
3
- sourceDocumentId: string;
4
- detectedAt: Date;
5
- diffSummary: string;
6
- riskLevel: 'low' | 'medium' | 'high';
2
+ id: string;
3
+ sourceDocumentId: string;
4
+ detectedAt: Date;
5
+ diffSummary: string;
6
+ riskLevel: 'low' | 'medium' | 'high';
7
7
  }
8
8
 
9
9
  interface 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;
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
17
  }
18
18
 
19
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: {
25
- kind: 'kb.review.requested';
26
- reviewTaskId: string;
27
- changeCandidateId: string;
28
- assignedRole: 'curator' | 'expert';
29
- createdAt: Date;
30
- }[];
20
+ candidates: Map<string, ChangeCandidate>;
21
+ reviewTasks: Map<string, ReviewTask>;
22
+ proposedRuleVersionIdsByCandidate: Map<string, string[]>;
23
+ approvedRuleVersionIds: Set<string>;
24
+ notifications: {
25
+ kind: 'kb.review.requested';
26
+ reviewTaskId: string;
27
+ changeCandidateId: string;
28
+ assignedRole: 'curator' | 'expert';
29
+ createdAt: Date;
30
+ }[];
31
31
  }
32
32
 
33
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
- };
34
+ return {
35
+ candidates: new Map(),
36
+ reviewTasks: new Map(),
37
+ proposedRuleVersionIdsByCandidate: new Map(),
38
+ approvedRuleVersionIds: new Set(),
39
+ notifications: [],
40
+ };
41
41
  }
42
42
 
43
43
  function stableId(prefix: string, value: string): string {
44
- return `${prefix}_${value.replace(/[^a-zA-Z0-9_-]/g, '_')}`;
44
+ return `${prefix}_${value.replace(/[^a-zA-Z0-9_-]/g, '_')}`;
45
45
  }
46
46
 
47
47
  export interface PipelineMemoryHandlers {
48
- runWatch(input: {
49
- jurisdiction: string;
50
- }): Promise<{ candidates: ChangeCandidate[] }>;
51
- createReviewTask(input: { changeCandidateId: string }): Promise<ReviewTask>;
52
- proposeRulePatch(input: {
53
- changeCandidateId: string;
54
- proposedRuleVersionIds: string[];
55
- }): Promise<{ proposedRuleVersionIds: string[] }>;
56
- markRuleVersionApproved(input: {
57
- ruleVersionId: string;
58
- }): Promise<{ ruleVersionId: string }>;
59
- submitDecision(input: {
60
- reviewTaskId: string;
61
- decision: 'approve' | 'reject';
62
- decidedBy: string;
63
- decidedByRole: 'curator' | 'expert';
64
- }): Promise<ReviewTask>;
65
- publishIfReady(input: {
66
- jurisdiction: string;
67
- }): Promise<{ published: boolean; reason?: string }>;
48
+ runWatch(input: {
49
+ jurisdiction: string;
50
+ }): Promise<{ candidates: ChangeCandidate[] }>;
51
+ createReviewTask(input: { changeCandidateId: string }): Promise<ReviewTask>;
52
+ proposeRulePatch(input: {
53
+ changeCandidateId: string;
54
+ proposedRuleVersionIds: string[];
55
+ }): Promise<{ proposedRuleVersionIds: string[] }>;
56
+ markRuleVersionApproved(input: {
57
+ ruleVersionId: string;
58
+ }): Promise<{ ruleVersionId: string }>;
59
+ submitDecision(input: {
60
+ reviewTaskId: string;
61
+ decision: 'approve' | 'reject';
62
+ decidedBy: string;
63
+ decidedByRole: 'curator' | 'expert';
64
+ }): Promise<ReviewTask>;
65
+ publishIfReady(input: {
66
+ jurisdiction: string;
67
+ }): Promise<{ published: boolean; reason?: string }>;
68
68
  }
69
69
 
70
70
  export function createPipelineMemoryHandlers(
71
- store: PipelineMemoryStore
71
+ store: PipelineMemoryStore
72
72
  ): PipelineMemoryHandlers {
73
- async function runWatch(input: { jurisdiction: string }) {
74
- // demo: always returns empty unless caller pre-seeds candidates
75
- const candidates = [...store.candidates.values()].filter(
76
- (c) => c.sourceDocumentId.startsWith(`${input.jurisdiction}_`) || true
77
- );
78
- return { candidates };
79
- }
73
+ async function runWatch(input: { jurisdiction: string }) {
74
+ // demo: always returns empty unless caller pre-seeds candidates
75
+ const candidates = [...store.candidates.values()].filter(
76
+ (c) => c.sourceDocumentId.startsWith(`${input.jurisdiction}_`) || true
77
+ );
78
+ return { candidates };
79
+ }
80
80
 
81
- async function createReviewTask(input: { changeCandidateId: string }) {
82
- const candidate = store.candidates.get(input.changeCandidateId);
83
- if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
84
- const assignedRole = candidate.riskLevel === 'high' ? 'expert' : 'curator';
85
- const id = stableId('review', input.changeCandidateId);
86
- const task: ReviewTask = {
87
- id,
88
- changeCandidateId: input.changeCandidateId,
89
- status: 'open',
90
- assignedRole,
91
- decision: undefined,
92
- decidedAt: undefined,
93
- decidedBy: undefined,
94
- };
95
- store.reviewTasks.set(id, task);
96
- store.notifications.push({
97
- kind: 'kb.review.requested',
98
- reviewTaskId: id,
99
- changeCandidateId: input.changeCandidateId,
100
- assignedRole,
101
- createdAt: new Date(),
102
- });
103
- return task;
104
- }
81
+ async function createReviewTask(input: { changeCandidateId: string }) {
82
+ const candidate = store.candidates.get(input.changeCandidateId);
83
+ if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
84
+ const assignedRole = candidate.riskLevel === 'high' ? 'expert' : 'curator';
85
+ const id = stableId('review', input.changeCandidateId);
86
+ const task: ReviewTask = {
87
+ id,
88
+ changeCandidateId: input.changeCandidateId,
89
+ status: 'open',
90
+ assignedRole,
91
+ decision: undefined,
92
+ decidedAt: undefined,
93
+ decidedBy: undefined,
94
+ };
95
+ store.reviewTasks.set(id, task);
96
+ store.notifications.push({
97
+ kind: 'kb.review.requested',
98
+ reviewTaskId: id,
99
+ changeCandidateId: input.changeCandidateId,
100
+ assignedRole,
101
+ createdAt: new Date(),
102
+ });
103
+ return task;
104
+ }
105
105
 
106
- async function proposeRulePatch(input: {
107
- changeCandidateId: string;
108
- proposedRuleVersionIds: string[];
109
- }): Promise<{ proposedRuleVersionIds: string[] }> {
110
- if (!store.candidates.has(input.changeCandidateId)) {
111
- throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
112
- }
113
- store.proposedRuleVersionIdsByCandidate.set(input.changeCandidateId, [
114
- ...input.proposedRuleVersionIds,
115
- ]);
116
- return { proposedRuleVersionIds: [...input.proposedRuleVersionIds] };
117
- }
106
+ async function proposeRulePatch(input: {
107
+ changeCandidateId: string;
108
+ proposedRuleVersionIds: string[];
109
+ }): Promise<{ proposedRuleVersionIds: string[] }> {
110
+ if (!store.candidates.has(input.changeCandidateId)) {
111
+ throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
112
+ }
113
+ store.proposedRuleVersionIdsByCandidate.set(input.changeCandidateId, [
114
+ ...input.proposedRuleVersionIds,
115
+ ]);
116
+ return { proposedRuleVersionIds: [...input.proposedRuleVersionIds] };
117
+ }
118
118
 
119
- async function markRuleVersionApproved(input: {
120
- ruleVersionId: string;
121
- }): Promise<{ ruleVersionId: string }> {
122
- store.approvedRuleVersionIds.add(input.ruleVersionId);
123
- return { ruleVersionId: input.ruleVersionId };
124
- }
119
+ async function markRuleVersionApproved(input: {
120
+ ruleVersionId: string;
121
+ }): Promise<{ ruleVersionId: string }> {
122
+ store.approvedRuleVersionIds.add(input.ruleVersionId);
123
+ return { ruleVersionId: input.ruleVersionId };
124
+ }
125
125
 
126
- async function submitDecision(input: {
127
- reviewTaskId: string;
128
- decision: 'approve' | 'reject';
129
- decidedBy: string;
130
- decidedByRole: 'curator' | 'expert';
131
- }) {
132
- const task = store.reviewTasks.get(input.reviewTaskId);
133
- if (!task) throw new Error('REVIEW_TASK_NOT_FOUND');
134
- const candidate = store.candidates.get(task.changeCandidateId);
135
- if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
136
- if (candidate.riskLevel === 'high' && input.decision === 'approve') {
137
- if (input.decidedByRole !== 'expert') throw new Error('FORBIDDEN_ROLE');
138
- }
139
- const decided: ReviewTask = {
140
- ...task,
141
- status: 'decided',
142
- decision: input.decision,
143
- decidedAt: new Date(),
144
- decidedBy: input.decidedBy,
145
- };
146
- store.reviewTasks.set(decided.id, decided);
147
- return decided;
148
- }
126
+ async function submitDecision(input: {
127
+ reviewTaskId: string;
128
+ decision: 'approve' | 'reject';
129
+ decidedBy: string;
130
+ decidedByRole: 'curator' | 'expert';
131
+ }) {
132
+ const task = store.reviewTasks.get(input.reviewTaskId);
133
+ if (!task) throw new Error('REVIEW_TASK_NOT_FOUND');
134
+ const candidate = store.candidates.get(task.changeCandidateId);
135
+ if (!candidate) throw new Error('CHANGE_CANDIDATE_NOT_FOUND');
136
+ if (candidate.riskLevel === 'high' && input.decision === 'approve') {
137
+ if (input.decidedByRole !== 'expert') throw new Error('FORBIDDEN_ROLE');
138
+ }
139
+ const decided: ReviewTask = {
140
+ ...task,
141
+ status: 'decided',
142
+ decision: input.decision,
143
+ decidedAt: new Date(),
144
+ decidedBy: input.decidedBy,
145
+ };
146
+ store.reviewTasks.set(decided.id, decided);
147
+ return decided;
148
+ }
149
149
 
150
- async function publishIfReady(_input: { jurisdiction: string }) {
151
- const openTasks = [...store.reviewTasks.values()].filter(
152
- (t) => t.status !== 'decided'
153
- );
154
- if (openTasks.length) {
155
- throw new Error('NOT_READY');
156
- }
157
- const rejected = [...store.reviewTasks.values()].some(
158
- (t) => t.decision === 'reject'
159
- );
160
- if (rejected) return { published: false, reason: 'REJECTED' };
150
+ async function publishIfReady(_input: { jurisdiction: string }) {
151
+ const openTasks = [...store.reviewTasks.values()].filter(
152
+ (t) => t.status !== 'decided'
153
+ );
154
+ if (openTasks.length) {
155
+ throw new Error('NOT_READY');
156
+ }
157
+ const rejected = [...store.reviewTasks.values()].some(
158
+ (t) => t.decision === 'reject'
159
+ );
160
+ if (rejected) return { published: false, reason: 'REJECTED' };
161
161
 
162
- // Ensure every proposed rule version is approved before publishing.
163
- for (const task of store.reviewTasks.values()) {
164
- if (task.decision !== 'approve') continue;
165
- const proposed =
166
- store.proposedRuleVersionIdsByCandidate.get(task.changeCandidateId) ??
167
- [];
168
- const unapproved = proposed.filter(
169
- (id) => !store.approvedRuleVersionIds.has(id)
170
- );
171
- if (unapproved.length) {
172
- throw new Error('NOT_READY');
173
- }
174
- }
175
- return { published: true };
176
- }
162
+ // Ensure every proposed rule version is approved before publishing.
163
+ for (const task of store.reviewTasks.values()) {
164
+ if (task.decision !== 'approve') continue;
165
+ const proposed =
166
+ store.proposedRuleVersionIdsByCandidate.get(task.changeCandidateId) ??
167
+ [];
168
+ const unapproved = proposed.filter(
169
+ (id) => !store.approvedRuleVersionIds.has(id)
170
+ );
171
+ if (unapproved.length) {
172
+ throw new Error('NOT_READY');
173
+ }
174
+ }
175
+ return { published: true };
176
+ }
177
177
 
178
- return {
179
- runWatch,
180
- createReviewTask,
181
- proposeRulePatch,
182
- markRuleVersionApproved,
183
- submitDecision,
184
- publishIfReady,
185
- };
178
+ return {
179
+ runWatch,
180
+ createReviewTask,
181
+ proposeRulePatch,
182
+ markRuleVersionApproved,
183
+ submitDecision,
184
+ publishIfReady,
185
+ };
186
186
  }
package/src/index.ts CHANGED
@@ -4,11 +4,11 @@
4
4
  * Automation proposes KB updates; humans verify; everything audited and notified.
5
5
  */
6
6
  export * from './entities';
7
- export * from './operations';
8
7
  export * from './events';
8
+ export { default as example } from './example';
9
9
  export * from './handlers';
10
10
  export * from './kb-update-pipeline.feature';
11
- export { default as example } from './example';
11
+ export * from './operations';
12
12
 
13
13
  export * from './presentations';
14
14
 
@@ -1,70 +1,70 @@
1
1
  import { defineFeature } from '@contractspec/lib.contracts-spec';
2
2
 
3
3
  export const KbUpdatePipelineFeature = defineFeature({
4
- meta: {
5
- key: 'kb-update-pipeline',
6
- version: '1.0.0',
7
- title: 'KB Update Pipeline (HITL)',
8
- description:
9
- 'Automation proposes KB patches; humans verify; publishing is blocked until approvals are complete.',
10
- domain: 'knowledge',
11
- owners: ['@examples'],
12
- tags: ['knowledge', 'pipeline', 'hitl', 'audit', 'notifications'],
13
- stability: 'experimental',
14
- },
15
- operations: [
16
- { key: 'kbPipeline.runWatch', version: '1.0.0' },
17
- { key: 'kbPipeline.createReviewTask', version: '1.0.0' },
18
- { key: 'kbPipeline.submitDecision', version: '1.0.0' },
19
- { key: 'kbPipeline.publishIfReady', version: '1.0.0' },
20
- ],
21
- events: [
22
- { key: 'kb.change.detected', version: '1.0.0' },
23
- { key: 'kb.change.summarized', version: '1.0.0' },
24
- { key: 'kb.patch.proposed', version: '1.0.0' },
25
- { key: 'kb.review.requested', version: '1.0.0' },
26
- { key: 'kb.review.decided', version: '1.0.0' },
27
- ],
28
- presentations: [
29
- { key: 'kb.dashboard', version: '1.0.0' },
30
- { key: 'kb.review.list', version: '1.0.0' },
31
- { key: 'kb.review.form', version: '1.0.0' },
32
- ],
33
- opToPresentation: [
34
- {
35
- op: { key: 'kbPipeline.runWatch', version: '1.0.0' },
36
- pres: { key: 'kb.dashboard', version: '1.0.0' },
37
- },
38
- {
39
- op: { key: 'kbPipeline.createReviewTask', version: '1.0.0' },
40
- pres: { key: 'kb.review.list', version: '1.0.0' },
41
- },
42
- {
43
- op: { key: 'kbPipeline.submitDecision', version: '1.0.0' },
44
- pres: { key: 'kb.review.form', version: '1.0.0' },
45
- },
46
- ],
47
- presentationsTargets: [
48
- { key: 'kb.dashboard', version: '1.0.0', targets: ['react', 'markdown'] },
49
- { key: 'kb.review.list', version: '1.0.0', targets: ['react', 'markdown'] },
50
- { key: 'kb.review.form', version: '1.0.0', targets: ['react'] },
51
- ],
52
- capabilities: {
53
- requires: [
54
- { key: 'identity', version: '1.0.0' },
55
- { key: 'notifications', version: '1.0.0' },
56
- { key: 'audit-trail', version: '1.0.0' },
57
- ],
58
- },
4
+ meta: {
5
+ key: 'kb-update-pipeline',
6
+ version: '1.0.0',
7
+ title: 'KB Update Pipeline (HITL)',
8
+ description:
9
+ 'Automation proposes KB patches; humans verify; publishing is blocked until approvals are complete.',
10
+ domain: 'knowledge',
11
+ owners: ['@examples'],
12
+ tags: ['knowledge', 'pipeline', 'hitl', 'audit', 'notifications'],
13
+ stability: 'experimental',
14
+ },
15
+ operations: [
16
+ { key: 'kbPipeline.runWatch', version: '1.0.0' },
17
+ { key: 'kbPipeline.createReviewTask', version: '1.0.0' },
18
+ { key: 'kbPipeline.submitDecision', version: '1.0.0' },
19
+ { key: 'kbPipeline.publishIfReady', version: '1.0.0' },
20
+ ],
21
+ events: [
22
+ { key: 'kb.change.detected', version: '1.0.0' },
23
+ { key: 'kb.change.summarized', version: '1.0.0' },
24
+ { key: 'kb.patch.proposed', version: '1.0.0' },
25
+ { key: 'kb.review.requested', version: '1.0.0' },
26
+ { key: 'kb.review.decided', version: '1.0.0' },
27
+ ],
28
+ presentations: [
29
+ { key: 'kb.dashboard', version: '1.0.0' },
30
+ { key: 'kb.review.list', version: '1.0.0' },
31
+ { key: 'kb.review.form', version: '1.0.0' },
32
+ ],
33
+ opToPresentation: [
34
+ {
35
+ op: { key: 'kbPipeline.runWatch', version: '1.0.0' },
36
+ pres: { key: 'kb.dashboard', version: '1.0.0' },
37
+ },
38
+ {
39
+ op: { key: 'kbPipeline.createReviewTask', version: '1.0.0' },
40
+ pres: { key: 'kb.review.list', version: '1.0.0' },
41
+ },
42
+ {
43
+ op: { key: 'kbPipeline.submitDecision', version: '1.0.0' },
44
+ pres: { key: 'kb.review.form', version: '1.0.0' },
45
+ },
46
+ ],
47
+ presentationsTargets: [
48
+ { key: 'kb.dashboard', version: '1.0.0', targets: ['react', 'markdown'] },
49
+ { key: 'kb.review.list', version: '1.0.0', targets: ['react', 'markdown'] },
50
+ { key: 'kb.review.form', version: '1.0.0', targets: ['react'] },
51
+ ],
52
+ capabilities: {
53
+ requires: [
54
+ { key: 'identity', version: '1.0.0' },
55
+ { key: 'notifications', version: '1.0.0' },
56
+ { key: 'audit-trail', version: '1.0.0' },
57
+ ],
58
+ },
59
59
 
60
- knowledge: [{ key: 'kb.knowledge.rules', version: '1.0.0' }],
60
+ knowledge: [{ key: 'kb.knowledge.rules', version: '1.0.0' }],
61
61
 
62
- telemetry: [{ key: 'kb-pipeline.telemetry', version: '1.0.0' }],
62
+ telemetry: [{ key: 'kb-pipeline.telemetry', version: '1.0.0' }],
63
63
 
64
- jobs: [{ key: 'kb-pipeline.job.watch', version: '1.0.0' }],
64
+ jobs: [{ key: 'kb-pipeline.job.watch', version: '1.0.0' }],
65
65
 
66
- docs: [
67
- 'docs.examples.kb-update-pipeline.goal',
68
- 'docs.examples.kb-update-pipeline.reference',
69
- ],
66
+ docs: [
67
+ 'docs.examples.kb-update-pipeline.goal',
68
+ 'docs.examples.kb-update-pipeline.reference',
69
+ ],
70
70
  });