@hippo-digital/hippocampus 2.8.1 → 2.9.1
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/CHANGELOG.md +51 -0
- package/bin/hippocampus.js +2 -0
- package/docs/design-actions-and-decisions.md +19 -0
- package/docs/review-and-promote-a-draft.md +77 -0
- package/docs/source-model.md +23 -3
- package/lib/collections.js +53 -0
- package/lib/consequences.js +2 -0
- package/lib/create-router.js +4 -0
- package/lib/hcd-views.js +4 -1
- package/lib/lint-knowledge.js +1 -1
- package/lib/load-knowledge.js +146 -136
- package/lib/schema-version.js +102 -1
- package/lib/schema.js +53 -1
- package/package.json +1 -1
- package/scripts/check-draft.js +48 -0
- package/scripts/import-design-actions.js +34 -5
- package/scripts/lib/draft.js +265 -0
- package/scripts/migrate-knowledge.js +22 -7
- package/scripts/promote-draft.js +92 -0
- package/scripts/promote-research-draft.js +10 -12
- package/scripts/smoke-routes.js +13 -2
- package/scripts/validate-knowledge.js +10 -0
- package/views/hippocampus/research.html +8 -0
|
@@ -21,7 +21,7 @@ const fs = require('fs')
|
|
|
21
21
|
const path = require('path')
|
|
22
22
|
const { openWorkbook, readSheetGrid } = require('./lib/xlsx-grid')
|
|
23
23
|
const { KnowledgeSchema } = require('../lib/schema')
|
|
24
|
-
const { readMeta, migrateInMemory } = require('../lib/schema-version')
|
|
24
|
+
const { readMeta, migrateInMemory, recordLevelNeeded } = require('../lib/schema-version')
|
|
25
25
|
const { createContextOrExit } = require('../lib/context')
|
|
26
26
|
const ctx = createContextOrExit()
|
|
27
27
|
const { knowledgeSourceDir } = ctx.paths
|
|
@@ -48,6 +48,7 @@ function main () {
|
|
|
48
48
|
const relationships = read('relationships.json')
|
|
49
49
|
|
|
50
50
|
const problems = []
|
|
51
|
+
const notes = []
|
|
51
52
|
const actions = []
|
|
52
53
|
const actionIdByTracker = new Map()
|
|
53
54
|
|
|
@@ -70,9 +71,10 @@ function main () {
|
|
|
70
71
|
title: text(row['Title']) || text(row['Action']).slice(0, 110),
|
|
71
72
|
status: status || 'proposed',
|
|
72
73
|
action: text(row['Action']),
|
|
73
|
-
rationale: [text(row['Rationale']), row['
|
|
74
|
+
rationale: [text(row['Rationale']), row['Dependency'] && `Dependency: ${text(row['Dependency'])}.`]
|
|
74
75
|
.filter(Boolean).join(' ') || `Recorded on tracker row ${tracker}.`,
|
|
75
|
-
insightIds: [...new Set(linked.filter(Boolean))]
|
|
76
|
+
insightIds: [...new Set(linked.filter(Boolean))],
|
|
77
|
+
...(text(row['Owner']) ? { owner: text(row['Owner']) } : {})
|
|
76
78
|
})
|
|
77
79
|
}
|
|
78
80
|
|
|
@@ -89,14 +91,22 @@ function main () {
|
|
|
89
91
|
(existingActions.some((a) => a.id === kebab(actionRef)) ? kebab(actionRef) : null)
|
|
90
92
|
if (actionRef && !actionId) problems.push(`Decision ${tracker}: design action "${actionRef}" not found in this workbook or in the knowledge base`)
|
|
91
93
|
|
|
94
|
+
// Who decided and when are fields, so a trace can ask for them and a
|
|
95
|
+
// check can find an accepted decision with nobody named. A date that
|
|
96
|
+
// can't be read is kept as words in the consequence rather than lost.
|
|
97
|
+
const agreed = text(row['Date agreed'])
|
|
98
|
+
const decidedOn = agreed ? toIsoDate(row['Date agreed']) : null
|
|
99
|
+
if (agreed && !decidedOn) notes.push(`Decision ${tracker}: "${agreed}" is not a date this importer can read, so it is kept in the consequence. Write it as YYYY-MM-DD to record it as the date decided.`)
|
|
92
100
|
decisions.push({
|
|
93
101
|
record: {
|
|
94
102
|
id: kebab(tracker),
|
|
95
103
|
title: text(row['Option']) || text(row['Decision']).slice(0, 110),
|
|
96
104
|
status: status || 'proposed',
|
|
97
105
|
decision: text(row['Decision']),
|
|
98
|
-
consequence: [text(row['Consequence']),
|
|
99
|
-
.filter(Boolean).join(' ') || 'Consequence not yet recorded.'
|
|
106
|
+
consequence: [text(row['Consequence']), agreed && !decidedOn && `Agreed ${agreed}.`]
|
|
107
|
+
.filter(Boolean).join(' ') || 'Consequence not yet recorded.',
|
|
108
|
+
...(text(row['Decided by']) ? { decidedBy: text(row['Decided by']) } : {}),
|
|
109
|
+
...(decidedOn ? { decidedOn } : {})
|
|
100
110
|
},
|
|
101
111
|
actionId
|
|
102
112
|
})
|
|
@@ -133,6 +143,8 @@ function main () {
|
|
|
133
143
|
console.log(`Decisions: ${decisions.length} (${DECISION_STATUS.map((s) => `${s} ${decisions.filter((d) => d.record.status === s).length}`).join(', ')})`)
|
|
134
144
|
console.log(`Relationships: ${links.length}`)
|
|
135
145
|
|
|
146
|
+
for (const note of notes) console.log(note)
|
|
147
|
+
|
|
136
148
|
if (problems.length) {
|
|
137
149
|
console.error(`\n${problems.length} problem${problems.length === 1 ? '' : 's'} to fix in the workbook:`)
|
|
138
150
|
for (const p of problems) console.error(`- ${p}`)
|
|
@@ -183,6 +195,8 @@ function main () {
|
|
|
183
195
|
for (const [file, data] of Object.entries(proposed)) {
|
|
184
196
|
fs.writeFileSync(path.join(knowledgeSourceDir, file), `${JSON.stringify(data, null, 2)}\n`)
|
|
185
197
|
}
|
|
198
|
+
const level = recordLevelNeeded(knowledgeSourceDir, migrated)
|
|
199
|
+
if (level) console.log(`This knowledge base now needs Hippocampus ${level} or later. Recorded in _meta.json.`)
|
|
186
200
|
console.log(`\nImported from ${path.relative(ctx.root, path.resolve(args.workbook))}. Run npx hippocampus validate to confirm.`)
|
|
187
201
|
}
|
|
188
202
|
|
|
@@ -217,6 +231,21 @@ function resolveInsight (ref, insightIds) {
|
|
|
217
231
|
return [...insightIds].find((id) => id.endsWith(`-${kebab(ref)}`)) || null
|
|
218
232
|
}
|
|
219
233
|
|
|
234
|
+
// Dates arrive from a workbook as YYYY-MM-DD, as dd/mm/yyyy, or as a
|
|
235
|
+
// spreadsheet serial number when the cell is formatted as a date.
|
|
236
|
+
function toIsoDate (value) {
|
|
237
|
+
const v = text(value)
|
|
238
|
+
let m = v.match(/^(\d{4})-(\d{2})-(\d{2})/)
|
|
239
|
+
if (m) return `${m[1]}-${m[2]}-${m[3]}`
|
|
240
|
+
m = v.match(/^(\d{1,2})[/.-](\d{1,2})[/.-](\d{4})$/)
|
|
241
|
+
if (m) return `${m[3]}-${m[2].padStart(2, '0')}-${m[1].padStart(2, '0')}`
|
|
242
|
+
if (/^\d{5}(\.\d+)?$/.test(v)) {
|
|
243
|
+
const date = new Date(Date.UTC(1899, 11, 30) + Math.floor(Number(v)) * 86400000)
|
|
244
|
+
return date.toISOString().slice(0, 10)
|
|
245
|
+
}
|
|
246
|
+
return null
|
|
247
|
+
}
|
|
248
|
+
|
|
220
249
|
function normalise (value, allowed) {
|
|
221
250
|
const v = text(value).toLowerCase().replace(/\s+/g, '-')
|
|
222
251
|
return allowed.includes(v) ? v : null
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { DraftSchema, recordSchemas } = require('../../lib/schema')
|
|
4
|
+
const { COLLECTION_FILES, RECORD_COLLECTIONS, readCollectionFile } = require('../../lib/collections')
|
|
5
|
+
const { checkKnowledgeData } = require('../../lib/load-knowledge')
|
|
6
|
+
const { readMeta, migrateInMemory } = require('../../lib/schema-version')
|
|
7
|
+
|
|
8
|
+
// One route into the knowledge base for any record. A draft is a file in
|
|
9
|
+
// hippocampus/imports/ that says what it adds, collection by collection; `draft
|
|
10
|
+
// check` says what it would do and changes nothing; `draft promote` adds it,
|
|
11
|
+
// once someone other than its author has reviewed it.
|
|
12
|
+
//
|
|
13
|
+
// Evidence, assumptions, requirements, screens and scenarios used to be added to
|
|
14
|
+
// the source files by hand, in an order that lived in the review notes. This is
|
|
15
|
+
// what replaces that.
|
|
16
|
+
|
|
17
|
+
const MANIFEST_NAME = 'drafts.json'
|
|
18
|
+
const NOTE_START = '<!-- hippocampus draft check: start -->'
|
|
19
|
+
const NOTE_END = '<!-- hippocampus draft check: end -->'
|
|
20
|
+
|
|
21
|
+
function manifestPath (ctx) {
|
|
22
|
+
return path.join(ctx.paths.manifestsDir, MANIFEST_NAME)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function readManifest (ctx) {
|
|
26
|
+
const file = manifestPath(ctx)
|
|
27
|
+
if (!fs.existsSync(file)) return { promoted: [] }
|
|
28
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function writeManifest (ctx, manifest) {
|
|
32
|
+
fs.mkdirSync(ctx.paths.manifestsDir, { recursive: true })
|
|
33
|
+
fs.writeFileSync(manifestPath(ctx), `${JSON.stringify(manifest, null, 2)}\n`)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Paths are printed and stored from the project root, so they are looked for
|
|
37
|
+
// there when they are not where the command was run.
|
|
38
|
+
function resolveFromRoot (ctx, value) {
|
|
39
|
+
return [path.resolve(value), path.resolve(ctx.root, value)].find((candidate) => fs.existsSync(candidate)) || null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function relative (ctx, file) {
|
|
43
|
+
return path.relative(ctx.root, file).split(path.sep).join('/')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The knowledge base as `validate` reads it: a project with no context yet
|
|
47
|
+
// gets the same stub from project.json, and an older format is migrated in
|
|
48
|
+
// memory first.
|
|
49
|
+
function readKnowledge (ctx) {
|
|
50
|
+
const dir = ctx.paths.knowledgeSourceDir
|
|
51
|
+
const knowledge = {}
|
|
52
|
+
for (const key of Object.keys(COLLECTION_FILES)) {
|
|
53
|
+
const file = path.join(dir, COLLECTION_FILES[key])
|
|
54
|
+
if (key === 'project') knowledge.project = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
55
|
+
else if (key === 'projectContext') {
|
|
56
|
+
knowledge.projectContext = fs.existsSync(file)
|
|
57
|
+
? JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
58
|
+
: { id: `${knowledge.project.id}-context`, title: knowledge.project.name, summary: knowledge.project.description }
|
|
59
|
+
} else knowledge[key] = readCollectionFile(dir, key)
|
|
60
|
+
}
|
|
61
|
+
return migrateInMemory(knowledge, readMeta(dir)).data
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Every id a file of any draft format adds: this route's `records`, or the
|
|
65
|
+
// top-level arrays of a research or older hand-written draft.
|
|
66
|
+
function idsInAnyDraft (json) {
|
|
67
|
+
const ids = []
|
|
68
|
+
const lists = json && json.records && typeof json.records === 'object'
|
|
69
|
+
? Object.values(json.records)
|
|
70
|
+
: Object.values(json || {})
|
|
71
|
+
for (const list of lists) {
|
|
72
|
+
if (!Array.isArray(list)) continue
|
|
73
|
+
for (const item of list) if (item && typeof item.id === 'string') ids.push(item.id)
|
|
74
|
+
}
|
|
75
|
+
return ids
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function allIds (knowledge) {
|
|
79
|
+
const ids = new Set()
|
|
80
|
+
for (const key of RECORD_COLLECTIONS) for (const item of knowledge[key] || []) ids.add(item.id)
|
|
81
|
+
return ids
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Everything both commands need to know about a draft, worked out once.
|
|
86
|
+
* Nothing here writes.
|
|
87
|
+
*/
|
|
88
|
+
function analyseDraft (ctx, draftPath) {
|
|
89
|
+
const problems = []
|
|
90
|
+
const notes = []
|
|
91
|
+
let json
|
|
92
|
+
try {
|
|
93
|
+
json = JSON.parse(fs.readFileSync(draftPath, 'utf8'))
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return { problems: [`${relative(ctx, draftPath)} is not valid JSON: ${error.message}`], notes, counts: {}, added: {}, links: [], after: [] }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const shape = DraftSchema.safeParse(json)
|
|
99
|
+
if (!shape.success) {
|
|
100
|
+
const issues = shape.error.issues.map((issue) => `${issue.path.join('.') || 'draft'}: ${issue.message}`)
|
|
101
|
+
return {
|
|
102
|
+
problems: [
|
|
103
|
+
`${relative(ctx, draftPath)} is not a draft this command reads. A draft has a "draft" block (title, and optionally after, reviewNote) and a "records" block keyed by collection.`,
|
|
104
|
+
...issues
|
|
105
|
+
],
|
|
106
|
+
notes,
|
|
107
|
+
counts: {},
|
|
108
|
+
added: {},
|
|
109
|
+
links: [],
|
|
110
|
+
after: []
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const draft = shape.data
|
|
115
|
+
const knowledge = readKnowledge(ctx)
|
|
116
|
+
const existing = allIds(knowledge)
|
|
117
|
+
const existingLinks = new Set((knowledge.relationships || []).map((r) => r.id))
|
|
118
|
+
|
|
119
|
+
// Records: each against its own schema, so a message names the record.
|
|
120
|
+
const added = {}
|
|
121
|
+
const counts = {}
|
|
122
|
+
const seen = new Set()
|
|
123
|
+
for (const [collection, records] of Object.entries(draft.records)) {
|
|
124
|
+
if (!RECORD_COLLECTIONS.includes(collection)) {
|
|
125
|
+
problems.push(`records.${collection}: not a collection. Use one of ${RECORD_COLLECTIONS.join(', ')}`)
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
added[collection] = []
|
|
129
|
+
counts[collection] = records.length
|
|
130
|
+
for (const record of records) {
|
|
131
|
+
const parsed = recordSchemas[collection].safeParse(record)
|
|
132
|
+
if (!parsed.success) {
|
|
133
|
+
for (const issue of parsed.error.issues) problems.push(`${collection} ${record.id}: ${issue.path.join('.') || 'record'}: ${issue.message}`)
|
|
134
|
+
}
|
|
135
|
+
if (seen.has(record.id)) problems.push(`${collection} ${record.id}: appears more than once in the draft`)
|
|
136
|
+
seen.add(record.id)
|
|
137
|
+
added[collection].push(record.id)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const draftIds = [...seen]
|
|
142
|
+
const alreadyIn = draftIds.filter((id) => existing.has(id) || existingLinks.has(id))
|
|
143
|
+
const alreadyPromoted = draftIds.length > 0 && alreadyIn.length === draftIds.length
|
|
144
|
+
if (!alreadyPromoted) {
|
|
145
|
+
for (const id of alreadyIn) problems.push(`${id} is already in the knowledge base`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// The drafts this one needs first: promoted through this route, or whose
|
|
149
|
+
// records are all in source already (applied by hand, or by another command).
|
|
150
|
+
const manifest = readManifest(ctx)
|
|
151
|
+
const after = (draft.draft.after || []).map((name) => {
|
|
152
|
+
const file = resolveFromRoot(ctx, name)
|
|
153
|
+
if (!file) return { name, status: 'missing' }
|
|
154
|
+
const promotedHere = manifest.promoted.some((entry) => entry.draft === relative(ctx, file))
|
|
155
|
+
let ids = []
|
|
156
|
+
try { ids = idsInAnyDraft(JSON.parse(fs.readFileSync(file, 'utf8'))) } catch (_) { ids = [] }
|
|
157
|
+
const inSource = ids.length > 0 && ids.every((id) => existing.has(id) || existingLinks.has(id))
|
|
158
|
+
return { name, status: promotedHere || inSource ? 'promoted' : 'not yet promoted' }
|
|
159
|
+
})
|
|
160
|
+
for (const item of after) {
|
|
161
|
+
if (item.status === 'missing') problems.push(`after: ${item.name} does not exist`)
|
|
162
|
+
if (item.status === 'not yet promoted') problems.push(`after: promote ${item.name} first`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// The whole knowledge base as it would be, checked by every rule validate
|
|
166
|
+
// applies. Problems the knowledge base already had are reported, not blamed
|
|
167
|
+
// on the draft.
|
|
168
|
+
const links = (draft.records.relationships || []).map((r) => `${r.from} ${r.type} ${r.to}${r.outcome ? ` (${r.outcome})` : ''}`)
|
|
169
|
+
if (!alreadyPromoted && !problems.length) {
|
|
170
|
+
const before = new Set(checkKnowledgeData(clone(knowledge)))
|
|
171
|
+
const merged = clone(knowledge)
|
|
172
|
+
for (const [collection, records] of Object.entries(draft.records)) {
|
|
173
|
+
merged[collection] = [...(merged[collection] || []), ...records]
|
|
174
|
+
}
|
|
175
|
+
for (const error of checkKnowledgeData(merged)) {
|
|
176
|
+
if (!before.has(error)) problems.push(error)
|
|
177
|
+
}
|
|
178
|
+
if (before.size) notes.push(`The knowledge base already has ${before.size} validation error${before.size === 1 ? '' : 's'} of its own. Run \`npx hippocampus validate\` to see them.`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return { draft, problems, notes, counts, added, links, after, alreadyPromoted, knowledge }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function clone (value) {
|
|
185
|
+
return JSON.parse(JSON.stringify(value))
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function summaryLines (ctx, draftPath, result) {
|
|
189
|
+
const lines = []
|
|
190
|
+
const total = Object.values(result.counts).reduce((a, b) => a + b, 0)
|
|
191
|
+
lines.push(`Draft: ${relative(ctx, draftPath)}${result.draft ? ` - ${result.draft.draft.title}` : ''}`)
|
|
192
|
+
lines.push(`Adds ${total} record${total === 1 ? '' : 's'}: ${Object.entries(result.counts).map(([k, n]) => `${k} ${n}`).join(', ') || 'none'}`)
|
|
193
|
+
for (const item of result.after) lines.push(`After ${item.name}: ${item.status}`)
|
|
194
|
+
return lines
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// The check's own account of the draft, written into its review note so the
|
|
198
|
+
// reviewer reads what the tool found rather than the drafter's description.
|
|
199
|
+
function noteBlock (ctx, draftPath, result, date) {
|
|
200
|
+
const lines = [NOTE_START, `### What \`hippocampus draft check\` found (${date})`, '']
|
|
201
|
+
lines.push('| Collection | Records | Ids |', '| --- | --- | --- |')
|
|
202
|
+
for (const [collection, ids] of Object.entries(result.added)) {
|
|
203
|
+
if (collection === 'relationships') continue
|
|
204
|
+
lines.push(`| ${collection} | ${ids.length} | ${ids.map((id) => `\`${id}\``).join(', ')} |`)
|
|
205
|
+
}
|
|
206
|
+
lines.push('')
|
|
207
|
+
if (result.links.length) {
|
|
208
|
+
lines.push(`Links (${result.links.length}):`, '', ...result.links.map((link) => `- \`${link}\``), '')
|
|
209
|
+
}
|
|
210
|
+
if (result.after.length) {
|
|
211
|
+
lines.push('Must be promoted first:', '', ...result.after.map((item) => `- ${item.name}: ${item.status}`), '')
|
|
212
|
+
}
|
|
213
|
+
lines.push(result.problems.length
|
|
214
|
+
? `Problems (${result.problems.length}):\n\n${result.problems.map((p) => `- ${p}`).join('\n')}`
|
|
215
|
+
: 'Problems: none. The knowledge base would still validate with this draft added.')
|
|
216
|
+
lines.push(NOTE_END)
|
|
217
|
+
return lines.join('\n')
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function writeNote (ctx, draftPath, result, explicit) {
|
|
221
|
+
const named = explicit || (result.draft && result.draft.draft.reviewNote)
|
|
222
|
+
let notePath = null
|
|
223
|
+
if (named) {
|
|
224
|
+
notePath = resolveFromRoot(ctx, named) || path.resolve(path.dirname(draftPath), named)
|
|
225
|
+
} else {
|
|
226
|
+
const sibling = draftPath.replace(/(\.draft)?\.json$/, '.review.md')
|
|
227
|
+
if (fs.existsSync(sibling)) notePath = sibling
|
|
228
|
+
}
|
|
229
|
+
if (!notePath) return null
|
|
230
|
+
const date = new Date().toISOString().slice(0, 10)
|
|
231
|
+
const block = noteBlock(ctx, draftPath, result, date)
|
|
232
|
+
const current = fs.existsSync(notePath) ? fs.readFileSync(notePath, 'utf8') : ''
|
|
233
|
+
const start = current.indexOf(NOTE_START)
|
|
234
|
+
const end = current.indexOf(NOTE_END)
|
|
235
|
+
const next = start >= 0 && end > start
|
|
236
|
+
? current.slice(0, start) + block + current.slice(end + NOTE_END.length)
|
|
237
|
+
: `${current.replace(/\s*$/, '')}${current ? '\n\n' : ''}${block}\n`
|
|
238
|
+
fs.writeFileSync(notePath, next)
|
|
239
|
+
return notePath
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function parseArgs (argv) {
|
|
243
|
+
const parsed = {}
|
|
244
|
+
for (let i = 0; i < argv.length; i++) {
|
|
245
|
+
if (!argv[i].startsWith('--')) continue
|
|
246
|
+
const [k, v] = argv[i].slice(2).split('=', 2)
|
|
247
|
+
const name = k.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
|
|
248
|
+
parsed[name] = v !== undefined ? v : (argv[i + 1] && !argv[i + 1].startsWith('--') ? argv[++i] : true)
|
|
249
|
+
}
|
|
250
|
+
return parsed
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
module.exports = {
|
|
254
|
+
MANIFEST_NAME,
|
|
255
|
+
NOTE_START,
|
|
256
|
+
NOTE_END,
|
|
257
|
+
analyseDraft,
|
|
258
|
+
readManifest,
|
|
259
|
+
writeManifest,
|
|
260
|
+
resolveFromRoot,
|
|
261
|
+
relative,
|
|
262
|
+
summaryLines,
|
|
263
|
+
writeNote,
|
|
264
|
+
parseArgs
|
|
265
|
+
}
|
|
@@ -6,7 +6,9 @@ const {
|
|
|
6
6
|
META_FILENAME,
|
|
7
7
|
readMeta,
|
|
8
8
|
migrateInMemory,
|
|
9
|
-
writeMeta
|
|
9
|
+
writeMeta,
|
|
10
|
+
unrecordedLevel,
|
|
11
|
+
recordLevelNeeded
|
|
10
12
|
} = require('../lib/schema-version')
|
|
11
13
|
|
|
12
14
|
// The loader migrates in memory and warns on every read. This is the command
|
|
@@ -41,18 +43,30 @@ function main () {
|
|
|
41
43
|
const sourceDir = path.join(project.dir, 'source')
|
|
42
44
|
const meta = readMeta(sourceDir)
|
|
43
45
|
|
|
44
|
-
if (meta.schemaVersion === CURRENT_SCHEMA_VERSION && !meta.implicit) {
|
|
45
|
-
console.log(`${project.name}: already at schemaVersion ${CURRENT_SCHEMA_VERSION}`)
|
|
46
|
-
continue
|
|
47
|
-
}
|
|
48
|
-
|
|
49
46
|
const raw = {}
|
|
50
47
|
for (const [key, filename] of Object.entries(collectionFiles)) {
|
|
51
48
|
const filePath = path.join(sourceDir, filename)
|
|
52
49
|
raw[key] = fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, 'utf8')) : []
|
|
53
50
|
}
|
|
51
|
+
|
|
52
|
+
if (meta.schemaVersion === CURRENT_SCHEMA_VERSION && !meta.implicit) {
|
|
53
|
+
// Nothing to migrate, but records added by hand may need a package level
|
|
54
|
+
// that _meta.json does not record yet. validate points here for that.
|
|
55
|
+
const unrecorded = unrecordedLevel(sourceDir, raw)
|
|
56
|
+
if (!unrecorded) {
|
|
57
|
+
console.log(`${project.name}: already at schemaVersion ${CURRENT_SCHEMA_VERSION}`)
|
|
58
|
+
} else if (dryRun) {
|
|
59
|
+
console.log(`${project.name}: would record that it needs Hippocampus ${unrecorded.level} (uses ${unrecorded.features.join(', ')})`)
|
|
60
|
+
} else {
|
|
61
|
+
recordLevelNeeded(sourceDir, raw)
|
|
62
|
+
console.log(`${project.name}: recorded that it needs Hippocampus ${unrecorded.level} (uses ${unrecorded.features.join(', ')})`)
|
|
63
|
+
}
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
|
|
54
67
|
raw.project = JSON.parse(fs.readFileSync(path.join(sourceDir, 'project.json'), 'utf8'))
|
|
55
|
-
|
|
68
|
+
const contextPath = path.join(sourceDir, 'project-context.json')
|
|
69
|
+
raw.projectContext = JSON.parse(fs.readFileSync(contextPath, 'utf8'))
|
|
56
70
|
|
|
57
71
|
const { data, version } = migrateInMemory(raw, meta)
|
|
58
72
|
|
|
@@ -69,6 +83,7 @@ function main () {
|
|
|
69
83
|
fs.writeFileSync(filePath, JSON.stringify(data[key], null, 2) + '\n')
|
|
70
84
|
}
|
|
71
85
|
writeMeta(sourceDir, { generator: 'hippocampus migrate:knowledge' })
|
|
86
|
+
recordLevelNeeded(sourceDir, data)
|
|
72
87
|
console.log(`${project.name}: migrated ${meta.schemaVersion} -> ${version}, ${META_FILENAME} stamped`)
|
|
73
88
|
}
|
|
74
89
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Add a reviewed draft's records to the knowledge base.
|
|
4
|
+
*
|
|
5
|
+
* npx hippocampus draft promote --draft hippocampus/imports/<folder>/<draft>.json --reviewer "<name or role>" [--dry-run]
|
|
6
|
+
*
|
|
7
|
+
* Refuses a draft that fails any check, whose `after` drafts are not in yet,
|
|
8
|
+
* or that has no reviewer. A reviewer is someone other than the draft's author:
|
|
9
|
+
* if the draft names its author in `draftedBy`, the two cannot be the same.
|
|
10
|
+
* Records who reviewed it, and when, in hippocampus/manifests/drafts.json.
|
|
11
|
+
*/
|
|
12
|
+
const fs = require('fs')
|
|
13
|
+
const path = require('path')
|
|
14
|
+
const { createContextOrExit } = require('../lib/context')
|
|
15
|
+
const { COLLECTION_FILES } = require('../lib/collections')
|
|
16
|
+
const { recordLevelNeeded } = require('../lib/schema-version')
|
|
17
|
+
const { analyseDraft, readManifest, writeManifest, resolveFromRoot, relative, summaryLines, parseArgs } = require('./lib/draft')
|
|
18
|
+
|
|
19
|
+
const ctx = createContextOrExit()
|
|
20
|
+
|
|
21
|
+
function main () {
|
|
22
|
+
const args = parseArgs(process.argv.slice(2))
|
|
23
|
+
if (!args.draft || args.draft === true) {
|
|
24
|
+
throw new Error('Usage: npx hippocampus draft promote --draft hippocampus/imports/<folder>/<draft>.json --reviewer "<name or role>" [--dry-run]')
|
|
25
|
+
}
|
|
26
|
+
const draftPath = resolveFromRoot(ctx, args.draft)
|
|
27
|
+
if (!draftPath) throw new Error(`No draft at ${args.draft}.`)
|
|
28
|
+
|
|
29
|
+
const result = analyseDraft(ctx, draftPath)
|
|
30
|
+
for (const line of summaryLines(ctx, draftPath, result)) console.log(line)
|
|
31
|
+
for (const note of result.notes) console.log(note)
|
|
32
|
+
|
|
33
|
+
const manifest = readManifest(ctx)
|
|
34
|
+
const where = relative(ctx, draftPath)
|
|
35
|
+
|
|
36
|
+
if (result.alreadyPromoted) {
|
|
37
|
+
console.log('Already promoted: every record in this draft is already in the knowledge base. Nothing was written.')
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (result.problems.length) {
|
|
42
|
+
console.error(`\nNot promoted. ${result.problems.length} problem${result.problems.length === 1 ? '' : 's'}:`)
|
|
43
|
+
for (const problem of result.problems) console.error(`- ${problem}`)
|
|
44
|
+
process.exit(1)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const reviewer = typeof args.reviewer === 'string' ? args.reviewer.trim() : ''
|
|
48
|
+
if (!reviewer) {
|
|
49
|
+
throw new Error('Not promoted. Say who reviewed the draft: --reviewer "<name or role>". Nothing reaches the knowledge base without a review.')
|
|
50
|
+
}
|
|
51
|
+
const author = result.draft.draft.draftedBy
|
|
52
|
+
if (author && String(author).trim().toLowerCase() === reviewer.toLowerCase()) {
|
|
53
|
+
throw new Error(`Not promoted. ${reviewer} drafted this, so cannot also be its reviewer.`)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (args.dryRun) {
|
|
57
|
+
console.log('\nDry run. Every check passed; nothing was written.')
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const dir = ctx.paths.knowledgeSourceDir
|
|
62
|
+
const merged = result.knowledge
|
|
63
|
+
for (const [collection, records] of Object.entries(result.draft.records)) {
|
|
64
|
+
const file = path.join(dir, COLLECTION_FILES[collection])
|
|
65
|
+
const current = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : []
|
|
66
|
+
const next = [...current, ...records]
|
|
67
|
+
fs.writeFileSync(file, `${JSON.stringify(next, null, 2)}\n`)
|
|
68
|
+
merged[collection] = next
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const level = recordLevelNeeded(dir, merged)
|
|
72
|
+
if (level) console.log(`This knowledge base now needs Hippocampus ${level} or later. Recorded in _meta.json.`)
|
|
73
|
+
|
|
74
|
+
manifest.promoted.push({
|
|
75
|
+
draft: where,
|
|
76
|
+
title: result.draft.draft.title,
|
|
77
|
+
promotedAt: new Date().toISOString(),
|
|
78
|
+
reviewedBy: reviewer,
|
|
79
|
+
...(result.draft.draft.draftedBy ? { draftedBy: result.draft.draft.draftedBy } : {}),
|
|
80
|
+
counts: result.counts
|
|
81
|
+
})
|
|
82
|
+
writeManifest(ctx, manifest)
|
|
83
|
+
|
|
84
|
+
console.log(`\nPromoted ${where}, reviewed by ${reviewer}. Review the change with \`git diff hippocampus/source\`.`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
main()
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error(error.message)
|
|
91
|
+
process.exit(1)
|
|
92
|
+
}
|
|
@@ -2,7 +2,9 @@ const fs = require('fs')
|
|
|
2
2
|
const path = require('path')
|
|
3
3
|
const { spawnSync } = require('child_process')
|
|
4
4
|
const { ResearchImportDraftSchema } = require('../lib/schema')
|
|
5
|
+
const { readAllRecordIds } = require('../lib/collections')
|
|
5
6
|
const { createContextOrExit } = require('../lib/context')
|
|
7
|
+
const { recordLevelNeeded } = require('../lib/schema-version')
|
|
6
8
|
const ctx = createContextOrExit()
|
|
7
9
|
const { knowledgeSourceDir, researchImportsDir, researchManifestPath } = ctx.paths
|
|
8
10
|
|
|
@@ -51,6 +53,10 @@ function main () {
|
|
|
51
53
|
for (const [key, filename] of Object.entries(collections)) {
|
|
52
54
|
writeJson(path.join(dataDir, filename), proposed[key])
|
|
53
55
|
}
|
|
56
|
+
// Before validation, so a package that predates what this draft uses
|
|
57
|
+
// refuses the knowledge base with an upgrade message, not a schema error.
|
|
58
|
+
const level = recordLevelNeeded(dataDir, proposed)
|
|
59
|
+
if (level) console.log(`This knowledge base now needs Hippocampus ${level} or later. Recorded in _meta.json.`)
|
|
54
60
|
|
|
55
61
|
// Run the sibling script directly rather than `npm run <script>`. Those npm
|
|
56
62
|
// scripts exist in this repo and in the demo, and in no host that installs the
|
|
@@ -291,19 +297,11 @@ function validateDraftReferences (proposed) {
|
|
|
291
297
|
}
|
|
292
298
|
}
|
|
293
299
|
|
|
300
|
+
// Every record already in the knowledge base, whatever its type. This list
|
|
301
|
+
// once stopped at assumptions, so a finding that linked to a requirement, a
|
|
302
|
+
// goal or a principle was refused as linking to something missing.
|
|
294
303
|
function readCanonicalEntityIds () {
|
|
295
|
-
|
|
296
|
-
'users.json',
|
|
297
|
-
'needs.json',
|
|
298
|
-
'scenarios.json',
|
|
299
|
-
'journeys.json',
|
|
300
|
-
'screens.json',
|
|
301
|
-
'evidence.json',
|
|
302
|
-
'decisions.json',
|
|
303
|
-
'assumptions.json'
|
|
304
|
-
]
|
|
305
|
-
|
|
306
|
-
return filenames.flatMap((filename) => readJson(path.join(dataDir, filename)).map((item) => item.id))
|
|
304
|
+
return [...readAllRecordIds(dataDir)]
|
|
307
305
|
}
|
|
308
306
|
|
|
309
307
|
function idSet (items) {
|
package/scripts/smoke-routes.js
CHANGED
|
@@ -25,7 +25,9 @@ async function main () {
|
|
|
25
25
|
const server = spawn(process.execPath, ['app.js'], {
|
|
26
26
|
cwd: ctx.root,
|
|
27
27
|
env: { ...process.env, PORT: String(port) },
|
|
28
|
-
stdio: ['ignore', 'pipe', 'pipe']
|
|
28
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
29
|
+
// Its own process group, so the kit's watchers can be stopped with it.
|
|
30
|
+
detached: process.platform !== 'win32'
|
|
29
31
|
})
|
|
30
32
|
server.stdout.on('data', (chunk) => { serverOutput += chunk })
|
|
31
33
|
server.stderr.on('data', (chunk) => { serverOutput += chunk })
|
|
@@ -43,7 +45,16 @@ async function main () {
|
|
|
43
45
|
// hung smoke test.
|
|
44
46
|
server.stdout.destroy()
|
|
45
47
|
server.stderr.destroy()
|
|
46
|
-
|
|
48
|
+
// Killing app.js alone leaves the kit's nodemon, Browsersync and esbuild
|
|
49
|
+
// watcher running, still writing into public/. In an install test that
|
|
50
|
+
// races the scratch project's deletion and fails a passing run with
|
|
51
|
+
// ENOTEMPTY. So the whole group goes, where the platform has groups.
|
|
52
|
+
try {
|
|
53
|
+
if (process.platform === 'win32') server.kill('SIGKILL')
|
|
54
|
+
else process.kill(-server.pid, 'SIGKILL')
|
|
55
|
+
} catch (_) {
|
|
56
|
+
// Already gone.
|
|
57
|
+
}
|
|
47
58
|
}
|
|
48
59
|
}
|
|
49
60
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const { createContextOrExit } = require('../lib/context')
|
|
2
|
+
const path = require('path')
|
|
2
3
|
const { lintKnowledge } = require('../lib/lint-knowledge')
|
|
4
|
+
const { unrecordedLevel } = require('../lib/schema-version')
|
|
3
5
|
|
|
4
6
|
const ctx = createContextOrExit()
|
|
5
7
|
const strict = process.argv.includes('--strict')
|
|
@@ -28,6 +30,14 @@ function main () {
|
|
|
28
30
|
console.log(`${project.name}: knowledge data is valid`)
|
|
29
31
|
|
|
30
32
|
for (const warning of warnings) console.warn(` warning ${warning.message}`)
|
|
33
|
+
|
|
34
|
+
// Records added by hand bypass the commands that record the level, and an
|
|
35
|
+
// older package then fails on them with a schema error, not an upgrade.
|
|
36
|
+
const unrecorded = unrecordedLevel(path.join(project.dir, 'source'), knowledge)
|
|
37
|
+
if (unrecorded) {
|
|
38
|
+
console.warn(` warning uses ${unrecorded.features.join(', ')}, which need Hippocampus ${unrecorded.level}, but _meta.json does not say so. Older versions will fail on it instead of asking for an upgrade. Run \`npx hippocampus migrate\` to record it.`)
|
|
39
|
+
if (strict) failed = true
|
|
40
|
+
}
|
|
31
41
|
if (!quiet) {
|
|
32
42
|
for (const info of infos) console.log(` info ${info.message}`)
|
|
33
43
|
}
|
|
@@ -64,6 +64,14 @@
|
|
|
64
64
|
<li class="knowledge-muted">None linked</li>
|
|
65
65
|
{% endfor %}
|
|
66
66
|
</ul>
|
|
67
|
+
{% if round.reviewers.length %}
|
|
68
|
+
<h3 class="nhsuk-heading-s">Reviewers</h3>
|
|
69
|
+
<ul class="nhsuk-list">
|
|
70
|
+
{% for reviewer in round.reviewers %}
|
|
71
|
+
<li>{{ reviewer.label }} <span class="knowledge-muted">{{ reviewer.role }}</span></li>
|
|
72
|
+
{% endfor %}
|
|
73
|
+
</ul>
|
|
74
|
+
{% endif %}
|
|
67
75
|
</div>
|
|
68
76
|
<div class="nhsuk-grid-column-one-third">
|
|
69
77
|
<h3 class="nhsuk-heading-s">Insights</h3>
|