@contractspec/example.kb-update-pipeline 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 +55 -0
- package/.turbo/turbo-build.log +56 -0
- package/CHANGELOG.md +195 -0
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/docs/index.d.ts +1 -0
- package/dist/docs/index.js +1 -0
- package/dist/docs/kb-update-pipeline.docblock.d.ts +1 -0
- package/dist/docs/kb-update-pipeline.docblock.js +31 -0
- package/dist/docs/kb-update-pipeline.docblock.js.map +1 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.js +3 -0
- package/dist/entities/models.d.ts +61 -0
- package/dist/entities/models.d.ts.map +1 -0
- package/dist/entities/models.js +74 -0
- package/dist/entities/models.js.map +1 -0
- package/dist/events.d.ts +74 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +150 -0
- package/dist/events.js.map +1 -0
- package/dist/example.d.ts +36 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/example.js +43 -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 +80 -0
- package/dist/feature.js.map +1 -0
- package/dist/handlers/index.d.ts +2 -0
- package/dist/handlers/index.js +3 -0
- package/dist/handlers/memory.handlers.d.ts +68 -0
- package/dist/handlers/memory.handlers.d.ts.map +1 -0
- package/dist/handlers/memory.handlers.js +93 -0
- package/dist/handlers/memory.handlers.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +12 -0
- package/dist/kb-update-pipeline.feature.d.ts +7 -0
- package/dist/kb-update-pipeline.feature.d.ts.map +1 -0
- package/dist/kb-update-pipeline.feature.js +140 -0
- package/dist/kb-update-pipeline.feature.js.map +1 -0
- package/dist/operations/index.d.ts +2 -0
- package/dist/operations/index.js +3 -0
- package/dist/operations/pipeline.d.ts +137 -0
- package/dist/operations/pipeline.d.ts.map +1 -0
- package/dist/operations/pipeline.js +183 -0
- package/dist/operations/pipeline.js.map +1 -0
- package/dist/presentations.d.ts +9 -0
- package/dist/presentations.d.ts.map +1 -0
- package/dist/presentations.js +71 -0
- package/dist/presentations.js.map +1 -0
- package/example.ts +1 -0
- package/package.json +79 -0
- package/src/docs/index.ts +1 -0
- package/src/docs/kb-update-pipeline.docblock.ts +30 -0
- package/src/entities/index.ts +1 -0
- package/src/entities/models.ts +53 -0
- package/src/events.ts +134 -0
- package/src/example.ts +27 -0
- package/src/feature.ts +38 -0
- package/src/handlers/index.ts +1 -0
- package/src/handlers/memory.handlers.test.ts +81 -0
- package/src/handlers/memory.handlers.ts +186 -0
- package/src/index.ts +15 -0
- package/src/kb-update-pipeline.feature.ts +59 -0
- package/src/operations/index.ts +1 -0
- package/src/operations/pipeline.ts +159 -0
- package/src/presentations.ts +69 -0
- package/tsconfig.json +19 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.js +17 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { DocBlock } from '@contractspec/lib.contracts/docs';
|
|
2
|
+
import { registerDocBlocks } from '@contractspec/lib.contracts/docs';
|
|
3
|
+
|
|
4
|
+
const docBlocks: DocBlock[] = [
|
|
5
|
+
{
|
|
6
|
+
id: 'docs.examples.kb-update-pipeline.goal',
|
|
7
|
+
title: 'KB Update Pipeline — Goal',
|
|
8
|
+
summary:
|
|
9
|
+
'Automation proposes KB patches; humans verify; publishing is blocked until approvals are complete.',
|
|
10
|
+
kind: 'goal',
|
|
11
|
+
visibility: 'public',
|
|
12
|
+
route: '/docs/examples/kb-update-pipeline/goal',
|
|
13
|
+
tags: ['knowledge', 'pipeline', 'hitl', 'audit'],
|
|
14
|
+
body: `## Why it matters
|
|
15
|
+
- Keeps humans as the verifiers (HITL) while automation does the busywork.\n- Produces an auditable chain: source change -> diff -> proposal -> review -> publish.\n\n## Guardrails\n- High-risk changes require expert approval.\n- Publishing fails if any included rule versions are not approved.\n- Review requests emit notifications/events.`,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: 'docs.examples.kb-update-pipeline.reference',
|
|
19
|
+
title: 'KB Update Pipeline — Reference',
|
|
20
|
+
summary:
|
|
21
|
+
'Entities, contracts, and events for the KB update pipeline example.',
|
|
22
|
+
kind: 'reference',
|
|
23
|
+
visibility: 'public',
|
|
24
|
+
route: '/docs/examples/kb-update-pipeline',
|
|
25
|
+
tags: ['knowledge', 'reference'],
|
|
26
|
+
body: `## Contracts\n- kbPipeline.runWatch\n- kbPipeline.createReviewTask\n- kbPipeline.submitDecision\n- kbPipeline.publishIfReady\n\n## Events\n- kb.change.detected\n- kb.change.summarized\n- kb.patch.proposed\n- kb.review.requested\n- kb.review.decided`,
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
registerDocBlocks(docBlocks);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './models';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ScalarTypeEnum,
|
|
3
|
+
defineEnum,
|
|
4
|
+
defineSchemaModel,
|
|
5
|
+
} from '@contractspec/lib.schema';
|
|
6
|
+
|
|
7
|
+
export const ChangeRiskLevelEnum = defineEnum('ChangeRiskLevel', [
|
|
8
|
+
'low',
|
|
9
|
+
'medium',
|
|
10
|
+
'high',
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export const ReviewAssignedRoleEnum = defineEnum('ReviewAssignedRole', [
|
|
14
|
+
'curator',
|
|
15
|
+
'expert',
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const ReviewDecisionEnum = defineEnum('ReviewDecision', [
|
|
19
|
+
'approve',
|
|
20
|
+
'reject',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export const ChangeCandidateModel = defineSchemaModel({
|
|
24
|
+
name: 'ChangeCandidate',
|
|
25
|
+
description: 'Candidate change detected in a source document.',
|
|
26
|
+
fields: {
|
|
27
|
+
id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
28
|
+
sourceDocumentId: {
|
|
29
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
30
|
+
isOptional: false,
|
|
31
|
+
},
|
|
32
|
+
detectedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
|
|
33
|
+
diffSummary: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
34
|
+
riskLevel: { type: ChangeRiskLevelEnum, isOptional: false },
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const ReviewTaskModel = defineSchemaModel({
|
|
39
|
+
name: 'ReviewTask',
|
|
40
|
+
description: 'Human verification task for a change candidate.',
|
|
41
|
+
fields: {
|
|
42
|
+
id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
43
|
+
changeCandidateId: {
|
|
44
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
45
|
+
isOptional: false,
|
|
46
|
+
},
|
|
47
|
+
status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }, // open|decided
|
|
48
|
+
assignedRole: { type: ReviewAssignedRoleEnum, isOptional: false },
|
|
49
|
+
decision: { type: ReviewDecisionEnum, isOptional: true },
|
|
50
|
+
decidedAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
|
|
51
|
+
decidedBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
52
|
+
},
|
|
53
|
+
});
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineEvent,
|
|
3
|
+
defineSchemaModel,
|
|
4
|
+
StabilityEnum,
|
|
5
|
+
} from '@contractspec/lib.contracts';
|
|
6
|
+
import { ScalarTypeEnum } from '@contractspec/lib.schema';
|
|
7
|
+
|
|
8
|
+
const KbChangeDetectedPayload = defineSchemaModel({
|
|
9
|
+
name: 'KbChangeDetectedPayload',
|
|
10
|
+
description: 'Emitted when a source change is detected.',
|
|
11
|
+
fields: {
|
|
12
|
+
changeCandidateId: {
|
|
13
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
14
|
+
isOptional: false,
|
|
15
|
+
},
|
|
16
|
+
sourceDocumentId: {
|
|
17
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
18
|
+
isOptional: false,
|
|
19
|
+
},
|
|
20
|
+
riskLevel: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const KbChangeDetectedEvent = defineEvent({
|
|
25
|
+
meta: {
|
|
26
|
+
key: 'kb.change.detected',
|
|
27
|
+
version: 1,
|
|
28
|
+
description: 'KB source change detected.',
|
|
29
|
+
stability: StabilityEnum.Experimental,
|
|
30
|
+
owners: [],
|
|
31
|
+
tags: [],
|
|
32
|
+
},
|
|
33
|
+
payload: KbChangeDetectedPayload,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const KbChangeSummarizedPayload = defineSchemaModel({
|
|
37
|
+
name: 'KbChangeSummarizedPayload',
|
|
38
|
+
description: 'Emitted when a change summary is produced.',
|
|
39
|
+
fields: {
|
|
40
|
+
changeCandidateId: {
|
|
41
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
42
|
+
isOptional: false,
|
|
43
|
+
},
|
|
44
|
+
summary: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
45
|
+
riskLevel: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export const KbChangeSummarizedEvent = defineEvent({
|
|
50
|
+
meta: {
|
|
51
|
+
key: 'kb.change.summarized',
|
|
52
|
+
version: 1,
|
|
53
|
+
description: 'KB change summarized.',
|
|
54
|
+
stability: StabilityEnum.Experimental,
|
|
55
|
+
owners: [],
|
|
56
|
+
tags: [],
|
|
57
|
+
},
|
|
58
|
+
payload: KbChangeSummarizedPayload,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const KbPatchProposedPayload = defineSchemaModel({
|
|
62
|
+
name: 'KbPatchProposedPayload',
|
|
63
|
+
description: 'Emitted when draft rule patches are proposed.',
|
|
64
|
+
fields: {
|
|
65
|
+
changeCandidateId: {
|
|
66
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
67
|
+
isOptional: false,
|
|
68
|
+
},
|
|
69
|
+
proposedRuleVersionIds: {
|
|
70
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
71
|
+
isArray: true,
|
|
72
|
+
isOptional: false,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
export const KbPatchProposedEvent = defineEvent({
|
|
78
|
+
meta: {
|
|
79
|
+
key: 'kb.patch.proposed',
|
|
80
|
+
version: 1,
|
|
81
|
+
description: 'KB rule patch proposed (draft versions created).',
|
|
82
|
+
stability: StabilityEnum.Experimental,
|
|
83
|
+
owners: [],
|
|
84
|
+
tags: [],
|
|
85
|
+
},
|
|
86
|
+
payload: KbPatchProposedPayload,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const KbReviewRequestedPayload = defineSchemaModel({
|
|
90
|
+
name: 'KbReviewRequestedPayload',
|
|
91
|
+
description: 'Emitted when a review is requested.',
|
|
92
|
+
fields: {
|
|
93
|
+
reviewTaskId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
94
|
+
changeCandidateId: {
|
|
95
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
96
|
+
isOptional: false,
|
|
97
|
+
},
|
|
98
|
+
assignedRole: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const KbReviewRequestedEvent = defineEvent({
|
|
103
|
+
meta: {
|
|
104
|
+
key: 'kb.review.requested',
|
|
105
|
+
version: 1,
|
|
106
|
+
description: 'KB review requested.',
|
|
107
|
+
stability: StabilityEnum.Experimental,
|
|
108
|
+
owners: [],
|
|
109
|
+
tags: [],
|
|
110
|
+
},
|
|
111
|
+
payload: KbReviewRequestedPayload,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const KbReviewDecidedPayload = defineSchemaModel({
|
|
115
|
+
name: 'KbReviewDecidedPayload',
|
|
116
|
+
description: 'Emitted when a review task is decided.',
|
|
117
|
+
fields: {
|
|
118
|
+
reviewTaskId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
119
|
+
decision: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
120
|
+
decidedBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
export const KbReviewDecidedEvent = defineEvent({
|
|
125
|
+
meta: {
|
|
126
|
+
key: 'kb.review.decided',
|
|
127
|
+
version: 1,
|
|
128
|
+
description: 'KB review decided.',
|
|
129
|
+
stability: StabilityEnum.Experimental,
|
|
130
|
+
owners: [],
|
|
131
|
+
tags: [],
|
|
132
|
+
},
|
|
133
|
+
payload: KbReviewDecidedPayload,
|
|
134
|
+
});
|
package/src/example.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const example = {
|
|
2
|
+
id: 'kb-update-pipeline',
|
|
3
|
+
title: 'KB Update Pipeline',
|
|
4
|
+
summary:
|
|
5
|
+
'Automation proposes KB updates; humans verify; everything audited and notified.',
|
|
6
|
+
tags: ['knowledge', 'pipeline', 'hitl', 'audit'],
|
|
7
|
+
kind: 'knowledge',
|
|
8
|
+
visibility: 'public',
|
|
9
|
+
docs: {
|
|
10
|
+
rootDocId: 'docs.examples.kb-update-pipeline',
|
|
11
|
+
},
|
|
12
|
+
entrypoints: {
|
|
13
|
+
packageName: '@contractspec/example.kb-update-pipeline',
|
|
14
|
+
feature: './feature',
|
|
15
|
+
contracts: './contracts',
|
|
16
|
+
handlers: './handlers',
|
|
17
|
+
docs: './docs',
|
|
18
|
+
},
|
|
19
|
+
surfaces: {
|
|
20
|
+
templates: true,
|
|
21
|
+
sandbox: { enabled: true, modes: ['markdown', 'specs', 'builder'] },
|
|
22
|
+
studio: { enabled: true, installable: true },
|
|
23
|
+
mcp: { enabled: true },
|
|
24
|
+
},
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export default example;
|
package/src/feature.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { FeatureModuleSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
|
|
3
|
+
export const KbUpdatePipelineFeature: FeatureModuleSpec = {
|
|
4
|
+
meta: {
|
|
5
|
+
key: 'kb-update-pipeline',
|
|
6
|
+
version: 1,
|
|
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 },
|
|
17
|
+
{ key: 'kbPipeline.createReviewTask', version: 1 },
|
|
18
|
+
{ key: 'kbPipeline.submitDecision', version: 1 },
|
|
19
|
+
{ key: 'kbPipeline.publishIfReady', version: 1 },
|
|
20
|
+
],
|
|
21
|
+
events: [
|
|
22
|
+
{ key: 'kb.change.detected', version: 1 },
|
|
23
|
+
{ key: 'kb.change.summarized', version: 1 },
|
|
24
|
+
{ key: 'kb.patch.proposed', version: 1 },
|
|
25
|
+
{ key: 'kb.review.requested', version: 1 },
|
|
26
|
+
{ key: 'kb.review.decided', version: 1 },
|
|
27
|
+
],
|
|
28
|
+
presentations: [],
|
|
29
|
+
opToPresentation: [],
|
|
30
|
+
presentationsTargets: [],
|
|
31
|
+
capabilities: {
|
|
32
|
+
requires: [
|
|
33
|
+
{ key: 'identity', version: 1 },
|
|
34
|
+
{ key: 'notifications', version: 1 },
|
|
35
|
+
{ key: 'audit-trail', version: 1 },
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './memory.handlers';
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createPipelineMemoryHandlers,
|
|
5
|
+
createPipelineMemoryStore,
|
|
6
|
+
} from './memory.handlers';
|
|
7
|
+
|
|
8
|
+
describe('@contractspec/example.kb-update-pipeline memory handlers', () => {
|
|
9
|
+
it('high-risk change cannot be approved by curator role', async () => {
|
|
10
|
+
const store = createPipelineMemoryStore();
|
|
11
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
12
|
+
|
|
13
|
+
store.candidates.set('c1', {
|
|
14
|
+
id: 'c1',
|
|
15
|
+
sourceDocumentId: 'EU_src1',
|
|
16
|
+
detectedAt: new Date(),
|
|
17
|
+
diffSummary: 'Change detected',
|
|
18
|
+
riskLevel: 'high',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c1' });
|
|
22
|
+
await expect(
|
|
23
|
+
pipeline.submitDecision({
|
|
24
|
+
reviewTaskId: review.id,
|
|
25
|
+
decision: 'approve',
|
|
26
|
+
decidedBy: 'u_curator',
|
|
27
|
+
decidedByRole: 'curator',
|
|
28
|
+
})
|
|
29
|
+
).rejects.toThrow('FORBIDDEN_ROLE');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('fires a notification when review is requested', async () => {
|
|
33
|
+
const store = createPipelineMemoryStore();
|
|
34
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
35
|
+
|
|
36
|
+
store.candidates.set('c2', {
|
|
37
|
+
id: 'c2',
|
|
38
|
+
sourceDocumentId: 'EU_src2',
|
|
39
|
+
detectedAt: new Date(),
|
|
40
|
+
diffSummary: 'Minor change',
|
|
41
|
+
riskLevel: 'low',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c2' });
|
|
45
|
+
expect(store.notifications.length).toBe(1);
|
|
46
|
+
expect(store.notifications[0]?.reviewTaskId).toBe(review.id);
|
|
47
|
+
expect(store.notifications[0]?.assignedRole).toBe('curator');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('publishing fails if any included rule versions are not approved', async () => {
|
|
51
|
+
const store = createPipelineMemoryStore();
|
|
52
|
+
const pipeline = createPipelineMemoryHandlers(store);
|
|
53
|
+
|
|
54
|
+
store.candidates.set('c3', {
|
|
55
|
+
id: 'c3',
|
|
56
|
+
sourceDocumentId: 'EU_src3',
|
|
57
|
+
detectedAt: new Date(),
|
|
58
|
+
diffSummary: 'Change',
|
|
59
|
+
riskLevel: 'low',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const review = await pipeline.createReviewTask({ changeCandidateId: 'c3' });
|
|
63
|
+
await pipeline.proposeRulePatch({
|
|
64
|
+
changeCandidateId: 'c3',
|
|
65
|
+
proposedRuleVersionIds: ['rv1', 'rv2'],
|
|
66
|
+
});
|
|
67
|
+
await pipeline.submitDecision({
|
|
68
|
+
reviewTaskId: review.id,
|
|
69
|
+
decision: 'approve',
|
|
70
|
+
decidedBy: 'u_curator',
|
|
71
|
+
decidedByRole: 'curator',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// only approve one of the proposed rule versions
|
|
75
|
+
await pipeline.markRuleVersionApproved({ ruleVersionId: 'rv1' });
|
|
76
|
+
|
|
77
|
+
await expect(
|
|
78
|
+
pipeline.publishIfReady({ jurisdiction: 'EU' })
|
|
79
|
+
).rejects.toThrow('NOT_READY');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
interface ChangeCandidate {
|
|
2
|
+
id: string;
|
|
3
|
+
sourceDocumentId: string;
|
|
4
|
+
detectedAt: Date;
|
|
5
|
+
diffSummary: string;
|
|
6
|
+
riskLevel: 'low' | 'medium' | 'high';
|
|
7
|
+
}
|
|
8
|
+
|
|
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;
|
|
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: {
|
|
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: {
|
|
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
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createPipelineMemoryHandlers(
|
|
71
|
+
store: PipelineMemoryStore
|
|
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
|
+
}
|
|
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
|
+
}
|
|
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
|
+
}
|
|
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
|
+
}
|
|
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
|
+
}
|
|
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' };
|
|
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
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
runWatch,
|
|
180
|
+
createReviewTask,
|
|
181
|
+
proposeRulePatch,
|
|
182
|
+
markRuleVersionApproved,
|
|
183
|
+
submitDecision,
|
|
184
|
+
publishIfReady,
|
|
185
|
+
};
|
|
186
|
+
}
|
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 './operations';
|
|
8
|
+
export * from './events';
|
|
9
|
+
export * from './handlers';
|
|
10
|
+
export * from './kb-update-pipeline.feature';
|
|
11
|
+
export { default as example } from './example';
|
|
12
|
+
|
|
13
|
+
export * from './presentations';
|
|
14
|
+
|
|
15
|
+
import './docs';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { FeatureModuleSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
|
|
3
|
+
export const KbUpdatePipelineFeature: FeatureModuleSpec = {
|
|
4
|
+
meta: {
|
|
5
|
+
key: 'kb-update-pipeline',
|
|
6
|
+
version: 1,
|
|
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 },
|
|
17
|
+
{ key: 'kbPipeline.createReviewTask', version: 1 },
|
|
18
|
+
{ key: 'kbPipeline.submitDecision', version: 1 },
|
|
19
|
+
{ key: 'kbPipeline.publishIfReady', version: 1 },
|
|
20
|
+
],
|
|
21
|
+
events: [
|
|
22
|
+
{ key: 'kb.change.detected', version: 1 },
|
|
23
|
+
{ key: 'kb.change.summarized', version: 1 },
|
|
24
|
+
{ key: 'kb.patch.proposed', version: 1 },
|
|
25
|
+
{ key: 'kb.review.requested', version: 1 },
|
|
26
|
+
{ key: 'kb.review.decided', version: 1 },
|
|
27
|
+
],
|
|
28
|
+
presentations: [
|
|
29
|
+
{ key: 'kb.dashboard', version: 1 },
|
|
30
|
+
{ key: 'kb.review.list', version: 1 },
|
|
31
|
+
{ key: 'kb.review.form', version: 1 },
|
|
32
|
+
],
|
|
33
|
+
opToPresentation: [
|
|
34
|
+
{
|
|
35
|
+
op: { key: 'kbPipeline.runWatch', version: 1 },
|
|
36
|
+
pres: { key: 'kb.dashboard', version: 1 },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
op: { key: 'kbPipeline.createReviewTask', version: 1 },
|
|
40
|
+
pres: { key: 'kb.review.list', version: 1 },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
op: { key: 'kbPipeline.submitDecision', version: 1 },
|
|
44
|
+
pres: { key: 'kb.review.form', version: 1 },
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
presentationsTargets: [
|
|
48
|
+
{ key: 'kb.dashboard', version: 1, targets: ['react', 'markdown'] },
|
|
49
|
+
{ key: 'kb.review.list', version: 1, targets: ['react', 'markdown'] },
|
|
50
|
+
{ key: 'kb.review.form', version: 1, targets: ['react'] },
|
|
51
|
+
],
|
|
52
|
+
capabilities: {
|
|
53
|
+
requires: [
|
|
54
|
+
{ key: 'identity', version: 1 },
|
|
55
|
+
{ key: 'notifications', version: 1 },
|
|
56
|
+
{ key: 'audit-trail', version: 1 },
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './pipeline';
|