decklight 0.8.0 → 0.9.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.
Files changed (133) hide show
  1. package/PLATFORM.md +666 -0
  2. package/README.md +224 -150
  3. package/SPEC.md +732 -886
  4. package/cli/agent-review.mjs +245 -0
  5. package/cli/agents.mjs +25 -5
  6. package/cli/associate.mjs +12 -12
  7. package/cli/audit.mjs +42 -10
  8. package/cli/banner.mjs +41 -17
  9. package/cli/bundle-audio.mjs +183 -0
  10. package/cli/bundle.mjs +383 -31
  11. package/cli/cast.mjs +7 -7
  12. package/cli/check.mjs +961 -0
  13. package/cli/clone-deck.mjs +179 -0
  14. package/cli/commands.mjs +345 -0
  15. package/cli/comments.mjs +64 -18
  16. package/cli/commit-flow.mjs +44 -19
  17. package/cli/commit-message.mjs +51 -74
  18. package/cli/commit-split.mjs +601 -0
  19. package/cli/deck-refs.mjs +59 -0
  20. package/cli/deck-routes.mjs +67 -0
  21. package/cli/deckfile.mjs +3 -5
  22. package/cli/decklight.mjs +68 -289
  23. package/cli/design-system-deps.mjs +209 -0
  24. package/cli/design-system-edit.mjs +194 -0
  25. package/cli/design-system-refs.mjs +409 -0
  26. package/cli/design-system.mjs +429 -0
  27. package/cli/doctor.mjs +115 -0
  28. package/cli/edit-git.mjs +353 -0
  29. package/cli/edit-handover.mjs +445 -0
  30. package/cli/edit-look.mjs +388 -0
  31. package/cli/edit-slides.mjs +683 -0
  32. package/cli/edit.mjs +2126 -842
  33. package/cli/enhance.mjs +483 -0
  34. package/cli/extension.mjs +10 -10
  35. package/cli/font-refs.mjs +273 -0
  36. package/cli/font.mjs +246 -0
  37. package/cli/git-tree.mjs +1 -1
  38. package/cli/git.mjs +65 -11
  39. package/cli/import.mjs +56 -10
  40. package/cli/init.mjs +400 -234
  41. package/cli/library.mjs +127 -0
  42. package/cli/loader.mjs +3 -1
  43. package/cli/marketplace.mjs +215 -21
  44. package/cli/narrate.mjs +176 -0
  45. package/cli/open-browser.mjs +63 -0
  46. package/cli/open.mjs +943 -0
  47. package/cli/pdf.mjs +41 -13
  48. package/cli/pkg.mjs +44 -2
  49. package/cli/plugin.mjs +43 -41
  50. package/cli/port-conflict.mjs +53 -23
  51. package/cli/pptx-export.mjs +71 -26
  52. package/cli/publish.mjs +8 -4
  53. package/cli/qr.mjs +1 -1
  54. package/cli/record.mjs +14 -12
  55. package/cli/remote.mjs +11 -11
  56. package/cli/restore.mjs +11 -4
  57. package/cli/review-ref.mjs +125 -0
  58. package/cli/review-remote.mjs +92 -48
  59. package/cli/review-routes.mjs +254 -0
  60. package/cli/review-store.mjs +47 -7
  61. package/cli/review-submit.mjs +47 -57
  62. package/cli/runtime-link.mjs +560 -0
  63. package/cli/serve.mjs +235 -44
  64. package/cli/setup.mjs +112 -0
  65. package/cli/sign.mjs +1 -1
  66. package/cli/skill-content.mjs +190 -33
  67. package/cli/skills.mjs +107 -24
  68. package/cli/start.mjs +110 -0
  69. package/cli/supervise.mjs +6 -6
  70. package/cli/theme-refs.mjs +515 -0
  71. package/cli/theme.mjs +181 -137
  72. package/cli/trust.mjs +83 -0
  73. package/cli/units.mjs +39 -42
  74. package/cli/update-check.mjs +1 -5
  75. package/cli/upgrade.mjs +194 -28
  76. package/cli/upstream.mjs +4 -3
  77. package/cli/util.mjs +4 -4
  78. package/cli/wizard.mjs +6 -6
  79. package/dist/build-info.json +1 -1
  80. package/dist/decklight.css +1126 -49
  81. package/dist/decklight.js +212 -115
  82. package/dist/decklight.js.map +4 -4
  83. package/docs/GLOSSARY.md +141 -0
  84. package/docs/architecture.svg +17 -17
  85. package/package.json +4 -2
  86. package/themes/README.md +9 -8
  87. package/themes/reveal-solarized.css +10 -0
  88. package/tools/args.mjs +20 -0
  89. package/tools/atomic-write.mjs +48 -0
  90. package/tools/brand-palette.mjs +717 -0
  91. package/tools/bridge.mjs +1 -1
  92. package/tools/build-groups.mjs +42 -0
  93. package/tools/color.mjs +48 -0
  94. package/tools/deck-html.mjs +396 -11
  95. package/tools/design-system-format.mjs +753 -0
  96. package/tools/elevenlabs-tts.mjs +110 -17
  97. package/tools/exec.mjs +35 -2
  98. package/tools/extension-check.mjs +31 -7
  99. package/tools/font-format.mjs +190 -0
  100. package/tools/font-stacks.mjs +24 -0
  101. package/tools/gemini-tts.mjs +3 -0
  102. package/tools/lipsync-config.mjs +103 -0
  103. package/tools/lipsync-engines.mjs +205 -2
  104. package/tools/lipsync-server.mjs +264 -41
  105. package/tools/lipsync.mjs +37 -18
  106. package/tools/local-voice.mjs +32 -42
  107. package/tools/narration-manifest.mjs +196 -0
  108. package/tools/narration-synth.mjs +416 -0
  109. package/tools/pptx.mjs +776 -19
  110. package/tools/publish-targets.mjs +1 -1
  111. package/tools/publish-voices.mjs +3 -3
  112. package/tools/recording-impact.mjs +7 -3
  113. package/tools/render-theme.mjs +33 -0
  114. package/tools/review-anchor.mjs +28 -12
  115. package/tools/semver.mjs +40 -0
  116. package/tools/sentences.mjs +180 -0
  117. package/tools/shot.mjs +14 -7
  118. package/tools/site-voices.mjs +232 -0
  119. package/tools/template-theme.mjs +1 -1
  120. package/tools/test-impact.mjs +19 -10
  121. package/tools/theme-check.mjs +30 -3
  122. package/tools/tts-cache.mjs +4 -3
  123. package/tools/tts-engines.mjs +38 -20
  124. package/tools/tts-setup.mjs +7 -7
  125. package/tools/video-options.mjs +34 -0
  126. package/tools/video.mjs +461 -48
  127. package/tools/voiceover-server.mjs +185 -104
  128. package/tools/voiceover.mjs +119 -261
  129. package/tools/zip.mjs +1 -1
  130. package/cli/dev.mjs +0 -623
  131. package/cli/present.mjs +0 -853
  132. package/cli/review.mjs +0 -361
  133. package/tools/publish-site-voices.mjs +0 -75
