@kioju/dsh-cognitive-dashboard 0.0.0-stage → 0.8.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/LICENSE +21 -0
- package/README.md +43 -2
- package/client.js +1924 -0
- package/cordis.patch.yml +13 -0
- package/docs/DESIGN.md +73 -0
- package/docs/DETAIL.md +25 -0
- package/icon.svg +6 -0
- package/index.js +615 -0
- package/locale/en.json +7 -0
- package/locale/ja.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +71 -6
package/index.js
ADDED
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of dsh-cognitive-dashboard.
|
|
3
|
+
*
|
|
4
|
+
* The dashboard's data is workspace-level. The objective tree is drawn from the
|
|
5
|
+
* model graph, read through the `cognitiveGraph` service that
|
|
6
|
+
* `dsh-cognitive-model` publishes — the same projection the browser reads, so
|
|
7
|
+
* supersession and conflict detection have one implementation. The findings
|
|
8
|
+
* ledger is read directly; there is no second tree source (D98). The recall
|
|
9
|
+
* block — the text a session starts from — is read through the same service
|
|
10
|
+
* (D108): the card shows the block the input received, not a second drawing of
|
|
11
|
+
* the material behind it.
|
|
12
|
+
*
|
|
13
|
+
* It is served to the browser through one session projection (`dashboard`)
|
|
14
|
+
* instead of a command, so opening the dashboard does not add command rows to
|
|
15
|
+
* the conversation log.
|
|
16
|
+
*
|
|
17
|
+
* Read-only. The projection never writes a ledger; it reads what the other
|
|
18
|
+
* plugins maintain.
|
|
19
|
+
*/
|
|
20
|
+
import { spawnSync } from 'node:child_process'
|
|
21
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
22
|
+
import path from 'node:path'
|
|
23
|
+
import { findProjectRoot } from '@kioju/dsh-cognitive-core'
|
|
24
|
+
|
|
25
|
+
export const name = 'cognitive-dashboard'
|
|
26
|
+
export const inject = ['sessionProjections']
|
|
27
|
+
|
|
28
|
+
const FINDINGS_FILE = '.dsh/cognitive-findings/findings.json'
|
|
29
|
+
const STALE_DAYS = 14
|
|
30
|
+
const DAY_MS = 86_400_000
|
|
31
|
+
const ACTIVE_STATUSES = new Set(['open', 'triaged', 'in_progress'])
|
|
32
|
+
const RELEVANT_EVENTS = new Set(['tool/result', 'turn/end', 'user/message', 'command/done'])
|
|
33
|
+
const PRIORITY_ORDER = { P0: 0, P1: 1, P2: 2, P3: 3 }
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The projection registry validates the state and the wire view through
|
|
37
|
+
* `schema.parse`. Both are produced by this module, so the schema is a
|
|
38
|
+
* pass-through rather than a second source of truth.
|
|
39
|
+
*/
|
|
40
|
+
const passthrough = { parse: (value) => value }
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read a JSON document and report whether it was missing, readable, or
|
|
44
|
+
* unreadable. A missing ledger and a corrupt one must not collapse into the
|
|
45
|
+
* same empty value: the dashboard shows "not recorded" for one and "could not
|
|
46
|
+
* read" for the other, and neither is a success (D79 / F4).
|
|
47
|
+
*/
|
|
48
|
+
function readJson(file, fallback, isValid) {
|
|
49
|
+
if (!existsSync(file)) return { state: 'missing', value: fallback }
|
|
50
|
+
try {
|
|
51
|
+
const value = JSON.parse(readFileSync(file, 'utf8'))
|
|
52
|
+
if (typeof isValid === 'function' && !isValid(value)) {
|
|
53
|
+
return { state: 'error', value: fallback }
|
|
54
|
+
}
|
|
55
|
+
return { state: 'ready', value }
|
|
56
|
+
} catch {
|
|
57
|
+
return { state: 'error', value: fallback }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const isFindings = (value) =>
|
|
62
|
+
value !== null &&
|
|
63
|
+
typeof value === 'object' &&
|
|
64
|
+
Array.isArray(value.findings) &&
|
|
65
|
+
(value.decisions === undefined || Array.isArray(value.decisions))
|
|
66
|
+
|
|
67
|
+
function countBy(items, keyOf) {
|
|
68
|
+
const counts = {}
|
|
69
|
+
for (const item of items) {
|
|
70
|
+
const key = keyOf(item)
|
|
71
|
+
if (key === undefined || key === null || key === '') continue
|
|
72
|
+
counts[key] = (counts[key] ?? 0) + 1
|
|
73
|
+
}
|
|
74
|
+
return counts
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function ageDays(createdAt) {
|
|
78
|
+
const at = Date.parse(createdAt)
|
|
79
|
+
return Number.isFinite(at) ? Math.max(0, Math.floor((Date.now() - at) / DAY_MS)) : 0
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Commit counts per node (D89).
|
|
84
|
+
*
|
|
85
|
+
* A node's `paths` point at files or directories inside one of the repositories
|
|
86
|
+
* beside the workspace, so one `git log` per repository is enough. The result is
|
|
87
|
+
* cached by HEAD: a projection runs on every tool result, and re-running git each
|
|
88
|
+
* time would make the dashboard the most expensive part of the session (D87).
|
|
89
|
+
* If git cannot be run, the count is simply absent; it is never guessed.
|
|
90
|
+
*/
|
|
91
|
+
const repoCache = new Map()
|
|
92
|
+
|
|
93
|
+
function readGitHead(repo) {
|
|
94
|
+
try {
|
|
95
|
+
const head = readFileSync(path.join(repo, '.git', 'HEAD'), 'utf8').trim()
|
|
96
|
+
if (!head.startsWith('ref: ')) return head
|
|
97
|
+
try {
|
|
98
|
+
return readFileSync(path.join(repo, '.git', head.slice(5)), 'utf8').trim()
|
|
99
|
+
} catch {
|
|
100
|
+
return head
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
return ''
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function gitCommits(repo) {
|
|
108
|
+
const result = spawnSync(
|
|
109
|
+
'git',
|
|
110
|
+
['-C', repo, 'log', '--format=%H %ct', '--name-only', '--no-renames'],
|
|
111
|
+
{ encoding: 'utf8', timeout: 5000, windowsHide: true },
|
|
112
|
+
)
|
|
113
|
+
if (result.error !== undefined || result.status !== 0 || typeof result.stdout !== 'string') {
|
|
114
|
+
return null
|
|
115
|
+
}
|
|
116
|
+
const commits = []
|
|
117
|
+
let current = null
|
|
118
|
+
for (const line of result.stdout.split('\n')) {
|
|
119
|
+
if (line === '') continue
|
|
120
|
+
const head = /^([0-9a-f]{7,40}) (\d+)$/.exec(line)
|
|
121
|
+
if (head !== null) {
|
|
122
|
+
current = { hash: head[1], at: Number(head[2]), files: [] }
|
|
123
|
+
commits.push(current)
|
|
124
|
+
} else if (current !== null) {
|
|
125
|
+
current.files.push(line)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return commits
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function commitsForRepo(repo) {
|
|
132
|
+
const head = readGitHead(repo)
|
|
133
|
+
const cached = repoCache.get(repo)
|
|
134
|
+
if (cached !== undefined && head !== '' && cached.head === head) return cached.commits
|
|
135
|
+
const commits = gitCommits(repo)
|
|
136
|
+
if (head !== '') repoCache.set(repo, { head, commits })
|
|
137
|
+
return commits
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The repository and repo-relative file a node link names, or undefined when
|
|
142
|
+
* the path does not exist. Links are resolved against the workspace first and
|
|
143
|
+
* its parent second: sibling repositories are named by their directory
|
|
144
|
+
* (`dsh-cognitive-model/src/graph/**`), the workspace's own files relative to
|
|
145
|
+
* it (`index.js`). A glob is reduced to its prefix directory.
|
|
146
|
+
*/
|
|
147
|
+
function repoFileOf(root, link) {
|
|
148
|
+
const cleaned = link.replace(/\/\*\*$/, '').replace(/\/\*$/, '').replace(/\/$/, '')
|
|
149
|
+
if (cleaned === '') return undefined
|
|
150
|
+
for (const base of [root, path.dirname(root)]) {
|
|
151
|
+
const absolute = path.join(base, cleaned)
|
|
152
|
+
if (!existsSync(absolute)) continue
|
|
153
|
+
let dir = absolute
|
|
154
|
+
let repo
|
|
155
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
156
|
+
if (existsSync(path.join(dir, '.git'))) {
|
|
157
|
+
repo = dir
|
|
158
|
+
break
|
|
159
|
+
}
|
|
160
|
+
const parent = path.dirname(dir)
|
|
161
|
+
if (parent === dir) break
|
|
162
|
+
dir = parent
|
|
163
|
+
}
|
|
164
|
+
if (repo === undefined) return undefined
|
|
165
|
+
return { repo, file: path.relative(repo, absolute).replaceAll('\\', '/') }
|
|
166
|
+
}
|
|
167
|
+
return undefined
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Commits whose changed files fall under one resolved link, or undefined. */
|
|
171
|
+
function commitsMatching(target) {
|
|
172
|
+
const commits = commitsForRepo(target.repo)
|
|
173
|
+
if (commits === null) return undefined
|
|
174
|
+
return commits.filter(
|
|
175
|
+
(commit) =>
|
|
176
|
+
target.file === '' ||
|
|
177
|
+
commit.files.some((file) => file === target.file || file.startsWith(`${target.file}/`)),
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The model graph, read through the service the model plugin publishes.
|
|
183
|
+
*
|
|
184
|
+
* The dashboard does not import the model package and does not project the
|
|
185
|
+
* event log itself: supersession, invalidation and conflict detection have one
|
|
186
|
+
* implementation, and this view reads its result. `unavailable` is a distinct
|
|
187
|
+
* state from `missing` — a profile without the model plugin and a workspace
|
|
188
|
+
* with no graph are different facts (F4).
|
|
189
|
+
*/
|
|
190
|
+
function graphOf(modelOf, root) {
|
|
191
|
+
if (typeof modelOf !== 'function') return { state: 'unavailable' }
|
|
192
|
+
let service
|
|
193
|
+
try {
|
|
194
|
+
service = modelOf()
|
|
195
|
+
} catch {
|
|
196
|
+
return { state: 'unavailable' }
|
|
197
|
+
}
|
|
198
|
+
if (service === null || service === undefined || typeof service.snapshot !== 'function') {
|
|
199
|
+
return { state: 'unavailable' }
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const snapshot = service.snapshot(root)
|
|
203
|
+
if (snapshot !== null && typeof snapshot === 'object' && typeof snapshot.state === 'string') {
|
|
204
|
+
return snapshot
|
|
205
|
+
}
|
|
206
|
+
return { state: 'error' }
|
|
207
|
+
} catch {
|
|
208
|
+
return { state: 'error' }
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function pushTo(map, key, value) {
|
|
213
|
+
const bucket = map.get(key)
|
|
214
|
+
if (bucket === undefined) map.set(key, [value])
|
|
215
|
+
else bucket.push(value)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The recall block, read through the same service.
|
|
220
|
+
*
|
|
221
|
+
* The card shows the block a session starts from rather than a second drawing
|
|
222
|
+
* of the graph (D108). The model owns the budget, the ordering and the
|
|
223
|
+
* per-session check, so this reads its result instead of assembling one: a
|
|
224
|
+
* second assembly could disagree with the text the input actually got.
|
|
225
|
+
*
|
|
226
|
+
* `unavailable` is the profile without the model plugin (or a model too old to
|
|
227
|
+
* publish the block); `missing` and `error` come from the model, which keeps
|
|
228
|
+
* "nothing recorded" and "could not read" apart (F4).
|
|
229
|
+
*/
|
|
230
|
+
function recallOf(modelOf, root) {
|
|
231
|
+
if (typeof modelOf !== 'function') return { state: 'unavailable' }
|
|
232
|
+
let service
|
|
233
|
+
try {
|
|
234
|
+
service = modelOf()
|
|
235
|
+
} catch {
|
|
236
|
+
return { state: 'unavailable' }
|
|
237
|
+
}
|
|
238
|
+
if (service === null || service === undefined || typeof service.recall !== 'function') {
|
|
239
|
+
return { state: 'unavailable' }
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
const recall = service.recall(root)
|
|
243
|
+
if (recall !== null && typeof recall === 'object' && typeof recall.state === 'string') {
|
|
244
|
+
return recall
|
|
245
|
+
}
|
|
246
|
+
return { state: 'error' }
|
|
247
|
+
} catch {
|
|
248
|
+
return { state: 'error' }
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function newestNodeFirst(a, b) {
|
|
253
|
+
const aAt = typeof a.updatedAt === 'string' ? a.updatedAt : ''
|
|
254
|
+
const bAt = typeof b.updatedAt === 'string' ? b.updatedAt : ''
|
|
255
|
+
if (aAt !== bAt) return aAt < bAt ? 1 : -1
|
|
256
|
+
return a.id.localeCompare(b.id)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The tree drawn from the graph (D97).
|
|
261
|
+
*
|
|
262
|
+
* One drawing with one vocabulary of observations: the structure and the
|
|
263
|
+
* acceptance come from the graph's `serves` chain and milestones, while
|
|
264
|
+
* investment counts what the workspace can see — tasks and findings linked to a
|
|
265
|
+
* node, commits touching its paths, and its docs.
|
|
266
|
+
*
|
|
267
|
+
* A task node is one work item: rewriting `task/current` supersedes the node, it
|
|
268
|
+
* does not add one.
|
|
269
|
+
*/
|
|
270
|
+
function graphTreeView(graph, findings, root) {
|
|
271
|
+
const nodes = Array.isArray(graph.nodes) ? graph.nodes : []
|
|
272
|
+
const edges = Array.isArray(graph.edges) ? graph.edges : []
|
|
273
|
+
const byId = new Map(nodes.map((node) => [node.id, node]))
|
|
274
|
+
const serves = new Map()
|
|
275
|
+
const requires = new Map()
|
|
276
|
+
const verifies = new Map()
|
|
277
|
+
for (const edge of edges) {
|
|
278
|
+
if (typeof edge.from !== 'string' || typeof edge.to !== 'string') continue
|
|
279
|
+
if (edge.type === 'serves') serves.set(edge.from, edge.to)
|
|
280
|
+
else if (edge.type === 'requires') pushTo(requires, edge.from, edge.to)
|
|
281
|
+
else if (edge.type === 'verifies') pushTo(verifies, edge.from, edge.to)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const pathOf = (id) => {
|
|
285
|
+
const path = []
|
|
286
|
+
const seen = new Set()
|
|
287
|
+
let current = id
|
|
288
|
+
while (typeof current === 'string' && !seen.has(current)) {
|
|
289
|
+
seen.add(current)
|
|
290
|
+
path.push(current)
|
|
291
|
+
current = serves.get(current)
|
|
292
|
+
}
|
|
293
|
+
return path.reverse()
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const treeNodes = nodes
|
|
297
|
+
.filter((node) => node.type === 'objective' || node.type === 'component')
|
|
298
|
+
.sort((left, right) => left.id.localeCompare(right.id))
|
|
299
|
+
const treeIds = new Set(treeNodes.map((node) => node.id))
|
|
300
|
+
const tasks = nodes.filter((node) => node.type === 'task').sort(newestNodeFirst)
|
|
301
|
+
|
|
302
|
+
// Active tasks that serve a node, directly or through a means node: a task
|
|
303
|
+
// counts for its own node and for every objective above it (D88).
|
|
304
|
+
const taskCounts = new Map()
|
|
305
|
+
for (const task of tasks) {
|
|
306
|
+
for (const step of pathOf(task.id)) {
|
|
307
|
+
taskCounts.set(step, (taskCounts.get(step) ?? 0) + 1)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const currentTaskNode = byId.get('task/current') ?? tasks[0]
|
|
312
|
+
const currentPath =
|
|
313
|
+
currentTaskNode === undefined
|
|
314
|
+
? byId.has('objective')
|
|
315
|
+
? ['objective']
|
|
316
|
+
: []
|
|
317
|
+
: pathOf(currentTaskNode.id)
|
|
318
|
+
const currentSet = new Set(currentPath)
|
|
319
|
+
const keyOf = (value) => String(value ?? '').trim().toLowerCase()
|
|
320
|
+
|
|
321
|
+
const blank = () => ({
|
|
322
|
+
taskOpen: 0,
|
|
323
|
+
taskAll: 0,
|
|
324
|
+
findingOpen: 0,
|
|
325
|
+
findingAll: 0,
|
|
326
|
+
docs: 0,
|
|
327
|
+
commits: 0,
|
|
328
|
+
// Repositories git could not answer for. The count above is then a lower
|
|
329
|
+
// bound, not a zero (F1 / D105).
|
|
330
|
+
commitFailures: 0,
|
|
331
|
+
milestoneDone: 0,
|
|
332
|
+
milestoneTotal: 0,
|
|
333
|
+
lastActivity: null,
|
|
334
|
+
})
|
|
335
|
+
const direct = new Map()
|
|
336
|
+
const slot = (id) => {
|
|
337
|
+
let entry = direct.get(id)
|
|
338
|
+
if (entry === undefined) {
|
|
339
|
+
entry = blank()
|
|
340
|
+
direct.set(id, entry)
|
|
341
|
+
}
|
|
342
|
+
return entry
|
|
343
|
+
}
|
|
344
|
+
const bumpActivity = (entry, at) => {
|
|
345
|
+
if (typeof at === 'string' && at !== '' && (entry.lastActivity === null || at > entry.lastActivity)) {
|
|
346
|
+
entry.lastActivity = at
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
for (const task of tasks) {
|
|
351
|
+
const parent = serves.get(task.id)
|
|
352
|
+
if (parent === undefined || !treeIds.has(parent)) continue
|
|
353
|
+
const entry = slot(parent)
|
|
354
|
+
entry.taskOpen += 1
|
|
355
|
+
entry.taskAll += 1
|
|
356
|
+
bumpActivity(entry, task.updatedAt)
|
|
357
|
+
}
|
|
358
|
+
for (const finding of findings) {
|
|
359
|
+
if (typeof finding.node !== 'string') continue
|
|
360
|
+
const id = keyOf(finding.node)
|
|
361
|
+
if (!treeIds.has(id)) continue
|
|
362
|
+
const entry = slot(id)
|
|
363
|
+
entry.findingAll += 1
|
|
364
|
+
if (ACTIVE_STATUSES.has(finding.status)) entry.findingOpen += 1
|
|
365
|
+
bumpActivity(entry, finding.updatedAt ?? finding.createdAt)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Own acceptance, docs and commits per node; a missing link is an observation
|
|
369
|
+
// too. Commits are counted from `paths`, not `docs` (D89). A repository git
|
|
370
|
+
// cannot read is not a zero: the count is a lower bound, `commitState` says so
|
|
371
|
+
// and `commitErrors` names the links, so "could not read" and "no commits"
|
|
372
|
+
// stay apart (F1 / D105).
|
|
373
|
+
const stale = new Map()
|
|
374
|
+
const commitErrors = new Map()
|
|
375
|
+
for (const node of treeNodes) {
|
|
376
|
+
const entry = slot(node.id)
|
|
377
|
+
const milestones = Array.isArray(node.milestones) ? node.milestones : []
|
|
378
|
+
entry.milestoneDone += milestones.filter((milestone) => milestone.done === true).length
|
|
379
|
+
entry.milestoneTotal += milestones.length
|
|
380
|
+
const docs = Array.isArray(node.docs) ? node.docs : []
|
|
381
|
+
const paths = Array.isArray(node.paths) ? node.paths : []
|
|
382
|
+
entry.docs += docs.length
|
|
383
|
+
bumpActivity(entry, node.updatedAt)
|
|
384
|
+
const missing = []
|
|
385
|
+
const unreadable = []
|
|
386
|
+
const seenCommits = new Set()
|
|
387
|
+
let lastCommit = null
|
|
388
|
+
for (const link of paths) {
|
|
389
|
+
const target = repoFileOf(root, link)
|
|
390
|
+
if (target === undefined) {
|
|
391
|
+
missing.push(link)
|
|
392
|
+
continue
|
|
393
|
+
}
|
|
394
|
+
const hits = commitsMatching(target)
|
|
395
|
+
if (hits === undefined) {
|
|
396
|
+
unreadable.push(link)
|
|
397
|
+
continue
|
|
398
|
+
}
|
|
399
|
+
for (const commit of hits) {
|
|
400
|
+
if (seenCommits.has(commit.hash)) continue
|
|
401
|
+
seenCommits.add(commit.hash)
|
|
402
|
+
if (lastCommit === null || commit.at > lastCommit) lastCommit = commit.at
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
entry.commits += seenCommits.size
|
|
406
|
+
entry.commitFailures += unreadable.length
|
|
407
|
+
if (unreadable.length > 0) commitErrors.set(node.id, unreadable)
|
|
408
|
+
if (lastCommit !== null) bumpActivity(entry, new Date(lastCommit * 1000).toISOString())
|
|
409
|
+
for (const link of docs) {
|
|
410
|
+
if (repoFileOf(root, link) === undefined) missing.push(link)
|
|
411
|
+
}
|
|
412
|
+
if (missing.length > 0) stale.set(node.id, missing)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Sum each node's own numbers over its branch, so an objective shows what its
|
|
416
|
+
// means carry (D89).
|
|
417
|
+
const total = new Map()
|
|
418
|
+
for (const [id, entry] of direct) {
|
|
419
|
+
for (const step of pathOf(id)) {
|
|
420
|
+
if (!treeIds.has(step)) continue
|
|
421
|
+
let sum = total.get(step)
|
|
422
|
+
if (sum === undefined) {
|
|
423
|
+
sum = blank()
|
|
424
|
+
total.set(step, sum)
|
|
425
|
+
}
|
|
426
|
+
sum.taskOpen += entry.taskOpen
|
|
427
|
+
sum.taskAll += entry.taskAll
|
|
428
|
+
sum.findingOpen += entry.findingOpen
|
|
429
|
+
sum.findingAll += entry.findingAll
|
|
430
|
+
sum.docs += entry.docs
|
|
431
|
+
sum.commits += entry.commits
|
|
432
|
+
sum.commitFailures += entry.commitFailures
|
|
433
|
+
sum.milestoneDone += entry.milestoneDone
|
|
434
|
+
sum.milestoneTotal += entry.milestoneTotal
|
|
435
|
+
if (entry.lastActivity !== null && (sum.lastActivity === null || entry.lastActivity > sum.lastActivity)) {
|
|
436
|
+
sum.lastActivity = entry.lastActivity
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const rows = treeNodes.map((node) => {
|
|
442
|
+
const sum = total.get(node.id) ?? blank()
|
|
443
|
+
const parent = serves.get(node.id)
|
|
444
|
+
const milestones = Array.isArray(node.milestones) ? node.milestones : []
|
|
445
|
+
return {
|
|
446
|
+
id: node.id,
|
|
447
|
+
subject: node.id,
|
|
448
|
+
kind: node.type,
|
|
449
|
+
// The label names the node; the value says what it is. The drawing uses
|
|
450
|
+
// the key, so the value stays in the panel and the accessible name.
|
|
451
|
+
value: node.label,
|
|
452
|
+
...(typeof node.value === 'string' && node.value !== '' ? { description: node.value } : {}),
|
|
453
|
+
depth: pathOf(node.id).length - 1,
|
|
454
|
+
parent: parent !== undefined && treeIds.has(parent) ? parent : null,
|
|
455
|
+
confidence: node.confidence,
|
|
456
|
+
paths: Array.isArray(node.paths) ? node.paths : [],
|
|
457
|
+
docs: Array.isArray(node.docs) ? node.docs : [],
|
|
458
|
+
serves: parent ?? null,
|
|
459
|
+
requires: requires.get(node.id) ?? [],
|
|
460
|
+
verifies: verifies.get(node.id) ?? [],
|
|
461
|
+
tasks: sum.taskOpen,
|
|
462
|
+
findings: sum.findingOpen,
|
|
463
|
+
investment: {
|
|
464
|
+
tasks: sum.taskAll,
|
|
465
|
+
findings: sum.findingAll,
|
|
466
|
+
commits: sum.commits,
|
|
467
|
+
docs: sum.docs,
|
|
468
|
+
// Exact only when every repository in the branch answered; otherwise
|
|
469
|
+
// `commits` is a lower bound and the view says so (D105).
|
|
470
|
+
commitState: sum.commitFailures > 0 ? 'unavailable' : 'ready',
|
|
471
|
+
total: sum.taskAll + sum.findingAll + sum.commits,
|
|
472
|
+
},
|
|
473
|
+
acceptance: { done: sum.milestoneDone, total: sum.milestoneTotal },
|
|
474
|
+
milestones: milestones.map((milestone) => ({
|
|
475
|
+
text: milestone.text,
|
|
476
|
+
done: milestone.done === true,
|
|
477
|
+
...(milestone.at === undefined ? {} : { at: milestone.at }),
|
|
478
|
+
})),
|
|
479
|
+
stale: stale.get(node.id) ?? [],
|
|
480
|
+
// This node's own links whose history could not be read; the branch-wide
|
|
481
|
+
// lower bound is `investment.commitState`.
|
|
482
|
+
commitErrors: commitErrors.get(node.id) ?? [],
|
|
483
|
+
lastActivity: sum.lastActivity,
|
|
484
|
+
current: currentSet.has(node.id),
|
|
485
|
+
}
|
|
486
|
+
})
|
|
487
|
+
|
|
488
|
+
const currentTask = currentTaskNode === undefined ? null : graphTaskView(currentTaskNode, serves)
|
|
489
|
+
const remaining = tasks
|
|
490
|
+
.filter((task) => task !== currentTaskNode)
|
|
491
|
+
.map((task) => graphTaskView(task, serves))
|
|
492
|
+
const unlinked = tasks.filter((task) => serves.get(task.id) === undefined).length
|
|
493
|
+
return { nodes: rows, currentTask, remaining, unlinkedTasks: unlinked }
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function graphTaskView(node, serves) {
|
|
497
|
+
return {
|
|
498
|
+
id: node.id,
|
|
499
|
+
value: node.label,
|
|
500
|
+
...(typeof node.value === 'string' && node.value !== '' ? { description: node.value } : {}),
|
|
501
|
+
confidence: node.confidence,
|
|
502
|
+
serves: serves.get(node.id) ?? null,
|
|
503
|
+
createdAt: node.updatedAt,
|
|
504
|
+
updatedAt: node.updatedAt,
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function snapshot(root, modelOf) {
|
|
509
|
+
const generatedAt = new Date().toISOString()
|
|
510
|
+
if (root === undefined) {
|
|
511
|
+
return {
|
|
512
|
+
generatedAt,
|
|
513
|
+
workspace: null,
|
|
514
|
+
readState: { findings: 'missing', graph: 'unavailable', tree: 'none' },
|
|
515
|
+
objective: { root: null, nodes: [], currentPath: [] },
|
|
516
|
+
currentTask: null,
|
|
517
|
+
remaining: [],
|
|
518
|
+
unlinkedTasks: 0,
|
|
519
|
+
findings: { total: 0, byStatus: {}, byPriority: {}, open: [], stale: 0, decisions: 0 },
|
|
520
|
+
recall: { state: 'missing' },
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const findingsRead = readJson(
|
|
525
|
+
path.join(root, FINDINGS_FILE),
|
|
526
|
+
{ findings: [], decisions: [] },
|
|
527
|
+
isFindings,
|
|
528
|
+
)
|
|
529
|
+
const findingsDoc =
|
|
530
|
+
findingsRead.state === 'ready' ? findingsRead.value : { findings: [], decisions: [] }
|
|
531
|
+
const findingList = Array.isArray(findingsDoc.findings) ? findingsDoc.findings : []
|
|
532
|
+
const activeFindings = findingList.filter((finding) => ACTIVE_STATUSES.has(finding.status))
|
|
533
|
+
|
|
534
|
+
// The tree has one source (D98): the graph the model plugin publishes. A
|
|
535
|
+
// profile without the model plugin draws no tree and says so; it does not
|
|
536
|
+
// read a second ledger.
|
|
537
|
+
const graph = graphOf(modelOf, root)
|
|
538
|
+
const fromGraph = graph.state === 'ready'
|
|
539
|
+
const tree = fromGraph
|
|
540
|
+
? graphTreeView(graph, findingList, root)
|
|
541
|
+
: { nodes: [], currentTask: null, remaining: [], unlinkedTasks: 0 }
|
|
542
|
+
const nodes = tree.nodes
|
|
543
|
+
// The block a session starts from, read in the same pass as the tree (D108).
|
|
544
|
+
// The model's read is cached, so this costs no second read of the log.
|
|
545
|
+
const recall = recallOf(modelOf, root)
|
|
546
|
+
const currentPath = nodes.filter((node) => node.current).map((node) => node.subject)
|
|
547
|
+
const rootNode = nodes.find((node) => node.kind === 'objective' && node.depth === 0) ?? null
|
|
548
|
+
|
|
549
|
+
const open = activeFindings
|
|
550
|
+
.map((finding) => ({
|
|
551
|
+
id: finding.id,
|
|
552
|
+
title: finding.title,
|
|
553
|
+
status: finding.status,
|
|
554
|
+
priority: finding.priority,
|
|
555
|
+
kind: finding.kind,
|
|
556
|
+
node: finding.node ?? null,
|
|
557
|
+
ageDays: ageDays(finding.updatedAt ?? finding.createdAt),
|
|
558
|
+
}))
|
|
559
|
+
.sort((left, right) => {
|
|
560
|
+
const byPriority = (PRIORITY_ORDER[left.priority] ?? 9) - (PRIORITY_ORDER[right.priority] ?? 9)
|
|
561
|
+
return byPriority !== 0 ? byPriority : right.ageDays - left.ageDays
|
|
562
|
+
})
|
|
563
|
+
|
|
564
|
+
return {
|
|
565
|
+
generatedAt,
|
|
566
|
+
workspace: root,
|
|
567
|
+
readState: {
|
|
568
|
+
findings: findingsRead.state,
|
|
569
|
+
graph: graph.state,
|
|
570
|
+
tree: fromGraph ? 'graph' : 'none',
|
|
571
|
+
},
|
|
572
|
+
objective: { root: rootNode, nodes, currentPath },
|
|
573
|
+
currentTask: tree.currentTask,
|
|
574
|
+
remaining: tree.remaining,
|
|
575
|
+
unlinkedTasks: tree.unlinkedTasks,
|
|
576
|
+
findings: {
|
|
577
|
+
total: findingList.length,
|
|
578
|
+
byStatus: countBy(findingList, (finding) => finding.status),
|
|
579
|
+
byPriority: countBy(activeFindings, (finding) => finding.priority),
|
|
580
|
+
open,
|
|
581
|
+
stale: activeFindings.filter(
|
|
582
|
+
(finding) => ageDays(finding.updatedAt ?? finding.createdAt) > STALE_DAYS,
|
|
583
|
+
).length,
|
|
584
|
+
decisions: Array.isArray(findingsDoc.decisions) ? findingsDoc.decisions.length : 0,
|
|
585
|
+
},
|
|
586
|
+
recall,
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
export async function apply(ctx) {
|
|
591
|
+
// The model plugin may be composed after this one (or not at all), so the
|
|
592
|
+
// service is looked up per read, not captured at apply time.
|
|
593
|
+
const modelOf = () => (typeof ctx.get === 'function' ? ctx.get('cognitiveGraph') : undefined)
|
|
594
|
+
const definition = {
|
|
595
|
+
key: 'dashboard',
|
|
596
|
+
stateVersion: 4,
|
|
597
|
+
stateSchema: passthrough,
|
|
598
|
+
init: (header) => ({
|
|
599
|
+
root:
|
|
600
|
+
typeof header?.cwd === 'string' && header.cwd !== ''
|
|
601
|
+
? findProjectRoot(header.cwd)
|
|
602
|
+
: undefined,
|
|
603
|
+
version: 0,
|
|
604
|
+
}),
|
|
605
|
+
// The ledgers change on tool results and turn boundaries. Re-read the files
|
|
606
|
+
// on the next relevant event instead of polling.
|
|
607
|
+
apply: (state, event) =>
|
|
608
|
+
RELEVANT_EVENTS.has(event?.type) ? { ...state, version: state.version + 1 } : state,
|
|
609
|
+
wire: {
|
|
610
|
+
viewSchema: passthrough,
|
|
611
|
+
view: (state) => snapshot(state.root, modelOf),
|
|
612
|
+
},
|
|
613
|
+
}
|
|
614
|
+
await ctx.sessionProjections.register(definition)
|
|
615
|
+
}
|
package/locale/en.json
ADDED
package/locale/ja.json
ADDED