@skitterbyte/skitterspec 23.13.0 → 23.15.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/src/tada/serve.js CHANGED
@@ -32,8 +32,11 @@ const BIN = path.join(__dirname, '..', '..', 'bin', 'skitterspec.js')
32
32
  * absolute day so a link keeps meaning the same thing tomorrow, and the
33
33
  * scope flags. `since` and `on` are both day strings (`YYYY-MM-DD`) or absent.
34
34
  */
35
- function tadaQuery({ on = null, since = null, all = false, chat = false, here = false } = {}) {
35
+ function tadaQuery({ on = null, since = null, all = false, chat = false, here = false, todo = false } = {}) {
36
36
  const q = new URLSearchParams()
37
+ // The to-do is always "this morning": its window is the engine's to find,
38
+ // so it carries the day it was asked on and nothing else.
39
+ if (todo) q.set('todo', '1')
37
40
  if (on) q.set('on', on)
38
41
  else if (since) q.set('since', since)
39
42
  if (all) q.set('all', '1')
@@ -50,7 +53,7 @@ function readTadaQuery(search, now = new Date()) {
50
53
  const since = q.get('since')
51
54
  const window = windowFor({ on: on || undefined, since: since || undefined }, now)
52
55
  const flag = (k) => ['1', 'true', ''].includes(q.get(k)) && q.has(k)
53
- return { on, since, all: flag('all'), chat: flag('chat'), here: flag('here'), window }
56
+ return { on, since, all: flag('all'), chat: flag('chat'), here: flag('here'), todo: flag('todo'), window }
54
57
  }
55
58
 
56
59
  /**
@@ -58,8 +61,8 @@ function readTadaQuery(search, now = new Date()) {
58
61
  * end: `--since friday` runs to now, so an end-keyed recap would be lost the
59
62
  * next morning while the link that names it still works.
60
63
  */
61
- function recapKey({ on, since, window, all, chat }) {
62
- const kind = on ? 'on' : since ? 'since' : 'on'
64
+ function recapKey({ on, since, window, all, chat, todo = false }) {
65
+ const kind = todo ? 'todo' : on ? 'on' : since ? 'since' : 'on'
63
66
  return `${kind}-${localDay(window.since)}${all ? '-all' : ''}${chat ? '-chat' : ''}`
64
67
  }
65
68
 
@@ -69,9 +72,12 @@ function recapPath(stateDir, key) {
69
72
 
70
73
  /** The CLI's digest for one window, run where the server's project lives. */
71
74
  function runDigest({ dir, ask, exec = execFile, bin = BIN }) {
72
- const args = [bin, 'tada', '--json']
75
+ const args = [bin, ask.todo ? 'todo' : 'tada', '--json']
73
76
  const day = localDay(ask.window.since)
74
- if (ask.on) args.push('--on', day)
77
+ if (ask.todo) {
78
+ // `todo` finds its own window from today; a page asked for a past day's
79
+ // to-do still shows this morning's, which is the only one there is.
80
+ } else if (ask.on) args.push('--on', day)
75
81
  else if (ask.since) args.push('--since', day)
76
82
  if (ask.all) args.push('--all-projects')
77
83
  if (ask.chat) args.push('--chat')
@@ -117,7 +123,7 @@ async function renderServedTada({ dir, stateDir, search, project = null, now = n
117
123
  html: renderTadaPage(out.digest, {
118
124
  recap,
119
125
  writtenAt,
120
- title: `Ta-da · ${humanLabel(ask.window)}`,
126
+ title: `${ask.todo ? 'To-do' : 'Ta-da'} · ${humanLabel(ask.window)}`,
121
127
  project,
122
128
  category: out.digest.category || null,
123
129
  }),
@@ -0,0 +1,250 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The morning half of the ta-da list (feat-spec-todo, phase 2): where each
5
+ * project was left on the last day with any activity, and what is ahead. It
6
+ * reads the same digest `skitterspec tada` does — once for the lookback, once
7
+ * for the day it found — so the two can never disagree about what happened.
8
+ * Pure: every input arrives already read.
9
+ */
10
+
11
+ const fs = require('node:fs')
12
+ const path = require('node:path')
13
+
14
+ const { localDay } = require('./window.js')
15
+ const { waitingPasses, readGate, gateState } = require('../env/review.js')
16
+
17
+ // Far enough to bridge a long weekend or a week away; past it, "where did I
18
+ // leave off" has no useful answer and the report says so.
19
+ const LOOKBACK_DAYS = 14
20
+
21
+ function daysBefore(d, n) {
22
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate() - n)
23
+ }
24
+
25
+ /** Every instant a digest saw something happen: prompts, sessions, commits, moves. */
26
+ function activityTimes(digest) {
27
+ const times = []
28
+ for (const p of digest.projects) {
29
+ for (const g of p.groups) {
30
+ for (const s of g.sessions) {
31
+ times.push(s.start, s.end)
32
+ for (const pr of s.prompts) times.push(pr.at)
33
+ }
34
+ for (const c of g.commits) times.push(c.at)
35
+ for (const e of g.events) times.push(e.at)
36
+ }
37
+ }
38
+ return times.filter(Boolean)
39
+ }
40
+
41
+ /**
42
+ * The local midnight of the most recent day before `todayStart` with any
43
+ * activity, at most `lookback` days back — so Monday reads Friday and a day
44
+ * off is skipped. Null when nothing falls in the lookback.
45
+ *
46
+ * @param {string[]} times ISO instants (`activityTimes`)
47
+ * @param {Date} todayStart local midnight today
48
+ */
49
+ function lastActiveDay(times, todayStart, lookback = LOOKBACK_DAYS) {
50
+ const floor = daysBefore(todayStart, lookback).getTime()
51
+ const end = todayStart.getTime()
52
+ let best = null
53
+ for (const t of times) {
54
+ const ms = Date.parse(t)
55
+ if (Number.isNaN(ms) || ms < floor || ms >= end) continue
56
+ if (best === null || ms > best) best = ms
57
+ }
58
+ if (best === null) return null
59
+ const d = new Date(best)
60
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate())
61
+ }
62
+
63
+ const EVENT = { written: 'written', started: 'started', completed: 'completed', cancelled: 'cancelled' }
64
+ const eventText = (e) => (e.kind === 'phase-done' ? `phase ${e.phase} done` : EVENT[e.kind] || e.kind)
65
+
66
+ /**
67
+ * Where one group was left: its last move or commit, whichever came later —
68
+ * a commit after a move is the newer fact. Null when the group only has
69
+ * sessions or an in-flight row.
70
+ */
71
+ function leftOf(g) {
72
+ const last = [
73
+ ...g.events.map((e) => ({ at: e.at, text: eventText(e) })),
74
+ ...g.commits.map((c) => ({ at: c.at, text: `${c.sha} ${c.subject}` })),
75
+ ].sort((a, b) => Date.parse(a.at) - Date.parse(b.at))
76
+ return last.length ? last[last.length - 1].text : null
77
+ }
78
+
79
+ // The last thing typed in the group's last session — what was on the
80
+ // person's mind when they stopped.
81
+ function lastPrompt(g) {
82
+ const s = g.sessions[g.sessions.length - 1]
83
+ const p = s && s.prompts[s.prompts.length - 1]
84
+ return p ? `${p.command ? `${p.command} ` : ''}${p.text}`.trim() : null
85
+ }
86
+
87
+ /**
88
+ * What stands in a project's way, read only for the specs in flight there: a
89
+ * pass waiting on the review page, or a gate armed by a phase that ended. A
90
+ * pass for a spec no longer in flight is an old one nobody is waiting on, and
91
+ * is left out rather than reported as a blocker.
92
+ *
93
+ * @param {object} a
94
+ * - `specs` the project's in-flight spec names
95
+ * - `passes` `waitingPasses(...).passes`
96
+ * - `gates` `{ [spec]: gateState(...) }`; a missing or `unknown` state is
97
+ * cannot-tell, and says nothing
98
+ */
99
+ function blockersFrom({ specs, passes = [], gates = {} }) {
100
+ const want = new Set(specs)
101
+ const out = []
102
+ for (const p of passes) {
103
+ if (want.has(p.spec)) out.push({ spec: p.spec, kind: 'verdict', verdict: p.verdict, code: p.code, at: p.at })
104
+ }
105
+ for (const spec of specs) {
106
+ const g = gates[spec]
107
+ if (g && g.state === 'armed') out.push({ spec, kind: 'gate', phase: g.gate ? g.gate.phase : null })
108
+ }
109
+ return out
110
+ }
111
+
112
+ /**
113
+ * `blockersFrom`, read off one repo's review store. A store that does not
114
+ * exist — a repo that never rendered a review — is nothing in the way, not a
115
+ * failure; an unreadable gate is `gateState`'s cannot-tell and says nothing.
116
+ *
117
+ * @param {string} reviews the repo's reviews dir (`<state dir>/reviews`)
118
+ * @param {string[]} specs its in-flight spec names
119
+ * @param {boolean} [required] `review.required`; false opts the gates out
120
+ */
121
+ function readBlockers(reviews, specs, required) {
122
+ if (!fs.existsSync(reviews)) return []
123
+ const gates = {}
124
+ for (const spec of specs) gates[spec] = gateState({ ...readGate(path.join(reviews, `${spec}.html`), spec), required })
125
+ return blockersFrom({ specs, passes: waitingPasses(reviews).passes, gates })
126
+ }
127
+
128
+ /**
129
+ * The to-do: the left-off day's projects, each open spec reduced to where it
130
+ * was left, the specs that day finished by name, today's activity so far, and
131
+ * what is blocking each project.
132
+ *
133
+ * @param {object} a
134
+ * - `leftOff` the digest for the left-off day, or null when none
135
+ * - `leftOffDay` that day's local midnight, or null
136
+ * - `today` today's digest so far
137
+ * - `home` the home project as a digest project (in-flight groups
138
+ * only) for when neither digest lists it, else null
139
+ * - `blockersFor` `(repo, specs) => blockers` — read only for a repo
140
+ */
141
+ function buildTodo({ leftOff, leftOffDay, today, home = null, blockersFor = () => [] }) {
142
+ const projects = new Map()
143
+ const keyOf = (p) => p.repo || p.name
144
+ const entry = (p) => {
145
+ const k = keyOf(p)
146
+ if (!projects.has(k)) projects.set(k, { repo: p.repo, name: p.name, here: false, groups: [], finished: [], today: { commits: 0, moves: 0 } })
147
+ const e = projects.get(k)
148
+ if (p.here) e.here = true
149
+ return e
150
+ }
151
+
152
+ for (const p of leftOff ? leftOff.projects : []) {
153
+ const e = entry(p)
154
+ for (const g of p.groups) {
155
+ // The morning wants what is still open. A spec the day finished is said
156
+ // once, by name; work on no spec is history, not a place to pick up.
157
+ if (!g.inFlight) {
158
+ if (g.spec && g.events.some((ev) => ev.kind === 'completed' || ev.kind === 'cancelled')) e.finished.push(g.spec)
159
+ continue
160
+ }
161
+ const left = leftOf(g)
162
+ const prompt = lastPrompt(g)
163
+ e.groups.push({ spec: g.spec, branch: g.branch, ...(g.ticket ? { ticket: g.ticket } : {}), left, prompt, inFlight: g.inFlight })
164
+ }
165
+ }
166
+ for (const p of today.projects) {
167
+ const e = entry(p)
168
+ const known = new Set(e.groups.map((g) => g.spec).filter(Boolean))
169
+ for (const g of p.groups) {
170
+ e.today.commits += g.commits.length
171
+ e.today.moves += g.events.length
172
+ // What is ahead in a project the left-off day did not touch.
173
+ if (g.spec && g.inFlight && !known.has(g.spec)) {
174
+ e.groups.push({ spec: g.spec, branch: g.branch, ...(g.ticket ? { ticket: g.ticket } : {}), left: null, prompt: null, inFlight: g.inFlight })
175
+ }
176
+ }
177
+ }
178
+ if (home && !projects.has(keyOf(home))) {
179
+ const e = entry({ ...home, here: true })
180
+ for (const g of home.groups) if (g.inFlight) e.groups.push({ spec: g.spec, branch: null, left: null, prompt: null, inFlight: g.inFlight })
181
+ }
182
+ // Ideas are counted for home only: the count is this project's to act on,
183
+ // and asking every project's tracker would turn a morning read into a crawl.
184
+ // Null — could not ask — and zero both print nothing.
185
+ if (home && home.ideas > 0) projects.get(keyOf(home)).ideas = home.ideas
186
+
187
+ const list = [...projects.values()]
188
+ .map((e) => {
189
+ const specs = e.groups.filter((g) => g.spec && g.inFlight).map((g) => g.spec)
190
+ return { ...e, blocked: e.repo && specs.length ? blockersFor(e.repo, specs) : [] }
191
+ })
192
+ .filter((e) => e.groups.length || e.finished.length || e.today.commits || e.today.moves || e.ideas)
193
+ // The home project first: it is the one this session acts on.
194
+ .sort((a, b) => (a.here !== b.here ? (a.here ? -1 : 1) : a.name.localeCompare(b.name)))
195
+
196
+ return {
197
+ kind: 'todo',
198
+ leftOff: leftOffDay ? { day: localDay(leftOffDay) } : null,
199
+ lookbackDays: LOOKBACK_DAYS,
200
+ ...(today.category ? { category: today.category } : {}),
201
+ projects: list,
202
+ notes: [...new Set([...(leftOff ? leftOff.notes : []), ...today.notes])],
203
+ }
204
+ }
205
+
206
+ const ageText = (at, now) => {
207
+ const ms = now - Date.parse(at)
208
+ if (Number.isNaN(ms) || ms < 0) return ''
209
+ const h = Math.floor(ms / 3_600_000)
210
+ return h < 24 ? ` · ${h}h` : ` · ${Math.floor(h / 24)}d`
211
+ }
212
+
213
+ function blockedText(b, now) {
214
+ if (b.kind === 'gate') return `${b.spec} — gate armed${b.phase ? `, phase ${b.phase}` : ''}: a verdict is owed`
215
+ return `${b.spec} — ${b.verdict || 'a'} verdict waiting (${b.code}${b.at ? ageText(b.at, now) : ''})`
216
+ }
217
+
218
+ /**
219
+ * The to-do as text — the same markers as `tada`, plus `left:` for where a
220
+ * group stopped, `finished:` for the specs that day closed, `blocked:` for what stands in a project's way, and `today:`
221
+ * for what has happened since midnight.
222
+ */
223
+ function formatTodo(todo, now = new Date()) {
224
+ const scope = todo.category ? ` · ${todo.category}` : ''
225
+ const head = todo.leftOff
226
+ ? `todo: left off ${todo.leftOff.label || todo.leftOff.day} · today ${todo.todayLabel || ''}`.trimEnd()
227
+ : `todo: nothing in the last ${todo.lookbackDays} days · today ${todo.todayLabel || ''}`.trimEnd()
228
+ const out = [`${head}${scope}`]
229
+ if (todo.projects.length === 0) out.push(' nothing in flight.')
230
+ for (const p of todo.projects) {
231
+ out.push('', `${p.name}${p.here ? ' (here)' : ''}${p.repo ? ` (${p.repo})` : ''}`)
232
+ for (const g of p.groups) {
233
+ out.push(` ${g.spec ? `${g.spec}${g.ticket ? ` [${g.ticket.id}]` : ''}` : `${g.branch || '(no branch)'} — no spec`}`)
234
+ if (g.left) out.push(` left: ${g.left}`)
235
+ if (g.prompt) out.push(` - ${g.prompt}`)
236
+ if (g.inFlight) out.push(` in flight: ${g.inFlight.detail}${g.inFlight.next ? ` · next: ${g.inFlight.next}` : ''}`)
237
+ }
238
+ if (p.finished.length) out.push(` finished: ${p.finished.join(', ')}`)
239
+ for (const b of p.blocked) out.push(` blocked: ${blockedText(b, now)}`)
240
+ if (p.ideas) out.push(` ideas: ${p.ideas} waiting · /spec-idea lists them`)
241
+ if (p.today.commits || p.today.moves) {
242
+ const parts = [p.today.commits && `${p.today.commits} commit${p.today.commits === 1 ? '' : 's'}`, p.today.moves && `${p.today.moves} move${p.today.moves === 1 ? '' : 's'}`]
243
+ out.push(` today: ${parts.filter(Boolean).join(', ')}`)
244
+ }
245
+ }
246
+ for (const n of todo.notes) out.push('', n)
247
+ return out.join('\n') + '\n'
248
+ }
249
+
250
+ module.exports = { LOOKBACK_DAYS, activityTimes, lastActiveDay, leftOf, blockersFrom, readBlockers, buildTodo, formatTodo, blockedText }