slicekit 0.1.2 → 0.2.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.
Files changed (66) hide show
  1. package/README.md +15 -13
  2. package/package.json +3 -2
  3. package/src/adapters/githost/fixture.js +24 -1
  4. package/src/adapters/githost/gh-json.js +69 -2
  5. package/src/adapters/githost/github.js +16 -1
  6. package/src/build/commit.js +152 -26
  7. package/src/build/config.js +50 -0
  8. package/src/build/pr.js +30 -15
  9. package/src/build/record.js +8 -2
  10. package/src/build/red.js +4 -3
  11. package/src/build/render.js +155 -0
  12. package/src/build/verdict.js +29 -0
  13. package/src/build/worktree.js +26 -5
  14. package/src/check/ci-event.js +17 -0
  15. package/src/check/rules/citations.js +53 -2
  16. package/src/check/rules/commands.js +32 -4
  17. package/src/check/rules/trailer.js +5 -16
  18. package/src/check/rules/workspace.js +86 -0
  19. package/src/cli/build.js +34 -8
  20. package/src/cli/ci.js +150 -0
  21. package/src/cli/decide.js +4 -3
  22. package/src/cli/gaps.js +6 -5
  23. package/src/cli/graph.js +79 -0
  24. package/src/cli/history.js +17 -0
  25. package/src/cli/hook.js +1 -1
  26. package/src/cli/impact.js +17 -0
  27. package/src/cli/index.js +39 -3
  28. package/src/cli/instructions.js +1 -1
  29. package/src/cli/intake.js +3 -3
  30. package/src/cli/query.js +39 -0
  31. package/src/cli/shipped.js +32 -0
  32. package/src/cli/state.js +42 -2
  33. package/src/cli/trace.js +21 -0
  34. package/src/cli/uncovered.js +32 -0
  35. package/src/cli/visualize.js +88 -0
  36. package/src/cli/workflow.js +6 -2
  37. package/src/config.js +90 -3
  38. package/src/git.js +35 -0
  39. package/src/graph/build.js +312 -0
  40. package/src/graph/docs.js +241 -0
  41. package/src/graph/git-walk.js +91 -0
  42. package/src/graph/history.js +60 -0
  43. package/src/graph/impact.js +143 -0
  44. package/src/graph/kitfiles.js +60 -0
  45. package/src/graph/load.js +93 -0
  46. package/src/graph/prs.js +136 -0
  47. package/src/graph/render.js +28 -0
  48. package/src/graph/schema.js +108 -0
  49. package/src/graph/shipped.js +32 -0
  50. package/src/graph/tests.js +45 -0
  51. package/src/graph/trace.js +83 -0
  52. package/src/graph/uncovered.js +16 -0
  53. package/src/graph/workspace.js +188 -0
  54. package/src/hook-cli.js +1 -1
  55. package/src/init.js +5 -2
  56. package/src/instructions.js +26 -8
  57. package/src/state/index.js +35 -4
  58. package/src/verdict.js +14 -2
  59. package/src/visualize/client.js +489 -0
  60. package/src/visualize/page.js +100 -0
  61. package/src/visualize/server.js +46 -0
  62. package/src/workflow.js +87 -9
  63. package/templates/instructions/agents/reviewer.md +7 -7
  64. package/templates/instructions/commands/build.md +34 -4
  65. package/templates/workflow/pins.json +2 -1
  66. package/templates/workflow/slicekit.yml +32 -1