@@ -0,0 +1,353 @@
1
+ // Copyright 2026 Gilles Philippart
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ // The routes that talk to GIT on the author's behalf: the commit window (what
5
+ // is uncommitted, a commit on the author's word, a subject or a whole split
6
+ // written by an agent, a tag, a push, "not now"), and the deck's durable
7
+ // history (the versions, what each changed, an old one shown or restored)
8
+ // (SPEC PRESENTING).
9
+ //
10
+ // They came out of cli/edit.mjs as the one group of that server that is about
11
+ // the REPOSITORY rather than the deck's text. They are also the group that
12
+ // needs the most of editMain, and the deps object says so honestly: the
13
+ // session's commit bookkeeping (how dirty the work is, the nag latch, a
14
+ // .gitattributes waiting for a commit to carry it) stays in editMain, because
15
+ // the timer, the agent's own commits and the exit commit read and write it
16
+ // too, and these routes reach it only through the functions, getters and
17
+ // setters handed in below. What is theirs alone (the push latch, the cache of
18
+ // what a version changed) lives here.
19
+ //
20
+ // The import back to edit.mjs, for the error page a failed preview draws, is a
21
+ // deliberate STATIC cycle, for the reason cli/edit-slides.mjs gives: edit.mjs
22
+ // ends in a top-level `await`, so a dynamic import from inside editMain would
23
+ // wait on an evaluation that is waiting on it. Nothing here reads that binding
24
+ // until a request arrives.
25
+
26
+ import { basename } from 'node:path';
27
+
28
+ import { previewError } from './edit.mjs';
29
+ import { git, gitAutocommit, commitSubject, oneline, remoteLine, remoteState, unpushed } from './git.mjs';
30
+ import { commitPlan, headTags, planWorking, pushBlocked, pushBranch, slideDiff, tagHead, unitSummary } from './commit-split.mjs';
31
+ import { describeWorking } from './commit-message.mjs';
32
+ import { deckHistory, decorateHistory, restoreDeck, deckAt, withBaseHref } from './restore.mjs';
33
+ import { linkRuntime } from './runtime-link.mjs';
34
+ import { linkReferences } from './deck-refs.mjs';
35
+
36
+ /**
37
+ * Put the git routes into `routes`, the table cli/edit.mjs dispatches on, and
38
+ * hand it back.
39
+ *
40
+ * `ctx` is the whole of what these handlers may touch:
41
+ *
42
+ * - the deck: `readDeck()`, `history` (a restore is one undo entry, and every
43
+ * commit answer carries the counts), `broadcast` (a push tells every page),
44
+ * `deckPath`, `deckRel` (the deck as the repository names it) and `root`
45
+ * (the repository);
46
+ * - the session's settings: `gitOn()` and `agentPref()`, getters because both
47
+ * are `let`s of editMain's (the preferred agent changes when the author
48
+ * picks another), `subjectsOff` (`--no-commit-messages`, fixed at start) and
49
+ * `describer()`, whether an agent can write a subject at all;
50
+ * - the commit bookkeeping editMain owns: `commitState()`, `measureDirty()`
51
+ * and `resetEpisode()`; `alsoNow()`, `alsoRels()` and `attrRel()`, the files
52
+ * that ride with the deck; `pendingAttr()` and `clearPendingAttr()`, the
53
+ * .gitattributes a commit has yet to carry; and `dismissNag()`, the window's
54
+ * "not now".
55
+ */
56
+ export function registerGitRoutes(routes, {
57
+ readDeck, history, broadcast = () => {}, deckPath, deckRel, root,
58
+ gitOn, agentPref, subjectsOff, describer,
59
+ commitState, measureDirty, resetEpisode, alsoNow, alsoRels, attrRel, pendingAttr, clearPendingAttr, dismissNag,
60
+ }) {
61
+ // ── committing on the author's word (SPEC PRESENTING) ─────────────────
62
+ // What is uncommitted, right now. The overlay opens on this rather than
63
+ // on whatever the last SSE event said, because K can be pressed at any
64
+ // moment and a stale count is a lie about what you are agreeing to.
65
+ function commitStatusRoute({ json }) {
66
+ if (gitOn()) measureDirty(); // the answer must be about NOW, not the last tick
67
+ return json(200, { ok: true, ...commitState(), deck: deckRel, ...(gitOn() ? whereItStands() : {}) });
68
+ }
69
+
70
+ // What the window's tag and push rows read: the tags on HEAD, and where the
71
+ // branch stands against its remote — a LOCAL read (remoteState never
72
+ // fetches), so "1 commit not pushed" is as of the last push or pull.
73
+ function whereItStands() {
74
+ const s = remoteState(root);
75
+ return {
76
+ tags: headTags(root),
77
+ push: {
78
+ state: s.state, remote: s.remote, branch: s.branch, upstream: s.upstream,
79
+ ahead: s.state === 'ok' ? s.ahead : s.unpushed, behind: s.behind, blocked: pushBlocked(s),
80
+ },
81
+ };
82
+ }
83
+
84
+ // The commit the author asked for. The subject is THEIRS — typed, or an
85
+ // agent's sentence they looked at and kept — so it goes through
86
+ // `commitSubject` like every other message that reaches a command line
87
+ // (one line, capped, never a leading `-`) and nothing else rewrites it:
88
+ // `describeCommit`'s amend is for messages decklight authored, and this
89
+ // one has an author.
90
+ function commitRoute({ body, json }) {
91
+ if (!gitOn()) return json(409, { ok: false, error: 'this session is not committing — open the deck with --git' });
92
+ let msg = '';
93
+ try { msg = String(JSON.parse(body || '{}').message ?? '').trim(); } catch { /* below */ }
94
+ if (!msg) return json(400, { ok: false, error: 'a commit needs a message' });
95
+ const subject = commitSubject(msg, `decklight: autosave ${basename(deckPath)}`);
96
+ // gitAutocommit reports false for "nothing to commit", which is not an
97
+ // error: it is the answer to pressing K twice.
98
+ const made = gitAutocommit(deckPath, root, subject, alsoNow());
99
+ if (!made) return json(200, { ok: true, committed: false, ...commitState() });
100
+ clearPendingAttr();
101
+ resetEpisode();
102
+ console.log(` git: committed ${deckRel} — "${subject}"`);
103
+ return json(200, {
104
+ ok: true, committed: true, subject, ...history.counts(), ...commitState(),
105
+ });
106
+ }
107
+
108
+ // A subject for work that is not committed yet — the overlay's "write one
109
+ // for me", asked only when that is pressed (or as the window opens, with
110
+ // --commit-messages). Refused when the session turned subjects off, or
111
+ // there is no agent to ask.
112
+ async function commitSubjectRoute({ json }) {
113
+ if (subjectsOff) {
114
+ return json(403, { ok: false, error: 'commit subjects are off in this session — it was started with --no-commit-messages' });
115
+ }
116
+ // A change that is only review records (a resolve, a comment of your
117
+ // own) gets its subject from the records, with no agent in the loop, so
118
+ // it is asked for first; the deck's own diff is what needs an agent.
119
+ const subject = await describeWorking({
120
+ cwd: root, deckPath, deckRel, agent: agentPref(),
121
+ template: `decklight: autosave ${basename(deckPath)}`,
122
+ also: alsoRels(),
123
+ });
124
+ if (subject) return json(200, { ok: true, subject });
125
+ if (!describer()) {
126
+ return json(409, { ok: false, error: 'no agent is installed on this machine to write one — decklight doctor lists the ones it can use' });
127
+ }
128
+ return json(200, { ok: true, subject: null });
129
+ }
130
+
131
+ // "write them for me": the uncommitted work cut into the commits an agent
132
+ // proposes (cli/commit-split.mjs). The plan carries `base`, the fingerprint
133
+ // of the work it was made on, which the split route checks before it
134
+ // commits anything. Asked only on the click, like the one-line subject.
135
+ async function commitPlanRoute({ json }) {
136
+ if (!gitOn()) return json(409, { ok: false, error: 'this session is not committing — open the deck with --git' });
137
+ if (subjectsOff) {
138
+ return json(403, { ok: false, error: 'commit subjects are off in this session — it was started with --no-commit-messages' });
139
+ }
140
+ const { work, commits } = await planWorking({
141
+ cwd: root, deckPath, deckRel, also: alsoRels(), agent: agentPref(),
142
+ });
143
+ if (!work) return json(409, { ok: false, code: 'FIRST', error: 'the first commit is one commit — write its message' });
144
+ if (!commits) return json(200, { ok: true, base: work.base, units: [], commits: [] });
145
+ if (!describer() && work.units.some((u) => u.file === deckRel)) {
146
+ return json(409, { ok: false, error: 'no agent is installed on this machine to write them — decklight doctor lists the ones it can use' });
147
+ }
148
+ return json(200, { ok: true, base: work.base, units: work.units.map(unitSummary), commits });
149
+ }
150
+
151
+ // The proposed commits, as the author left them: each subject theirs now,
152
+ // so each goes through commitSubject like any message. All the ticked ones
153
+ // land or none does (commitPlan moves the branch once); an unticked one
154
+ // (`skip`) stays uncommitted, and `left` hands it back re-cut against the
155
+ // new HEAD so the window can offer it again.
156
+ function commitSplitRoute({ body, json }) {
157
+ if (!gitOn()) return json(409, { ok: false, error: 'this session is not committing — open the deck with --git' });
158
+ let req = {};
159
+ try { req = JSON.parse(body || '{}'); } catch { return json(400, { ok: false, error: 'bad request' }); }
160
+ const commits = Array.isArray(req.commits) ? req.commits : [];
161
+ const taking = commits.filter((c) => !c?.skip);
162
+ if (!taking.length) return json(400, { ok: false, code: 'EMPTY', error: 'no commit is ticked' });
163
+ if (taking.some((c) => !String(c?.message ?? '').trim())) {
164
+ return json(400, { ok: false, error: 'every commit needs a message' });
165
+ }
166
+ const made = commitPlan({
167
+ cwd: root, deckRel, also: alsoRels(), base: String(req.base ?? ''), commits,
168
+ template: `decklight: autosave ${basename(deckPath)}`,
169
+ });
170
+ if (!made.ok) return json(made.code === 'STALE' ? 409 : 500, { ok: false, code: made.code, error: made.error });
171
+ // the .gitattributes rides the sidecar's commit: still pending if that was left out
172
+ if (!pendingAttr() || made.files.includes(attrRel())) clearPendingAttr();
173
+ resetEpisode();
174
+ measureDirty(); // what was left out is uncommitted work, starting now
175
+ for (const s of made.subjects) console.log(` git: committed ${deckRel} — "${s}"`);
176
+ return json(200, {
177
+ ok: true, committed: true, count: made.subjects.length, subjects: made.subjects, subject: made.subjects.at(-1),
178
+ left: made.left,
179
+ ...history.counts(), ...commitState(), ...whereItStands(),
180
+ });
181
+ }
182
+
183
+ // An annotated tag on HEAD, named by the author.
184
+ function commitTagRoute({ body, json }) {
185
+ if (!gitOn()) return json(409, { ok: false, error: 'this session is not committing — open the deck with --git' });
186
+ let name = '';
187
+ try { name = String(JSON.parse(body || '{}').name ?? ''); } catch { /* below */ }
188
+ const made = tagHead(root, name);
189
+ if (!made.ok) return json(made.code === 'GIT' ? 500 : 409, { ok: false, code: made.code, error: made.error });
190
+ console.log(` git: tagged ${made.sha.slice(0, 7)} ${made.tag}`);
191
+ return json(200, { ok: true, tag: made.tag, ...whereItStands() });
192
+ }
193
+
194
+ // The push, on the author's click and only then: the branch and the
195
+ // annotated tags on it. One at a time; a minute at most; no prompt can
196
+ // hang it (pushBranch). The SSE `commit` event tells every page the
197
+ // count went to zero.
198
+ let pushing = false;
199
+ async function commitPushRoute({ body, json }) {
200
+ if (!gitOn()) return json(409, { ok: false, error: 'this session is not committing — open the deck with --git' });
201
+ if (pushing) return json(409, { ok: false, code: 'BUSY', error: 'a push is already on its way' });
202
+ // tags go with the branch unless the page says not to (Settings… → Push
203
+ // tags): an absent or unreadable body is the default, which sends them
204
+ let tags = true;
205
+ try { tags = JSON.parse(body || '{}').tags !== false; } catch { /* the default */ }
206
+ pushing = true;
207
+ try {
208
+ const done = await pushBranch(root, { state: remoteState(root), tags });
209
+ if (!done.ok) return json(done.code === 'BLOCKED' ? 409 : 502, { ok: false, code: done.code, error: done.error });
210
+ console.log(` git: pushed ${done.branch} to ${done.remote}${tags ? '' : ' (tags kept here)'}`);
211
+ broadcast('commit', commitState());
212
+ // `pushedTags`, not `tags`: whereItStands() already answers `tags`, the
213
+ // names on HEAD, and the window paints both
214
+ return json(200, { ok: true, pushed: true, remote: done.remote, branch: done.branch, pushedTags: tags, ...whereItStands() });
215
+ } finally { pushing = false; }
216
+ }
217
+
218
+ function commitDismissRoute({ json }) {
219
+ // Not the same as committing: the work stays uncommitted and the
220
+ // snapshot keeps running. It only means "stop asking about THIS one".
221
+ dismissNag();
222
+ return json(200, { ok: true, ...commitState() });
223
+ }
224
+
225
+ // ── the deck's durable history (#129): what the R overlay reads ────
226
+ // Loopback-only like every other /deck/edit/* path: this serves arbitrary
227
+ // historical revisions of the deck, which is nobody else's business.
228
+ function historyRoute({ json }) {
229
+ if (!gitOn()) return json(409, { ok: false, error: 'git is off for this session — there is no history' });
230
+ try {
231
+ // One round trip serves the whole overlay: the commits, which of them
232
+ // exist nowhere but this machine, and where the branch stands. All of
233
+ // it is a LOCAL read — `unpushed` and `@{u}` read remote-tracking
234
+ // refs, so opening the history never touches the network (SPEC
235
+ // PRESENTING).
236
+ // `slides` is what that version WAS, `add`/`del` what it CHANGED —
237
+ // the two questions a hash and a subject cannot answer, and the ones
238
+ // that tell "tightened the wording" apart from "cut four slides"
239
+ // before you restore it rather than after.
240
+ const entries = decorateHistory(deckHistory(deckPath, root), deckPath, root);
241
+ const remote = remoteState(root);
242
+ // `pushed` is null for "not a question worth answering here", and the
243
+ // two cases are different: git could not tell us, or there is no
244
+ // remote at all — in which case EVERY commit is unpushed and marking
245
+ // all of them is a wall of arrows saying what the footer says once.
246
+ const local = ['no-remote', 'ambiguous-remote'].includes(remote.state) ? null : unpushed(root);
247
+ const set = local ? new Set(local) : null;
248
+ for (const e of entries) e.pushed = set ? !set.has(e.full) : null;
249
+ // The LINE is computed here, not in the player: cli/git.mjs is Node
250
+ // (it spawns git), the runtime has zero dependencies and cannot
251
+ // import it, and duplicating the wording in the browser is how the
252
+ // four places that talk about unpushed work start disagreeing.
253
+ return json(200, { ok: true, entries, remote: { ...remote, line: remoteLine(remote) } });
254
+ } catch (e) { return json(500, { ok: false, error: oneline(e) }); }
255
+ }
256
+
257
+ /**
258
+ * A page this server writes ITSELF — an old version of the deck, a template
259
+ * preview — goes out the way `staticFiles` sends every other page: a deck
260
+ * that is data (#520) gets the runtime, its stylesheet, its theme and the
261
+ * themes it marks referenced. Without it the frame drew the deck's bare
262
+ * markup, dark text on the panel's dark ground — a black rectangle. The
263
+ * `<base>` comes first, so those references resolve from the root, where
264
+ * `staticFiles` answers them, not from under /deck/edit/.
265
+ */
266
+ const asServed = (html) => linkReferences(linkRuntime(withBaseHref(html)));
267
+
268
+ /**
269
+ * Which slides a version changed, added and removed against the one before
270
+ * it, in reading order: what the History overlay's "changes" view walks
271
+ * (PRESENTING). `parent` is the version a removed slide is shown from; the
272
+ * deck's first version has none, and everything in it was added. A hash is
273
+ * all this takes, because a ref is handed to `git show` and an option-shaped
274
+ * one would be read as an option.
275
+ */
276
+ const historyChanges = new Map(); // full hash → answer; a commit never changes
277
+ function historyChangesRoute({ url, json }) {
278
+ if (!gitOn()) return json(409, { ok: false, error: 'git is off for this session' });
279
+ const ref = url.searchParams.get('ref') ?? '';
280
+ if (!/^[0-9a-f]{4,64}$/i.test(ref)) return json(400, { ok: false, error: 'a version is named by its hash' });
281
+ try {
282
+ const full = git(['rev-parse', '--verify', '--quiet', `${ref}^{commit}`], root);
283
+ if (historyChanges.has(full)) return json(200, historyChanges.get(full));
284
+ const after = deckAt(deckPath, full, root);
285
+ let parent = null, before = '';
286
+ try {
287
+ parent = git(['rev-parse', '--short', `${full}^`], root);
288
+ before = deckAt(deckPath, parent, root);
289
+ } catch { /* the first commit, or one the deck was not in yet */ }
290
+ const answer = { ok: true, ref: full.slice(0, 7), parent, ...slideDiff(before, after) };
291
+ historyChanges.set(full, answer);
292
+ return json(200, answer);
293
+ } catch { return json(404, { ok: false, error: 'no such revision of this deck' }); }
294
+ }
295
+
296
+ function deckAtRoute({ res, url, json, CORS }) {
297
+ if (!gitOn()) return json(409, { ok: false, error: 'git is off for this session' });
298
+ try {
299
+ // <base href="/"> because this is served from /deck/edit/, not the root:
300
+ // without it every relative ../dist and ./casts path in the deck
301
+ // would resolve one directory too deep and the preview would be bare.
302
+ const html = asServed(deckAt(deckPath, url.searchParams.get('ref') || '', root));
303
+ res.writeHead(200, { ...CORS, 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-cache' });
304
+ return res.end(html);
305
+ } catch (e) {
306
+ // Two different failures, and answering both with the same 404 is how
307
+ // #508 hid for a release: a deck over Node's stdout cap threw ENOBUFS
308
+ // here, was reported as a missing revision, and the preview iframe drew
309
+ // a bare 404 body as a black rectangle. A ref git does not know is the
310
+ // only 404; anything else is this server's problem and says so — in the
311
+ // terminal, and in the frame, which is where somebody is looking.
312
+ // git's own two ways of saying "not here": `invalid object name 'x'` for a
313
+ // ref it cannot resolve, `path 'x' does not exist in 'HEAD'` for a deck that
314
+ // was not in that commit. Everything else — ENOBUFS above all (#508) — is a
315
+ // failure of this server, not a missing revision, and must not wear a 404.
316
+ const missing = /invalid object name|unknown revision|does not exist in|exists on disk, but not in/i
317
+ .test(String(e.stderr || e.message || e));
318
+ const why = missing ? 'no such revision of this deck' : oneline(e);
319
+ if (!missing) console.log(` history: previewing ${basename(deckPath)} failed — ${why}`);
320
+ res.writeHead(missing ? 404 : 500, { ...CORS, 'content-type': 'text/html; charset=utf-8' });
321
+ return res.end(previewError(why));
322
+ }
323
+ }
324
+
325
+ function restoreRoute({ body, json }) {
326
+ if (!gitOn()) return json(409, { ok: false, error: 'git is off for this session' });
327
+ const { ref } = JSON.parse(body || '{}');
328
+ if (typeof ref !== 'string' || !ref.trim()) throw new Error('bad payload');
329
+ const before = readDeck();
330
+ let result;
331
+ try { result = restoreDeck(deckPath, ref.trim(), root); }
332
+ catch (e) { return json(400, { ok: false, error: oneline(e) }); }
333
+ // Z takes a restore back like any other edit — the git-level move and
334
+ // the keystroke-level stack stay in step rather than disagreeing.
335
+ if (result.changed) history.record(before);
336
+ console.log(` restored ${basename(deckPath)} to ${result.short}`);
337
+ return json(200, { ok: true, ...result, ...history.counts() });
338
+ }
339
+
340
+ routes.set('GET /deck/edit/commit', commitStatusRoute);
341
+ routes.set('POST /deck/edit/commit', commitRoute);
342
+ routes.set('POST /deck/edit/commit/subject', commitSubjectRoute);
343
+ routes.set('POST /deck/edit/commit/dismiss', commitDismissRoute);
344
+ routes.set('POST /deck/edit/commit/plan', commitPlanRoute);
345
+ routes.set('POST /deck/edit/commit/split', commitSplitRoute);
346
+ routes.set('POST /deck/edit/commit/tag', commitTagRoute);
347
+ routes.set('POST /deck/edit/commit/push', commitPushRoute);
348
+ routes.set('GET /deck/edit/history', historyRoute);
349
+ routes.set('GET /deck/edit/history/at', deckAtRoute);
350
+ routes.set('GET /deck/edit/history/changes', historyChangesRoute);
351
+ routes.set('POST /deck/edit/restore', restoreRoute);
352
+ return routes;
353
+ }