@@ -0,0 +1,489 @@
1
+ /* The visualize page's script (REQ-4.7). A classic script: it defines one global, slicekitViews,
2
+ whose view functions take the graph and return HTML strings, so a test runs them under node:vm
3
+ without a browser. Every text it inserts goes through escapeHtml. Only when a document exists does
4
+ it read the inlined graph and fill the page. It loads nothing. */
5
+ var slicekitViews = (function () {
6
+ 'use strict';
7
+
8
+ var STATES = ['todo', 'tests-red', 'building', 'draft-pr', 'in-review', 'changes-requested', 'ci-green', 'ready', 'merged', 'released'];
9
+ var ENTITIES = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
10
+
11
+ function escapeHtml(value) {
12
+ return String(value === undefined || value === null ? '' : value).replace(/[&<>"']/g, function (c) {
13
+ return ENTITIES[c];
14
+ });
15
+ }
16
+
17
+ function cmp(a, b) {
18
+ return a < b ? -1 : a > b ? 1 : 0;
19
+ }
20
+ function keyOf(id) {
21
+ var s = String(id);
22
+ return s.slice(s.indexOf(':') + 1);
23
+ }
24
+ function kindOf(id) {
25
+ var s = String(id);
26
+ var i = s.indexOf(':');
27
+ return i === -1 ? '' : s.slice(0, i);
28
+ }
29
+ // REQ-1.10 after REQ-1.9; P2/S-03 after P1/S-10: digits compared as numbers.
30
+ function natural(a, b) {
31
+ var x = String(a).split(/(\d+)/);
32
+ var y = String(b).split(/(\d+)/);
33
+ for (var i = 0; i < Math.min(x.length, y.length); i++) {
34
+ if (x[i] === y[i]) continue;
35
+ if (i % 2 === 1) return Number(x[i]) - Number(y[i]) || cmp(x[i], y[i]);
36
+ return cmp(x[i], y[i]);
37
+ }
38
+ return x.length - y.length;
39
+ }
40
+ function day(iso) {
41
+ return iso ? String(iso).slice(0, 10) : '';
42
+ }
43
+ // Only a web address may become a link.
44
+ function webOnly(url) {
45
+ return /^https?:\/\//i.test(String(url || '')) ? String(url) : null;
46
+ }
47
+
48
+ var cache = typeof WeakMap === 'function' ? new WeakMap() : null;
49
+ function index(graph) {
50
+ if (cache && cache.has(graph)) return cache.get(graph);
51
+ var byId = new Map();
52
+ var kinds = new Map();
53
+ var outs = new Map();
54
+ var ins = new Map();
55
+ (graph.nodes || []).forEach(function (n) {
56
+ byId.set(n.id, n);
57
+ if (!kinds.has(n.kind)) kinds.set(n.kind, []);
58
+ kinds.get(n.kind).push(n);
59
+ });
60
+ (graph.edges || []).forEach(function (e) {
61
+ if (!outs.has(e.from)) outs.set(e.from, []);
62
+ outs.get(e.from).push(e);
63
+ if (!ins.has(e.to)) ins.set(e.to, []);
64
+ ins.get(e.to).push(e);
65
+ });
66
+ function pick(map, id, kind, otherKind, end) {
67
+ var list = (map.get(id) || []).filter(function (e) {
68
+ return (!kind || e.kind === kind) && (!otherKind || kindOf(e[end]) === otherKind);
69
+ });
70
+ var seen = new Set();
71
+ var nodes = [];
72
+ list.forEach(function (e) {
73
+ var n = byId.get(e[end]);
74
+ if (n && !seen.has(n.id)) {
75
+ seen.add(n.id);
76
+ nodes.push(n);
77
+ }
78
+ });
79
+ return nodes;
80
+ }
81
+ var idx = {
82
+ node: function (id) {
83
+ return byId.get(id) || null;
84
+ },
85
+ byKind: function (kind) {
86
+ return (kinds.get(kind) || []).slice();
87
+ },
88
+ // The nodes an edge of `kind` reaches from `id` (out) or comes from into `id` (in).
89
+ out: function (id, kind, otherKind) {
90
+ return pick(outs, id, kind, otherKind, 'to');
91
+ },
92
+ in: function (id, kind, otherKind) {
93
+ return pick(ins, id, kind, otherKind, 'from');
94
+ },
95
+ neighbours: function (id) {
96
+ var list = [];
97
+ (outs.get(id) || []).forEach(function (e) {
98
+ list.push({ edge: e.kind, dir: 'out', node: byId.get(e.to) });
99
+ });
100
+ (ins.get(id) || []).forEach(function (e) {
101
+ list.push({ edge: e.kind, dir: 'in', node: byId.get(e.from) });
102
+ });
103
+ return list.filter(function (x) {
104
+ return x.node;
105
+ });
106
+ },
107
+ };
108
+ if (cache) cache.set(graph, idx);
109
+ return idx;
110
+ }
111
+
112
+ // A workspace repository's pull request is <repo>#<n>, as src/graph/render.js names it; one of the
113
+ // program repository is #<n>.
114
+ function prName(p) {
115
+ return p.repo ? keyOf(p.id) : '#' + (p.number || keyOf(p.id));
116
+ }
117
+ function title(n) {
118
+ var key = keyOf(n.id);
119
+ if (n.kind === 'commit') return key.slice(0, 7) + ' ' + (n.subject || n.label || '');
120
+ if (n.kind === 'pull-request') return prName(n) + ' ' + (n.label || '');
121
+ if (n.kind === 'file' || n.kind === 'test' || n.kind === 'intake' || n.kind === 'repo') return key;
122
+ if (n.kind === 'release') return key;
123
+ return n.label && n.label !== key ? key + ' ' + n.label : key;
124
+ }
125
+ // A clickable node: a button carrying the node's id.
126
+ function nodeButton(n, text) {
127
+ return '<button type="button" class="node" data-id="' + escapeHtml(n.id) + '">' + escapeHtml(text === undefined ? title(n) : text) + '</button>';
128
+ }
129
+ function empty(text) {
130
+ return '<p class="empty">' + escapeHtml(text) + '</p>';
131
+ }
132
+ function list(nodes, render) {
133
+ if (nodes.length === 0) return '';
134
+ return '<ul>' + nodes.map(function (n) {
135
+ return '<li>' + (render ? render(n) : nodeButton(n)) + '</li>';
136
+ }).join('') + '</ul>';
137
+ }
138
+ function byNatural(a, b) {
139
+ return natural(keyOf(a.id), keyOf(b.id));
140
+ }
141
+
142
+ // The requirement tree: spec file, then requirement, then the slices implementing it and the tests covering it.
143
+ function requirements(graph) {
144
+ var idx = index(graph);
145
+ var reqs = idx.byKind('requirement').sort(byNatural);
146
+ if (reqs.length === 0) return '<h2>Requirements</h2>' + empty('No requirement in the graph.');
147
+ var files = new Map();
148
+ reqs.forEach(function (r) {
149
+ var file = String(r.from || '').replace(/:\d+$/, '');
150
+ if (!files.has(file)) files.set(file, []);
151
+ files.get(file).push(r);
152
+ });
153
+ var html = '<h2>Requirements</h2><ul class="tree">';
154
+ Array.from(files.keys()).sort(natural).forEach(function (file) {
155
+ html += '<li><code>' + escapeHtml(file) + '</code><ul>';
156
+ files.get(file).forEach(function (r) {
157
+ var slices = idx.in(r.id, 'implements', 'slice').sort(byNatural);
158
+ var tests = idx.in(r.id, 'covers', 'test').sort(byNatural);
159
+ html += '<li>' + nodeButton(r);
160
+ if (slices.length || tests.length) {
161
+ html += '<ul>' + slices.map(function (s) {
162
+ return '<li><span class="kind">slice</span>' + nodeButton(s) + ' <span class="n">' + escapeHtml(s.state || '') + '</span></li>';
163
+ }).join('') + tests.map(function (t) {
164
+ return '<li><span class="kind">test</span>' + nodeButton(t) + '</li>';
165
+ }).join('') + '</ul>';
166
+ } else {
167
+ html += ' <span class="empty">no slice, no test</span>';
168
+ }
169
+ html += '</li>';
170
+ });
171
+ html += '</ul></li>';
172
+ });
173
+ return html + '</ul>';
174
+ }
175
+
176
+ // Slices by state, in REQ-1.8's order; a state outside it goes last.
177
+ function board(graph) {
178
+ var idx = index(graph);
179
+ var slices = idx.byKind('slice').sort(byNatural);
180
+ var columns = STATES.slice();
181
+ slices.forEach(function (s) {
182
+ var st = s.state || 'todo';
183
+ if (columns.indexOf(st) === -1) columns.push(st);
184
+ });
185
+ var html = '<h2>Board</h2>';
186
+ if (slices.length === 0) html += empty('No slice in the graph.');
187
+ html += '<div class="board">';
188
+ columns.forEach(function (st) {
189
+ var here = slices.filter(function (s) {
190
+ return (s.state || 'todo') === st;
191
+ });
192
+ html += '<div class="column"><h3>' + escapeHtml(st) + ' <span class="count">' + here.length + '</span></h3>';
193
+ html += here.length ? list(here) : '';
194
+ html += '</div>';
195
+ });
196
+ return html + '</div>';
197
+ }
198
+
199
+ function sliceCommits(idx, slice) {
200
+ return idx.in(slice.id, 'in', 'commit').sort(function (a, b) {
201
+ return cmp(a.date || '', b.date || '') || cmp(a.id, b.id);
202
+ });
203
+ }
204
+
205
+ // Each slice from its first commit to its merge (or its last commit while open).
206
+ function timeline(graph) {
207
+ var idx = index(graph);
208
+ var rows = [];
209
+ var none = [];
210
+ idx.byKind('slice').forEach(function (s) {
211
+ var commits = sliceCommits(idx, s);
212
+ if (commits.length === 0) {
213
+ none.push(s);
214
+ return;
215
+ }
216
+ var start = commits[0].date || '';
217
+ var last = commits[commits.length - 1].date || start;
218
+ rows.push({ slice: s, start: start, end: s.mergedAt || last, merged: Boolean(s.mergedAt) });
219
+ });
220
+ rows.sort(function (a, b) {
221
+ return cmp(a.start, b.start) || byNatural(a.slice, b.slice);
222
+ });
223
+ var html = '<h2>Timeline</h2>';
224
+ if (rows.length === 0) html += empty('No slice has a commit yet.');
225
+ else {
226
+ var lo = Math.min.apply(null, rows.map(function (r) { return Date.parse(r.start) || 0; }));
227
+ var hi = Math.max.apply(null, rows.map(function (r) { return Date.parse(r.end) || 0; }));
228
+ var span = Math.max(hi - lo, 1);
229
+ html += '<ul class="rows">' + rows.map(function (r) {
230
+ var left = (((Date.parse(r.start) || lo) - lo) / span) * 100;
231
+ var width = Math.max((((Date.parse(r.end) || lo) - (Date.parse(r.start) || lo)) / span) * 100, 0.5);
232
+ if (left + width > 100) width = 100 - left;
233
+ return '<li>' + nodeButton(r.slice) +
234
+ '<span class="dates">' + escapeHtml(day(r.start)) + ' to ' + escapeHtml(day(r.end)) + (r.merged ? '' : ' (open)') + '</span>' +
235
+ '<div class="track" aria-hidden="true"><div class="bar" style="margin-left:' + left.toFixed(2) + '%;width:' + width.toFixed(2) + '%"></div></div></li>';
236
+ }).join('') + '</ul>';
237
+ }
238
+ if (none.length) html += '<h3>No commit yet</h3>' + list(none.sort(byNatural));
239
+ return html;
240
+ }
241
+
242
+ // Each file with the number of slices whose commits touched it, most first.
243
+ function heat(graph) {
244
+ var idx = index(graph);
245
+ var rows = [];
246
+ idx.byKind('file').forEach(function (f) {
247
+ var slices = new Set();
248
+ idx.in(f.id, 'touched', 'commit').forEach(function (c) {
249
+ idx.out(c.id, 'in', 'slice').forEach(function (s) {
250
+ slices.add(s.id);
251
+ });
252
+ });
253
+ if (slices.size > 0) rows.push({ file: f, n: slices.size });
254
+ });
255
+ rows.sort(function (a, b) {
256
+ return b.n - a.n || cmp(keyOf(a.file.id), keyOf(b.file.id));
257
+ });
258
+ var html = '<h2>File heat</h2>';
259
+ if (rows.length === 0) return html + empty('No file has been touched by a slice.');
260
+ var max = rows[0].n;
261
+ return html + '<ul class="rows">' + rows.map(function (r) {
262
+ return '<li>' + nodeButton(r.file) + '<span class="n">' + r.n + (r.n === 1 ? ' slice' : ' slices') + '</span>' +
263
+ '<div class="track" aria-hidden="true"><div class="bar" style="width:' + ((r.n / max) * 100).toFixed(2) + '%"></div></div></li>';
264
+ }).join('') + '</ul>';
265
+ }
266
+
267
+ function sliceWithRepo(s) {
268
+ return nodeButton(s) + ' <span class="n">repo ' + escapeHtml(s.repo || '.') + '</span>';
269
+ }
270
+
271
+ // Each contract with its producer and consumer slices and their repositories.
272
+ function contracts(graph) {
273
+ var idx = index(graph);
274
+ var all = idx.byKind('contract').sort(byNatural);
275
+ var html = '<h2>Contracts</h2>';
276
+ if (all.length === 0) return html + empty('No contract in the graph.');
277
+ return html + all.map(function (c) {
278
+ var producers = idx.in(c.id, 'produces', 'slice').sort(byNatural);
279
+ var consumers = idx.in(c.id, 'consumes', 'slice').sort(byNatural);
280
+ return '<div class="card">' + nodeButton(c) +
281
+ '<h3>Producer</h3>' + (producers.length ? list(producers, sliceWithRepo) : empty('none named')) +
282
+ '<h3>Consumers</h3>' + (consumers.length ? list(consumers, sliceWithRepo) : empty('none named')) + '</div>';
283
+ }).join('');
284
+ }
285
+
286
+ function prNumber(a, b) {
287
+ return (Number(a.number) || 0) - (Number(b.number) || 0) || cmp(a.id, b.id);
288
+ }
289
+
290
+ // Each release with its pull requests and the requirements their slices implement.
291
+ function releases(graph) {
292
+ var idx = index(graph);
293
+ var all = idx.byKind('release').sort(byNatural);
294
+ var html = '<h2>Releases</h2>';
295
+ if (all.length === 0) return html + empty('No release record in the graph.');
296
+ return html + all.map(function (r) {
297
+ var prs = idx.in(r.id, 'released-in', 'pull-request').sort(prNumber);
298
+ var reqs = new Map();
299
+ prs.forEach(function (p) {
300
+ idx.out(p.id, 'in', 'slice').forEach(function (s) {
301
+ idx.out(s.id, 'implements', 'requirement').forEach(function (q) {
302
+ reqs.set(q.id, q);
303
+ });
304
+ });
305
+ });
306
+ var reqList = Array.from(reqs.values()).sort(byNatural);
307
+ return '<div class="card">' + nodeButton(r, 'Release ' + keyOf(r.id)) +
308
+ '<h3>Pull requests</h3>' + (prs.length ? list(prs) : empty('none')) +
309
+ '<h3>Requirements</h3>' + (reqList.length ? list(reqList) : empty('none')) + '</div>';
310
+ }).join('');
311
+ }
312
+
313
+ // The repository a commit belongs to: its slice's, else the one its files sit in.
314
+ function commitRepo(idx, commit) {
315
+ var slices = idx.out(commit.id, 'in', 'slice');
316
+ for (var i = 0; i < slices.length; i++) {
317
+ var r = idx.node('repo:' + (slices[i].repo || '.'));
318
+ if (r) return r;
319
+ }
320
+ var files = idx.out(commit.id, 'touched', 'file');
321
+ for (var j = 0; j < files.length; j++) {
322
+ var repos = idx.out(files[j].id, 'in', 'repo');
323
+ if (repos.length) return repos[0];
324
+ }
325
+ return null;
326
+ }
327
+
328
+ function link(url, text) {
329
+ return '<a href="' + escapeHtml(url) + '" target="_blank" rel="noopener noreferrer">' + escapeHtml(text) + '</a>';
330
+ }
331
+
332
+ function commitLine(idx, c) {
333
+ var sha = keyOf(c.id);
334
+ var repo = commitRepo(idx, c);
335
+ var web = repo && webOnly(repo.web);
336
+ var shaHtml = web ? link(web.replace(/\/+$/, '') + '/commit/' + sha, sha.slice(0, 7)) : '<code>' + escapeHtml(sha.slice(0, 7)) + '</code>';
337
+ return shaHtml + ' ' + nodeButton(c, c.subject || c.label || sha) + ' <span class="n">' + escapeHtml(day(c.date)) + '</span>';
338
+ }
339
+
340
+ function prLine(p) {
341
+ var url = webOnly(p.url);
342
+ var text = prName(p);
343
+ return (url ? link(url, text) : '<code>' + escapeHtml(text) + '</code>') + ' ' + nodeButton(p, p.label || text) +
344
+ (p.state ? ' <span class="n">' + escapeHtml(String(p.state).toLowerCase()) + '</span>' : '');
345
+ }
346
+
347
+ // The slices whose work a node gathers: itself for a slice, its slices for anything above or below one.
348
+ function slicesOf(idx, n) {
349
+ switch (n.kind) {
350
+ case 'slice': return [n];
351
+ case 'requirement': return idx.in(n.id, 'implements', 'slice');
352
+ case 'contract': return idx.in(n.id, 'produces', 'slice').concat(idx.in(n.id, 'consumes', 'slice'));
353
+ case 'phase': return idx.in(n.id, 'in', 'slice');
354
+ case 'pull-request':
355
+ case 'verdict': return idx.out(n.id, 'in', 'slice');
356
+ case 'release': {
357
+ var out = [];
358
+ idx.in(n.id, 'released-in', 'pull-request').forEach(function (p) {
359
+ out = out.concat(idx.out(p.id, 'in', 'slice'));
360
+ });
361
+ return out;
362
+ }
363
+ case 'commit': return idx.out(n.id, 'in', 'slice');
364
+ default: return [];
365
+ }
366
+ }
367
+
368
+ function uniq(nodes) {
369
+ var seen = new Set();
370
+ return nodes.filter(function (n) {
371
+ if (!n || seen.has(n.id)) return false;
372
+ seen.add(n.id);
373
+ return true;
374
+ });
375
+ }
376
+
377
+ // The detail panel for one node: what it is, its files, commits and pull requests, then its other neighbours.
378
+ function detail(graph, id) {
379
+ var idx = index(graph);
380
+ var n = idx.node(id);
381
+ if (!n) return empty('Not in the graph: ' + id);
382
+ var slices = uniq(slicesOf(idx, n));
383
+ var commits;
384
+ var files;
385
+ if (n.kind === 'commit') {
386
+ commits = [n];
387
+ } else if (n.kind === 'file') {
388
+ commits = idx.in(n.id, 'touched', 'commit');
389
+ } else {
390
+ commits = [];
391
+ slices.forEach(function (s) {
392
+ commits = commits.concat(idx.in(s.id, 'in', 'commit'));
393
+ });
394
+ }
395
+ commits = uniq(commits).sort(function (a, b) {
396
+ return cmp(a.date || '', b.date || '') || cmp(a.id, b.id);
397
+ });
398
+ files = [];
399
+ if (n.kind !== 'file') {
400
+ commits.forEach(function (c) {
401
+ files = files.concat(idx.out(c.id, 'touched', 'file'));
402
+ });
403
+ }
404
+ files = uniq(files).sort(function (a, b) {
405
+ return cmp(keyOf(a.id), keyOf(b.id));
406
+ });
407
+ var prs = n.kind === 'pull-request' ? [n] : [];
408
+ var prSlices = n.kind === 'file' ? uniq([].concat.apply([], commits.map(function (c) { return idx.out(c.id, 'in', 'slice'); }))) : slices;
409
+ prSlices.forEach(function (s) {
410
+ prs = prs.concat(idx.in(s.id, 'in', 'pull-request'));
411
+ });
412
+ if (n.kind === 'release') prs = prs.concat(idx.in(n.id, 'released-in', 'pull-request'));
413
+ prs = uniq(prs).sort(prNumber);
414
+
415
+ var html = '<h2><span class="kind">' + escapeHtml(n.kind) + '</span>' + escapeHtml(title(n)) + '</h2>';
416
+ if (n.from) html += '<p class="meta">from <code>' + escapeHtml(n.from) + '</code></p>';
417
+ if (n.kind === 'slice') html += '<p class="meta">state ' + escapeHtml(n.state || 'todo') + ', repo ' + escapeHtml(n.repo || '.') + (n.mergedAt ? ', merged ' + escapeHtml(day(n.mergedAt)) : '') + '</p>';
418
+ if (n.kind === 'repo' && webOnly(n.web)) html += '<p>' + link(n.web, n.web) + '</p>';
419
+ if (n.kind !== 'slice' && slices.length) html += '<h3>Slices</h3>' + list(slices.sort(byNatural));
420
+ if (n.kind === 'file') html += '<h3>Slices</h3>' + (prSlices.length ? list(prSlices.sort(byNatural)) : empty('none'));
421
+ if (n.kind !== 'file') html += '<h3>Files</h3>' + (files.length ? list(files) : empty('none'));
422
+ html += '<h3>Commits</h3>' + (commits.length ? list(commits, function (c) { return commitLine(idx, c); }) : empty('none'));
423
+ html += '<h3>Pull requests</h3>' + (prs.length ? list(prs, prLine) : empty('none'));
424
+
425
+ var shown = new Set([n.id]);
426
+ slices.concat(commits, files, prs).forEach(function (x) { shown.add(x.id); });
427
+ var others = idx.neighbours(n.id).filter(function (x) { return !shown.has(x.node.id); });
428
+ if (others.length) {
429
+ html += '<h3>Linked</h3><ul>' + others.sort(function (a, b) {
430
+ return cmp(a.edge, b.edge) || cmp(a.node.id, b.node.id);
431
+ }).map(function (x) {
432
+ return '<li><span class="kind">' + escapeHtml(x.dir === 'out' ? x.edge : x.edge + ' by') + '</span>' + nodeButton(x.node) + '</li>';
433
+ }).join('') + '</ul>';
434
+ }
435
+ return html;
436
+ }
437
+
438
+ return {
439
+ escapeHtml: escapeHtml,
440
+ requirements: requirements,
441
+ board: board,
442
+ timeline: timeline,
443
+ heat: heat,
444
+ contracts: contracts,
445
+ releases: releases,
446
+ detail: detail,
447
+ };
448
+ })();
449
+
450
+ if (typeof document !== 'undefined') {
451
+ (function () {
452
+ var graph = JSON.parse(document.getElementById('graph').textContent);
453
+ var meta = document.getElementById('meta');
454
+ if (meta) {
455
+ meta.textContent = 'built ' + (graph.builtAt || '?') + ' from ' + String(graph.sha || '?').slice(0, 7) + ', ' +
456
+ (graph.nodes || []).length + ' nodes, ' + (graph.edges || []).length + ' edges';
457
+ }
458
+ var sections = document.querySelectorAll('section[data-view]');
459
+ Array.prototype.forEach.call(sections, function (section) {
460
+ var name = section.getAttribute('data-view');
461
+ try {
462
+ section.innerHTML = slicekitViews[name](graph);
463
+ } catch (e) {
464
+ section.textContent = 'This view failed: ' + (e && e.message ? e.message : e);
465
+ }
466
+ });
467
+ var buttons = document.querySelectorAll('nav button[data-show]');
468
+ function show(name) {
469
+ Array.prototype.forEach.call(sections, function (s) {
470
+ s.hidden = s.getAttribute('data-view') !== name;
471
+ });
472
+ Array.prototype.forEach.call(buttons, function (b) {
473
+ b.setAttribute('aria-pressed', String(b.getAttribute('data-show') === name));
474
+ });
475
+ }
476
+ Array.prototype.forEach.call(buttons, function (b) {
477
+ b.addEventListener('click', function () {
478
+ show(b.getAttribute('data-show'));
479
+ });
480
+ });
481
+ var panel = document.getElementById('detail');
482
+ document.addEventListener('click', function (event) {
483
+ var target = event.target && event.target.closest ? event.target.closest('[data-id]') : null;
484
+ if (!target || !panel) return;
485
+ panel.innerHTML = slicekitViews.detail(graph, target.getAttribute('data-id'));
486
+ if (window.matchMedia && window.matchMedia('(max-width: 760px)').matches && panel.scrollIntoView) panel.scrollIntoView();
487
+ });
488
+ })();
489
+ }
@@ -0,0 +1,100 @@
1
+ // The visualize page (REQ-4.7, D-04): one self-contained HTML file. The graph is inlined as an
2
+ // escaped application/json block, the page script (client.js) and the CSS are inline, and the
3
+ // Content-Security-Policy forbids loading anything, so the file works opened from disk offline.
4
+ import fs from 'node:fs';
5
+
6
+ export const CSP = "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data:";
7
+
8
+ const CLIENT = fs.readFileSync(new URL('./client.js', import.meta.url), 'utf8');
9
+
10
+ const VIEWS = [
11
+ ['requirements', 'Requirements'],
12
+ ['board', 'Board'],
13
+ ['timeline', 'Timeline'],
14
+ ['heat', 'File heat'],
15
+ ['contracts', 'Contracts'],
16
+ ['releases', 'Releases'],
17
+ ];
18
+
19
+ // JSON that cannot end the script block it sits in: <, >, & and the two JavaScript line separators
20
+ // are written as \u escapes, which JSON.parse reads back unchanged.
21
+ export function inlineJson(graph) {
22
+ return JSON.stringify(graph).replace(/[<>&\u2028\u2029]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
23
+ }
24
+
25
+ const STYLE = `
26
+ :root {
27
+ color-scheme: light dark;
28
+ --bg: #fbfbfa; --panel: #ffffff; --ink: #1c1c1a; --muted: #5f5f5a; --line: #deded8;
29
+ --accent: #1f5fbf; --accent-ink: #ffffff; --chip: #f0f0ec; --bar: #1f5fbf; --focus: #b35c00;
30
+ }
31
+ @media (prefers-color-scheme: dark) {
32
+ :root {
33
+ --bg: #141413; --panel: #1d1d1b; --ink: #ecebe6; --muted: #a3a29b; --line: #34332f;
34
+ --accent: #7fb0ff; --accent-ink: #0d1626; --chip: #272724; --bar: #7fb0ff; --focus: #ffb35c;
35
+ }
36
+ }
37
+ * { box-sizing: border-box; }
38
+ html { -webkit-text-size-adjust: 100%; }
39
+ body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
40
+ header { padding: 16px 16px 8px; }
41
+ h1 { margin: 0; font-size: 1.25rem; }
42
+ .meta { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; overflow-wrap: anywhere; }
43
+ nav { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 16px; border-bottom: 1px solid var(--line); }
44
+ nav button { font: inherit; padding: 6px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--ink); cursor: pointer; }
45
+ nav button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
46
+ button:focus-visible, a:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
47
+ .layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(260px, 34%); gap: 16px; padding: 16px; }
48
+ @media (max-width: 760px) { .layout { grid-template-columns: minmax(0, 1fr); } }
49
+ main, aside { min-width: 0; }
50
+ section[hidden] { display: none; }
51
+ h2 { font-size: 1rem; margin: 0 0 8px; }
52
+ h3 { font-size: 0.9rem; margin: 12px 0 4px; color: var(--muted); }
53
+ ul { margin: 0; padding-left: 18px; }
54
+ li { margin: 2px 0; }
55
+ .node { font: inherit; text-align: left; padding: 1px 6px; border: 1px solid transparent; border-radius: 4px; background: var(--chip); color: var(--ink); cursor: pointer; overflow-wrap: anywhere; }
56
+ .node:hover { border-color: var(--accent); }
57
+ .kind { color: var(--muted); font-size: 0.8rem; margin-right: 4px; }
58
+ .empty { color: var(--muted); }
59
+ .board { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
60
+ .column { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
61
+ .column ul { list-style: none; padding: 0; }
62
+ .count { color: var(--muted); font-weight: normal; }
63
+ .rows { list-style: none; padding: 0; }
64
+ .rows li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; align-items: center; padding: 4px 0; border-bottom: 1px solid var(--line); }
65
+ .track { grid-column: 1 / -1; height: 6px; background: var(--chip); border-radius: 3px; }
66
+ .bar { height: 6px; min-width: 4px; background: var(--bar); border-radius: 3px; }
67
+ .dates, .n { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
68
+ .card { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; margin: 0 0 8px; }
69
+ aside { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 12px; align-self: start; }
70
+ aside a { color: var(--accent); }
71
+ code { font-family: ui-monospace, "Cascadia Code", Menlo, monospace; font-size: 0.85em; }
72
+ `;
73
+
74
+ export function renderPage(graph) {
75
+ const nav = VIEWS.map(([id, label], i) => `<button type="button" data-show="${id}" aria-pressed="${i === 0}">${label}</button>`).join('');
76
+ const sections = VIEWS.map(([id, label], i) => `<section data-view="${id}" aria-label="${label}"${i === 0 ? '' : ' hidden'}></section>`).join('\n');
77
+ return `<!doctype html>
78
+ <html lang="en">
79
+ <head>
80
+ <meta charset="utf-8">
81
+ <meta http-equiv="Content-Security-Policy" content="${CSP}">
82
+ <meta name="viewport" content="width=device-width, initial-scale=1">
83
+ <title>slicekit graph</title>
84
+ <style>${STYLE}</style>
85
+ </head>
86
+ <body>
87
+ <header><h1>slicekit graph</h1><p class="meta" id="meta"></p></header>
88
+ <nav aria-label="Views">${nav}</nav>
89
+ <div class="layout">
90
+ <main>
91
+ ${sections}
92
+ </main>
93
+ <aside id="detail" aria-live="polite"><p class="empty">Choose a node to see its files, commits and pull requests.</p></aside>
94
+ </div>
95
+ <script type="application/json" id="graph">${inlineJson(graph)}</script>
96
+ <script>${CLIENT}</script>
97
+ </body>
98
+ </html>
99
+ `;
100
+ }
@@ -0,0 +1,46 @@
1
+ // The visualize server (REQ-4.7): node:http bound to 127.0.0.1 only, serving the page at / and the
2
+ // graph at /graph.json; every other path is 404. It opens nothing and reaches nothing.
3
+ import http from 'node:http';
4
+ import { GraphError } from '../graph/schema.js';
5
+
6
+ const HOST = '127.0.0.1';
7
+
8
+ // -> Promise<{url, port, close() -> Promise<void>}>; a port in use rejects with GraphError code 1.
9
+ export function startServer({ html, graphJson, port = 0 }) {
10
+ const routes = new Map([
11
+ ['/', { type: 'text/html; charset=utf-8', body: Buffer.from(html, 'utf8') }],
12
+ ['/graph.json', { type: 'application/json; charset=utf-8', body: Buffer.from(graphJson, 'utf8') }],
13
+ ]);
14
+ let allowed = new Set();
15
+ const server = http.createServer((req, res) => {
16
+ const pathOnly = String(req.url ?? '').split('?')[0];
17
+ // A page on another site can point its own name at 127.0.0.1 (DNS rebinding); only a request
18
+ // that names this server by its loopback address or localhost is answered (S-19 review H-1).
19
+ const named = allowed.has(String(req.headers.host ?? '').toLowerCase());
20
+ const hit = named && (req.method === 'GET' || req.method === 'HEAD') ? routes.get(pathOnly) : undefined;
21
+ const { type, body, status } = hit ? { ...hit, status: 200 } : { type: 'text/plain; charset=utf-8', body: Buffer.from('not found\n'), status: 404 };
22
+ res.writeHead(status, { 'content-type': type, 'content-length': body.length, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' });
23
+ res.end(req.method === 'HEAD' ? undefined : body);
24
+ });
25
+ return new Promise((resolve, reject) => {
26
+ server.once('error', (e) => {
27
+ if (e.code === 'EADDRINUSE') reject(new GraphError(1, `port ${port} is in use; pass --port`));
28
+ else reject(e);
29
+ });
30
+ server.listen(port, HOST, () => {
31
+ const bound = server.address().port;
32
+ allowed = new Set([`${HOST}:${bound}`, `localhost:${bound}`]);
33
+ // A client leaves the default port out of Host (S-19 review H-3).
34
+ if (bound === 80) allowed = new Set([...allowed, HOST, 'localhost']);
35
+ let closing = null;
36
+ const close = () => {
37
+ closing ??= new Promise((done) => {
38
+ server.close(() => done());
39
+ server.closeAllConnections();
40
+ });
41
+ return closing;
42
+ };
43
+ resolve({ url: `http://${HOST}:${bound}/`, port: bound, close });
44
+ });
45
+ });
46
+ }